Syntax Fundamentals

SELECT and FROM

The two words that start almost every SQL query, and what the database actually does with them.

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.

SQL
SELECT first_name, last_name
FROM employees;

Ten rows come back — one per employee — and only the two columns you asked for.

* means every column

SQL
SELECT *
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 writeThe database does
1. SELECT3. picks the columns
2. FROM1. finds the table
3. WHERE2. 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

  • SELECT with no FROM — 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

  1. Which runs first, SELECT or FROM?
  2. Why can a SELECT alias not be used in WHERE?
  3. Name one concrete cost of SELECT *.

Next

Next: Choosing and renaming columns — aliases, expressions, and how to make a result set readable.

SELECT and FROM — SQL — The Interactive Visual Notebook