SQL Alias (Alias)
- Previous Page SQL Between
- Next Page SQL Join
By using SQL, you can specify aliases (Alias) for column names and table names.
SQL Alias
SQL table alias syntax
SELECT column_name(s) FROM table_name AS alias_name
SQL column alias syntax
SELECT column_name AS alias_name FROM table_name
Alias instance: Use table name alias
Assuming we have two tables, respectively, "Persons" and "Product_Orders". We assign aliases "p" and "po" to them.
Now, we want to list all the orders of "John Adams".
We can use the following SELECT statement:
SELECT po.OrderID, p.LastName, p.FirstName FROM Persons AS p, Product_Orders AS po WHERE p.LastName='Adams' AND p.FirstName='John'
SELECT Statement Without Aliases:
SELECT Product_Orders.OrderID, Persons.LastName, Persons.FirstName FROM Persons, Product_Orders WHERE Persons.LastName='Adams' AND Persons.FirstName='John'
From the two SELECT statements above, you can see that aliases make the query program easier to read and write.
Alias Example: Using a column alias
Table Persons:
Id | LastName | FirstName | Address | City |
---|---|---|---|---|
1 | Adams | John | Oxford Street | London |
2 | Bush | George | Fifth Avenue | New York |
3 | Carter | Thomas | Changan Street | Beijing |
SQL:
SELECT LastName AS Family, FirstName AS Name FROM Persons
Result:
Family | Name |
---|---|
Adams | John |
Bush | George |
Carter | Thomas |
- Previous Page SQL Between
- Next Page SQL Join