Learn SQL through fun interactive exercises ✨
Expected output:
Select columns from a table
SELECT name FROM users
SELECT * FROM products
SELECT name, price FROM products
-- * means all columns
Filter rows with conditions
WHERE price > 100
WHERE name = 'Alice'
WHERE age >= 18 AND dept = 'IT'
WHERE name LIKE 'A%'
-- % matches any characters
Sort results
ORDER BY name ASC
ORDER BY price DESC
ORDER BY dept, salary DESC
-- ASC=ascending, DESC=descending
Combine tables
INNER JOIN orders ON o.cust_id = c.id
LEFT JOIN orders ON ...
JOIN products p ON o.prod_id = p.id
-- table alias: o, c, p
Group rows for aggregation
GROUP BY department
GROUP BY category HAVING COUNT > 2
GROUP BY region ORDER BY total
-- Use HAVING for group filters
Calculate values
COUNT(*) as total
SUM(salary) as total
AVG(price) as average
MIN(age), MAX(age)
Limit number of results
LIMIT 10
LIMIT 10 OFFSET 20
LIMIT 5, 10
-- Skip 5, take 10
Useful commands
DISTINCT category
col IN ('a', 'b', 'c')
price BETWEEN 10 AND 100
IS NULL / IS NOT NULL
Query inside query
WHERE sal > (SELECT AVG(sal) FROM emp)
WHERE dept IN (SELECT dept FROM ...)
SELECT * FROM (SELECT ...)
Rename columns/tables
COUNT(*) as employee_count
SUM(price) as total_price
employees as e
products as p
All IT employees sorted by salary:
SELECT * FROM employees WHERE department = 'IT' ORDER BY salary DESC
Revenue per customer:
SELECT c.name, SUM(o.total) as revenue FROM customers c JOIN orders o ON c.id = o.customer_id GROUP BY c.id
Top 3 expensive products:
SELECT * FROM products ORDER BY price DESC LIMIT 3
Categories with 2+ products:
SELECT category, COUNT(*) as cnt FROM products GROUP BY category HAVING COUNT(*) >= 2
Made with 💜 for Sara • SQL Practice App