SQL Topic collection

SQL Joins SQL Topic exercises

Practice reliable INNER, LEFT, FULL, CROSS, self, semi, anti, range, temporal, and many-to-many join patterns.

Exercises
16
Difficulty
Beginner to Advanced

What this collection tests

Skills and query patterns

Relationship cardinality, NULL preservation, ON-versus-WHERE semantics, fan-out prevention, and source reconciliation

Curated exercises

Choose an exercise

Work in the live editor where supported. Advanced guided labs state their engine boundary before you open the workspace.

  1. Exercise 1Easy

    Employees with Department Names (INNER JOIN)

    Return employee_id, first_name, last_name, and department_name for matched employees, ordered by employee_id.

    • Joins
    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  2. Exercise 2Medium

    Departments with Their Employees (LEFT JOIN — Empty Groups Visible)

    Return every department and its employee details, using NULL employee fields for empty departments.

    • Joins
    • CASE expressions
    • NULL handling

    SQLite + PostgreSQL + MySQL live · 2 guided

    Open exercise
  3. Exercise 3Easy

    All Departments with Employee Count (LEFT JOIN)

    Return every department with its employee_count, ordered by count descending and department_id.

    • Joins
    • Aggregation
    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  4. Exercise 4Medium

    Employee Details — Department + Job (Multi-INNER JOIN)

    Chain the employee-to-department and employee-to-job relationships and return the requested profile.

    • Joins
    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  5. Exercise 5Medium

    Employees with Manager Name (Self-Join + LEFT JOIN)

    Return every employee with direct-manager names, leaving manager fields NULL for top-level employees.

    • Joins
    • NULL handling
    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  6. Exercise 6Easy

    USING — Compact Equality Join on Same-Named Columns

    Join employees to departments with USING(department_id) and return the requested directory columns.

    • Joins
    • Sorting

    SQLite + PostgreSQL + MySQL live · 2 guided

    Open exercise
  7. Exercise 7Easy

    CROSS JOIN — Every Department × Every Job (Cartesian Product)

    Generate the intentional Cartesian product of every department and every job.

    • Joins
    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  8. Exercise 8Hard

    Pairs of Employees with the Same Job Title

    Return every unique same-job employee pair with its job title.

    • Joins
    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  9. Exercise 9Medium

    Keep Unassigned Employees in an As-Of Project Roster

    Return every employee and any project assignment active on '2024-07-01'.

    • Joins
    • Date analysis
    • NULL handling

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  10. Exercise 10Medium

    Many-to-Many — Employees ↔ Projects via Junction Table

    Follow employees through project_assignments to projects and return one row per assignment.

    • Joins
    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  11. Exercise 11Medium

    Semi-Join via EXISTS — Departments That Have a Manager

    Return each department for which an employee in that department has at least one direct report.

    • Joins
    • Subqueries
    • Filtering

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  12. Exercise 12Medium

    Anti-Join via NOT EXISTS — Employees Without Direct Reports

    Return employees for whom no reporting employee exists.

    • Joins
    • Subqueries
    • Filtering

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  13. Exercise 13Hard

    Prevent Fan-Out in an Order Reconciliation

    Return each order with item_total, successful_payment_total, and balance_due without multiplying either measure.

    • Joins
    • Subqueries
    • Aggregation

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  14. Exercise 14Medium

    Inequality JOIN — Map Salaries to Pay Bands (ON ... BETWEEN ...)

    Join employees to every job range containing their salary and return all matching bands.

    • Joins
    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  15. Exercise 15Hard

    Temporal "As-Of" JOIN — Salary on a Specific Date

    Join employees to salary-history intervals containing the as-of date.

    • Joins
    • Date analysis
    • NULL handling

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  16. Exercise 16Medium

    Reconcile CRM and Billing Accounts with FULL OUTER JOIN

    FULL OUTER JOIN CRM and billing accounts by account_code and label each row as Matched, CRM Only, or Billing Only.

    • Joins
    • CASE expressions
    • NULL handling

    SQLite + PostgreSQL + MySQL live · 2 guided

    Open exercise
01

Attempt

Write a query or design from the brief before opening any hints.

02

Validate

Run it against the included dataset and inspect the output.

03

Explain

Compare the verified answer and explain each choice aloud.

Learn before practicing SQL Joins

Strengthen your understanding with these targeted learning topics:

Continue practicing

SQL Practice Online

Open the interactive workspace and practice across SQL topics.