Academic Block

SQL Aliases Get Certified

Learn how to use SQL aliases to give columns and tables temporary names that make queries easier to read, using SQLite-compatible syntax.

What are SQL Aliases?

An SQL alias is a temporary name given to a column, expression or table during a query. It does not change anything stored in the database and exists only while the query runs.

Aliases are especially useful when column names are long, when calculations need a meaningful label, and when a query uses several tables.

Column Alias Syntax

A column alias is created with the AS keyword, placed right after the column or expression.

What this code does: Renames the two columns in the result. The output columns are called employee_name and annual_salary instead of first_name and salary, while the table itself is not changed. The query returns 3 rows: Aarav (65000), Meera (72000) and Rohan (58000).

WITH employees(first_name, salary) AS (
  VALUES
    ('Aarav', 65000),
    ('Meera', 72000),
    ('Rohan', 58000)
)
SELECT first_name AS employee_name,
       salary AS annual_salary
FROM employees;

Alias Without AS

The AS keyword is optional in SQLite. You can write the alias directly after the column name.

What this code does: Works exactly like the AS version: the result columns are named product_name and product_price. Using AS is usually clearer, because a missing comma between two columns can be mistaken for an alias. The query returns 3 rows: Keyboard (800), Mouse (400) and Monitor (1500).

WITH products(name, price) AS (
  VALUES
    ('Keyboard', 800),
    ('Mouse', 400),
    ('Monitor', 1500)
)
SELECT name product_name,
       price product_price
FROM products;

Alias for Calculated Columns

A calculated expression has no useful name of its own, so an alias gives it a meaningful label.

What this code does: Multiplies price by quantity for each row and labels the result total_cost. Without the alias, the column would be named after the whole expression. The query returns 3 rows: Keyboard (1600), Mouse (1200) and Monitor (1500).

WITH products(name, price, quantity) AS (
  VALUES
    ('Keyboard', 800, 2),
    ('Mouse', 400, 3),
    ('Monitor', 1500, 1)
)
SELECT name,
       price * quantity AS total_cost
FROM products;

Alias with Aggregate Functions

An alias gives a meaningful name to the result of an aggregate function such as COUNT(), SUM() or AVG().

What this code does: Calculates three summaries of the four sales (1200, 800, 1500 and 700) in one row, labelled total_sales, average_sale and number_of_sales. The total is 4200, the average is 1050 and there are 4 sales. The query returns 1 row: 4200 (1050.0, 4).

WITH sales(amount) AS (
  VALUES
    (1200),
    (800),
    (1500),
    (700)
)
SELECT SUM(amount) AS total_sales,
       AVG(amount) AS average_sale,
       COUNT(amount) AS number_of_sales
FROM sales;

Alias with GROUP BY

Aliases give clear names to aggregate results when you group rows.

What this code does: Groups the employees by department and returns one row per group, with the columns department_name, employee_count and average_salary. IT averages 68500 and HR averages 60000. The query returns 2 rows: HR (2, 60000.0) and IT (2, 68500.0).

WITH employees(name, department, salary) AS (
  VALUES
    ('Aarav', 'IT', 65000),
    ('Meera', 'IT', 72000),
    ('Rohan', 'HR', 58000),
    ('Anika', 'HR', 62000)
)
SELECT department AS department_name,
       COUNT(*) AS employee_count,
       AVG(salary) AS average_salary
FROM employees
GROUP BY department;

Table Aliases

You can also give a table a temporary name. Table aliases are useful when table names are long, or when the same table is used more than once.

What this code does: Gives the employees table the short name e and then uses it to qualify each column (e.first_name). The result is the same as without the alias. The query returns 3 rows: Aarav (IT), Meera (Finance) and Rohan (Sales).

WITH employees(first_name, department) AS (
  VALUES
    ('Aarav', 'IT'),
    ('Meera', 'Finance'),
    ('Rohan', 'Sales')
)
SELECT e.first_name,
       e.department
FROM employees AS e;

Table Alias Without AS

Just like column aliases, SQLite lets you leave out AS when you create a table alias.

What this code does: The alias p is written directly after the table name. It means the same as products AS p. The query returns 3 rows: Laptop (60000), Tablet (25000) and Phone (30000).

WITH products(name, price) AS (
  VALUES
    ('Laptop', 60000),
    ('Tablet', 25000),
    ('Phone', 30000)
)
SELECT p.name,
       p.price
FROM products p;

Aliases with Multiple Tables

Table aliases are most useful when a query uses more than one table, because they make column references shorter and clearer.

What this code does: Joins employees (alias e) to departments (alias d) using the department ID. Both tables have an id column, and the aliases make clear which one is meant. The result columns are named employee_name and department. The query returns 3 rows: Aarav (IT), Meera (Finance) and Rohan (IT).

WITH employees(id, name, department_id) AS (
  VALUES
    (1, 'Aarav', 10),
    (2, 'Meera', 20),
    (3, 'Rohan', 10)
),
departments(id, department_name) AS (
  VALUES
    (10, 'IT'),
    (20, 'Finance')
)
SELECT e.name AS employee_name,
       d.department_name AS department
FROM employees AS e
JOIN departments AS d
  ON e.department_id = d.id;

Aliases with JOIN

Short table aliases make JOIN queries easier to read, especially when several tables are involved.

What this code does: Matches each order to its customer through c.id = o.customer_id. Aman has two orders, so the name appears twice. The result columns are named customer and order_amount. The query returns 3 rows: Aman (1200), Aman (500) and Neha (850).

WITH customers(id, customer_name) AS (
  VALUES
    (1, 'Aman'),
    (2, 'Neha')
),
orders(id, customer_id, amount) AS (
  VALUES
    (101, 1, 1200),
    (102, 2, 850),
    (103, 1, 500)
)
SELECT c.customer_name AS customer,
       o.amount AS order_amount
FROM customers AS c
JOIN orders AS o
  ON c.id = o.customer_id;

Table Aliases with a Self Join

When the same table is used twice in one query, aliases are required to tell the two copies apart.

What this code does: The employees table is used twice: as e for the employee and as m for the manager. Each employee’s manager_id is matched to another row’s id. Rohan has no manager, so the LEFT JOIN shows NULL. The result columns are named employee and manager. The query returns 3 rows: Aarav (Rohan), Meera (Rohan) and Rohan (NULL).

WITH employees(id, name, manager_id) AS (
  VALUES
    (1, 'Aarav', 3),
    (2, 'Meera', 3),
    (3, 'Rohan', NULL)
)
SELECT e.name AS employee,
       m.name AS manager
FROM employees AS e
LEFT JOIN employees AS m
  ON e.manager_id = m.id;

Using Aliases with ORDER BY

A column alias can be used in the ORDER BY clause, which keeps queries with calculated columns easy to read.

What this code does: Calculates total_cost and then sorts by that alias from the largest to the smallest. This works because ORDER BY runs after the SELECT list has been calculated. The query returns 3 rows: Keyboard (1600), Monitor (1500) and Mouse (1200).

WITH products(name, price, quantity) AS (
  VALUES
    ('Keyboard', 800, 2),
    ('Mouse', 400, 3),
    ('Monitor', 1500, 1)
)
SELECT name,
       price * quantity AS total_cost
FROM products
ORDER BY total_cost DESC;

Aliases with WHERE

WHERE runs before the SELECT list is calculated, so standard SQL does not allow a column alias there. SQLite happens to accept it, but many other databases do not. For portable code, repeat the expression.

What this code does: Repeats the calculation price * quantity in WHERE and keeps the rows whose total is above 1300. Mouse totals only 1200, so it is removed. The query returns 2 rows: Keyboard (1600) and Monitor (1500).

WITH products(name, price, quantity) AS (
  VALUES
    ('Keyboard', 800, 2),
    ('Mouse', 400, 3),
    ('Monitor', 1500, 1)
)
SELECT name,
       price * quantity AS total_cost
FROM products
WHERE price * quantity > 1300;

Using an Alias in WHERE with a Subquery

If you want to filter on an alias, calculate it in a subquery first. The outer query can then use the alias like a normal column in any database.

What this code does: The inner query calculates total_cost, and the outer query filters on it with WHERE total_cost > 1300. This is portable and avoids repeating the expression. The query returns 2 rows: Keyboard (1600) and Monitor (1500).

WITH products(name, price, quantity) AS (
  VALUES
    ('Keyboard', 800, 2),
    ('Mouse', 400, 3),
    ('Monitor', 1500, 1)
)
SELECT name, total_cost
FROM (
  SELECT name,
         price * quantity AS total_cost
  FROM products
) AS t
WHERE total_cost > 1300;

Aliases with Subqueries

A subquery in FROM must be given a table alias, and it can then be queried like a temporary table.

What this code does: The innermost query builds three rows of data, the middle query calculates total_cost, and the outer query reads the result through the alias p, keeping the totals above 1000. All three totals are above 1000 (1600, 1200 and 1500). The query returns 3 rows: Keyboard (1600), Mouse (1200) and Monitor (1500).

SELECT p.product_name,
       p.total_cost
FROM (
  SELECT name AS product_name,
         price * quantity AS total_cost
  FROM (
    SELECT 'Keyboard' AS name, 800 AS price, 2 AS quantity
    UNION ALL
    SELECT 'Mouse', 400, 3
    UNION ALL
    SELECT 'Monitor', 1500, 1
  )
) AS p
WHERE p.total_cost > 1000;

Multiple Column Aliases

Every column in a query can have its own alias, including calculated ones.

What this code does: Gives each column a descriptive label (student_name, total_marks, subject_count and average_marks) and calculates the average mark per subject, rounded to two decimals. CAST(... AS REAL) makes sure the division keeps its decimal part. The query returns 3 rows: Aman (420, 5, 84.0), Neha (455, 5, 91.0) and Rahul (390, 5, 78.0).

WITH students(name, marks, subjects) AS (
  VALUES
    ('Aman', 420, 5),
    ('Neha', 455, 5),
    ('Rahul', 390, 5)
)
SELECT name AS student_name,
       marks AS total_marks,
       subjects AS subject_count,
       ROUND(CAST(marks AS REAL) / subjects, 2) AS average_marks
FROM students;

Aliases with COUNT

An alias makes the result of COUNT() easier to understand, and it can be reused in ORDER BY.

What this code does: Counts the orders per customer and sorts by the order_count alias from the highest to the lowest. Neha and Rahul tie with one order each, so customer_name is used as a tie-breaker. The query returns 3 rows: Aman (3), Neha (1) and Rahul (1).

WITH orders(order_id, customer) AS (
  VALUES
    (101, 'Aman'),
    (102, 'Neha'),
    (103, 'Aman'),
    (104, 'Rahul'),
    (105, 'Aman')
)
SELECT customer AS customer_name,
       COUNT(*) AS order_count
FROM orders
GROUP BY customer
ORDER BY order_count DESC, customer_name;

Quoted Aliases

If an alias contains spaces or special characters, enclose it in double quotes.

What this code does: The result columns are labelled Product Name and Product Price, with spaces, which is useful for report-style output. Without the quotes, a space would end the alias and cause a syntax error. The query returns 3 rows: Keyboard (800), Mouse (400) and Monitor (1500).

WITH products(name, price) AS (
  VALUES
    ('Keyboard', 800),
    ('Mouse', 400),
    ('Monitor', 1500)
)
SELECT name AS "Product Name",
       price AS "Product Price"
FROM products;

Common Uses of SQL Aliases

Alias Type
Example
Purpose
Column Alias
name AS employee_name
Rename a column in the result
Calculated Alias
price * quantity AS total
Name a calculated result
Table Alias
employees AS e
Shorten a table reference
Aggregate Alias
COUNT(*) AS total
Give an aggregate result a meaningful name
Subquery Alias
(SELECT ...) AS t
Name a temporary result so it can be queried

Advantages of SQL Aliases

  • Make query results easier to understand.
  • Provide shorter names for long table names.
  • Make calculated columns easy to identify.
  • Improve the readability of JOIN queries.
  • Make aggregate results more descriptive.

Best Practices

  • Use meaningful aliases that clearly describe the data.
  • Use short table aliases in queries that involve several tables.
  • Use AS when it improves readability.
  • Do not rely on a column alias inside WHERE. SQLite allows it, but other databases do not.
  • Give every subquery in FROM its own alias.
  • Use quoted aliases only when a result label really needs spaces.

SQL wildcards make pattern matching flexible. % and _ work with LIKE, while * and ? work with SQLite’s GLOB. Click Test My Code on any example above to run it live, or Code Explanation to walk through the query block by block.

Ctrl + Enter to run code in the editor Esc to close the editor

🧪 Test Your SQL Code

Edit the SQL code on the left and click “Run Code” to see the result on the right.

📝 SQL Code
👁️ Preview (query result)

Click “Run Code” to see the result here.