Welcome to

Sara's SQL Trainer

Learn SQL through fun interactive exercises ✨

🗃️

Tables

🎯

Exercise

Expected output:


                        
💻

Your Query

📊

Your Result

📖

SQL Cheat Sheet

SELECT

Select columns from a table

SELECT name FROM users SELECT * FROM products SELECT name, price FROM products -- * means all columns

WHERE

Filter rows with conditions

WHERE price > 100 WHERE name = 'Alice' WHERE age >= 18 AND dept = 'IT' WHERE name LIKE 'A%' -- % matches any characters

ORDER BY

Sort results

ORDER BY name ASC ORDER BY price DESC ORDER BY dept, salary DESC -- ASC=ascending, DESC=descending

JOIN

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 BY

Group rows for aggregation

GROUP BY department GROUP BY category HAVING COUNT > 2 GROUP BY region ORDER BY total -- Use HAVING for group filters

Aggregate

Calculate values

COUNT(*) as total SUM(salary) as total AVG(price) as average MIN(age), MAX(age)

LIMIT

Limit number of results

LIMIT 10 LIMIT 10 OFFSET 20 LIMIT 5, 10 -- Skip 5, take 10

Other

Useful commands

DISTINCT category col IN ('a', 'b', 'c') price BETWEEN 10 AND 100 IS NULL / IS NOT NULL

Subquery

Query inside query

WHERE sal > (SELECT AVG(sal) FROM emp) WHERE dept IN (SELECT dept FROM ...) SELECT * FROM (SELECT ...)

AS (Alias)

Rename columns/tables

COUNT(*) as employee_count SUM(price) as total_price employees as e products as p

💡 Quick Examples

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