AppDividend
Latest Code Tutorials

SQL Aliases Example | Alias In SQL Tutorial

0

SQL Aliases Example | Alias In SQL Tutorial is today’s topic. Aliases are the temporary names that exist for a particular query. It 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 Aliases 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

ID NAME CITY
1 Shubh Kolkata
2 Karan Allahabad
3 Suraj Kota
4 Akash Vizag

 

Marks:

ID NAME MARKS Age
1 Shubh 90 21
2 Rounak 91 21
3 Suraj 92 22
4 Akash 93 22

 

Let’s Begin with column alias:

QUERY:

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

Output:

Roll NAME CITY
1 Shubh Kolkata
3 Suraj Kota

 

Explanation:

So, in the above query, we have used AS keyword to change the name of ID column to Roll and displayed the details of those students whose city first character belongs with 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:

ID NAME CITY MARKS
1 Shubh Kolkata 90
3 Suraj Kota 92
4 Akash Vizag 93

 

Explanation:

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

Finally, SQL Aliases Example | Alias In SQL 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.