Before you start, you need
Project: query a library database · Practice: master window functions · Practice: modify data inside a transaction · Practice: design a table with rules and an index
How it works
Complete five activities: filters and ordering, joins and summaries, CTEs and window functions, data changes and table design. Each activity runs separately and stores its own result.
Start without hints. If you get stuck, try part of the query and look at what it returns. You can ask for help, but the result will be marked as solved with help.
There is no automatic overall score. Check the status of each block and see which skill you have shown. A correct revenue query does not replace the filtering, data-change or design activities.
If you need to revise, go back to the relevant lesson and then return to the exam. Marking the page as finished is a manual decision: it is not the same as solving its five activities.
Basic queries
Practice
Block 1: filters and ordering
Show name and stock for available products whose name contains the letter 'o'. Sort by stock descending and name ascending.
Expected result: 5 rows with the columns name, stock, in that order. Keep the row order requested.
View tables and data
Open this panel to see the full data.
You need both availability and a letter in the name.
Sort by highest stock first, and use the name for ties.
Join the conditions with AND and use a pattern that accepts the letter in any position.
Solution explained
SELECT name,stock FROM products WHERE stock > 0 AND name LIKE '%o%' ORDER BY stock DESC,name;The filter combines availability and a pattern; the ordering is checked separately.
Intermediate queries
Assessment by skill
Block 2: relationships and aggregation
Calculate the amount of non-cancelled orders per customer. Return name and the sum with the alias billing; keep only customers whose sum is above 100. Do not change any data.
Expected result: 6 rows with the columns name, billing, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
Each final row must represent one customer, with the sum of their non-cancelled orders.
Filter out cancelled orders before grouping.
Filter the sum with HAVING.
Solution explained
SELECT c.name, SUM(p.total) AS billing FROM customers AS c JOIN orders AS p ON c.id = p.customer_id WHERE p.status <> 'cancelled' GROUP BY c.id, c.name HAVING SUM(p.total) > 100;The query follows a sequence you can check: join, row filter, summary and group filter.
Block 3: CTEs and window functions
Practice
Block 3: CTEs and window functions
Using a CTE, number employees per department by salary descending and id ascending. Return department and name for the first position in each group, sorted by department.
Expected result: 5 rows with the columns department, name, in that order. Keep the row order requested.
View tables and data
Open this panel to see the full data.
Why should position 1 be filtered in an outer query and not where ROW_NUMBER is calculated?
Number the rows inside a CTE with PARTITION BY department and ORDER BY salary DESC, id.
The outer query filters WHERE position = 1 and sorts by department.
Solution explained
WITH positions AS (SELECT department,name,ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC,id) AS position FROM employees) SELECT department,name FROM positions WHERE position = 1 ORDER BY department;The CTE makes it possible to filter on the rank outside the window.
Advanced queries
Practice
Block 4: a committed change
Order 102 has been shipped. Open a transaction, change only its status to 'shipped', check id and status, and commit.
We check the full final state: the change you were asked to make and that every other row is untouched. Review the output of each statement too.
Every attempt resets the data, so include all the steps you need to check in a single attempt.
View tables and data
Open this panel to see the full data.
What should the SELECT show before the transaction is closed, and which value is committed afterwards?
Open with BEGIN, change only order 102 with UPDATE ... WHERE id = 102, and check its id and status.
Finish with COMMIT, because the shipment is already confirmed.
Solution explained
BEGIN; UPDATE orders SET status = 'shipped' WHERE id = 102; SELECT id,status FROM orders WHERE id = 102; COMMIT;The scope, the provisional check and the committed state are all checked.
Block 5: design
Practice
Block 5: design
Create ratings so that each customer can rate a product from 0 to 10 only once, and prepare its index. Use the following columns and rules:
- id: INTEGER PRIMARY KEY.
- product_id: INTEGER NOT NULL REFERENCES products(id).
- customer_id: INTEGER NOT NULL REFERENCES customers(id).
- score: INTEGER NOT NULL with a CHECK that accepts 0 to 10, both included.
- A customer can rate each product only once: the pair product_id and customer_id must be unique.
- Create idx_ratings_product on product_id.
We check the requested structure, the valid rows, the default values and that invalid rows are rejected.
Every attempt resets the data, so include all the steps you need to check in a single attempt.
View tables and data
Open this panel to see the full data.
Which scores should the table accept? 0 is also valid.
Combine NOT NULL, REFERENCES and CHECK in the columns and add UNIQUE (product_id, customer_id) at the end.
Create the index with a second statement: CREATE INDEX idx_ratings_product ON ratings(product_id).
Solution explained
CREATE TABLE ratings (id INTEGER PRIMARY KEY, product_id INTEGER NOT NULL REFERENCES products(id), customer_id INTEGER NOT NULL REFERENCES customers(id), score INTEGER NOT NULL CHECK (score BETWEEN 0 AND 10), UNIQUE (product_id, customer_id)); CREATE INDEX idx_ratings_product ON ratings(product_id);The block is checked against valid and invalid rows, not just SQL keywords.
Result and next steps
Check the status of each activity: pending, solved with help or solved without help. If a skill needs work, go back to its lesson before trying the block again.
- A correct query shows the skill its block tests.
- Check rows, missing values and ordering, not just that the query runs.
- Using hints is recorded as help.
- Go back to the lesson for any skill you still need to practise.