LEFT JOIN in SQL

Level 3109 keystrokes

LEFT JOIN returns all rows from the left table and matched rows from the right.

SQL code

-- Show each user's name and the total of their order
1SELECT
2 users.name,
3 orders.total
4FROM
5 users
-- LEFT JOIN keeps every row of users, even without a matching order
6LEFT JOIN
-- For a user with no order (Carol), orders.total is NULL
7 orders ON users.id = orders.user_id
-- Sort the rows alphabetically by user name
8ORDER BY
9 users.name;

Output

+-------+-------+
| name  | total |
+-------+-------+
| Alice | 99.50 |
| Bob   | 75.25 |
| Carol | NULL  |
+-------+-------+
3 rows in set
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this SQL code and how often they appear.

  • .× 5
  • _× 1
  • ,× 1
  • ;× 1
  • =× 1

More SQL examples at level 3

← SQL typing practice