Practice: The Price List
Pixel Shop is redoing its price list, and you have all the tools for it. A price list is a good test of this lesson: almost every column in it is calculated.
Functions can go inside each other, and so can everything else you've learned: a function can be an argument of another function, a calculation can be inside a function, and a function can be in a condition of CASE. The initials of each customer, for example, are the first letter of the name and the first letter after the space:
SELECT name,
SUBSTR(name, 1, 1) || SUBSTR(name, INSTR(name, ' ') + 1, 1) AS initials
FROM customers
LIMIT 3;| name | initials |
|---|---|
| Emma Clark | EC |
| Liam Turner | LT |
| Olivia Brown | OB |
A few tips for queries like this:
- Build a long column step by step. Write the innermost part first, such as
INSTR(name, ' '), run it and check the result, and only then wrap it in the next function. - Count the parentheses. Every function opens one, and every one has to be closed;
incomplete inputor a syntax error often means that one is missing. - Think of whole division and of
NULL: does the calculation need a.0? Can a column beNULL, and what should the result be then? - Give every calculated column a name with
AS.
Instructions
The price list on the website shows a badge next to each game. Show the title, the price and badge: pre-order for games that aren't out yet, new for games released in 2025, and in stock for the others. Sort the games by price from the highest, and games with the same price by title:
| title | price | badge |
|---|---|---|
| Knight's Oath | 60 | new |
| Goal Rush | 50 | new |
| Ice Hockey Pro | 45 | in stock |
| Dragon Forge | 40 | in stock |
| Robot Rebellion | 40 | pre-order |
| … | … | … |
Games released before 2023 go on sale at 25% off. Below it, show their title, their price and sale_price: the price times 0.75, rounded to whole dollars and shown as a whole number (23, not 23.0). Sort them by release date:
| title | price | sale_price |
|---|---|---|
| Cube Logic | 5 | 4 |
| Castle Siege | 30 | 23 |
| Ocean Deep | 15 | 11 |
| Lost Temple | 20 | 15 |
| Moon Farm | 20 | 15 |
Receipts have room for 13 characters of a title. Show label: titles of up to 13 characters as they are, and longer ones cut to their first 10 characters followed by ..., so 13 characters in all. Then show the price. Sort the games by price from the highest, and games with the same price by title:
| label | price |
|---|---|
| Knight's Oath | 60 |
| Goal Rush | 50 |
| Ice Hockey... | 45 |
| … | … |
Start programming for free
10/10