SELECT decides which columns come out and what shape they take. It's the verb you'll
type in almost every query, so let's make it second nature.
SELECT * is great for peeking, but naming columns is clearer, faster, and puts them in
the order you want. Swap the column list around and run:
AS (aliases)Column names in the result don't have to match the table. AS gives them friendlier
labels — essential once you start computing values:
A SELECT item can be an expression, not just a column. Prices with tax, quantities
doubled, strings upper-cased — all computed per row. Watch the with_tax column: it
doesn't exist in the table, the engine builds it for each row:
100.0 and not 100?
Writing / 100.0 forces decimal math. It's a good habit in real SQL too, where integer
division can silently chop off the fraction (10 / 100 becoming 0).
ROUND(x, 2) then tidies the result to 2 decimal places.
DISTINCT — collapse duplicatesHow many different categories are there? Run the first query and you'll see "Coffee" three
times. Add DISTINCT and the duplicates fold away:
You can transform text as it comes out. UPPER, LOWER and LENGTH are
the everyday ones:
name and the year length of their name…
or anything you like. There's no wrong experiment. The engine will catch mistakes gently.
AS do in SELECT price AS dollars?AS is purely a label for the result column. It doesn't change data
or filter anything.SELECT DISTINCT category FROM products returns…DISTINCT removes duplicate output rows, leaving the 3 unique categories.price * 2?AS.