Practice challenges
Last updated
Five questions, no walkthrough. Write the query, press Run to inspect, and press Check when you think the rows are right. Everything you need is in the earlier lessons. Use Reset if you want a clean slate.
Stuck on one? Ask your AI assistant for a hint, not the answer: Give me a hint for this task, not the full query. Try the hint here first. If you do take a full query from the AI, run it and read the rows before you press Check, so you learn why it works.
1. Penang customers
Return the name of every customer based in Penang.
CREATE TABLE customers ( id INTEGER PRIMARY KEY, name TEXT, city TEXT, signup_date TEXT ); INSERT INTO customers VALUES (1, 'Aisyah Rahman', 'Kuala Lumpur', '2023-01-12'), (2, 'Lim Wei Jie', 'Penang', '2023-02-03'), (3, 'Arjun Pillai', 'Johor Bahru', '2023-02-20'), (4, 'Nurul Huda', 'Kuala Lumpur', '2023-03-15'), (5, 'Tan Mei Ling', 'Ipoh', '2023-05-01'), (6, 'Faiz Hassan', 'Penang', '2023-06-10'); CREATE TABLE products ( id INTEGER PRIMARY KEY, name TEXT, category TEXT, price REAL ); INSERT INTO products VALUES (1, 'Standard License', 'Software', 199.0), (2, 'Pro License', 'Software', 499.0), (3, 'Onboarding Workshop', 'Service', 1200.0), (4, 'Support Plan', 'Service', 300.0), (5, 'Data Pack', 'Add-on', 89.0); CREATE TABLE orders ( id INTEGER PRIMARY KEY, customer_id INTEGER, order_date TEXT, status TEXT ); INSERT INTO orders VALUES (1001, 1, '2023-03-01', 'paid'), (1002, 1, '2023-04-12', 'paid'), (1003, 2, '2023-04-15', 'paid'), (1004, 3, '2023-05-02', 'refunded'), (1005, 4, '2023-05-20', 'paid'), (1006, 2, '2023-06-01', 'pending'), (1007, 5, '2023-06-18', 'paid'); CREATE TABLE order_items ( order_id INTEGER, product_id INTEGER, quantity INTEGER ); INSERT INTO order_items VALUES (1001, 1, 2), (1001, 5, 1), (1002, 2, 1), (1003, 1, 1), (1003, 4, 1), (1004, 3, 1), (1005, 2, 2), (1005, 5, 3), (1006, 1, 1), (1007, 3, 1), (1007, 4, 2);
SELECT FROM customers ;
SELECT name FROM customers WHERE city = 'Penang';
2. Paid orders, newest first
Return id and order_date for paid orders, newest first.
CREATE TABLE customers ( id INTEGER PRIMARY KEY, name TEXT, city TEXT, signup_date TEXT ); INSERT INTO customers VALUES (1, 'Aisyah Rahman', 'Kuala Lumpur', '2023-01-12'), (2, 'Lim Wei Jie', 'Penang', '2023-02-03'), (3, 'Arjun Pillai', 'Johor Bahru', '2023-02-20'), (4, 'Nurul Huda', 'Kuala Lumpur', '2023-03-15'), (5, 'Tan Mei Ling', 'Ipoh', '2023-05-01'), (6, 'Faiz Hassan', 'Penang', '2023-06-10'); CREATE TABLE products ( id INTEGER PRIMARY KEY, name TEXT, category TEXT, price REAL ); INSERT INTO products VALUES (1, 'Standard License', 'Software', 199.0), (2, 'Pro License', 'Software', 499.0), (3, 'Onboarding Workshop', 'Service', 1200.0), (4, 'Support Plan', 'Service', 300.0), (5, 'Data Pack', 'Add-on', 89.0); CREATE TABLE orders ( id INTEGER PRIMARY KEY, customer_id INTEGER, order_date TEXT, status TEXT ); INSERT INTO orders VALUES (1001, 1, '2023-03-01', 'paid'), (1002, 1, '2023-04-12', 'paid'), (1003, 2, '2023-04-15', 'paid'), (1004, 3, '2023-05-02', 'refunded'), (1005, 4, '2023-05-20', 'paid'), (1006, 2, '2023-06-01', 'pending'), (1007, 5, '2023-06-18', 'paid'); CREATE TABLE order_items ( order_id INTEGER, product_id INTEGER, quantity INTEGER ); INSERT INTO order_items VALUES (1001, 1, 2), (1001, 5, 1), (1002, 2, 1), (1003, 1, 1), (1003, 4, 1), (1004, 3, 1), (1005, 2, 2), (1005, 5, 3), (1006, 1, 1), (1007, 3, 1), (1007, 4, 2);
SELECT id, order_date FROM orders ;
SELECT id, order_date FROM orders WHERE status = 'paid' ORDER BY order_date DESC;
3. Revenue per product
Total units sold per product_id, named units, busiest first.
CREATE TABLE customers ( id INTEGER PRIMARY KEY, name TEXT, city TEXT, signup_date TEXT ); INSERT INTO customers VALUES (1, 'Aisyah Rahman', 'Kuala Lumpur', '2023-01-12'), (2, 'Lim Wei Jie', 'Penang', '2023-02-03'), (3, 'Arjun Pillai', 'Johor Bahru', '2023-02-20'), (4, 'Nurul Huda', 'Kuala Lumpur', '2023-03-15'), (5, 'Tan Mei Ling', 'Ipoh', '2023-05-01'), (6, 'Faiz Hassan', 'Penang', '2023-06-10'); CREATE TABLE products ( id INTEGER PRIMARY KEY, name TEXT, category TEXT, price REAL ); INSERT INTO products VALUES (1, 'Standard License', 'Software', 199.0), (2, 'Pro License', 'Software', 499.0), (3, 'Onboarding Workshop', 'Service', 1200.0), (4, 'Support Plan', 'Service', 300.0), (5, 'Data Pack', 'Add-on', 89.0); CREATE TABLE orders ( id INTEGER PRIMARY KEY, customer_id INTEGER, order_date TEXT, status TEXT ); INSERT INTO orders VALUES (1001, 1, '2023-03-01', 'paid'), (1002, 1, '2023-04-12', 'paid'), (1003, 2, '2023-04-15', 'paid'), (1004, 3, '2023-05-02', 'refunded'), (1005, 4, '2023-05-20', 'paid'), (1006, 2, '2023-06-01', 'pending'), (1007, 5, '2023-06-18', 'paid'); CREATE TABLE order_items ( order_id INTEGER, product_id INTEGER, quantity INTEGER ); INSERT INTO order_items VALUES (1001, 1, 2), (1001, 5, 1), (1002, 2, 1), (1003, 1, 1), (1003, 4, 1), (1004, 3, 1), (1005, 2, 2), (1005, 5, 3), (1006, 1, 1), (1007, 3, 1), (1007, 4, 2);
SELECT product_id, SUM(quantity) AS units FROM order_items ;
SELECT product_id, SUM(quantity) AS units FROM order_items GROUP BY product_id ORDER BY units DESC;
4. Orders with the customer city
Each order id with the customer's city, joining orders to customers.
CREATE TABLE customers ( id INTEGER PRIMARY KEY, name TEXT, city TEXT, signup_date TEXT ); INSERT INTO customers VALUES (1, 'Aisyah Rahman', 'Kuala Lumpur', '2023-01-12'), (2, 'Lim Wei Jie', 'Penang', '2023-02-03'), (3, 'Arjun Pillai', 'Johor Bahru', '2023-02-20'), (4, 'Nurul Huda', 'Kuala Lumpur', '2023-03-15'), (5, 'Tan Mei Ling', 'Ipoh', '2023-05-01'), (6, 'Faiz Hassan', 'Penang', '2023-06-10'); CREATE TABLE products ( id INTEGER PRIMARY KEY, name TEXT, category TEXT, price REAL ); INSERT INTO products VALUES (1, 'Standard License', 'Software', 199.0), (2, 'Pro License', 'Software', 499.0), (3, 'Onboarding Workshop', 'Service', 1200.0), (4, 'Support Plan', 'Service', 300.0), (5, 'Data Pack', 'Add-on', 89.0); CREATE TABLE orders ( id INTEGER PRIMARY KEY, customer_id INTEGER, order_date TEXT, status TEXT ); INSERT INTO orders VALUES (1001, 1, '2023-03-01', 'paid'), (1002, 1, '2023-04-12', 'paid'), (1003, 2, '2023-04-15', 'paid'), (1004, 3, '2023-05-02', 'refunded'), (1005, 4, '2023-05-20', 'paid'), (1006, 2, '2023-06-01', 'pending'), (1007, 5, '2023-06-18', 'paid'); CREATE TABLE order_items ( order_id INTEGER, product_id INTEGER, quantity INTEGER ); INSERT INTO order_items VALUES (1001, 1, 2), (1001, 5, 1), (1002, 2, 1), (1003, 1, 1), (1003, 4, 1), (1004, 3, 1), (1005, 2, 2), (1005, 5, 3), (1006, 1, 1), (1007, 3, 1), (1007, 4, 2);
SELECT o.id, c.city FROM orders AS o JOIN customers AS c ON ;
SELECT o.id, c.city FROM orders AS o JOIN customers AS c ON o.customer_id = c.id;
5. Customers with no paid order
Names of customers who have never placed a paid order. Hint: left join to only the paid orders.
CREATE TABLE customers ( id INTEGER PRIMARY KEY, name TEXT, city TEXT, signup_date TEXT ); INSERT INTO customers VALUES (1, 'Aisyah Rahman', 'Kuala Lumpur', '2023-01-12'), (2, 'Lim Wei Jie', 'Penang', '2023-02-03'), (3, 'Arjun Pillai', 'Johor Bahru', '2023-02-20'), (4, 'Nurul Huda', 'Kuala Lumpur', '2023-03-15'), (5, 'Tan Mei Ling', 'Ipoh', '2023-05-01'), (6, 'Faiz Hassan', 'Penang', '2023-06-10'); CREATE TABLE products ( id INTEGER PRIMARY KEY, name TEXT, category TEXT, price REAL ); INSERT INTO products VALUES (1, 'Standard License', 'Software', 199.0), (2, 'Pro License', 'Software', 499.0), (3, 'Onboarding Workshop', 'Service', 1200.0), (4, 'Support Plan', 'Service', 300.0), (5, 'Data Pack', 'Add-on', 89.0); CREATE TABLE orders ( id INTEGER PRIMARY KEY, customer_id INTEGER, order_date TEXT, status TEXT ); INSERT INTO orders VALUES (1001, 1, '2023-03-01', 'paid'), (1002, 1, '2023-04-12', 'paid'), (1003, 2, '2023-04-15', 'paid'), (1004, 3, '2023-05-02', 'refunded'), (1005, 4, '2023-05-20', 'paid'), (1006, 2, '2023-06-01', 'pending'), (1007, 5, '2023-06-18', 'paid'); CREATE TABLE order_items ( order_id INTEGER, product_id INTEGER, quantity INTEGER ); INSERT INTO order_items VALUES (1001, 1, 2), (1001, 5, 1), (1002, 2, 1), (1003, 1, 1), (1003, 4, 1), (1004, 3, 1), (1005, 2, 2), (1005, 5, 3), (1006, 1, 1), (1007, 3, 1), (1007, 4, 2);
SELECT c.name FROM customers AS c LEFT JOIN orders AS o ON o.customer_id = c.id AND o.status = 'paid' WHERE ;
SELECT c.name FROM customers AS c LEFT JOIN orders AS o ON o.customer_id = c.id AND o.status = 'paid' WHERE o.id IS NULL;
That is the working core of analytics SQL. From here the same clauses scale to far bigger tables, and tools like Power BI generate exactly this kind of query under the hood. The next step is to point these skills at a real database and answer a question you actually have.