SQL LEARNING PLATFORM
Queries within queries — nesting for power.
Learning Blocks
Interactive Queries
Concepts you'll master
Subquery in WHERE to filter on a computed value
Subquery with IN for list-based filtering
Subquery in FROM as a derived table
EXISTS — checking if a subquery returns any rows
🌟 Think of it this way: Subqueries are like asking two questions where the answer to the first feeds the second: 'What is the average salary?' → ₹70,125. 'Who earns more than ₹70,125?' Both steps happen in one SQL statement.
SELECT name, salary FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);
SELECT name FROM customers WHERE customer_id IN ( SELECT customer_id FROM orders WHERE order_date BETWEEN '2024-01-01' AND '2024-01-31' );
EXISTS is faster than IN for large subqueries. It short-circuits as soon as one matching row is found:
SELECT c.name FROM customers c WHERE EXISTS ( SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id );
💡 Engineering Insight: EXISTS vs IN performance: EXISTS stops scanning as soon as it finds the first match. IN collects every matching ID first. On tables with millions of rows, EXISTS can be 10-100x faster. Use EXISTS when you only care 'does a matching row exist?' not 'how many?'
Practice your SQL skills