FAQ: Queries - As

This community-built FAQ covers the “As” exercise from the lesson “Queries”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development
Data Science

Learn SQL

FAQs on the exercise As

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

In the lesson, it says:

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?

1 Like

I’m not sure i’m int eh correct spot to ask questions. but I’m curious.

in the excersice it teaches us to call the columns and give them an alias. okay check.

however, if i want to call ALL of the columns but only give some of them aliases, how do i do so?

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.

In this lesson it states:

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. :slightly_smiling_face:

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;