Step 1 Understand it
Why you need it. A can have dozens of , and you usually need two or three. You also often need values that are not stored at all — a salary after a raise, a name and city into one label. lets you choose and calculate exactly the columns you need.
New words in this topic (tap one to see what it means)
Watch it happen
Read the animation as text
- FROM employees runs first: the database takes every row of the table, with all its columns.
- SELECT name, salary keeps only the columns you list. The others are simply not in the result.
- salary + 5000 is worked out separately for every row. AS after_raise names the new column; without it, the column would be called "salary + 5000".
- One result row for every table row (8), with just these columns. Nothing is saved: after_raise exists only in this result.
Picture it
flowchart LR T["employees<br/>emp_id, name, dept_id,<br/>manager_id, salary, hire_date"] -->|"FROM: every row"| S["SELECT name, salary,<br/>salary + 5000 AS after_raise"] S --> R["result<br/>name, salary, after_raise"]
The idea
After SELECT you write a list, separated by commas. Each item can be a column name (salary), a calculation (salary + 5000), or a fixed value ('hello'). gives an item a new name in the result, called an alias. SELECT * means every column, in table order. The rows come from the table named after FROM, and every row of that table is used, because there is no filter yet.
The shape of it
SELECT column, expression AS new_name
FROM table_name;Words in lower case are the parts you fill in.
How it works, step by step
- The reads FROM first: it takes every row of employees.
- For each row, it works out every item in the SELECT list: name, salary, and salary + 5000.
- AS after_raise names the calculated column; without it, the column would be called "salary + 5000".
- The result has one row for every row of the table, with just these columns.
- Nothing is saved. The calculated column exists only in this result.
Analogy
Ordering a set menu but saying "just the starter and the dessert, please". FROM is the menu you are ordering from; SELECT is the courses you want on your plate, in the order you name them.
Where the analogy breaks
A waiter can only bring what is on the menu. SELECT can also bring things that don't exist in the table, because it can calculate new values from the columns on the fly.
Worked examples
Choose columns and calculate a new one
SELECT name, salary, salary + 5000 AS after_raise
FROM employees
ORDER BY emp_id;8 rows
| name | salary | after_raise |
|---|---|---|
| Asha | 95000 | 100000 |
| Ben | 70000 | 75000 |
| Chen | 70000 | 75000 |
| Dara | 60000 | 65000 |
| Eli | 45000 | 50000 |
| Fatima | 52000 | 57000 |
| Gus | 38000 | 43000 |
| Hana | 80000 | 85000 |
after_raise is calculated for every row and named with AS. The employees table itself is unchanged.
Join text together with ||
SELECT dept_name || ' in ' || location AS where_it_is
FROM departments
ORDER BY dept_id;4 rows
| where_it_is |
|---|
| Engineering in London |
| Sales in Manchester |
| Marketing in London |
| HR in Leeds |
|| glues together. The text in single quotes, ' in ', is a fixed value added to every row.
Must-know points
- SELECT chooses and calculates columns; FROM says which table the rows come from.
- Separate items with commas — and no comma after the last one.
- AS gives a column an alias. Use one for every calculated column, so the result has a readable name.
SELECT *is fine for looking at a table, but in real name the columns you need: it is clearer, and faster on wide tables.- Without a , every row of the table is in the result.
Common mistakes
- A trailing comma before FROM:
SELECT name, salary, FROM employeesis a syntax error. - Using double quotes for text. 'London' is text; "London" means a column called London in .
- Using an alias in WHERE in the same query. WHERE runs before SELECT, so the alias doesn't exist yet (Phase 2 explains why).
In other databases
- Joining text: PostgreSQL and use
||; usesCONCAT(a, b); uses+orCONCAT.CONCATworks almost everywhere. - AS is optional for column aliases in every major database (
salary + 5000 after_raiseworks), but writing AS is clearer.
Step 2 Go further (free, optional)
Optional. The notes above are written to be enough on their own. These free resources give a second explanation and more practice.
- Watch
-
Read
W3Schools — SQL SELECT Statement
short page with a try-it editor, free
-
Practice
SQLBolt — Lesson 1: SELECT queries 101
interactive exercises in the browser, free
Step 3 Practise
Try each one yourself first: out loud, or written and run in the practice box below, which saves every query you run. Then open the answer and compare.
-
ExplainSay what it does and when to use it, in plain words, with no notes.
Explain what SELECT and FROM each do, and what AS is for, using the employees table.
Show answer
FROM says which table to read:
FROM employeesmeans "look at the employees sheet".SELECT says which columns to give back, and in what order:
SELECT name, salaryreturns just those two, even though the table has six. It can also work things out, such assalary + 5000.AS gives a column a new name in the result:
salary + 5000 AS after_raise. Without it, the column would be called "salary + 5000". AS only renames the result; the table itself does not change. -
Writefrom a blank page, then run it
Write a query that returns each product's name, its price, and the price of 3 of them, called price_for_three.
Show answer
Work out the new column in SELECT and name it with AS. Every product comes back, because there is no WHERE.
sqlSELECT product_name, price, price * 3 AS price_for_three FROM products ORDER BY product_id;7 rows
product_name price price_for_three Pen 2 6 Notebook 5 15 Mug 8 24 Desk lamp 25 75 Backpack 30 90 Headphones 40 120 Phone case 15 45 -
Predictsay it before you run it
How many rows and columns does
SELECT name, name FROM employees;return?Show answer
8 rows and 2 columns. There is no WHERE, so you get one row per employee. SELECT returns exactly what you list, and you listed name twice, so both columns are the same name. Asking for a column twice is allowed; it is just pointless.
sqlSELECT name, name FROM employees ORDER BY emp_id;8 rows
name name Asha Asha Ben Ben Chen Chen Dara Dara Eli Eli Fatima Fatima Gus Gus Hana Hana -
Edge casesSay what happens with NULLs, ties, duplicates or no rows at all, and fix it.
What does
salary + 5000give for a row where salary is ? (No employee here has one — reason it out, then test it withSELECT NULL + 5000;.)Show answer
NULL. NULL means "unknown", and unknown plus 5000 is still unknown. There is no error and no 5000: the NULL quietly spreads through the sum.
If a missing salary should count as 0, say so:
COALESCE(salary, 0) + 5000.sqlSELECT NULL + 5000;1 row
NULL + 5000 NULL
Practice box: write and run your answers here
Your queries in this box
Step 4 Interview-ready, then prove it
Your interview answer
About 20 to 40 seconds, said out loud.
SELECT defines the columns of the result — stored columns, , or constants — and FROM names the source table. I alias every calculated column with AS so the output is readable, and I avoid SELECT star in production queries: naming columns is clearer, it reads less data, and the query doesn't silently change when someone adds a column.
Follow-up questions
Why avoid SELECT * in production code?
It reads columns you don't need, can stop the database using a , and the result changes shape when a column is added — which can break the code that reads it.
Can you SELECT without FROM?
Yes in SQLite, PostgreSQL, MySQL and SQL Server: SELECT 2 + 3; returns 5. Oracle needs FROM dual.
Tick each check when you can do it without notes
A topic is finished when you pass all four checks, not when you have read it. Be strict: if you had to look at the notes, it is not ticked yet.
- Explain Say what it does and when to use it, in plain words, with no notes.
- Write Write the query from a blank page, then run it to check.
- Predict Read a query and say which rows it returns before you run it.
- Edge cases Say what happens with NULLs, ties, duplicates or no rows at all, and fix it.
0 of 4 checks ticked.
Saved in this browser. Back it up or move it to another device.