Part 02 / Topic 2 of 9
Expressions and aliases
Columns can be computed, not just copied. The stock's shelf value is price times stock, and AS names the result:
SELECT name, price, stock, price * stock AS shelf_value
FROM products
WHERE stock > 0 AND stock < 20;
name | price | stock | shelf_value
---------------+---------+-------+-------------
Laptop Pro 14 | 1200.00 | 15 | 18000.00
Laptop Air 13 | 950.00 | 8 | 7600.00
Desk Lamp | 45.00 | 12 | 540.00
(3 rows)
Without AS, calculated columns come back as ?column? — name them, always.
Two quoting rules meet here, and mixing them up is the classic day-one error:
- Single quotes = text values:
'Pune' - Double quotes = identifiers (column/table names):
"first_name"
You almost never need double quotes — they only matter for names with capitals or spaces, which section 7 of Part 1 told you not to create. One place they are handy: a report wants a human column heading.
SELECT name AS "Product", price AS "Price (USD)"
FROM products
ORDER BY price DESC
LIMIT 3;
Product | Price (USD)
---------------+-------------
Laptop Pro 14 | 1200.00
Laptop Air 13 | 950.00
Phone X | 799.00
(3 rows)
🧪 Quick quiz — answer from memory, then check.
- What is the difference between
'name'and"name"in a query? - Write a column that shows each product's price with 18% tax, named
with_tax.
Show the answers
'name'is the four-letter text value;"name"refers to a column called name.SELECT price * 1.18 AS with_tax FROM products;
Done here? The practice set for Part 2 — 8 exercises with solutions — is on the part page.