INNER JOIN

Concepts covered: Inner Joins

Every join in this lesson so far has been an INNER JOIN. It keeps a pair of rows when the test is true and keeps nothing else. This section reads the syntax piece by piece, then looks at what 'nothing else' costs you.

customers now has a new shopper, Dave, who has never ordered. Play the query and watch what happens to his row.

SELECT customers.name, orders.order_id, orders.total
FROM customers
INNER JOIN orders
  ON customers.customer_id = orders.customer_id
customers
customer_idname
1Alice
2Bob
3Carol
4Dave
orders
order_idcustomer_idtotal
101150
102130
103275
104320

Reading the query

Each column is written as table.column. The prefix is required for customer_id, because both tables have a column with that name and the database refuses to guess which one you mean. name exists in only one table, so its prefix is optional. Writing it anyway tells the next reader where every column comes from.

The keyword JOIN on its own means INNER JOIN, and both spellings run the same query. This lesson writes the full form so the join type is always visible.

Unmatched rows vanish from both sides

Dave's row was tested against all 4 orders and every test was false, so he is missing from the result. The same thing happens on the other side. The tables below add order 105, which carries customer_id 9, and no customer has that value. This query also writes orders first, to show that swapping the 2 tables leaves the passing pairs unchanged.

SELECT
  orders.order_id,
  orders.total,
  customers.name
FROM orders
INNER JOIN customers
  ON orders.customer_id = customers.customer_id
orders
order_idcustomer_idtotal
101150
102130
103275
104320
105960
customers
customer_idname
1Alice
2Bob
3Carol
4Dave
Result
order_idtotalname
10150Alice
10230Alice
10375Bob
10420Carol

4 rows again. Dave is absent because he has no order, and order 105 is absent because it has no customer. An INNER JOIN result contains only what exists in both tables. That makes it the right choice when a row is useless without its partner, and a risky one when every row must be accounted for.

Do
  • Compare the result's row count with each table's row count
  • Prefix every column with its table name
  • Join a foreign key to the primary key it points at
Avoid
  • Assume every row of the first table appears in the result
  • Report a total without checking for unmatched rows
  • Pick keys by column name alone

The joined result is a table

Once the pairs are kept, the result behaves like any table. WHERE can filter its rows, and GROUP BY can fold them into one row per group. The query in the question below counts orders and adds up spending for each customer name. It runs on the original data: 4 customers, and orders 101 to 104.

customers holds Alice, Bob, Carol and Dave, and Dave has no orders. Alice's orders total 50 and 30. What does this query return?

SELECT
  customers.name,
  COUNT(*) AS order_count,
  SUM(orders.total) AS total_spent
FROM customers
INNER JOIN orders
  ON customers.customer_id = orders.customer_id
GROUP BY customers.name

Run it and compare the result with your prediction.

SELECT
  customers.name,
  COUNT(*) AS order_count,
  SUM(orders.total) AS total_spent
FROM customers
INNER JOIN orders
  ON customers.customer_id = orders.customer_id
GROUP BY customers.name
customers
customer_idname
1Alice
2Bob
3Carol
4Dave
orders
order_idcustomer_idtotal
101150
102130
103275
104320
Result
nameorder_counttotal_spent
Alice280
Bob175
Carol120

A report built on this query shows 3 customers and never mentions Dave. Listing customers with 0 orders takes a different kind of join, which a later lesson covers.

Build one yourself

2 short exercises put the syntax in your hands. The first gives you the lines of a query out of order. The second leaves blanks in a join for you to fill.

Show each order's order_id beside the name of the customer who placed it. Drag the tiles into the slots to build the query. 2 tiles do not belong.

The wrong ON tile compared order_id with customer_id: both INTEGER, different meanings, the same trap as the item_id join in section 1. A join always has 3 parts in a fixed order: the join keywords with the second table, then ON, then a test with one column from each table that holds the same identifier.

Every INNER JOIN you write comes down to 4 decisions, made in this order: which 2 tables, which keys, how many rows to expect, and which rows you are willing to lose.

About this lesson

This section is part of the Joins: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.