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:!!!

10 Likes

thanks for your sharing!

4 Likes

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

4 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?

2 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.

8 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.

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!