Intro
SQL aliases are used to give a table, or a column in a table, a temporary name
They are often used to make columns more readable
They only exist for the duration of the query
Sample Data
These are the tables we will use as an example:
SELECT * FROM ar_invoices
| invoice# | customer# | salesperson# | invoice_date | invoice_amount | amount_paid | current_month_interest_charges | total_interest_charges |
|---|---|---|---|---|---|---|---|
| 1 | 1 | 1 | 2019-06-17 10:43:53.727 | 100.00 | 1.00 | 0.00 | 0.00 |
| 2 | 2 | 2 | 2019-05-28 10:43:53.727 | 50.00 | 0.00 | 5.00 | 10.00 |
| 3 | 3 | 2 | 2019-06-17 10:43:53.727 | 70.00 | 0.00 | 10.00 | 20.00 |
| 4 | 2 | 1 | 2019-07-07 10:43:53.727 | 80.00 | 0.00 | 0.00 | 0.00 |
SELECT * FROM ar_salespeople
| salesperson# | salesperson_name |
|---|---|
| 1 | bob |
| 2 | mary |
| 3 | tim |
Column Alias Syntax
SELECT column_name AS alias_name
FROM table_name
SELECT invoice_amount AS [Invoice Amount] FROM ar_invoices
| Invoice Amount |
|---|
| 100.00 |
| 50.00 |
| 70.00 |
| 80.00 |
Table Alias Syntax
SELECT column_name(s)
FROM table_name AS alias_name
SELECT i.invoice_amount, s.salesperson_name
FROM ar_invoices AS i,
ar_salespeople AS s
WHERE i.salesperson# = s.salesperson#
| invoice_amount | salesperson_name |
|---|---|
| 100.00 | bob |
| 50.00 | mary |
| 70.00 | mary |
| 80.00 | bob |