SQL Alias (AS) Explained for Beginners With Examples
SQL alias explained for beginners: learn how to use AS to rename columns and tables, with simple copy-and-paste examples, joins, and common mistakes to avoid.
SQL alias explained for beginners: learn how to use AS to rename columns and tables, with simple copy-and-paste examples, joins, and common mistakes to avoid.
What is an SQL alias? An SQL alias is a temporary nickname you give to a column or a table inside one query. You create it with the keyword AS. The nickname lasts only while the query runs, and it never changes your real data. In this beginner guide you will learn how SQL aliases work, why they make results easier to read, and how to use them with joins and calculations.
You do not need any experience. We define every new word the first time we use it, and each example is small enough to copy and run.
An alias is a second name. Think of a person called Alexander who goes by Alex. The person is the same, but the short name is easier to use. In SQL, a column or table can have a long or unclear name. An alias lets you show or use a friendlier one.
There are two kinds of alias. A column alias renames a column in the results. A table alias gives a table a short name inside the query. Both use the same keyword: AS.
We will use a small table called employees. A table is a grid of data with rows and columns. Run this to create it and add a few rows.
CREATE TABLE employees (
id INT,
first_name VARCHAR(50),
last_name VARCHAR(50),
salary INT,
department_id INT
);
INSERT INTO employees VALUES
(1, 'Ana', 'Lopez', 3000, 1),
(2, 'Ben', 'Carter', 4000, 2),
(3, 'Chen', 'Wei', 3500, 1);The first statement builds the table with five columns. The second adds three rows, one per employee. Each row lists values in the same order as the columns.
A column alias changes the heading shown in your results. Put AS after the column, then the new name.
SELECT first_name AS name,
salary AS monthly_pay
FROM employees;Line 1 picks first_name and shows it under the heading name. Line 2 picks salary and shows it as monthly_pay. Line 3 says which table to read from. The table itself is unchanged. Only the headings in the result are different.
Aliases are most useful when you calculate something. Without one, the database invents an ugly heading such as ?column? or salary * 12.
SELECT first_name,
salary * 12 AS yearly_pay
FROM employees;The expression salary * 12 multiplies each monthly salary by twelve. AS yearly_pay names the new column so the result is clear. Ana's row would show 36000 under yearly_pay.
If you want a heading with spaces, wrap it in quotes. In standard SQL and PostgreSQL, use double quotes. MySQL also accepts backticks.
SELECT first_name AS "First Name",
salary AS "Monthly Pay"
FROM employees;The double quotes tell the database the whole phrase is one name. Without quotes, a space would cause an error. For simple work, a name with an underscore, like monthly_pay, is easier and safer.
A table alias gives a table a short name for the length of one query. It is handy when table names are long or when you join several tables. A join is a way to combine rows from two tables. Here is a second table for departments.
CREATE TABLE departments (
id INT,
name VARCHAR(50)
);
INSERT INTO departments VALUES
(1, 'Sales'),
(2, 'Engineering');This makes a departments table with two rows. Notice that both tables have a column called id. That will matter in a moment.
SELECT e.first_name,
d.name AS department
FROM employees AS e
JOIN departments AS d
ON e.department_id = d.id;FROM employees AS e makes e the nickname for employees. JOIN departments AS d makes d the nickname for departments. ON matches each employee's department_id with a department's id. In the SELECT line, e.first_name means the first_name column of e. The prefix tells the database which table you mean, which avoids confusion when both tables share a column name. Most databases let you drop AS for table aliases, so FROM employees e works too. Keeping AS is clearer for beginners.
You can use a column alias to sort the results. ORDER BY sorts rows.
SELECT first_name,
salary * 12 AS yearly_pay
FROM employees
ORDER BY yearly_pay DESC;DESC means highest first. The database sorts by the alias yearly_pay, so you do not repeat the calculation. Ben appears first because he earns the most.
SQL does not run a query in the order you write it. It handles FROM and WHERE before SELECT. So the alias does not exist yet when WHERE runs. This fails in most databases:
SELECT salary * 12 AS yearly_pay
FROM employees
WHERE yearly_pay > 40000;The database says the column yearly_pay does not exist. Repeat the calculation in WHERE instead: WHERE salary * 12 > 40000. Another option is to put the query inside a subquery and filter outside it.
| Type | Example | What it does |
|---|---|---|
| Column alias | salary AS pay | Renames a column in the results |
| Calculation alias | salary * 12 AS yearly | Names a calculated column |
| Table alias | FROM employees AS e | Gives a table a short name |
| Quoted alias | AS "Monthly Pay" | Allows spaces in the name |
Aliases are most useful with joins. Read our beginner guide to SQL joins next.
Learn SQL joinsAn alias is a temporary name for a column or table, created with the AS keyword. It only exists while the query runs. It does not change your stored data.
Usually no. Most databases accept SELECT salary pay or FROM employees e. Still, writing AS makes your query easier to read, so it is a good habit for beginners.
Not in most databases. WHERE runs before SELECT, so the alias is not known yet. Repeat the expression in WHERE or use a subquery.
No. The alias changes only the heading in that query's results. To rename a column for good, you need an ALTER TABLE statement.
They shorten long table names and show which table each column comes from. This avoids errors when two tables share a column name such as id.
Need help building a database-driven website or app? Talk to the Fepiq team.
Contact FepiqOccasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.
Keep reading
Learn SQL AND, OR and NOT operators with simple examples. See how to combine conditions in WHERE and avoid the common mistakes beginners make.
Learn JavaScript template literals with simple examples: backticks, ${} variables, and multi-line strings. A beginner-friendly guide you can copy and run.
Let's build something
Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.