Example 1
Compute the total stock value for each inventory item
price * quantity is evaluated per row and named total_value with AS. The alias can then be used in ORDER BY to sort by the computed column. Cherries (5 × 30 = 150) rank first, then Apples (2 × 50 = 100), then Bananas (1 × 80 = 80).
CREATE TABLE inventory (id INT, name VARCHAR(50), price INT, quantity INT);
INSERT INTO
inventory
VALUES
(1, 'Apples', 2, 50),
(2, 'Bananas', 1, 80),
(3, 'Cherries', 5, 30);SELECT
name,
price,
quantity,
price * quantity AS total_value
FROM
inventory
ORDER BY
total_value DESC;| name | price | quantity | total_value |
|---|---|---|---|
| Cherries | 5 | 30 | 150 |
| Apples | 2 | 50 | 100 |
| Bananas | 1 | 80 | 80 |
Identical syntax and result across all engines.