Yogesh Chauhan's Blog

Learn how to give a temporary name to a column or to a table using SQL Aliases

in SQL/MySQL on August 24, 2019

SQL Aliases

We use SQL Aliases to give a temporary name to a table or a column. Sometimes the column names are too lengthy or just complicated so that we can’t show it in the results to the user. In those cases, Aliases are really handy.

NOTE: We can only use Aliases in the single query results. We have to make Aliases again with new query even if we want to use same Aliases.

Syntax


SELECT column AS alias
FROM table;

SELECT column/columns
FROM table AS alias;

The first syntax is for column Aliases and the second one is for table Aliases.

Example


SELECT Name AS Name, Population AS Population 
FROM country 
LIMIT 10;

In the query above, I am creating two Aliases. One is Name and second one is Population. I haven’t changed any names since both the names are simplified already. 

You can see the results in DEMO link provided at the end of this article.

Let’s see how to combine columns.


SELECT Name, CONCAT(Region,', ',Continent,', ',LocalName) AS RCL 
FROM country 
LIMIT 10;

We use CONCAT keyword to combine two or more columns and create Aliases.

NOTE: The above syntax is exclusively used for MySQL only.

The SQL query above will combine the data from Region, Continent and LocalName columns to make one Aliases RCL. You can see the results in DEMO link provided at the end of this article.

Alias for Tables


SELECT c.Language, b.Name, b.Region 
FROM countrylanguage AS c, country AS b 
WHERE c.Language='French' 
AND c.CountryCode=b.Code;

In the query above I am creating Aliases for 2 tables. One is c for countrylanguage tables and other one is b for country table. I have used those Aliases names to pull up data from two tables.

For e.g. c.Language pulls up data from the table countrylanguage and column Language, b.Name pulls up data from country table and Name column and b.Region pulls up data from the same country table but Region column. 

After pulling up the data, I’ve applied a WHERE clause in which I am applying two conditions.

One is, I want the data for the Language French so c.Language=’French’ (do not forget to add quotes). The other condition is I want the data in both tables and one way to do that is comparing the country code from both of the tables.

Now in the countrylanguage the name of the country code column is CountryCode but in the country table it’s just Code.

So we have to be careful while comparing those two columns or the query won’t work. So at the end the second condition is c.CountryCode=b.Code and I am using AND operator to make sure both of the conditions are right. That’s it. See the results by yourself in the DEMO.


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
Sorting Object Arrays in JavaScriptJavaScriptList of social media icon logo color codes in HEXMiscellaneousConfiguring Modules with @forward rule in SCSS (Sass)SCSSWhat are Modules and Components in Angular?AngularHow to Clone Objects in PHP?MiscellaneousHow different is Handling Events in React vs HTML DOM?React