SQL Topic collection

SQL Subqueries SQL Topic exercises

Practice scalar, derived-table, correlated, EXISTS, NULL-safe anti-subquery, quantified, and row-subquery patterns.

Exercises
12
Difficulty
Beginner to Advanced

What this collection tests

Skills and query patterns

Subquery cardinality, correlation, NULL semantics, IN, EXISTS, ANY/ALL, derived tables, and tie-safe top-per-group logic

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

    Find Employees Above the Company Average

    Use a scalar subquery in WHERE to return employee_id, first_name, last_name, and salary.

    • Subqueries
    • Aggregation
    • Filtering

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

    Open exercise
  2. Exercise 2Easy

    Show Each Salary Against the Company Average

    Use a scalar subquery in the SELECT list to calculate company_avg and salary_minus_avg for every employee.

    • Subqueries
    • Aggregation
    • Sorting

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

    Open exercise
  3. Exercise 3Easy

    Find Employees in West Coast Departments with IN

    Use IN with a subquery that returns matching department IDs, then return the requested employee and department columns.

    • Joins
    • Subqueries
    • Filtering

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

    Open exercise
  4. Exercise 4Medium

    Find Departments Larger Than the Average Department

    Build department headcounts in a derived table, average them in a scalar subquery, and filter the outer grouped result.

    • Joins
    • Subqueries
    • Aggregation

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

    Open exercise
  5. Exercise 5Medium

    Find Employees Above Their Department Average

    Use a correlated scalar subquery that references the outer employee department.

    • Joins
    • Subqueries
    • Aggregation

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

    Open exercise
  6. Exercise 6Medium

    Find Departments with an Above-Average Earner

    Use correlated EXISTS for department membership and a scalar subquery for the company average.

    • Joins
    • Subqueries
    • Aggregation

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

    Open exercise
  7. Exercise 7Medium

    Find Accounts Without Flags Using NULL-Safe NOT EXISTS

    Return account_id and account_name for accounts with no matching row in subquery_account_flags.

    • Subqueries
    • Filtering
    • Sorting

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

    Open exercise
  8. Exercise 8Hard

    Find Departments Where Every Employee Clears the Pay Floor

    Express “every employee earns above $60,000” with an existence guard and a correlated NOT EXISTS violation check.

    • Subqueries
    • Filtering
    • Sorting

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

    Open exercise
  9. Exercise 9Hard

    Find Salaries Higher Than Any HR Salary

    Apply ANY semantics to the HR salary subquery and return the qualifying employees.

    • Subqueries
    • Filtering
    • Sorting

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

    Open exercise
  10. Exercise 10Hard

    Find Salaries Higher Than All HR Salaries

    Apply ALL semantics to the HR salary subquery and return the qualifying employees.

    • Subqueries
    • Filtering
    • Sorting

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

    Open exercise
  11. Exercise 11Hard

    Find Every Top-Paid Employee per Department

    Use a correlated MAX scalar subquery scoped to the outer employee department.

    • Joins
    • Subqueries
    • Filtering

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

    Open exercise
  12. Exercise 12Hard

    Find Every Top-Paid Employee per Job with a Row Subquery

    Match each (job_id, salary) pair against grouped job maxima returned by a multi-column subquery.

    • Joins
    • Subqueries
    • Aggregation

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 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 Subqueries

Strengthen your understanding with these targeted learning topics:

Continue practicing

SQL Practice Online

Open the interactive workspace and practice across SQL topics.