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;
nameinitials
Emma ClarkEC
Liam TurnerLT
Olivia BrownOB

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 input or 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 be NULL, 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:

titlepricebadge
Knight's Oath60new
Goal Rush50new
Ice Hockey Pro45in stock
Dragon Forge40in stock
Robot Rebellion40pre-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:

titlepricesale_price
Cube Logic54
Castle Siege3023
Ocean Deep1511
Lost Temple2015
Moon Farm2015

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:

labelprice
Knight's Oath60
Goal Rush50
Ice Hockey...45
……

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

10/10