Learn SQL
-- tables.select_rows

SELECT — Reading Rows in SQL

Ask for exactly the columns and rows you need

--concept

SELECT reads data. List the columns you want (or * for all), name the table, and the engine returns a fresh result table. Selecting only needed columns is faster and clearer than SELECT *, and the result can be filtered (WHERE), sorted (ORDER BY) and summarised (GROUP BY) — SELECT is the backbone everything else builds on.

--how it works
1

Choose the columns

SELECT name, department picks specific columns; * means all of them. Explicit lists are faster to read and survive schema changes.

2

Name the source

FROM employees — one table for now; FROM can also name a joined or nested result later.

3

Receive a table back

The result is itself a table: same shape every run, orderable, nestable, aggregatable.

4

Rename outputs with AS

SELECT salary AS monthly_pay — the alias changes the result header only, not the stored data.

5

Deduplicate with DISTINCT

SELECT DISTINCT department collapses repeated values — covered fully in its own topic.

--visualization
idnamedepartmentsalaryage1AliceEngineering85000292BobSales52000353CynthiaEngineering91000414DavidMarketing47000385ElenaSales58000266FaridEngineering7300033source: employees (5 columns)
Stage 1 / 3
SELECT name, department, salary FROM employees — three of five columns wanted.
--example
query.sql
1SELECT name, department, salary
2FROM employees;
Result
namedepartmentsalary
AliceEngineering85000
BobSales52000
CynthiaEngineering91000
DavidMarketing47000
ElenaSales58000
FaridEngineering73000

All six rows return, but only three of the five columns — projection in action. Swap in * to see everything, or DISTINCT department to collapse duplicates.

--variants

SELECT *

Every column, in table-definition order.

query.sql
1SELECT * FROM employees;
Result
idnamedepartmentsalaryage
1AliceEngineering8500029
2BobSales5200035
3CynthiaEngineering9100041
4DavidMarketing4700038
5ElenaSales5800026
6FaridEngineering7300033
1All five columns of all six rows return — the whole grid.
2Handy for exploration; discouraged in shipped application code.
3Column order belongs to the table definition, not to you.

Chosen columns

Projection: keep only the columns the question needs.

query.sql
1SELECT name, department, salary
2FROM employees;
Result
namedepartmentsalary
AliceEngineering85000
BobSales52000
CynthiaEngineering91000
DavidMarketing47000
ElenaSales58000
FaridEngineering73000
1Same six rows, three columns — narrower results, less I/O, clearer intent.
2The output column order follows your SELECT list, not the table's.
3Add or reorder names freely — the data does not move.

Aliased output

AS renames a column in the result header.

query.sql
1SELECT name AS employee_name,
2 salary AS annual_pay
3FROM employees;
Result
employee_nameannual_pay
Alice85000
Bob52000
Cynthia91000
David47000
Elena58000
Farid73000
1Two aliases appear as the result headers.
2The underlying employees table is unchanged — aliasing is presentation.
3Reports and API layers lean on aliases for stable output names.

Computed columns

Expressions compute a new column per row.

query.sql
1SELECT name,
2 salary * 12 AS annual_pay
3FROM employees;
Result
nameannual_pay
Alice1020000
Bob624000
Cynthia1092000
David564000
Elena696000
Farid876000
1salary * 12 is evaluated per row, like a formula dragged down a spreadsheet.
2The alias gives the computed column a name for the result header.
3Any expression is legal here — arithmetic, string glue, function calls.
--key facts
Projectionchoosing columns · Selection: choosing rows (WHERE)
AliasAS renames a column in the output — handy for reports
Avoid SELECT *in real apps: explicit columns survive schema changes
--use cases
Every dashboard, list page and export — SELECT is the read backbone
Narrowing wide tables to just the columns a report needs
Producing stable column names for APIs via AS aliases
Quick per-row math (tax, totals, unit conversion) as computed columns
--under the hood
Selecting fewer columns is genuinely faster — engines can skip untouched columns in columnar storage and move less data otherwise.
Avoid SELECT * in application code: a new column in the table silently changes your result shape.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.