SELECT
SQL · Querying Data
Theory
SELECT is the statement that reads data. You list the columns or expressions you want after SELECT and name the source table after FROM. Each item in the list becomes one column of the result, and you can rename it with AS, for example SELECT name, salary * 12 AS annual_salary FROM employees.
SELECT * returns every column, which is handy for exploring but fragile in real code because the output changes when someone adds a column. SELECT never changes the stored table: it produces a new, temporary result set. On its own it returns every row; you add WHERE to filter, ORDER BY to sort and GROUP BY to summarise. Expressions can use arithmetic, string functions and CASE, so SELECT is also where you shape and calculate values.
Why we use it
Tables often hold dozens of columns. SELECT lets you fetch only the columns you need, calculate new ones on the fly and rename them clearly, without copying or altering the stored data.
When to use it
Use SELECT for every read query. List columns explicitly in production code and reports; keep SELECT * for quick exploration. If you need to change stored data, use UPDATE or INSERT instead, since SELECT only reads.
How it works
- Start with FROM employees
- Read each row of the table
- Evaluate each item in the SELECT list for that row
- Apply aliases given with AS
- Is there a WHERE / ORDER BY / LIMIT? Yes: Apply those clauses as well. No: Return every row as-is.
- Return the result set
Syntax
SELECT column1, column2
FROM table_name;Example
SELECT first_name, last_name FROM employees;Key points
- SELECT * returns every column; list columns explicitly in real code
- AS renames a column or expression in the output
- Expressions like salary * 12 are computed per row
- SELECT only reads; the stored table is never changed
- Combine with WHERE, ORDER BY and GROUP BY for real questions