SELECT and FROM
Nearly every query you will ever write starts with these two words. SELECT says
which columns you want. FROM says which table to take them from.
SELECT column_a, column_b
FROM table_name;
That is the whole shape. Everything else in this course adds to it.
Your first real query
This runs against a real employees table. Press Run.
SQLSELECT first_name, last_name FROM employees;
Ten rows come back — one per employee — and only the two columns you asked for.
* means every column
SQLSELECT * FROM departments;
Convenient while exploring. Avoid it in real code: if someone later adds a column, your query silently starts returning it, and anything downstream that expected a fixed shape breaks. Name your columns once you know what you want.
The order you write is not the order it runs
This surprises people. You write SELECT first, but the database resolves
FROM first — it has to know where the rows come from before it can work out
which columns to show.
| You write | The database does |
|---|---|
1. SELECT | 3. picks the columns |
2. FROM | 1. finds the table |
3. WHERE | 2. filters the rows |
This is why a column alias defined in SELECT cannot be used in WHERE — at the
moment WHERE runs, the alias does not exist yet. There is a full lesson on
execution order later in this module.
Semicolons
The ; ends a statement. Some tools require it, some do not. Get in the habit:
it costs nothing and it is required the moment you run two statements together.
Common mistakes
SELECTwith noFROM— valid in some databases (SELECT 1;) but usually a typo.- Misspelling a column — the database tells you exactly which name it did not recognise. Read the message; it is almost always right.
- Using
*in production code — see above.
Interview question
What is the difference between
SELECT *and naming columns?
Naming columns returns a stable shape, transfers less data, and lets the database
use a covering index. SELECT * returns whatever the table happens to have today,
which changes under you when the schema changes.
Check yourself
- Which runs first,
SELECTorFROM? - Why can a
SELECTalias not be used inWHERE? - Name one concrete cost of
SELECT *.
Next
Next: Choosing and renaming columns — aliases, expressions, and how to make a result set readable.