SQL exercises with solutions
1. Customers from Berlin Beginner
Output the name and registration date of all customers from Berlin, sorted by registration date (oldest first).
Note
WHERE stadt = 'Berlin' and ORDER BY registriert.
Show sample solution
SELECT name, registriert
FROM kunden
WHERE stadt = 'Berlin'
ORDER BY registriert;2. Cheap Non-Books Beginner
Find all products that cost less than 100 and do not belong to the category Bücher. Show name, category, and price, cheapest first.
Note
Combine two conditions with AND; "not" is <>.
Show sample solution
SELECT name, kategorie, preis
FROM produkte
WHERE preis < 100 AND kategorie <> 'Bücher'
ORDER BY preis;3. Statistics per Category Beginner
For each product category, show the number of products and the average price (rounded to 2 decimal places). Show only categories with at least one product over 20 (hint: a row-level condition).
Note
GROUP BY kategorie; the condition "price over 20" checks individual products, so it is a WHERE. Result: Elektronik, Bücher, Möbel.
Show sample solution
SELECT kategorie, COUNT(*) AS anzahl, ROUND(AVG(preis), 2) AS schnitt
FROM produkte
WHERE preis > 20
GROUP BY kategorie;4. Insert and Update Beginner
Create the customer "Finn" from Köln (registered on 2024-04-01). Afterwards, set the city of Eva (id 5) to "Bonn".
Note
INSERT INTO kunden (name, stadt, registriert) VALUES (...) and UPDATE ... SET ... WHERE id = 5.
Show sample solution
INSERT INTO kunden (name, stadt, registriert)
VALUES ('Finn', 'Köln', '2024-04-01');
UPDATE kunden SET stadt = 'Bonn' WHERE id = 5;5. Customers without an Order Intermediate
List all customers who have never placed an order. Write the solution using a LEFT JOIN and an anti-join condition.
Note
LEFT JOIN on bestellungen and then WHERE b.id IS NULL. Result: Eva.
Show sample solution
SELECT k.name
FROM kunden k
LEFT JOIN bestellungen b ON b.kunde_id = k.id
WHERE b.id IS NULL;6. Revenue per Customer Intermediate
Compute the total revenue of all non-cancelled orders for each customer (quantity times price). Customers without revenue should appear with 0. Sort in descending order.
Note
Use chains of LEFT JOINs, put the status filter in the ON condition, and use COALESCE(SUM(...), 0). Ada: 1049 + 30 + 59 = 1138.
Show sample solution
SELECT k.name,
COALESCE(SUM(bp.menge * p.preis), 0) AS umsatz
FROM kunden k
LEFT JOIN bestellungen b ON b.kunde_id = k.id AND b.status <> 'storniert'
LEFT JOIN bestellpositionen bp ON bp.bestellung_id = b.id
LEFT JOIN produkte p ON p.id = bp.produkt_id
GROUP BY k.id, k.name
ORDER BY umsatz DESC;7. Most Expensive Product per Category Advanced
Output the most expensive product for each category (category, name, price) – using a window function.
Note
ROW_NUMBER() OVER (PARTITION BY kategorie ORDER BY preis DESC) in a subquery, then WHERE rn = 1.
Show sample solution
SELECT kategorie, name, preis
FROM (
SELECT kategorie, name, preis,
ROW_NUMBER() OVER (PARTITION BY kategorie ORDER BY preis DESC) AS rn
FROM produkte
) t
WHERE rn = 1;8. Cumulative Order Value Advanced
For each non-cancelled order, show id, date, and order value, as well as the running total of the order values by date. Use a CTE for the order value.
Note
Order value = SUM(menge * preis) per order in the CTE; then SUM(wert) OVER (ORDER BY datum, id). Expected running totals: 1049, 1079, 1164, 1223, 2312 (order 4 is cancelled).
Show sample solution
WITH bw AS (
SELECT b.id, b.datum, SUM(bp.menge * p.preis) AS wert
FROM bestellungen b
JOIN bestellpositionen bp ON bp.bestellung_id = b.id
JOIN produkte p ON p.id = bp.produkt_id
WHERE b.status <> 'storniert'
GROUP BY b.id, b.datum
)
SELECT id, datum, wert,
SUM(wert) OVER (ORDER BY datum, id) AS laufende_summe
FROM bw
ORDER BY datum, id;