Views
Some queries are needed again and again: the manager wants the totals of the orders every week, and support keeps looking for the games that are out. A view gives a query a name, and then you can use it as if it were a table:
CREATE VIEW released_games AS
SELECT title, genre, price
FROM games
WHERE released IS NOT NULL;After CREATE VIEW, the name and AS comes an ordinary SELECT. The view is then queried like any table, with WHERE, ORDER BY, joins and all:
SELECT title, price
FROM released_games
WHERE genre = 'Action'
ORDER BY price;| title | price |
|---|---|
| Shadow Ninja | 25 |
| Zombie Town | 30 |
| Star Pilots | 35 |
A view holds no rows of its own. It keeps only the query, and the database runs it whenever you use the view. So a view always shows the current data: when Robot Rebellion gets its release date, it appears among the released games, and nobody has to change the view:
UPDATE games
SET released = '2026-02-27'
WHERE id = 18;
SELECT title, price
FROM released_games
WHERE genre = 'Action'
ORDER BY price;| title | price |
|---|---|
| Shadow Ninja | 25 |
| Zombie Town | 30 |
| Star Pilots | 35 |
| Robot Rebellion | 40 |
Views are handy for long queries with joins and groups, which others can then use without knowing how they work, and for calculations that should be done the same way everywhere. A view can also leave out columns that some people shouldn't see. The columns of a view are named as in its SELECT.
You can only read a view: INSERT, UPDATE and DELETE on it fail with cannot modify released_games because it is a view. The data is changed in the tables the view reads. A view you no longer need is deleted with DROP VIEW released_games;, and a view you want to change, you delete and create again. Like the tables you create, views don't appear in the Tables tab.
Typical mistake: a calculated column without an alias. In a view made of SELECT genre, SUM(price) FROM games GROUP BY genre, the second column is called SUM(price), which is awkward to use in queries on the view. With SUM(price) AS total, it's simply total.
Instructions
Create a view order_totals with one row for each paid order and these columns: order_id (the ID of the order), customer (the name of the customer), ordered_at and total (what the order's items cost together: the quantity times the price of each item in order_items). Below it, show the three orders with the highest total from the view; for equal totals, the smaller order_id first:
| order_id | customer | ordered_at | total |
|---|---|---|---|
| 11 | Liam Turner | 2024-09-30 | 85 |
| 14 | Emma Clark | 2024-12-20 | 75 |
| 5 | Noah Wilson | 2024-04-02 | 65 |
Use the view to show how much each customer has spent, in the columns customer and spent, sorted by the amount from the largest, and customers with the same amount by name. The result starts like this:
| customer | spent |
|---|---|
| Emma Clark | 200 |
| Liam Turner | 150 |
| Noah Wilson | 150 |
| Olivia Brown | 150 |
| Jack Harris | 95 |
| Oscar King | 95 |
| … | … |
A view always shows the current data. At the end of the file, add a new paid order: order 31 of Henry Scott (customer 12) on 2025-12-30, with one copy of Knight's Oath (game 15), which he got for $50 in a sale. Then show all columns of the rows of the view whose customer is Henry Scott:
| order_id | customer | ordered_at | total |
|---|---|---|---|
| 31 | Henry Scott | 2025-12-30 | 50 |
Start programming for free
9/10