AI Hints
Homeโ€บSQL Tutorialโ€บJoining Tables
SQL ยท Lesson 43 of 126

Your First JOIN

Orders with customer names.

โฑ 2 min readโ–ถ Video at 1:00:44๐ŸŽ“ Free
Watch this lesson in the SQL courseWatch on YouTube โ†—Full SQL course

Helen wants a list of orders, with the customer's name next to each one. The order is in one table, and the name is in another. Join brings them together.

Your First JOIN
From the video: Your First JOIN
  • Orders and names, side by side
  • Two different tables
  • JOIN brings them together

JOIN ... ON

After from orders, we write join customers. Then on, which says how the rows match up. A customer row matches an order row when their customer IDs are equal. Because two tables now share column names, we write the table name, a dot, and the column. Run it. Every order now shows the customer's name.

query.sqlsql
SELECT orders.order_id,
       orders.order_date,
       customers.first_name,
       customers.last_name
FROM orders
JOIN customers
  ON customers.customer_id = orders.customer_id
LIMIT 4;
Output
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ order_id โ”‚ order_date โ”‚ first_name โ”‚ last_name โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 1001     โ”‚ 2025-03-06 โ”‚ Tara       โ”‚ Brown     โ”‚
โ”‚ 1002     โ”‚ 2024-11-24 โ”‚ Tara       โ”‚ Brown     โ”‚
โ”‚ 1003     โ”‚ 2024-06-22 โ”‚ Grace      โ”‚ Lopez     โ”‚
โ”‚ 1004     โ”‚ 2025-02-19 โ”‚ Grace      โ”‚ Lopez     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

What JOIN does

Here's what happens inside. For each order, SQL looks up the customer with the matching ID. It glues the two rows together, side by side, into one wide row. Then select picks the columns you asked for.

Summary

Join, then on, then the matching columns. That's the pattern for every join you'll ever write. Next, a way to make joins much shorter to type.

Key takeaways
  • JOIN another table
  • ON says how rows match
  • table.column avoids confusion
Next up: Short Names for Tables โ€” table aliases