Reading data with SELECT
Column and table aliases
Name result columns and shorten table references without changing stored data.
Lesson 7 of 84
Reading: about 2 min
Lesson + practice: about 10 min
Difficulty: Beginner
On this page4 sections
This lesson uses DDI_SQL_Training dataset version 1.0.0 on SQL Server 2019 or later. A report may need clearer column headings than the names stored in a table. Aliases let you label the result without changing the database.
Run the example
SELECT c.CustomerID AS ID, c.FirstName AS GivenName, c.City
FROM dbo.Customers AS c
ORDER BY c.CustomerID;Read it step by step
- FROM dbo.Customers AS c gives Customers the short name c for this statement. The alias disappears when the query finishes.
- c.CustomerID identifies the source column. AS ID changes only its result heading. The same pattern turns FirstName into GivenName.
- ORDER BY c.CustomerID asks SQL Server to display the rows in ID order. The first three therefore have IDs 1, 2, and 3.
Check the result
| ID | GivenName | City |
|---|---|---|
| 1 | Alex | London |
| 2 | Maya | Manchester |
| 3 | Leo | Birmingham |
Notice that the heading is ID, not CustomerID, and GivenName, not FirstName. The values are unchanged. The table still has columns named CustomerID and FirstName after this query.
Practice
Return ProductName under the heading Name and ListPrice under the heading Price, using p as the Products alias.
Check your answer and expected result
Reveal answer
SELECT p.ProductName AS Name, p.ListPrice AS Price FROM dbo.Products AS p;| Name | Price |
|---|---|
| Laptop Stand | 39.00 |
| USB-C Dock | 129.00 |
| Webcam | 79.00 |
| Notebook | 8.50 |
| Pen Set | 12.00 |
| Desk Lamp | 45.00 |
| Headphones | 89.00 |
| Speaker | 55.00 |
| Microphone | 119.00 |
| Wireless Mouse | 28.00 |
| Keyboard | 65.00 |
| Cable Pack | 14.00 |
| Desk Chair | 189.00 |
| Side Table | 95.00 |
| Shelf Unit | 149.00 |
This returns 15 product rows.
Your progress
Reveal the check answer or reach the end to complete this lesson.