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.
- 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 → - 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 → - 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 → - 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 → - 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 → - 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 → - 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 → - 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 → - 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 → - 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 → - 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 → - 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 →
Attempt
Write a query or design from the brief before opening any hints.
Validate
Run it against the included dataset and inspect the output.
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
Build the next SQL skill
CTEs & Window Functions
Practice modular CTE pipelines, deterministic window analytics, period comparisons, deduplication, frames, and gaps-and-islands.
SQL Joins
Practice reliable INNER, LEFT, FULL, CROSS, self, semi, anti, range, temporal, and many-to-many join patterns.
SQL Aggregations
Build reliable SQL metrics from aggregate functions through grain, fan-out, weighted ratios, rollups, percentiles, and approximate counts.
Open the interactive workspace and practice across SQL topics.