AppDividend
Latest Code Tutorials

SQL Alias Example | Alias In SQL Tutorial

0

SQL Alias is the temporary name that exists for a particular query. Alias is used for better understanding and reducing the workload of writing big column names. Aliases are the temporary name and the changes made with the column name while retrieving does not reflect in the original database.

SQL Alias Example

An Alias is a shorthand for the table or column name. Aliases reduce the amount of typing required to write the query. Complex queries with the aliases are generally easier to read. Aliases are useful with JOINs and SQL aggregates: SUM, COUNT, etc. SQL Alias only exists for the duration of that query.

Aliases are useful in these scenarios. When there is more than one table involved in the query Functions are used in that query. When the column names are big or not very readable. When there are two or more columns are combined together. Alias is also used when more than one table is involved, i.e., when the joins are used. These types of Aliases are known as Table Alias.

Syntax: (For column alias)

Select column as alias_name FROM table_name;

Parameters:

  1. Column: Fields in the table.
  2. Alias_name: Temporary names that are to be used apart from the original column names.
  3. Table_name: Name of the table.

Syntax: (For Table Alias)

Select column from table_name as alias_name;

Parameters:

  1. Column: Fields in the table.
  2. Table_name: Name of the table.
  3. Alias_name: Temporary names that are to be used apart from the original table names.

NOTE:

  1. The alias name should be enclosed within the quotes if it contains whitespace.
  2. The alias name is valid within the scope of the SQL statement.

Let’s understand the above syntaxes with the help of an example.

EXAMPLE:

Consider two tables:

Students

IDNAMECITY
1ShubhKolkata
2KaranAllahabad
3SurajKota
4AkashVizag

 

Marks:

IDNAMEMARKSAge
1Shubh9021
2Rounak9121
3Suraj9222
4Akash9322

 

Let’s Begin with column alias:

QUERY:

Select ID AS Roll, Name from Students where city like ‘K%’;

Output:

RollNAMECITY
1ShubhKolkata
3SurajKota

 

Explanation:

So, in the above query, we have used the AS keyword to change the name of the ID column to Roll and displayed the details of those students whose city the first character belongs to K.

For more information about showing the information in this format, refer to SQL WILDCARD OPERATORS.

In the above example, we have also filtered the data using Where clause and Select an ID as Roll and name columns from a table.

Now, Let’s discuss an example for table alias.

Query:

Select S.ID, S.NAME, S.CITY, M.MARKS from Students AS S, Marks AS M where S.ID=M.ID;

Output:

IDNAMECITYMARKS
1ShubhKolkata90
3SurajKota92
4AkashVizag93

 

Explanation:

So, in the above query, we have displayed the marks of those students whose Id’s in students and marks table are the same.

Finally, SQL Alias Example Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.