top of page

SQL (1): LEFT JOIN




Welcome to the new SQL series! In this series, I'll teach how to use various SQL commands under Microsoft SQL Server environment. Let's dive in.



Let's look at an example:


SELECT p.firstName, p.lastName, a.city, a.state
    FROM Person p LEFT JOIN Address a ON p.personId = a.personId


SELECT specifies the columns you want to include in the output.


p.firstName, p.lastName refers to the firstName and lastName columns in table p respectively.


a.city, a.state refers to the city and state columns in table a respectively.


FROM means retrieving data from table Person and assigns it to variable p.


LEFT JOIN combines table p with table Address which assigns to variabel a.


ON specifies that the rows in the two tables should be matched using the primary key, which is the personId column in both table p and a.



And we can see the output:


+-----------+----------+---------------+----------+
| firstName | lastName | city          | state    |
+-----------+----------+---------------+----------+
| Harmony   | Pang     | Null          | Null     |
| Joe       | Smith    | New York City | New York |
+-----------+----------+---------------+----------+











Congratulations on completing this tutorial!


Don't hesitate to continue exploring SQL and experimenting with different scenarios.



See you in SQL (2)!








© 2023 Harmony Pang. All rights reserved.








Comments


bottom of page