This guide is specifically for MySQL syntax.
Intermediate-level SQL Subqueries practice exercises with solutions.
Exercise 1
Question: Find employees who earn more than the average salary.
SELECT name, salary FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);
A scalar subquery returns a single value and can be used with comparison operators in WHERE clause.
Exercise 2
Question: Find products that have never been ordered.
SELECT name FROM products WHERE id NOT IN (SELECT DISTINCT product_id FROM order_items);
Subquery with NOT IN finds values that don't exist in another table's results.
MySQL-Specific Notes
This page covers MySQL syntax. Other databases may have different syntax for similar operations.