Step 2 - Selecting Specific Columns When Executing the Join?

Screen Link:

Step 2

This was my answer which threw an error. Why?

SELECT name AS country_name FROM facts AS f
INNER JOIN cities AS c ON c.facts_id = facts.id
LIMIT 5;

DQ Answer:

SELECT c.*, f.name country_name FROM facts f
INNER JOIN cities c ON c.facts_id = f.id
LIMIT 5;

1). why do I have to specify f.name AS country_name and not just name AS country_name? It doesn’t just know that its for facts since that the only table on that row (in my answer)?
2) Why do I have to type c.* to specify that I want all columns? If you just do the inner join
does it not just include all the columns for cities by default unless otherwise specified?
3) So basically when doing a join and you want to specify columns to be included, you just have to follow the syntax: table_name.column_name all the time?

A paragraph explaining this important nuance in the Learn Section would be helpful.

Thank you

It’s not the only table on that row in your code since you are joining it with another table. It needs to know which table the column corresponds to.

If you use * instead of c*, you will get all columns from both tables. Which, as per the instructions, is not what is required.

Not all the time. If you have a column in table 1 that is not in table 2, you can just use that column’s name normally as there is no conflict. However, as per me, it would be better to use the table_name.column_name format because it allows for better code readability. And if you ever have to return to that code for any reason, you can instantly figure out which table that column corresponds to.

You can provide them with content feedback through the ? in the top-right side of the content page.

1 Like

Thank you @the_doctor for explaining this so well. Very helpful!

1 Like