In SQL, the SELECT AS clause is an essential feature that helps improve query readability and makes our database results more understandable. By aliasing columns and tables, we can provide meaningful names to our output, making complex queries easier to interpret and manage.
In this article, we will explain SQL SELECT AS syntax, its uses, and practical examples to help us become proficient in SQL query optimization.
What is the SQL SELECT AS Clause?
The SELECT AS clause in SQL is used to rename columns or expressions in the result set of a query. This is known as aliasing. Aliases provide a temporary name for a column or expression that makes the results more readable, particularly when working with aggregate functions, joins, or complex calculations.
For instance, instead of displaying a generic column name, we can give it a more meaningful label. This is especially helpful in enhancing the SQL query readability, making it easier for users to interpret results without modifying the underlying table structure.
SQL SELECT AS Syntax
1. Alias for Columns
SELECT column_name AS alias_name
FROM table_name;
Key Terms
column_name: The original name of the column in the table.
alias_name: The temporary name you want to assign to the column in the output.
2. Alias for Tables
SELECT column_name
FROM table_name AS alias_name;
Key Terms
table_name: The original name of the table.
alias_name: The temporary name assigned to the table.
Examples Using SELECT AS Clause in SQL
For better understanding of SQL SELECT AS Alias we will consider two tables which are Orders and Customers with multiple columns as defined below:
Table 1: Orders
| OrderID |
CustomerID |
OrderDate |
TotalAmount |
| 1 |
101 |
2024-08-01 |
250 |
| 2 |
102 |
2024-08-02 |
300 |
| 3 |
103 |
2024-08-03 |
150 |
| 4 |
101 |
2024-08-04 |
450 |
| 5 |
104 |
2024-08-05 |
200 |
Table 2: Customers
| CustomerID |
CustomerName |
| 101 |
Alice |
| 102 |
Bob |
| 103 |
Charlie |
| 104 |
Dana |
Example 1: Alias for Columns
To select data from the Orders table and rename the columns for clarity, we can use aliases.
Query:
SELECT OrderID AS "Order Number",
CustomerID AS "Client ID",
OrderDate AS "Date",
TotalAmount AS "Amount"
FROM Orders;
Output
| Order Number |
Client ID |
Date |
Amount |
| 1 |
101 |
2024-08-01 |
250 |
| 2 |
102 |
2024-08-02 |
300 |
| 3 |
103 |
2024-08-03 |
150 |
| 4 |
101 |
2024-08-04 |
450 |
| 5 |
104 |
2024-08-05 |
200 |
Example 2: Using Aliases With a Space Character
If we want to include spaces in our column aliases, we must enclose the alias name in quotes.
Query:
SELECT CustomerID AS "Customer ID",
TotalAmount AS "Order Amount"
FROM Orders;
Output
| Customer ID |
Order Amount |
| 101 |
250 |
| 102 |
300 |
| 103 |
150 |
| 101 |
450 |
| 104 |
200 |
Example 3: Concatenate Columns
To create a full name by concatenating FirstName and LastName columns from the Customers table (assuming it has FirstName and LastName columns), we can use concatenation with an alias.
Query:
SELECT CONCAT(FirstName, ' ', LastName) AS "Full Name"
FROM Customers;
Output
| Full Name |
| Alice Smith |
| Bob Brown |
| Charlie Davis |
| Dana White |
Example 4: Alias for Tables
Using aliases for tables can simplify our queries, especially when joining tables. Hereâs how we can use table aliases to join the Orders and Customers tables and select specific fields. Where, O is an alias for the Orders table and C is an alias for the Customers table.
Query:
SELECT O.OrderID AS "Order Number",
C.CustomerName AS "Customer"
FROM Orders O
JOIN Customers C ON O.CustomerID = C.CustomerID;
Output
| Order Number |
Customer |
| 1 |
Alice |
| 2 |
Bob |
| 3 |
Charlie |
| 4 |
Alice |
| 5 |
Dana |
Conclusion
The SELECT AS clause is an essential tool for creating clean, efficient, and readable SQL queries. Whether we’re renaming columns, creating user-friendly reports, or simplifying complex joins, aliases significantly improve the structure of our SQL queries. Mastering the SELECT AS clause will help us write better SQL code, enhance the clarity of our reports, and streamline our database management system.
Similar Reads
SQL - SELECT LAST
SELECT LAST is a concept or function often used to describe retrieving the last record or last row from a table in SQL. Although MS Access supports a LAST() function to directly fetch the last value from a column, this function is not universally supported across all SQL-based databases. Instead, in
5 min read
SQL SELECT FIRST
The SELECT FIRST clause is used in some SQL databases (primarily MS Access) to retrieve the first record from a table based on the order in which data is stored or queried. It is commonly used when you need to access just one entry, such as the first row based on the natural order or after sorting b
3 min read
SQL Select Database
The USE DATABASE statement is a command in certain SQL-based database management systems that allows users to select and set a specific database as the default for the current session. By selecting a database, subsequent queries are executed within the context of that database, making it easier to i
3 min read
SQL - SELECT NULL
In SQL, the term NULL represents a missing or undefined value in a table. A NULL value is different from a zero or an empty string; it signifies that no data exists for that field. Understanding how to work with NULL values is crucial for accurate data retrieval and manipulation. In this article, we
3 min read
SQL SELECT Query
The select query in SQL is one of the most commonly used SQL commands to retrieve data from a database. With the select command in SQL, users can access data and retrieve specific records based on various conditions, making it an essential tool for managing and analyzing data. In this article, weâll
4 min read
SQL SELECT COUNT()
In SQL, the SELECT statement is a fundamental tool for retrieving data from a database. When paired with the COUNT() function, it becomes even more powerful, enabling efficient data aggregation and analysis. This article provides a detailed explanation of SELECT and COUNT() in SQL, including syntax,
4 min read
PL/SQL SELECT FROM
PL/SQL is Oracle procedural extension for SQL which allows for more powerful and flexible database manipulation. The SELECT statement is one of the most fundamental and widely used commands in SQL. It allows users to retrieve data from one or more tables in a database. In this article, we will learn
5 min read
SQLite SELECT Query
SQLite is a serverless, popular, easy-to-use, relational database system that is written in c programming language. SQLite is a database engine that is built into all the popular devices that we use every day of our lives including TV, Mobile, and Computer. As we know that we create the tables in th
6 min read
SQL | INTERSECT Clause
In SQL, the INTERSECT clause is used to retrieve the common records between two SELECT queries. It returns only the rows that are present in both result sets. This makes INTERSECT an essential clause when we need to find overlapping data between two or more queries. In this article, we will explain
5 min read
SET vs SELECT in SQL
In SQL, SET and SELECT are powerful commands that are commonly used for assigning values to variables and retrieving data. While both are essential, they serve different purposes and are optimized for different scenarios. Understanding the differences between these two commands can greatly enhance o
5 min read