SELECT & Projection
Three columns sit in users: id, name, email, but you only want a contact sheet. Writing name, email instead of grabbing the whole row is the first move every query makes, and the cheapest one to get wrong.
- ▸The output is a vertical cut: same rows, only the columns you name, in the order you name them.
- ▸Request every column and the result is tied to the table's shape, so a schema change can silently reshape it.
- ▸You name the columns first, but the engine reads each row first and computes those columns from it.
The users table has three columns: id, name, email, and three rows: Ada, Brent, and a second Ada with a different email. You want a contact sheet, just the names and the emails. So you name those two columns and leave id out: SELECT name, email FROM users.
The result is a new table. Same three rows, every one of them, but only the two columns you asked for. Nothing filtered the rows away; you cut down the columns, not across the rows. That vertical cut is projection, the operation that restricts a result to a chosen set of columns.
The column list is also the column order. SELECT name, email puts name first; SELECT email, name puts email first, regardless of how the table was declared. The output takes its shape from your list, not from the table.
One quiet consequence: if you project down to columns that repeat, like SELECT name, two people named Ada come back as two identical rows. The result is a bag, duplicates and all. SQL keeps them unless you say otherwise, and the tool to say otherwise comes in a later topic.