Column Aliasing & Expressions: Examples
Module: SQL Fundamentals
Basic Column Aliasing with AS
basic
HR wants a formatted employee report with readable column names for an internal dashboard
SELECT
employee_id AS emp_id,
first_name AS first,
last_name AS last,
salary AS monthly_salary,
salary * 12 AS annual_salary,
CONCAT(first_name, ' ', last_name) AS full_name
FROM employees
ORDER BY annual_salary DESC;
emp_id | first | last | monthly_salary | annual_salary | full_name
1 | Alice | Chen | 10000 | 120000 | Alice Chen
2 | Bob | Patel | 8500 | 102000 | Bob Patel
3 | Carol | Nguyen | 7200 | 86400 | Carol Nguyen
AS renames columns in output without changing the table. salary * 12 calculates annual salary inline. CONCAT builds full_name. ORDER BY annual_salary uses the SELECT alias — valid because ORDER BY runs after SELECT in execution order.
All
Arithmetic Operator Precedence
basic
Finance team needs discounted totals and tax-inclusive prices, and the formula must respect operator precedence
SELECT
product_name,
unit_price,
quantity,
discount_pct,
-- Wrong without parentheses: discount subtracted AFTER multiplication
unit_price * quantity - discount_pct AS wrong_discount,
-- Correct: discount applied to subtotal
(unit_price * quantity) * (1 - discount_pct) AS discounted_total,
-- Tax applied to discounted amount
(unit_price * quantity) * (1 - discount_pct) * 1.08 AS total_with_tax
FROM order_items;
product_name | unit_price | quantity | discount_pct | wrong_discount | discounted_total | total_with_tax
Laptop | 1200.00 | 2 | 0.10 | 2399.90 | 2160.00 | 2332.80
Keyboard | 80.00 | 5 | 0.05 | 399.95 | 380.00 | 410.40
Without parentheses, * binds tighter than - so discount_pct gets subtracted from the full product — wrong. Parentheses enforce: (1) calculate subtotal, (2) apply discount percentage, (3) apply tax. This is a common production bug in ETL pipelines.
All
Cross-Database String Concatenation
basic