You learn SQL by writing it. Try these before you read the answers.
Sample tables
customers(id, name, city)
orders(id, customer_id, total, created_at)
Questions
- List all customers.
- List customers from "Lagos".
- List the 5 largest orders.
- Count how many orders exist.
- Show total revenue.
- Show revenue per customer.
- Show customers with more than 3 orders.
- List customers with their order totals.
- Find customers who have no orders.
- Find the average order total.
Answers
-- 1
SELECT * FROM customers;
-- 2
SELECT * FROM customers WHERE city = 'Lagos';
-- 3
SELECT * FROM orders ORDER BY total DESC LIMIT 5;
-- 4
SELECT COUNT(*) FROM orders;
-- 5
SELECT SUM(total) FROM orders;
-- 6
SELECT customer_id, SUM(total) FROM orders GROUP BY customer_id;
-- 7
SELECT customer_id FROM orders GROUP BY customer_id HAVING COUNT(*) > 3;
-- 8
SELECT c.name, o.total FROM customers c JOIN orders o ON o.customer_id = c.id;
-- 9
SELECT c.name FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.id IS NULL;
-- 10
SELECT AVG(total) FROM orders;
Where Teyro fits
Teyro's SQL and Databases for Beginners course turns this into short lessons you can play in a few minutes a day. It has 50 lessons, and you can start free.
The bottom line
Type the queries yourself, change them and see what happens.



