The Smallest and the Largest

MIN(column) finds the smallest value of a column, MAX(column) the largest one. The cheapest and the most expensive game:

SELECT MIN(price) AS cheapest,
       MAX(price) AS most_expensive
FROM games;
cheapestmost_expensive
560

They work on text and dates too. For text, the smallest value is the first one in alphabetical order and the largest the last one, in the same order in which ORDER BY sorts. Dates written as 'YYYY-MM-DD' sort as dates do, so MIN finds the earliest one and MAX the latest:

SELECT MIN(city) AS first_city,
       MAX(city) AS last_city,
       MIN(joined) AS first_joined,
       MAX(joined) AS last_joined
FROM customers;
first_citylast_cityfirst_joinedlast_joined
BristolManchester2023-01-152025-10-03

Notice that the first city isn't NULL, although ORDER BY city puts NULL first. Like the other aggregate functions, MIN and MAX skip NULL.

As you can see, one query can have several aggregate functions, and different ones. They all work on the same rows, those that WHERE has left. A short summary of the action games:

SELECT COUNT(*) AS game_count,
       MIN(price) AS cheapest,
       MAX(price) AS most_expensive,
       ROUND(AVG(price), 1) AS average_price
FROM games
WHERE genre = 'Action';
game_countcheapestmost_expensiveaverage_price
4254032.5

Typical mistake: you want the title of the cheapest game and write SELECT title, MIN(price) FROM games;. In SQLite, this happens to work: with MIN and MAX, it takes the other columns from the row with the smallest or the largest value, here Cube Logic. But other databases report an error, and when two games share the lowest price, SQLite shows just one of them. Use what you know from lesson 2 instead, ORDER BY price LIMIT 1, and to see whether another game costs the same, show a few more rows.

Instructions

Show the date of the first and of the last order, in columns first_order and last_order:

first_orderlast_order
2024-01-202025-12-22

Below it, summarize the reviews of Dragon Forge (game_id 1): their number, the fewest and the most stars, and the average stars rounded to 2 decimal places, in columns review_count, fewest_stars, most_stars and average_stars:

review_countfewest_starsmost_starsaverage_stars
4354.25

And from games, show the title that comes first in alphabetical order and the one that comes last, in columns first_title and last_title:

first_titlelast_title
Bakery StoryZombie Town

Start programming for free

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

Or sign up with:

4/10