SQL Practice Logo

SQLPractice Online

Column Aliasing & Expressions: Concept

Module: SQL Fundamentals

Column aliases rename output columns for readability and API contract compliance. Expressions compute new values from existing columns using arithmetic, string operations, and functions. Together they transform raw relational data into meaningful, presentation-ready results — without altering the underlying table.

**Column Aliases — Syntax & Scope**

Aliases rename columns in query output without changing the underlying table structure.

Syntax variants:

column_name AS alias_name ← recommended, most readable

column_name alias_name ← AS is optional but less clear

column_name "alias name" ← double quotes for spaces/reserved words

**SQL Execution Order and Alias Availability:**

SQL processes clauses in this logical order:

1. FROM / JOIN

2. WHERE

3. GROUP BY

4. HAVING

5. SELECT ← aliases are DEFINED here

6. DISTINCT

7. ORDER BY ← aliases can be USED here

8. LIMIT / OFFSET

Consequences:

WHERE cannot reference SELECT aliases (WHERE runs before SELECT)

HAVING cannot reference SELECT aliases in strict engines (PostgreSQL)

ORDER BY CAN reference SELECT aliases (ORDER BY runs after SELECT)

GROUP BY: MySQL allows alias references, PostgreSQL does not

**Table Aliases:**

Table aliases shorten JOIN queries and resolve ambiguous column references.

FROM employees e JOIN departments d ON e.dept_id = d.id

SELECT e.first_name, d.name AS department_name

**Arithmetic Expressions — Operator Precedence:**

1. Parentheses: () — highest priority

2. Unary minus: -value

3. Multiplication, Division, Modulo: * / %

4. Addition, Subtraction: + - — lowest arithmetic priority

Without parentheses: 2 + 3 * 4 = 14 (not 20)

With parentheses: (2 + 3) * 4 = 20

**String Concatenation — Cross-Database:**

PostgreSQL / Oracle: first_name || ' ' || last_name

MySQL: CONCAT(first_name, ' ', last_name)

SQL Server: first_name + ' ' + last_name (NOT NULL-safe — use CONCAT())

All engines safe: CONCAT(first_name, ' ', last_name)

NULL Propagation Rule: In string concatenation, if ANY operand is NULL, the result is NULL.

'Hello' || NULL = NULL (PostgreSQL/Oracle standard behavior)