All posts
SQLNovember 4, 20267 min read

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.

F
Fepiq Team
Fepiq

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.

What is an SQL alias?

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.

The example table

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.

sql
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.

Column aliases with AS

A column alias changes the heading shown in your results. Put AS after the column, then the new name.

sql
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 for calculations

Aliases are most useful when you calculate something. Without one, the database invents an ugly heading such as ?column? or salary * 12.

sql
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.

Aliases with spaces

If you want a heading with spaces, wrap it in quotes. In standard SQL and PostgreSQL, use double quotes. MySQL also accepts backticks.

sql
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.

Table aliases

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.

sql
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.

sql
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.

Aliases in ORDER BY

You can use a column alias to sort the results. ORDER BY sorts rows.

sql
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.

Where you cannot use a column alias

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:

sql
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.

Quick reference

TypeExampleWhat it does
Column aliassalary AS payRenames a column in the results
Calculation aliassalary * 12 AS yearlyNames a calculated column
Table aliasFROM employees AS eGives a table a short name
Quoted aliasAS "Monthly Pay"Allows spaces in the name

Common mistakes

  • Using an alias in WHERE. Repeat the expression instead.
  • Forgetting quotes around a name with spaces.
  • Using a table alias in SELECT but forgetting to define it in FROM.
  • Choosing unclear aliases like x or t1 in big queries. Pick short but meaningful names.
  • Thinking the alias changes the table. It only affects the current query.

Aliases are most useful with joins. Read our beginner guide to SQL joins next.

Learn SQL joins

Frequently asked questions

What is an alias in SQL?+

An 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.

Is the AS keyword required in SQL?+

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.

Can I use an alias in a WHERE clause?+

Not in most databases. WHERE runs before SELECT, so the alias is not known yet. Repeat the expression in WHERE or use a subquery.

Does an SQL alias rename the column permanently?+

No. The alias changes only the heading in that query's results. To rename a column for good, you need an ALTER TABLE statement.

Why use table aliases in joins?+

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 Fepiq

Get new posts in your inbox

Occasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.

Keep reading

Related posts

All posts

Let's build something

Ready to ship your next product with Fepiq?

Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.