SQL SELECT AS

SQL SELECT AS – Column and Table Aliases in SQL

SQL SELECT AS – Column and Table Aliases in SQL

Interested in above project ,Click Below
WhatsApp
Telegram
LinkedIn

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_DateBuyerItemQuantity
05-01-2022RahulSmartphone3
18-03-2022SimranLaptop1
30-06-2023AakashHeadphones5

See also  IN vs EXISTS in SQL: A Complete Guide

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_DateCustomerItemQuantity
05-01-2022RahulSmartphone3
18-03-2022SimranLaptop1
30-06-2023AakashHeadphones5

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_NoNameGenderMobileCityAgeMarks
1Aman JoshiMale9123456789Delhi2185
2Priya MehtaFemale8765432109Mumbai2290
3Rahul VermaMale9988776655Kolkata2388
4Shruti ShahFemale9876543210Chennai2492
5Kabir RaoMale9654321098Jaipur2089

Query 1: Calculate the Average Marks

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

Result:

StudentAverage_Marks
Aman Joshi88.80

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

See also  SQL DELETE DATABASE – A Complete Guide

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

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

Result:

Student_IDContact
19123456789
28765432109
39988776655
49876543210
59654321098

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_IDStudent_Info
19123456789, Delhi
28765432109, Mumbai
39988776655, Kolkata
49876543210, Chennai
59654321098, 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.

See also  SQL DELETE TABLE: Understanding DELETE, TRUNCATE, and DROP Statements

Result:

Roll_NoNameGenderMobileCity
3Rahul VermaMale9988776655Kolkata


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

🎓 Need Complete Final Year Project?

Get Source Code + Report + PPT + Viva Questions (Instant Access)

🛒 Visit UpdateGadh Store →
💬 Chat Now