# 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.
2. Explain Like I'm 5
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 TraceSELECT Statements Visual Simulator
SELECT u.name, (u.balance * 1.05) AS projected_balance
FROM users u
WHERE u.status = 'active';Active InputStep 1: Input & Parsing Stage
Initiating SELECT Statements
Checking Table Invariants
The database engine inspects active tables, verifies permissions, and prepares the physical operation.
4. Real-World Example
An analytics dashboard displays total employee compensation including base salary and bonuses.
| id | first_name | salary | bonus_percent |
|---|---|---|---|
| 1 | Alice | 100000 | 0.10 |
| 2 | Bob | 80000 | 0.05 |
5. SQL Syntax
ANSI SQL / PostgreSQL DialectSELECT first_name AS employee_name, salary, (salary * bonus_percent) AS bonus_amount, salary + (salary * bonus_percent) AS total_compensation FROM employees;
SELECT 42 AS answer, CURRENT_DATE AS today, UPPER('hello') AS greeting;6. Important Rules & Behaviors
| Rule | Behavioral Explanation |
|---|---|
| Avoid SELECT * in Production | SELECT * fetches unneeded columns, breaks covering index scans, and risks application failure when columns change. |
| Logical Execution Order | FROM -> WHERE -> GROUP BY -> HAVING -> SELECT -> ORDER BY -> LIMIT. Aliases defined in SELECT are not visible in WHERE. |
| Column Aliases with Spaces | Aliases with spaces or special characters must be wrapped in double quotes: "Total Compensation". |
| Logical query processing order | FROM is evaluated first, followed by WHERE, GROUP BY, HAVING, SELECT, and ORDER BY. |
| Alias availability scoping | Column 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;
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
Why does the query "SELECT salary * 2 AS double_sal FROM emp WHERE double_sal > 50000" fail in standard SQL?