Skip to content
Back to 50 Concepts
Topic 6 of 50query-executionBeginner

# Topic 6: SELECT Statements

SELECT projections, column aliases, scalar expressions, arithmetic transformations, and the logical query execution order.

1. Definition

The SELECT statement is the foundational DQL command used to project and retrieve data from one or more tables or views.

Relational Invariant:Deterministic relational algebra contract evaluated by the Cost-Based Optimizer.

2. Explain Like I'm 5

Intuitive mental model for beginners

SELECT is like a custom photo filter. You have a huge photo album (the table), and SELECT lets you pick exactly which faces (columns) you want to see on your screen.

3. Animation & Visual Simulation

Interactive Engine Trace
Interactive Engine Simulation

SELECT Statements Visual Simulator

Step 1 of 3
SQL>SELECT u.name, (u.balance * 1.05) AS projected_balance FROM users u WHERE u.status = 'active';Active Input

Step 1: Input & Parsing Stage

Engine Action: Validate SQL syntax and resolve schema metadata for SELECT Statements
SQL Input

Initiating SELECT Statements

Schema Catalog

Checking Table Invariants

How the Database Executes This:

The database engine inspects active tables, verifies permissions, and prepares the physical operation.

💡 Filtering Camera: Only capture the specific objects you want in the frame, rather than taking a panoramic shot of the entire city and throwing away 90% of it.

4. Real-World Example

An analytics dashboard displays total employee compensation including base salary and bonuses.

Employees
idfirst_namesalarybonus_percent
1Alice1000000.10
2Bob800000.05
Relational walkthrough: The query calculates total compensation on the fly and aliases the result.

5. SQL Syntax

ANSI SQL / PostgreSQL Dialect
Step 1: Column projection and aliasing
SELECT
  first_name AS employee_name,
  salary,
  (salary * bonus_percent) AS bonus_amount,
  salary + (salary * bonus_percent) AS total_compensation
FROM employees;
Expressions in the SELECT list transform raw column values into calculated fields.
Step 2: Selecting literal constants and system functions
SELECT 42 AS answer, CURRENT_DATE AS today, UPPER('hello') AS greeting;
SELECT can evaluate scalar expressions without querying any table.

6. Important Rules & Behaviors

RuleBehavioral Explanation
Avoid SELECT * in ProductionSELECT * fetches unneeded columns, breaks covering index scans, and risks application failure when columns change.
Logical Execution OrderFROM -> WHERE -> GROUP BY -> HAVING -> SELECT -> ORDER BY -> LIMIT. Aliases defined in SELECT are not visible in WHERE.
Column Aliases with SpacesAliases with spaces or special characters must be wrapped in double quotes: "Total Compensation".
Logical query processing orderFROM is evaluated first, followed by WHERE, GROUP BY, HAVING, SELECT, and ORDER BY.
Alias availability scopingColumn aliases defined in SELECT are not visible in WHERE because WHERE evaluates before SELECT.

7. Deep Dive: Why SELECT * Destroys Index-Only Scans

When an index contains all requested columns (e.g. index on (user_id, status)), the engine reads directly from the index leaves (Index-Only Scan). If you write SELECT *, the engine must perform costly heap disk lookups for every matching row.

-- Fast Index-Only Scan:
SELECT user_id, status FROM users WHERE user_id = 42;

-- Slow Table Heap Scan (because of SELECT *):
SELECT * FROM users WHERE user_id = 42;
Caution: Never use SELECT * in production API endpoints or latency-critical paths.

8. Advantages & Disadvantages

Advantages

  • •Fine-grained control over network payload size.
  • •Supports on-the-fly mathematical and text transformations.
  • •Enables covering index optimizations when projecting specific columns.

Disadvantages & Trade-offs

  • •Writing SELECT * transfers unused data over the wire and wastes buffer cache.

9. Common Interview Questions

Q1.What is the exact 8-step logical query processing order of a SQL statement?

The logical processing order determines when columns and aliases become available: 1. FROM & JOINs (cartesian product & ON filtering), 2. WHERE (row filtering), 3. GROUP BY (bucketing), 4. HAVING (group filtering), 5. SELECT (evaluating expressions & aliases), 6. DISTINCT (duplicate elimination), 7. ORDER BY (sorting), 8. LIMIT / OFFSET (windowing).

Q2.Why can you NOT use a column alias defined in SELECT inside the WHERE clause?

Because of the logical query processing order: the WHERE clause evaluates at Step 2 to filter rows from disk, whereas the SELECT clause (which assigns column aliases) evaluates at Step 5. When the WHERE clause is evaluated, the alias does not exist yet. You must repeat the expression in WHERE or wrap the query in a CTE/subquery.

Q3.Why is "SELECT *" considered a severe production antipattern?

1. It breaks Covering Index (Index-Only) scans by forcing the engine to visit heap disk pages for unindexed columns. 2. It wastes network bandwidth and serializes unused data across the wire. 3. It pollutes database shared buffer caches with unnecessary columns (like large TEXT or JSONB). 4. If table columns are added or reordered, application code relying on column positions breaks.

Q4.What is Projection Pushdown in query optimization?

Projection Pushdown is a Cost-Based Optimizer transformation where the planner pushes the list of required columns down into the lowest physical scan operators (Sequential Scan or Index Scan). The engine only reads the necessary column byte offsets from the 8 KB page into memory, discarding unused attributes immediately.

Q5.Can a SELECT statement lock rows or cause concurrency blocking?

Under standard MVCC (Multi-Version Concurrency Control), regular SELECT queries read snapshot data without acquiring locks: readers never block writers, and writers never block readers. However, if a query uses SELECT ... FOR UPDATE or SELECT ... FOR SHARE, it acquires row-level exclusive or shared locks, blocking concurrent transactions from modifying or locking those rows.

10. Interview-Ready Answer

“The SELECT statement projects and computes columns from relational sets. In production systems, explicit column projection is mandatory to preserve network bandwidth and enable database Index-Only Scans.”

11. Quick Test

Scenario Question:

Why does the query "SELECT salary * 2 AS double_sal FROM emp WHERE double_sal > 50000" fail in standard SQL?

Select your answer: