BA Prince the B.A. Subscribe

Home / SQL for Business Analysts

SQL INNER JOIN Explained With a Simple Example

SQL INNER JOIN Explained With a Simple Example

Watch on YouTube Subscribe

An INNER JOIN combines two tables where a key matches. Join orders to customers to show each order with the customer's name, and see why one order disappears from the result.

Step by step

Why join

  1. Two tables, one answer. Your manager wants a list of orders with each customer's name. The orders are in one table and the customers are in another. An INNER JOIN puts them together in one result.
  2. The orders table. The orders table only stores a customer_id, which is a number. It does not store the customer's name. To see it, run SELECT order_id, customer_id, order_total FROM orders; to show the order ID, the customer ID and the total for each order. It returns 141 orders.
    SELECT order_id, customer_id, order_total
    FROM orders;
  3. Just a number. The customer_id values are just numbers. The names are in the customers table, which has the same customer_id column. That shared column is how the 2 tables connect.

Write the join

  1. Short names. Start a new query: SELECT o.order_id, c.first_name, o.order_total FROM orders AS o. The o is an alias, a short name for the table, so o.order_total means order_total from orders. Customers will get the alias c, so c.first_name comes from customers.
    SELECT o.order_id, c.first_name,
      o.order_total
    FROM orders AS o
  2. INNER JOIN. Add INNER JOIN customers AS c on the next line. This brings the customers table into the query, with c as its short name.
    
    INNER JOIN customers AS c
  3. Match on the key. Finish with ON o.customer_id = c.customer_id; which tells SQL how to match the rows: the customer_id in orders must equal the customer_id in customers. It's the key both tables share.
    
      ON o.customer_id = c.customer_id;
  4. Orders with names. Run the query. Each order now shows the customer's first name next to the order ID and the total.
  5. Names from customers. The first_name values come from the customers table. SQL matched each order to its customer row by row using customer_id.

The missing order

  1. One order missing. Check the row count: 140, not 141. One order has a customer_id that doesn't match any row in customers, so the INNER JOIN leaves that order out without any warning.

Recap

  1. The INNER JOIN rule. INNER JOIN keeps only the rows that match in both tables. Name both tables, then use ON to say which columns match. To keep the orders that have no match, use a LEFT JOIN instead.

Joining three tables

Add one more INNER JOIN line for each extra table, each with its own ON. If each order also has a product_id, add INNER JOIN products AS p ON o.product_id = p.product_id after the customers join. Move the semicolon from the end of the customers ON line to the end of this new line, and add p.product_name to the SELECT list. Now you see the customer's name and the product name on each order.

SQL reads the joins in the order you write them, so each new ON can use any table above it. Check the row count after every join, not just at the end, so you know which join dropped rows. The lesson How to Join Three Tables in SQL goes through it step by step.

Inner, left, right and full joins

INNER JOIN keeps only rows that match in both tables. LEFT JOIN keeps every row from the first table, with blanks (NULL) where the second has no match. RIGHT JOIN is the same the other way round: every row from the second table. FULL JOIN keeps every row from both tables, matched where it can.

Most people use INNER and LEFT almost all the time, and write a RIGHT JOIN as a LEFT JOIN with the tables swapped. Not every database has all four: MySQL has no FULL JOIN, and SQLite only added RIGHT and FULL in version 3.39. The lesson SQL Joins Explained: INNER vs LEFT vs RIGHT vs FULL compares all four on the same tables.

When the join gives you more rows

This lesson lost a row because one order had no customer. A join can also give you more rows, and that is often fine. One customer can have many orders, so if you start from customers and join orders, a customer with 3 orders shows up 3 times. That is a one-to-many join, and it is normal.

It is a problem when the side that should have one row per key has two, like the duplicate customer in the mistakes below. A quick rule: joining orders to customers should give no more rows than the orders table has. The lessons Primary and Foreign Keys in SQL Tables and Why Your SQL Join Returns Too Many Rows explain the keys behind this.

Common mistakes

  • Not checking the row count. An INNER JOIN quietly drops rows that have no match, like the 1 order lost here (141 became 140). Count the rows before and after the join. If any are missing, find them with a LEFT JOIN.
  • Leaving the table name off a shared column. customer_id is in both tables, so SELECT customer_id on its own gives an "ambiguous column" error in most databases. Write o.customer_id or c.customer_id.
  • Matching on the wrong columns. Something like ON o.order_id = c.customer_id still runs, but it links orders to the wrong customers. Match the column that means the same thing in both tables, here customer_id.
  • Duplicate keys in the second table. If a customer_id shows up twice in customers, each of that customer's orders shows up twice in the result and the totals add up too high. Check that the key is unique before you join.

When a business analyst uses this

When you check a data migration before sign-off, join the migrated orders to the customer master on customer_id and compare the row counts. If the join returns fewer rows than the orders table, some orders point at customers that don't exist. Log them in the RAID log as a data quality issue.

Questions people ask

Is JOIN the same as INNER JOIN?

Yes. In standard SQL, JOIN on its own means INNER JOIN, so both give the same result. Writing INNER JOIN makes the query easier to read.

What is the difference between INNER JOIN and LEFT JOIN?

INNER JOIN keeps only the rows that match in both tables. LEFT JOIN keeps every row from the first table, and shows NULL in the second table's columns when there is no match, so the missing order would still appear.

More in SQL for Business Analysts