The idea
SELECT * FROM products; shows everything. That's handy for a first look, but at work you almost never want everything. A report about prices doesn't need stock levels, and a customer email list doesn't need signup dates. SELECT lets you pick exactly the columns you need, and that's the first step of every query you'll ever write.
Think of a table as a wide spreadsheet. FROM says which sheet to open. SELECT says which columns to keep visible, and in which order.
Picking columns
Say the marketing team is putting together a price list for the website. They need the name and price of every product, and nothing else. List the columns you want after SELECT, separated by commas, and the database leaves out all the others:
SELECT name, price
FROM products;
name | price
----------------------+--------
French Press 600ml | 185000
Pour-over Kettle | 320000
Arabica Gayo 250g | 95000
Robusta Lampung 250g | 65000
Toraja Kalosi 250g | 110000
Ceramic Mug | 55000
Notebook A5 Dotted | 45000
Gel Pen Set | 38000
Desk Organizer | 89000
Wireless Mouse | 175000
USB-C Hub | 289000
Desk Lamp LED | 245000
Scented Candle | 75000
Throw Pillow | 120000
Bamboo Plant | 60000
(15 rows)
- The columns appear in the order you list them, not the order they're stored in the table.
- There's no comma after the last column. A trailing comma (
SELECT name, price, FROM ...) is one of the most common SQL errors. - Line breaks and spaces don't matter to the database. Putting
FROMon its own line just makes the query easier to read.
You can put the columns in any order you like:
SELECT price, name, category
FROM products;
price | name | category
--------+----------------------+-------------
185000 | French Press 600ml | Kitchen
320000 | Pour-over Kettle | Kitchen
95000 | Arabica Gayo 250g | Coffee
...
(15 rows)
How SQL reads your query
You write SELECT first, but the database reads FROM first. It opens the table, then picks the columns. Keep that in mind, because it explains a lot of later rules:
| You write | The database does |
|---|---|
SELECT name, price | 2. keep only these columns |
FROM products | 1. open this table |
This order will grow as you learn WHERE, GROUP BY and ORDER BY, and knowing it saves hours of confusion.
When something goes wrong
SQL errors look scary, but they're very specific. Ask for a column that doesn't exist:
SELECT name, prices
FROM products;
ERROR: column "prices" does not exist
The database tells you exactly which name it couldn't find. The fix is almost always a typo: here, the column is price, not prices. Check the Tables tab whenever you're not sure of a name.
Now the trailing comma:
SELECT name, price,
FROM products;
ERROR: syntax error at or near "FROM"
"Syntax error at or near FROM" means "something just before FROM is wrong". The database read the comma, expected another column, and found FROM instead.
When you get a syntax error, look at the word it names, then look just before it. That's where the mistake usually is.
Comments
Two dashes start a comment: everything after -- on that line is ignored by the database. Use comments to explain why a query exists:
-- Price list for the marketing team, July campaign
SELECT name, category, price
FROM products;
name | category | price
----------------------+-------------+--------
French Press 600ml | Kitchen | 185000
Pour-over Kettle | Kitchen | 320000
Arabica Gayo 250g | Coffee | 95000
...
(15 rows)
Why not always use SELECT *?
* is perfect for exploring a table you don't know. In a query you'll keep, list the columns:
- It's clearer. Anyone reading
SELECT name, email FROM customersknows exactly what the report contains. - It's safer. If someone adds a column to the table next month,
SELECT *quietly changes your report's shape. A dashboard or Python script reading it may break. - It's faster. On big tables, fetching fewer columns means less data moved. With millions of rows, that matters.
Use SELECT * to look. Use named columns to work.
Several queries in one go
You can run more than one statement at once, ending each with a semicolon. The site shows each result separately:
SELECT name, city FROM customers;
SELECT name, department FROM employees;
name | department
-----------------+------------
Rina Hartono | Management
Agus Salim | Sales
Maya Anggraini | Operations
...
(12 rows)
The output above shows the last result. In the editor, you'll see both, one after the other.
Formatting style
There's no single official style, but this one is widely used and easy to read:
SELECT
name,
category,
price
FROM products;
- Keywords in capitals (
SELECT,FROM), names in lowercase. - One column per line when there are more than two or three. Adding, removing or commenting out a column then touches exactly one line.
- Each clause on its own line, so the structure is visible at a glance.
Why this matters for clean code
Choosing columns on purpose is the SQL version of naming variables well: it makes your intent obvious. A query that says SELECT name, email FROM customers is self-documenting. Your future self, your teammates and your Python scripts all benefit from queries that return exactly what they promise, nothing more.
Recap
SELECT col1, col2 FROM table;returns only those columns, in that order.- The database reads
FROMfirst, thenSELECT. - No comma after the last column. Errors point at the word after the mistake.
--starts a comment.- Use
*to explore, named columns in anything you keep.