Skip to content

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
First rows of the customers table
idnamecity
1NorthwindAuckland
2Kea LtdWellington
3HalcyonAuckland
4BrightsmithDunedin

1 more row in the table. Your query sees all 5.

6 rows
First rows of the orders table
idcustomer_iditemamount
1011desk lamp120
1023chair340
1031monitor arm210
1045filing box65

2 more rows in the table. Your query sees all 6.