Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
SQL / POSTGRESQL / Learn / LESSON 07
LEFT JOIN keeps every left-side row. Unmatched right-side columns become NULL. This includes customers without orders. A WHERE condition on the right side can accidentally remove those rows.
Example: SELECT c.name, o.id AS order_id FROM customers c LEFT JOIN orders o ON c.id = o.customer_id lists every customer; without an order, order_id is NULL. That differs from INNER JOIN, which drops unmatched customers.
Small example:
SELECT c.id, o.id AS order_id FROM customers c LEFT JOIN orders o ON c.id = o.customer_id;
Working tables for this task. Grading also uses other datasets with the same schema.
| id | name | category | price | stock |
|---|---|---|---|---|
| 1 | Book Light | books | 12 | 8 |
| 2 | Bowl | home | 20 | 4 |
| 3 | Lamp | home | 30 | 2 |
| 4 | Notebook | books | 10 | 12 |
| 5 | Box | NULL | 5 | 0 |
| 6 | Book Ocean | books | 20 | 1 |
| 7 | Mug | home | 30 | 3 |
| 8 | Pen | books | 19.99 | 7 |
| 9 | Cable | tools | 20.01 | 5 |
| 10 | Clip | tools | 9.99 | 3 |
| 11 | Fan | home | 30.01 | 1 |
| 12 | Book Edge | books | 20.01 | 2 |
| id | name | city |
|---|---|---|
| 1 | Mira | Sofia |
| 2 | Niko | Varna |
| 3 | Iva | Sofia |
| 4 | Alex | NULL |
| 5 | Boris | Plovdiv |
| 6 | Lena | Burgas |
| id | customer_id | total |
|---|---|---|
| 1 | 1 | 20 |
| 2 | 1 | 30 |
| 3 | 2 | 10 |
| 4 | 3 | 0 |
| 5 | 6 | 40 |
Expected columns: customer, order_id · row order is not required
Expected result (displayed data):
Loading…LEFT JOIN keeps every left-side row. Unmatched right-side columns become NULL. This includes customers without orders. A WHERE condition on the right side can accidentally remove those rows. Example: SELECT c.name, o.id AS order_id FROM customers c LEFT JOIN orders o ON c.id = o.customer_id lists every customer; without an order, order_id is NULL. That differs from INNER JOIN, which drops unmatched customers.
QUERY STANDBY
›