SQL - Left Join

Use all the result form left side even that are null at the second table. Will return null when can t find result in right table.


SELECT p.firstName , p.lastName, a.city , a.state

FROM

Person p

LEFT JOIN

Address a ON p.personId = a.personId;
//USING(persionId) < Also OK

Input: 
Person table:
+----------+----------+-----------+
| personId | lastName | firstName |
+----------+----------+-----------+
| 1        | Wang     | Allen     |
| 2        | Alice    | Bob       |
+----------+----------+-----------+
Address table:
+-----------+----------+---------------+------------+
| addressId | personId | city          | state      |
+-----------+----------+---------------+------------+
| 1         | 2        | New York City | New York   |
| 2         | 3        | Leetcode      | California |
+-----------+----------+---------------+------------+
Output: 
+-----------+----------+---------------+----------+
| firstName | lastName | city          | state    |
+-----------+----------+---------------+----------+
| Allen     | Wang     | Null          | Null     |
| Bob       | Alice    | New York City | New York |
+-----------+----------+---------------+----------+

留言

此網誌的熱門文章

MAP - Sort with stream

JAVA - DSF Example