SQL SELECT AS

SQL SELECT AS – Column and Table Aliases in SQL

SQL SELECT AS

SQL AS is a powerful tool that allows developers to assign temporary names to columns or tables. This feature helps improve query readability, enhances presentation, and makes result sets more understandable without permanently altering the database schema.

By using AS, you can label columns and tables in a way that makes sense for specific queries, making it easier to analyze and interpret the data.

Complete Python Course with Advance topics:-Click Here
SQL Tutorial :-Click Here

Syntax of SQL SELECT AS

SELECT Column_Name1 AS New_Column_Name, Column_Name2 AS New_Column_Name FROM Table_Name;

  • Column_Name: The original column name from the table.
  • New_Column_Name: A temporary alias for that column.
  • Table_Name: The name of the table containing the columns.

Aliases only exist for the duration of the query execution and do not modify the original database structure.

Example 1: Assigning Temporary Column Names

Consider a table named purchases with the following data:

Order_Date Buyer Item Quantity
05-01-2022 Rahul Smartphone 3
18-03-2022 Simran Laptop 1
30-06-2023 Aakash Headphones 5

Now, if we want to rename Order_Date as Purchase_Date and Buyer as Customer, we use the following query:

SELECT Order_Date AS 'Purchase_Date', Buyer AS 'Customer', Item, Quantity FROM purchases;

Result:

Purchase_Date Customer Item Quantity
05-01-2022 Rahul Smartphone 3
18-03-2022 Simran Laptop 1
30-06-2023 Aakash Headphones 5

As seen above, the original column names remain unchanged in the database, but for this query, we have customized their appearance.

Example 2: Using SQL AS with Aggregate Functions

Let’s consider another table named students with the following data:

Roll_No Name Gender Mobile City Age Marks
1 Aman Joshi Male 9123456789 Delhi 21 85
2 Priya Mehta Female 8765432109 Mumbai 22 90
3 Rahul Verma Male 9988776655 Kolkata 23 88
4 Shruti Shah Female 9876543210 Chennai 24 92
5 Kabir Rao Male 9654321098 Jaipur 20 89

Query 1: Calculate the Average Marks

SELECT Name AS Student, AVG(Marks) AS Average_Marks FROM students;

Result:

Student Average_Marks
Aman Joshi 88.80

(Note: The average is calculated for all students, but SQL only returns one row since there is no GROUP BY clause.)

Query 2: Retrieve Roll Numbers and Mobile Numbers with Custom Labels

SELECT Roll_No AS 'Student_ID', Mobile AS 'Contact' FROM students;

Result:

Student_ID Contact
1 9123456789
2 8765432109
3 9988776655
4 9876543210
5 9654321098

Query 3: Combine Multiple Columns Using CONCAT()

If we want to merge the student’s mobile number and city into one column named Student_Info, we can use CONCAT():

SELECT Roll_No AS 'Student_ID', CONCAT(Mobile, ', ', City) AS Student_Info FROM students;

Result:

Student_ID Student_Info
1 9123456789, Delhi
2 8765432109, Mumbai
3 9988776655, Kolkata
4 9876543210, Chennai
5 9654321098, Jaipur

Example 3: Assigning a Temporary Name to a Table

SQL also allows us to create table aliases for easier reference.

Consider the students table from the previous example. If we want to retrieve details for a specific student (Roll No 3) but use a shorter alias for the table, we can do this:

SELECT s.Roll_No, s.Name, s.Gender, s.Mobile, s.City FROM students AS s WHERE s.Roll_No = 3;

Here, s is the temporary alias assigned to the students table, making it easier to reference columns.

Result:

Roll_No Name Gender Mobile City
3 Rahul Verma Male 9988776655 Kolkata


Download New Real Time Projects :-Click here
Complete Advance AI topics:- CLICK HERE

Conclusion

The SQL AS statement is an essential tool for enhancing query readability. Whether renaming columns for presentation purposes, using aliases for aggregate functions, or simplifying table references, AS makes SQL queries more intuitive and structured.

Using aliases effectively can improve the clarity of reports, ease query maintenance, and enhance collaboration among developers and analysts. Next time you run an SQL query, try implementing aliases for better organization and interpretation!

For more database-related tutorials and SQL tips, stay connected with UpdateGadh.


sql select as table
sql with
sql alias
sql alias in where clause
group by sql
alias in sql example
sql as table
sql table alias join
sql
sql join
sql select as example
sql select as column
sql select as oracle
sql select as w3schools

Post Comment