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;
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?
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.
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.
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.
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.