SQL AS renames a column or table to a more convenient alias (a correlation name) without changing the original names in the database. This makes writing queries easier when the original table or column names are long or complicated.
Contents
SQL AS: Main Tips
- SQL AS is used as a temporary name for the purpose of a particular SQL query.
- Aliases ensure better readability.
SQL AS for Columns
Example
SELECT col_name AS al_name
FROM tbl_name;
The example below describes both CustomerName and ContactName aliases.
When there are spaces between names of the columns, you must use square brackets or quotation marks:
Pros Main Features
- Easy to use with a learn-by-doing approach
- Offers quality content
- Gamified in-browser coding experience
- The price matches the quality
- Suitable for learners ranging from beginner to advanced
- Free certificates of completion
- Focused on data science skills
- Flexible learning timetable
Pros Main Features
- Simplistic design (no unnecessary information)
- High-quality courses (even the free ones)
- Variety of features
- Nanodegree programs
- Suitable for enterprises
- Paid Certificates of completion
Pros Main Features
- A wide range of learning programs
- University-level courses
- Easy to navigate
- Verified certificates
- Free learning track available
- University-level courses
- Suitable for enterprises
- Verified certificates of completion
SQL AS for Tables
Example
SELECT col_name(s)
FROM tbl_name AS al_name;
The example below picks out every order from customer with CustomerID=4. The aliases of the tables are given o and c for the orders and the customers table. This is done to make the SQL code shorter.
Example
SELECT o.ID, c.Name
FROM Customers AS c, Customer_orders AS o
WHERE c.id = 2 AND c.ID = o.customer_id;
And a matching example with no alias:
Example
SELECT Customer_orders.ID, Customers.Name
FROM Customers, Customer_orders
WHERE Customers.id = 2 AND Customers.ID = Customer_orders.customer_id;
Demo Database
In the table below you can see a piece of Customers table:
ID | Name | Contact | Address | City | Postal_Code | Country |
---|---|---|---|---|---|---|
1 | Ben Choplinks | Ben Choplink | Obeesre Str. 51 | Rome | 11207 | Italy |
2 | Donald Rich | Donald Richario | Avda. de la Confgfstitución 4122 | Tallin | 17021 | Estonia |
3 | Lilly Smilkins | Lilly Smilkin | Matadsderos 2312 | Eguero | 14023 | Mexico |
4 | Brandinina | Tom Hitchins | 110 Hanegover Sq. | London | WB2 2DP | UK |
5 | Carizmos | Christiano Kerrys | Berguvsesvägen 9 | Luleå | S-968 43 | Sweden |
Customer_Orders
id | customer_id | employee_id |
---|---|---|
20408 | 2 | 7 |
85471 | 37 | 3 |
75864 | 77 | 8 |
SQL AS: Summary
- Using alias does not change a column or table name in the original database.
- Aliases make complicated or long names easier read.