SQL SELECT AS

·

·

Rename Columns with SQL SELECT AS

You can use a form of SQL SELECT AS to rename columns in your query results.

So far you’ve seen where queries results return results named after the table columns. This is fine for most cases, but once you start working with expressions, you’ll see this doesn’t work well.

To rename a column use AS.

In this example we’re renaming FirstName to First and LastName to Last.

SELECT FirstName AS First , LastName AS Last FROM Person.Person;
SELECT FirstName AS First
	 , LastName AS Last
FROM Person.Person;

The AS is optional, can you rewrite the above SQL without using AS?

— Try rewriting our first example without using AS.
-- Answer
SELECT FirstName First
	, LastName Last
FROM Person.Person;

Renaming columns may seem like a nice to have feature, but it also comes in handy when we want to return a calculated result.

For instance, suppose you wanted to return the LastName in all upper case. You could write

SELECT UPPER(LastName)
FROM Person.Person
SQL SELECT AS - No Column name specified.

But once you run the query, you’ll see no column name is returned. To ensure the column is given a name, you can use AS.

SELECT UPPER(LastName) as [LAST NAME] FROM Person.Person
-- Answer
SELECT UPPER(LastName) as [LAST NAME]
FROM Person.Person

your Turn!

Can you write a query to return a distinct list of upper case first names? Name the column [Distinct Names].

Write your query below they try it.

-- Answer
SELECT DISTINCT UPPER(FirstName) AS [Distinct Names]
FROM Person.Person;

Additional SELECT Statement Resources

If you’re looking to learn more about the SELECT command, the I would recommend you read our article Simple Select Queries.

To learn more about functions, such as UPPER, check out our article Introduction to Common String Functions.

Looking for a super SQL book? Check out SQL Queries for Mere Mortals.

More from the blog


MySQL PostgreSQL SQLite SqlServer