SQL Aliases


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
1112019-06-17 10:43:53.727100.001.000.000.00
2222019-05-28 10:43:53.72750.000.005.0010.00
3322019-06-17 10:43:53.72770.000.0010.0020.00
4212019-07-07 10:43:53.72780.000.000.000.00
SELECT * 
FROM ar_salespeople
salesperson# salesperson_name
1bob
2mary
3tim

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.00bob
50.00mary
70.00mary
80.00bob