SQL Practice Logo

SQLPractice Online

Column Aliasing & Expressions: Performance

Module: SQL Fundamentals

1. SELECT expressions execute only on returned rows — low cost after WHERE filtering reduces the row set

2. WHERE expressions execute on every table row before filtering — wrap functions around indexed columns and pay full scan penalty

3. Non-SARGable pattern: WHERE YEAR(order_date) = 2024 forces full scan even on indexed date column

4. SARGable rewrite: WHERE order_date >= '2024-01-01' AND order_date < '2025-01-01' uses range index scan

5. Arithmetic on indexed column breaks SARGability: WHERE salary * 12 > 120000 — move math to constant side: WHERE salary > 10000

6. UPPER/LOWER on indexed column breaks index: store normalized data at INSERT time and query directly

7. Table alias resolution has zero runtime cost — aliases are compile-time syntactic sugar only

8. CONCAT with many columns adds minor per-row CPU cost — negligible unless concatenating millions of rows with large strings

Expressions in SELECT execute only on returned rows — low cost after WHERE has filtered the data

Expressions in WHERE execute on every table row — functions on indexed columns cause full scans

SARGable rewrite: WHERE salary > 10000 instead of WHERE salary * 12 > 120000

Date range SARGable rewrite: WHERE order_date >= 2024-01-01 AND order_date < 2025-01-01 instead of WHERE YEAR(order_date) = 2024

Store data normalized (lowercase) at INSERT time — query directly without UPPER/LOWER to preserve index

ROUND in SELECT adds minimal overhead — always use it for financial calculations to prevent floating-point drift

WHERE annual_salary > 100000 fails — WHERE runs before SELECT, alias does not exist yet

NULL concatenation with ||: any NULL operand returns NULL — always COALESCE nullable columns

SQL Server + operator propagates NULL: first_name + NULL = NULL — use CONCAT() instead

Missing parentheses in (price * quantity) * tax_rate vs price * quantity * tax_rate — same result here but dangerous habit

Arithmetic on indexed column breaks SARGability: WHERE salary * 12 > N forces full scan

Function wrapping an indexed column breaks index: WHERE UPPER(name) = 'X' → full scan