Loading
Lesson 39
Courses / HackerRank SQL Problem Solving Solution Challenges
African Cities | SQL Basic Join | HackerRank Solution

Video Transcript

Hey everyone, I will do African cities from the basic join section of SQL in HackerRank. So we're given these two tables city and country and they match by the city.country code with the country.code columns. And we're asked to query the name of all cities where the continent is Africa. Now I can do here first if we start select and we're going to do name from city, but we got to be careful. Does name appear in the other column? It does. So country.name appears. So that's ambiguous. Two columns with the same name, right? Two different tables. So we have to say city.name. That's the name of the table to differentiate with the country.name. Now with that out of the way, we have to do a filter here because we want only the ones whose continent is Africa. Now we don't have any column about continent in the city table. So we have to refer to the country table. So we have to combine these two tables and match the records based on the country code. So here the where would be the continent, the Africa. Now continent is a column that only appears in the country table. Therefore we don't have to explicitly disambiguate with a country.continent there. Now select city.name from city. That's fine, but we need to get continent from the other tables. So we have to do a join here. What kind of join are we going to do? Let me show you the Venn diagram that I like to reference a lot for SQL joins. Whenever you need to do it and you don't know which one, refer to this diagram. Now we're going to do the one in the center here called an inner join where we have two tables A and B and we're going to have records that both appear in A and B that is the intersection between these two tables. So we're going to match these two according to city.countrycode and country.code. So here we're going to say inner join, the other table is country, okay? On, you have to say on, what's the condition to match the records from one table with the other? We're going to say country.code, that's the column, has to be equal to the city.countrycode. So these two values have to match so that we have the two combined together into a single row. Now let's submit the code and see what we get.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: