Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A shop keeps this table, Products.
ProductID Name Category Price Stock
G01 Trowel Garden 6.5 14
K02 Teapot Kitchen 12.5 3
G03 Hose Garden 19.0 0
K04 Whisk Kitchen 4.0 22
G05 Seed tray Garden 3.5 40
K06 Ladle Kitchen 5.0 9Write down the output of this script.
SELECT Name, Stock
FROM Products
WHERE Category = 'Kitchen'
ORDER BY Stock ASC;How many rows does it output, and what is the stock of the last row?
ORDER BY field ASC sorts the output smallest first (A to Z, earliest date first); DESC sorts largest first. The clauses come in the order SELECT, FROM, WHERE, ORDER BY. Keep the records that pass, sort them, then show the selected fields.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Step 1. Keep the kitchen products. Teapot (), Whisk () and Ladle ().
Step 2. Sort them by stock, smallest first. , , .
Step 3. The output.
SELECT Name, Stock
FROM Products
WHERE Category = 'Kitchen'
ORDER BY Stock ASC;The output:
Name Stock
Teapot 3
Ladle 9
Whisk 22Answer: rows; the last row is Whisk with in stock.
Log in to join the discussion.
Loading…