SELECT

SELECT is the statement you'll write more than any other. It reads data from one or more tables and returns a result — itself a table of rows and columns. This lesson covers choosing columns, calculating new ones and naming them with aliases.

Selecting columns

SQL
SELECT name, category, price
FROM products;
Result 12 rows
namecategoryprice
The Patient GardenerBooks24.9
Learning SQL Step by StepBooks32.5
Night Sky AtlasBooks45
Wireless EarbudsElectronics59.99
USB-C ChargerElectronics19.99
Mechanical KeyboardElectronics89
Ceramic MugHome12.5
Desk LampHome34
Linen ThrowHome49
Jigsaw Puzzle 1000Toys18
Wooden Train SetToys39.9
Smart PlugElectronics14.99

List the columns you want after SELECT, separated by commas, and the table after FROM. The result has the columns in the order you listed them.

Selecting every column

SQL
SELECT * FROM departments;
Result 5 rows
idnamelocation
1SalesLisbon
2EngineeringBerlin
3MarketingLisbon
4SupportDublin
5ResearchNULL

* means "all columns". It's handy for exploring, but in application code list columns explicitly: it's clearer, sends less data, and doesn't break when someone adds a column to the table.

Calculated columns

SQL
SELECT name, price, stock, price * stock
FROM products
WHERE category = 'Home';
Result 3 rows
namepricestockprice * stock
Ceramic Mug12.52002500
Desk Lamp3415510
Linen Throw49301470

A column in the select list can be any expression: arithmetic, function calls, text joined together. The database calculates it for each row.

Column aliases with AS

SQL
SELECT name AS product,
       price * stock AS stock_value
FROM products
WHERE category = 'Home';
Result 3 rows
productstock_value
Ceramic Mug2500
Desk Lamp510
Linen Throw1470

AS gives a column a new name in the result. Aliases make calculated columns readable and are how applications refer to them. The AS keyword is optional (price * stock stock_value also works), but including it is clearer.

SQL
SELECT first_name || ' ' || last_name AS "Full name",
       salary / 12 AS "Monthly salary"
FROM employees
WHERE department_id = 4;
Result 2 rows
Full nameMonthly salary
Ethan Walsh5000
Priya Nair3500

Put an alias in double quotes if it contains spaces or capital letters you want to keep. || joins text (lesson 13).

Selecting values without a table

SQL
SELECT 2 + 3 AS answer, 'hello' AS greeting, upper('sql') AS shout;
Result 1 row
answergreetingshout
5helloSQL

Many databases (SQLite, PostgreSQL, MySQL, SQL Server) let you SELECT expressions without FROM — useful for testing functions. Oracle requires FROM dual.

Table aliases

SQL
SELECT p.name, p.price
FROM products AS p
WHERE p.price > 50;
Result 2 rows
nameprice
Wireless Earbuds59.99
Mechanical Keyboard89

Tables can have aliases too. They're short to type and essential once queries involve several tables (lesson 23). Prefixing a column with its table (p.name) says exactly where it comes from.

The order clauses are written in

A full SELECT can have many clauses. They must appear in this order, though you'll meet them one at a time:

SQL
SELECT   columns
FROM     tables
WHERE    row filter
GROUP BY grouping
HAVING   group filter
ORDER BY sorting
LIMIT    row count;

Try it yourself

List each employee's full name, job title and salary with a 10% raise, giving the raise column a clear name.

Try it yourself
Result