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;
titleprice
Shadow Ninja25
Zombie Town30
Star Pilots35

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;
titleprice
Shadow Ninja25
Zombie Town30
Star Pilots35
Robot Rebellion40

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_idcustomerordered_attotal
11Liam Turner2024-09-3085
14Emma Clark2024-12-2075
5Noah Wilson2024-04-0265

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:

customerspent
Emma Clark200
Liam Turner150
Noah Wilson150
Olivia Brown150
Jack Harris95
Oscar King95
……

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_idcustomerordered_attotal
31Henry Scott2025-12-3050

Start programming for free

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

Or sign up with:

9/10