Act fast, special offers end soon! Up to $294 is savings when you get Premium today.
Get offer codes

Joining Data in SQL 179-7: Different queries but the same result

Good day!

While I was working on mission 179-7, I typed the code for the problem like the below and got the right result. However, before I moved on to the next page I checked the answer sheet and found that their code is different from mine. Whereas I filtered the row after joining the table, in the answer, they did the filtering on the original table first and then joined the table to the other one.

Screen Link:
https://app.dataquest.io/m/179/joining-data-in-sql/7/combining-joins-with-subqueries

My Code:

SELECT c.name capital_city, f.name country, c.population
FROM facts f
INNER JOIN (
            SELECT * FROM cities 
            WHERE capital = 1
            ) c ON c.facts_id = f.id
WHERE c.population > 10000000
ORDER BY c.population DESC;

Suggested Answer:

SELECT c.name capital_city, f.name country, c.population population
FROM facts f
INNER JOIN (
            SELECT * FROM cities
            WHERE capital = 1
            AND population > 10000000
           ) c ON c.facts_id = f.id
ORDER BY 3 DESC;

I can understand how the two codes are following different order when they are executed yet showing the same result but want to know if it’s just a matter of writing style that my code is different from the provided answer or there’s something more at deeper level.

Thank you!

1 Like

Hi @idenk9725,

There are certainly differences in the way the query is getting executed due to the stage at which we are filtering the rows. However, fundamentally, both are same! :slightly_smiling_face:

Best,
Sahil

Hello @Sahil,

Your reply explains what I wanted know, that is, if they are fundamentally the same or not.

Thanks! :slightly_smiling_face:

Regards,

idenk9725

1 Like