Providing Meaningful Names for Columns via SQL query (Column Aliases)

SQL studing: Column Aliases

Problem

You would like to change the names of the columns that are returned by your query so they are more readable and understandable. Consider this query that returns the salaries and commissions for each employee:

1 select sal,comm
2   from emp

What’s SAL? Is it short for sale? Is it someone’s name? What’s COMM? Is it communication? You want the results to have more meaningful labels.

Solution

To change the names of your query results, use the AS keyword in the form original_name AS new_name. Some databases do not require AS, but all accept it:

 1 select sal as salary, comm as commission
 2   from emp

SALARY   COMMISSION
-------  ----------
    800
   1600         300
   1250         500
   2975
   1250        1400
   2850
   2450
   3000
   5000
   1500           0
   1100
    950
   3000
   1300

Discussion

Using the AS keyword to give new names to columns returned by your query is known as aliasing those columns. The new names that you give are known as aliases. Creating good aliases can go a long way toward making a query and its results understandable to others.

 

Вас заинтересует / Intresting for you:

A Brief History of SQL
A Brief History of SQL 5393 views Александров Попков Wed, 17 Oct 2018, 15:04:29
How to Retrieving a Subset of ...
How to Retrieving a Subset of ... 1697 views Денис Wed, 14 Jul 2021, 04:45:37
SQL: how to determine which ro...
SQL: how to determine which ro... 1394 views Денис Tue, 06 Jul 2021, 18:47:53
Referencing an Aliased Column ...
Referencing an Aliased Column ... 6678 views Денис Wed, 14 Jul 2021, 12:59:03
Comments (0)
There are no comments posted here yet
Leave your comments
Posting as Guest
×
Suggested Locations