Loading
Lesson 35
Courses / HackerRank SQL Problem Solving Solution Challenges
Population Density Difference | SQL Aggregation | HackerRank Solution

Video Transcript

Hey everybody, I'll be doing the exercise population density difference from the subsection aggregation of SQL in hacker rank. So we're given the city table has the columns ID name, country code, district and population. We're asked to query the difference between the maximum and minimum populations in city. So this question is all about the column population. So we take value population, that's the lowest, and we take what's the greatest. We have to subtract greatest minus lowest, and that's going to give us the difference. Now SQL has the functions min for to take the minimum value and max to get the maximum. So we're going to use those and just subtract them. So let's get started here. I'm going to do select star from city just to get started here. And now we want to care about the population. So select population. Now I need first the maximum value. How can I get the maximum value, the greatest value I can find in all the rows for this population. So we're going to use max parenthesis population. That's going to take the maximum value that it can find for population. Now we have to subtract, I'm going to say minus. Now I need to take the minimum. Can you guess which function is going to be? M, I, N, min of population. So we take the maximum value minus the minimum value, and that's going to give us the difference. I'm going to click submit code. And here's the difference.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: