Episode 2 – Renaming a Column with an Alias

Episode 1 left off using a simple SQL query SELECTing four columns FROM the Computers table in PDQ Inventory:

SELECT ComputerId,
       Name,
       OSName,
       IsOnline
FROM Computers

When writing SQL queries there will be times, for any reason, you’d rather call a column by a different name. You can easily rename a column (or table, introduced in a future video and blog post) using an alias. An alias is a temporary name and is only active during the span of the query.

An alias is not a permanent name change

The alias is always written directly after the column name using the AS command. Include the alias in single quotes:

rename a column with an alias

SELECT ComputerId,
       Name AS ‘Computer Name’,
       OSName,
       IsOnline
FROM Computers

TIP: The last column in a SELECT statement never has any punctuation after it. Notice how all other columns are separated by commas. If you accidentally leave a comma after the last column of your SELECT statement, you will receive a syntax error. In PDQ Inventory, the syntax error looks something like: 

Expanding the error description by clicking the blue question mark symbol will show you a more detailed visual:

The description states the error is ‘near “FROM”. This is because the FROM statement is directly after the syntax error.

Check out a live video here.

Your email address will not be published.

Your Name

This site uses Akismet to reduce spam. Learn how your comment data is processed.