Create a temporary name using ALIAS in SQL

SQL ALIAS is used to create a temporary name (called an alias) for a column or table. This command allows you to change the temporary name for a table or column with a different name in the database.

  1. TABLE ALIAS is used to shorten SQL for easier readability or when you need to manually connect (for example, listing the same table more than once in the FROM clause).
  2. COLUMN ALIAS is used to create column headers in the results for easy viewing or meeting the purpose of a particular SQL query.

Changing this name is temporary and does not change the original table name in the database.

Syntax

The syntax of TABLE ALIAS (table alias) is as follows:

 SELECT cot1, cot2. 
FROM ten_bang AS ten_tamthoi
WHERE [dieu_kien];

The syntax of COLUMN ALIAS (alias column) is as follows:

 SELECT ten_cot AS ten_tamthoi 
FROM ten_bang
WHERE [dieu_kien];

Example of using ALIAS

Suppose the two tables are NHANVIEN and TIENTHUONG with the following records:

Table 1: NHANVIEN

 +----+----------+-----+-----------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | +----+----------+-----+-----------+----------+ | 1 | Thanh | 32 | Haiphong | 2000.00 | | 2 | Loan | 25 | Hanoi | 1500.00 | | 3 | Nga | 23 | Hanam | 2000.00 | | 4 | Manh | 25 | Hue | 6500.00 | | 5 | Huy | 27 | Hatinh | 8500.00 | | 6 | Cao | 22 | HCM | 4500.00 | | 7 | Lam | 24 | Hanoi | 10000.00 | +----+----------+-----+-----------+----------+

Table 2: TIENTHUONG

 +-----+---------------------+-------------+--------+ |TT_ID| NGAY | NHANVIEN_ID | SOTIEN | +-----+---------------------+-------------+--------+ | 102 | 2019-01-08 00:00:00 | 3 | 3000 | | 100 | 2019-01-08 00:00:00 | 3 | 1500 | | 101 | 2019-02-20 00:00:00 | 2 | 1560 | | 103 | 2018-12-20 00:00:00 | 4 | 2060 | +-----+---------------------+-------------+--------+ 

Here's how to use TABLE ALIAS in SQL:

 SQL> SELECT C.ID, C.TEN, C.TUOI, O.SOTIEN 
FROM NHANVIEN AS C, TIENTHUONG AS O
WHERE C.ID = O.NHANVIEN_ID;

The result is:

+----+----------+-----+--------+ | ID | NAME | AGE | AMOUNT | +----+----------+-----+--------+ | 3 | Nga | 23 | 3000 | | 3 | Nga | 23 | 1500 | | 2 | Loan | 25 | 1560 | | 4 | Manh | 25 | 2060 | +----+----------+-----+--------+ 

And the usage of COLUMN ALIAS in SQL is as follows:

 SQL> SELECT ID AS NHANVIEN_ID, TEN AS NHANVIEN_TEN 
FROM NHANVIEN
WHERE LUONG IS NOT NULL;

The above example will return the result:

+-------------+---------------+ | NHANVIEN_ID | NHANVIEN_NAME | +-------------+---------------+ | 1 | Thanh | | 2 | Loan | | 3 | Nga | | 4 | Manh | | 5 | Huy | | 6 | Cao | | 7 | Lam | +-------------+---------------+ 

In the next articles, Quantrimang will discuss with you the index (INDEX) in SQL. Have you remembered to it!

Previous article: NULL value in SQL

Next article: Index (INDEX) in SQL

4.3 ★ | 4 Vote

May be interested

  • Install frames and temporary images on FacebookInstall frames and temporary images on Facebook
    with a temporary avatar feature on facebook, you no longer have to remember to change your avatar after holiday or anniversary instead, it will automatically do it for you.
  • What is Temporary File? How to delete temporary files on windows computersWhat is Temporary File? How to delete temporary files on windows computers
    when you use the software, some temporary files are created and not deleted, filling up memory for a long time. here's how to delete temporary files on a windows computer.
  • Automatically delete temporary files every time a Windows 10 computer is openedAutomatically delete temporary files every time a Windows 10 computer is opened
    whenever you create a file, a corresponding temporary file will also be created with the extension .tmp. over time, temporary files will increasingly fill up and take up hard drive space on your computer. so you should clean up these temporary files to free up hard drive space, and to improve the performance of your windows 10 computer.
  • Steps to remove temporary files stored on KasperskySteps to remove temporary files stored on Kaspersky
    like other programs and antivirus applications, in the process of using kaspersky internet security software, kaspersky will store temporary files in a folder hidden deep in the system drive.
  • How to use the temporary permission feature of Android 11 on any phone?How to use the temporary permission feature of Android 11 on any phone?
    not everyone uses pixel phones. that means you are not likely to experience the latest android 11 features. the following article will tell you how to get temporary, one-time permissions on android 11 without rooting.
  • How to Get a Temporary Phone Number in the USHow to Get a Temporary Phone Number in the US
    a temporary phone number is a good way to protect privacy in situations such as a 'blind date' (a date between two strangers) or a meeting set up on an online classified site. you can use temporary phone numbers through applications that connect using mobile minutes or simple wifi networks. prepaid cell phones and sim cards are also simple options for getting a temporary phone number. skype and google voice are popular options for getting an additional phone number for short-term use (or you can keep it long-term if you want).
  • LOCAL TEMPORARY TABLE in SQL ServerLOCAL TEMPORARY TABLE in SQL Server
    this tutorial explains how to use local temporary table in sql server with syntax and examples.
  • How to Rid Your Computer of Temporary FilesHow to Rid Your Computer of Temporary Files
    temporary files are created as safeguards against errors, accidental shut-downs, and other interruptions amidst running programs. while these files help you quickly recover your information, they also take up random access memory (ram),...
  • Instructions for temporarily blocking FacebookInstructions for temporarily blocking Facebook
    sometimes you want to temporarily lock your facebook account to spend time concentrating on exams or for work but you don't know how to do it. moreover, you still wonder if if you miss the lock, then if you want to reopen later, will it be possible? in the article below, we will help you answer that question
  • How to fix 'You've been signed in with a temporary profile' error on Windows 10How to fix 'You've been signed in with a temporary profile' error on Windows 10
    this article will guide you how to fix you've been signed in with a temporary profile for user accounts on windows 10.