Skip to content
Reference notes from the interactive SQL course, which covers the same ground with a live SQL console, graded challenges and a final exam.
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.

All 43 lessons in SQL — The Interactive Visual Notebook
  1. Introduction to SQL
  2. Why SQL Matters
  3. What is an RDBMS?
  4. Tables, Rows, and Columns
  5. SQL Command Categories (DDL/DML/DCL/DQL)
  6. SELECT and FROM
  7. Aliases and Expressions
  8. DISTINCT and Sorting
  9. Limiting Results
  10. Query Execution Order
  11. WHERE and Comparison Operators
  12. Combining Conditions — AND, OR, NOT
  13. IN, BETWEEN, and LIKE
  14. Working with NULL
  15. Aggregate Functions
  16. GROUP BY
  17. HAVING — Filtering Groups
  18. Grouping in Practice
  19. INNER JOIN
  20. LEFT JOIN and Outer Joins
  21. Self Joins
  22. Cross Joins and Join Pitfalls
  23. UNION, INTERSECT, and EXCEPT
  24. Subqueries
  25. EXISTS and Correlated Subqueries
  26. Common Table Expressions (WITH)
  27. Recursive CTEs
  28. Window Functions — The OVER Clause
  29. Ranking Functions
  30. Frames and Running Totals
  31. LAG and LEAD
  32. Window Function Patterns
  33. What an Index Is
  34. Reading a Query Plan
  35. Designing Indexes
  36. Writing Queries an Index Can Use
  37. Transactions and ACID
  38. Isolation Levels
  39. Locks and Deadlocks
  40. Choosing Data Types
  41. Constraints and Keys
  42. Normalisation
  43. Designing a Schema