SQL Aliases Use and Purpose in SQL
SQL Aliases
SQL aliases are used to rename a table or a column on temporary basis. These aliases are used to avoid duplication of field names especially when multiple tables with same field names are joined together or when a function is applied to any column.
In general terms, aliases are used to make columns easily understandable and readable.
Demo Table
For establishing a clear understanding of the above functions, following Products table will be used:
ID | Products | Price |
1 | Keyboard | 50 |
2 | Mouse | 50 |
3 | LCD | 100 |
4 | Speaker | 65.25 |
5 | Printer | 120 |
Syntax for Column Alias
SELECT col_name_1 AS alias_name_1,
col_name_2 AS alias_name_2, … , col_name_n AS alias_name_n
FROM tbl_name;
Example for Column Alias
SELECT ID, Products AS [Product List] FROM Products;
The above query will return following:
ID | Product List |
1 | Keyboard |
2 | Mouse |
3 | LCD |
4 | Speaker |
5 | Printer |
Syntax for Table Alias
SELECT col_name(s) FROM tbl_name AS alias_name;
Example for Table Alias
SELECT * FROM Products AS Product_List;
Aliases for columns and tables are useful when:
- Lengthy column names are used or are not user-friendly
- Two or more columns are merged together to create single field
- There are two or more than two tables included in a query
- SQL Functions are part of the query
More Related Articles For You
- Architecture of DBMS
- CODDS RULE DBMS
- Database Models in DBMS
- Relational DBMS Concepts
- Keys and Types of keys in Database
- Database Normalization
- Generalization, Specialization and Aggregation Concepts in DBMS
- ERD Diagram Tutorial with Examples in DBMS
- Introduction to SQL
- How to Create Query in SQL, Create Table, Delete Table and User Insert Info Statements
- Alter Query Command in SQL, Add, Modify and Drop Column in Table
- TCL Commands in SQL
- Truncate Query, Drop and Rename Query in SQL
- All DML Statement in SQL, Select Statement, Delete, Insert and Update Statement
- Data Control Language DCL Revoke and Grant Command in SQL
- Select Statement or Select Query in SQL Explained with Examples
- Distinct Keyword Explained in SQL
- WHERE Statement or WHERE Clause in SQL Statement Explained
- AND & OR Operators in SQL
- LIKE Operator in SQL
- ORDER BY Clause Sorting Explained in SQL
- Group By Clause in SQL
- Having Clause Explained in SQL
- SQL Constraints
- SQL Aggregate Functions
- SQL Joins Types, Use and Purpose
- SQL Sequence Syntax and Use with Examples
- SQL View
- SET Operation in SQL, UNION, UNION ALL, Intersect and Minus