Codility SQL Practice Questions & Answers
Easy - Question 1:
You are given a table `elements(v integer not null)`. Write a query to return the sum of all values.
SELECT SUM(v) AS total FROM elements;
Easy - Question 2:
You are given a table `data(id integer)`. Write a query to count the number of unique values in column `id`.
SELECT COUNT(DISTINCT id) AS unique_count FROM data;
Medium - Question 3:
You are given a table `items(name text)`. Write a query to return the names of items that appear more than
once.
SELECT name FROM items GROUP BY name HAVING COUNT(*) > 1;
Medium - Question 4:
You are given a table `sales(department text, amount integer)`. Write a query to return the maximum sale
amount per department.
SELECT department, MAX(amount) AS max_sales FROM sales GROUP BY department;
Hard - Question 5:
You are given a table `scores(score integer)`. Write a query to return the second highest score.
SELECT MAX(score) AS second_highest FROM scores WHERE score < (SELECT MAX(score)
FROM scores);
Hard - Question 6:
You are given two tables:
- `customers(id, name)`
- `orders(customer_id)`
Codility SQL Practice Questions & Answers
Write a query to return the names of customers who did not make any orders.
SELECT name FROM customers WHERE id NOT IN (SELECT customer_id FROM orders);