Lesson 1 of 4

Progress: 0/4 exercises solved (0%)

Solve all exercises below to unlock the next lesson.

INNER JOIN

Lesson Description

Combine rows from two tables where a condition matches.

Easy Project

Mini project: Build a people-and-departments report that lists employee names, department names, and validates row counts to prove your join is correct.

JOIN Visual

employees

departments

Match keys first, then verify row counts after the join.

INNER JOIN — combine matching rows from two tables

A JOIN (also written INNER JOIN) combines rows from two tables where a condition is met. Rows without a match in either table are excluded.

Syntax

SELECT t1.col, t2.col
FROM table1 t1
JOIN table2 t2 ON t2.foreign_key = t1.primary_key;

Example: employees with their department name

SELECT
  e.first_name,
  e.last_name,
  d.name AS department
FROM employees e
JOIN departments d ON d.id = e.department_id;

e and d are table aliases — shorter names assigned for the query.

Why alias tables?

  • Shorter to type.
  • Required when joining the same table twice.
  • Disambiguates identical column names (e.id vs d.id).

Joining multiple tables

You can chain JOINs. Each adds one more table:

SELECT e.first_name, d.name AS dept, j.title AS job
FROM employees  e
JOIN departments d ON d.id  = e.department_id
JOIN jobs        j ON j.id  = e.job_id;

Result size

If table A has 100 rows and table B has 50 rows, the JOIN returns at most 100 rows (one per employee). It can return more if the ON condition matches multiple rows in B (a one-to-many match multiplies rows).

Tips

  • JOIN and INNER JOIN are identical — both exclude non-matching rows.
  • Always specify ON — a JOIN without ON is a CROSS JOIN (cartesian product).
  • If a column name is unique across joined tables, you can omit the table prefix.

Learning context

This lesson is part of Joins. Focus on the core idea in INNER JOIN, then validate with deliberate practice.

What to master

  • Choose the base table (one row = what?)
  • Match join keys on true primary/foreign pairs
  • Validate row count after every join

Common mistakes

  • Joining many-to-many without pre-aggregation
  • Using DISTINCT to hide a bad join
  • Forgetting LEFT JOIN when keeping base rows

High-level strategy

  • Start with INNER JOIN to verify key matches
  • Switch to LEFT JOIN only when preserving base rows matters
  • Use COUNT(*) and COUNT(DISTINCT key) as join safety checks

Task ladder

  1. Baseline query: get a correct first output.
  2. Add one complication: edge case, join, filter, or ranking rule.
  3. Re-run and refine until output is stable and explainable.

Transparent data checks

  • Check row count before and after each major clause.
  • Validate one manual sample row for correctness.
  • Confirm final ordering and column naming for trust.

Retention loop

  • Run the solution once.
  • Rewrite from memory with one variation.
  • Explain the logic in plain English in one sentence.

Logical reasoning for commands

JOIN ... ON

Why: Connects rows only when keys truly match.

Memory cue: No key match = no trusted relationship.

LEFT JOIN

Why: Keeps base table rows even when lookup data is missing.

Memory cue: Left keeps the story intact.

COUNT(DISTINCT key)

Why: Confirms joins did not multiply business entities.

Memory cue: Distinct key count should stay believable.

Concept check

Quick check: INNER JOIN

3 quick questions. One at a time. Instant score at the end.

Exercise 1

Employees with department name

Enterprise Workforce Analyticsmedium

Return first_name, last_name, and the department name (alias department) for every employee that has a department.

Run a query to see results here.
Exercise 2

Order items with product name

Commerce Operations Intelligencemedium

Return order_id, product name (alias product), quantity, and unit_price for every order item.

Run a query to see results here.
Exercise 3

Appointment: patient and doctor names

Clinical Care Performanceeasy

From the hospital sandbox, return patient first_name (alias patient_first), patient last_name (alias patient_last), doctor first_name (alias doctor_first), and doctor last_name (alias doctor_last) for every appointment.

Run a query to see results here.
Exercise 4

Products with their category name

Commerce Operations Intelligenceeasy

From the store sandbox, return product name (alias product), category name (alias category), and price, ordered by category, then product.

Run a query to see results here.

Lesson 1 of 4

0/4 solved (0%)

Finish this lesson to unlock next.