ALIAS in SQL Server
ALIASES in SQL Server is used to create temporary names (called aliases) for columns or tables.
- COLUMN ALIASES is used to create column headers in results for easy viewing
- TABLE ALIASES 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).
Syntax for ALIASES alias
Syntax to set alias for column
ten _cot [ AS ] bi_danh
or set the alias for the table
ten_bang [AS] bi_danh
Variable name or variable value
ten_cot
The original name of the column to which you want the alias
ten_bang
The original name of the table to which you want the alias
AS
option. Most programmers use the keyword AS when setting the alias for the column but not when setting the table. Whether it is used or not, it does not affect the alias in MySQL. Unlike other databases, this is an optional option in MySQL. (The examples below will use AS when setting aliases for columns and removing AS when setting tables).
bi_danh
temporary names set for columns or tables.
Note
- If bi_danh contains spaces, it must be placed in quotation marks.
- You can use spaces when setting aliases for columns. But often, do not use spaces when setting tables.
- The bi-name is only valid in SQL statements.
For example - set the alias for the column
Often aliases are used to create column headers in results that are easy to see.
SELECTnhanvien_id, ten + ho AS NAME
FROM nhanvien
WHERE ten =
'Sarah';
In this example, we set the alias for the second column (ie, combining the first and last name) as NAME. In the result, NAME will be the title of the second column. Because the above bi_danh has no spaces, no quotes are needed. But if you want, use this mark.
SELECT nhanvien_id, ten + ho AS 'NAME'
FROM nhanvien
WHERE ten = 'S
arah';
This is another example that needs to put bi_danh in quotation marks.
SELECT nhanvien_id, ten + ho AS 'TEN NHAN VIEN'
FROM nhanvien
WHERE ten = 'Sara
h';
In this example, the second column in the result is set to TEN NHAN VIEN.
For example - set the alias for the table
Setting the alias for the table can be used to list a table more than once in the FROM clause (or self-connect) or shorten the table name to make it easier to read.
SELECT s.sanpham_ten, hangtonkho.chatluong
FROM sanpham s
INNER JOIN hangtonkho
ON s.sanpham_id = hangtonkho.sanpham_id
ORDER BY s.tsanph
am_ten ASC, hangtonkho.chatluong DESC;
The above example creates an alias for the sanpham table which is s. Now in the SQL statement, it is possible to call the sanpham table s. When creating a table alias, it is not necessary to create an alias for all tables given in the FROM clause. For example, create an alias for the hangtonkho table as shown below.
SELECT s.sanpham_ten, h.chatluong
FROM sanpham s
INNER JOIN hangtonkho h
ON s.sanpham_id = h.sanpham_id
ORDER BY s.sanpham_ten ASC,
h.chatluong DESC;
The hangtonkho board is called h and the sanpham board is called s.
Previous article: Conditions NOT in SQL Server
Next lesson: JOIN in SQL Server
You should read it
May be interested
- 7 great ideas using Raspberry Pi as a serverraspberry pi is a great solution for many computer projects, from learning programming to remote control a car to building a basic stop-motion animation studio. but do you know that raspberry pi can also be used as a server? here are some ideas for using raspberry pi as a server.
- New points in SQL Server 2017the sql server 2017 version is primarily connected to linux, bringing the power of sql to linux. in short, you can install sql server 2017 on linux, using sql server 2017 on linux-based docker containers. sql server 2017 also allows you to choose development languages, develop it on-premise or cloud-based.
- Instructions for setting up and managing FTP Server on Windows 10if you want to create a private cloud for sharing and converting large files without restrictions, you can create an ftp server (file transfer protocol server) on your windows 10 computer.
- Create VPN Server on Windows 8no need to install any additional applications, you can easily 'turn' your computer into a vpn server if you're using windows 8. in this way, you can share data from the computer. as a simple lan system in the form of remote access. & a
- What is the future of server virtualization?server virtualization can help combat poor server performance, make better use of computing capabilities, limit energy consumption and improve data center flexibility.
- Learn about the architecture of MS SQL Serverin the previous articles, you already know briefly about sql server, how to install sql server on the computer. in this section we will learn about the architecture of sql server.
- Instructions for installing MS SQL Serverthis is a step by step guide to installing ms sql server.
- How to install DNS Server on Windows Server 2019from microsoft, the domain name system (dns) is one of the industry standard protocol sets that includes tcp / ip, along with dns client and dns server that provide name resolution services that map names to the ip addresses of computers. .
- Install Windows Server 2003 and create a backup servernetwork management documentation server 2003 as details of how to install, create a companion server in case the main server is malfunctioning, create a domain, join the client to the domain, the conection {remote desktop from the client data to server}, set adsl router parameters to server, turn off 1 working machine immediately.
- Instructions to change DNS Server on Windows, Mac, iOS and Androidby default, your computer will automatically obtain dns information from your isp (isp). sometimes, dns servers are unstable and you are blocked from accessing certain websites. or the default dns server is blocked by some websites for a number of reasons. in this case you should change the dns server with the free public dns server.