Produced by: The Cabin by: Peter Edited by: Peter

Hello, I’m Peter

Get ready to start reading the SQL section of LeetCode in the near future. I’m sure many of you know LeetCode. After all, many Internet companies use it to test candidates, especially algorithmic questions. It’s like a huge question bank. As workers, what we can do is to practice more and improve our ability. I hope we can be more confident and calm when interviewing in the future.

In addition to algorithms, there are also many SQL questions in LeetCode. The authors in the comments section provide many excellent methods and ideas. I hope that after the serialization of leetcode-SQL, whether it is the interview in the future or writing SQL at ordinary times can be more handy. Publish your own exercises, maybe it will be helpful to you. If there are mistakes, I hope you can criticize and correct them, and welcome to provide more excellent ideas.

Leetcode-175 – Combines two tables

The specific description of the topic is as follows:

The answer

  • Left join, where the join results retain all the data of the left table
  • A right join preserves all the data in the right table
  • An inner join takes public data from two tables
select
	p.FirstName
	,p.LastName
	,a.City
	,a.State
from Person as p
left join Address as a
on p.PersonId = a.PersonId
Copy the code

Multiple connection understanding

The following is my own collation of a few different forms of SQL join graphics, from the syntax and results of the join on the visual display, convenient readers understand the inner meaning of different join, as a learning memo.

  • Left JOIN: takes only the contents of the left table
  • Right Join: fetch only the contents of the right table
  • Inner join: Fetch the same part of two tables

MySQL does not support full (outer) join