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

  1. Start with FROM employees
  2. Read each row of the table
  3. Evaluate each item in the SELECT list for that row
  4. Apply aliases given with AS
  5. Is there a WHERE / ORDER BY / LIMIT? Yes: Apply those clauses as well. No: Return every row as-is.
  6. Return the result set

Syntax

SELECT column1, column2
FROM table_name;

Example

SELECT first_name, last_name FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic