INNER JOIN
Two tables, one result, matched on the column they have in common
0 of 3 done · Lesson 6 of 8
Understand
The orders table records who placed each order as a number, because storing the customer name on every order would mean fixing it in six places when somebody changes their name. Useful for the database, useless for the person who asked you what Halcyon has been buying. A join puts the two tables back together for the length of one query, matching each order to the customer whose id it carries.
SELECT columns_from_either_table FROM the_first_table INNER JOIN the_second_table ON what_makes_a_row_match;
SELECTwhich columns you wantFROMthe first tableINNER JOINthe table to bring inONwhat makes a row matchWHEREwhich rows to keep
The tables you are working with
5 rows
| id | name | city |
|---|---|---|
| 1 | Northwind | Auckland |
| 2 | Kea Ltd | Wellington |
| 3 | Halcyon | Auckland |
| 4 | Brightsmith | Dunedin |
1 more row in the table. Your query sees all 5.
6 rows
| id | customer_id | item | amount |
|---|---|---|---|
| 101 | 1 | desk lamp | 120 |
| 102 | 3 | chair | 340 |
| 103 | 1 | monitor arm | 210 |
| 104 | 5 | filing box | 65 |
2 more rows in the table. Your query sees all 6.