Can we alias multiple columns in a single query?

Question

Can we alias multiple columns in a single query?

Answer

Yes, you can alias multiple columns at a time in the same query.

It is advisable to always include quotes around the new alias. Quotes are required when the alias contains spaces, punctuation marks, special characters or reserved keywords. It is simply more convenient to always include quotes around the new alias.

Example

SELECT course_id AS "Course ID", exercise_id AS "Exercise ID"
FROM bugs;
25 Likes

thanks sharing! :smiley:!!!

11 Likes

thanks for your sharing!

4 Likes

Is it possible to give two aliases to the same column? I would assume not?

3 Likes

It is possible! This is a completely valid query:

SELECT id, name AS "name 1", name AS "name 2" FROM employees;

Possible? Yes. Useful? Probably not :slight_smile:

22 Likes

Thanks!!

When giving an alias to a column, is that a permanent command? So whenever I query the dataset, I can now use my alias instead of the original column title, until I takeaway the alias with another command and restore the column to its original name?

3 Likes

I do have the same doubt. When I was doing the exercise, I entered SELECT * FROM table to see if there was any indication of the aliases I had just set. But I didn’t see. Not sure if that alone answers the doubt, though.

2 Likes

Alias is used to increase readability. When you want to use it with the SELECT command, it will just retrieve the data from the database, and not change the database.

Alias is not actually re-naming the column in the database, but it’s helping you to clear the difficulty in reading the result from your query.
For example; If I have Netflix_rating_score in a database column , you can easily set the result of your query to;

SELECT Netflix_rating_score AS ‘Rating’
FROM Netflix;

To conclude, Rating will appear in your query result at that moment but no changes has taken place in the actual database column.
So if you query (( SELECT * FROM Netflix; )), you will still see the column Netflix_rating_score. This is so because, the AS constrain works just like a computer RAM.

The RAM saves stuff while you are working on them, and those stuff disappear when they are not in use or active or when the computer is shutdown.

10 Likes

Thanks!!!
makes sense as anyway ‘select’ command does not change anything.

Thanks - good to know.

SELECT imdb_rating AS ‘IMDb’
FROM movies;

SELECT * FROM movies;

I entered this code but still the code result shows IMDb row only, not the movies table, reason?

1 Like

I am also trying to present all the rows from movies where imdb_rating is alias as rating and it does not work…
idk why

When you write:

SELECT imdb_rating AS ‘IMDb’
FROM movies;

SELECT * FROM movies;

You query the DB twice and you get two results. Scroll down and you’ll see the second return with the full table - but without the alias.

1 Like

nice one! :smiley: thanks for sharing :smiley:

1 Like

This code should work to alias rating as imdb_rating:

SELECT imdb_rating AS ‘rating’

FROM movies;

Unless you want the rest of the table as well. Select only ‘selects’ the column you specified. If you want everything else you would need the rest of the columns like:

SELECT *, imdb_rating AS ‘rating’

FROM movies;

Here you can see that the original imdb_rating column still exists and the new rating column is a duplicate of the imdb_rating, with the alias now as rating.

1 Like

Does it require double quotes, or can it also use single quotes? I’ve just started learning SQL etc, and I’m not sure if I remember double quotes being used much, as usually they’re single quotes.

Thanks!

Thank you for the answer

Hey there. This is a late reply.

SQLite uses either single or double quotes but other RDBM systems can vary. MySQL also uses single or double quotes, but Postgres, Oracle, SQL Server, Snowflake (cloud-based data warehouse), and others use double quotes.

The original poster already mentioned when quotations are required, but the AS keyword is typically only required when using a reserved keyword as an alias when not using quotations. Reserved keywords must be single quoted in SQLite.

All these systems will accept unquoted multiple word aliases separated by an underscore when not starting with a symbol, number, or special character. MySQL is the exception and will accept aliases starting with a number without quotations.

The following examples are all valid:
SELECT first_name
SELECT first_name AS 'First Name'
SELECT first_name '1st Name'
SELECT first_name AS 'Select'
SELECT first_name 'SELECT'
SELECT first_name AS 1st_Name (MySQL only)
SELECT first_name "First Name" (all RDBMs mentioned as well as others)

All the single-quoted examples work for SQLite and MySQL but can be replaced with double quotes for other systems.