Practice exercises for SQL beginners
Reading about SQL is not enough. The only way to actually learn it is to write queries — make mistakes, debug them, and try again. These exercises give you structured problems to work through, from your first SELECT all the way to subqueries.
Before you start
You need somewhere to run queries. Both of these are free and require no setup:
- BigQuery Sandbox — Google's cloud SQL environment. Free tier, no credit card needed. Best for learning industry-standard SQL.
- SQLiteOnline.com — Runs entirely in your browser. Paste in the CREATE TABLE statements and start querying immediately. Great for quick practice.
Tip: Try writing the query yourself before revealing the answer. Even an incorrect attempt will teach you more than reading the solution.
Beginner5 exercises
Select all columns
SELECT *Write a query to return all rows and columns from the orders table.
▶Show answer
Try writing the query yourself before revealing the answer
SELECT * FROM orders;Select specific columns
SELECT columnsReturn only the customer_name and order_date from the orders table.
▶Show answer
Try writing the query yourself before revealing the answer
SELECT customer_name, order_date FROM orders;Filter with WHERE
WHEREReturn all orders where the order_status is "completed".
▶Show answer
Try writing the query yourself before revealing the answer
SELECT * FROM orders WHERE order_status = 'completed';Count rows
COUNTHow many orders are in the orders table?
▶Show answer
Try writing the query yourself before revealing the answer
SELECT COUNT(*) FROM orders;Sort results
ORDER BYReturn all orders sorted by order_date from newest to oldest.
▶Show answer
Try writing the query yourself before revealing the answer
SELECT * FROM orders ORDER BY order_date DESC;Intermediate5 exercises
Group and count
GROUP BY + COUNTHow many orders does each customer have? Return customer_name and order count.
▶Show answer
Try writing the query yourself before revealing the answer
SELECT customer_name, COUNT(*) as order_count FROM orders GROUP BY customer_name;Average value
AVG + GROUP BYWhat is the average order value per customer?
▶Show answer
Try writing the query yourself before revealing the answer
SELECT customer_name, AVG(order_value) as avg_value FROM orders GROUP BY customer_name;Filter groups with HAVING
HAVINGReturn customers who have placed more than 5 orders.
▶Show answer
Try writing the query yourself before revealing the answer
SELECT customer_name, COUNT(*) as order_count FROM orders GROUP BY customer_name HAVING COUNT(*) > 5;JOIN two tables
INNER JOINReturn the order_id and customer email for all orders. Orders table has customer_id, customers table has customer_id and email.
▶Show answer
Try writing the query yourself before revealing the answer
SELECT o.order_id, c.email FROM orders o INNER JOIN customers c ON o.customer_id = c.customer_id;LEFT JOIN
LEFT JOINReturn all customers and their total order count, including customers with zero orders.
▶Show answer
Try writing the query yourself before revealing the answer
SELECT c.customer_name, COUNT(o.order_id) as order_count FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id GROUP BY c.customer_name;Advanced2 exercises
Subquery
SubqueryReturn all orders with a value above the average order value.
▶Show answer
Try writing the query yourself before revealing the answer
SELECT * FROM orders WHERE order_value > (SELECT AVG(order_value) FROM orders);CASE WHEN
CASE WHENAdd a column categorizing each order as Low (<50), Medium (50-200), or High (>200) based on order_value.
▶Show answer
Try writing the query yourself before revealing the answer
SELECT *, CASE WHEN order_value < 50 THEN 'Low' WHEN order_value <= 200 THEN 'Medium' ELSE 'High' END as value_tier FROM orders;Ready to go further?
Build on these skills in the Data Analyst track — quiz-gated lessons that take you from SQL basics to real analyst workflows.
Build on these skills in the Data Analyst track