AS is a keyword in SQL that allows you to rename a column or table using an alias.
and then:
When using AS , the columns are not being renamed in the table. The aliases only appear in the result.
(emphasis mine). I find this confusing. It sounds like the AS keyword is actually used to add a name to the column (additional name = alias). I.e. after running
SELECT imdb_rating AS 'IMDb'
FROM movies;
the imdb_rating column can now be addressed in further SQL statements either as imdb_rating or as IMDb, correct?
If I understood this correctly: What exactly is the point of doing that? Won’t it just be a source of errors if one thing gets two names?
If you need more details about navigating the forums and where to ask your query then the following link will help-
Typically you’d be selecting the columns you want explicitly anyway so just use AS more than once if necessary. I’m not aware of any shortcuts that aren’t at the least a bit hacky but you can try a web search for the particular version you’re working with to check your options.
AS is a keyword in SQL that allows you to rename a column or table using an alias. The new name can be anything you want as long as you put it inside of single quotes. Here we renamed the name column as Titles.
Some important things to note:
Although it’s not always necessary, it’s best practice to surround your aliases with single quotes.
When using AS, the columns are not being renamed in the table. The aliases only appear in the result.
However, in later lessons, whenever I use aliases I get syntax errors for the single quotes. Did a quick search and found that using double quotes or square brackets is the correct syntax for aliases with spaces for SQLite. Just wondering why we’re taught one thing and then our work is tested against different rules.
You are correct in that this command is creating an alias, not renaming as stated in the text. Creating short aliases can be helpful in the future when creating code that manipulates data with complicated structural names.
I believe the point is to make the results more readable and understandable. While in this example we’re simply changing imdb_rating to IMDb, but I think there are column names that are extremely long and hold no value to anyone. It’d be useful if you’re working with a column named 00004059769C94 which could mean book titles, but your audience wouldn’t know unless you created an alias. Hopefully, that helps.
Can we alias multiple columns in a single query?
Yes, it’s possible. While I couldn’t get it to run as two separate lines. I used the following by placing a comma between the alias AS commands to piggyback off the same SELECT command.
SELECT imdb_rating AS ‘IMDb’, name AS ‘calling’
FROM movies;