DEV Community

Manoj Kumar
Manoj Kumar

Posted on

HackerRank SQL — Getting American City Names with Population Over 120000

This one was a little different from the last two. Instead of grabbing everything with SELECT star, this time the problem only wanted one column — the NAME of the city. So I had to be a bit more specific with what I was selecting.

The conditions were simple though. Only American cities, meaning COUNTRYCODE has to be USA, and only the ones with a population bigger than 120000.

Here is what I wrote:

SELECT NAME
FROM CITY
WHERE COUNTRYCODE = 'USA'
AND POPULATION > 120000;
Enter fullscreen mode Exit fullscreen mode

SELECT NAME means I only want the city names, nothing else. FROM CITY is the table. Then the WHERE part has two conditions joined with AND, which means both have to be true at the same time. The city has to be in the USA and its population has to be more than 120000. If either one fails, that row gets skipped.

The AND keyword is what made this problem slightly different. When you have more than one condition, AND makes sure all of them have to match, not just one of them.

Small step up from the previous problems but still very easy to follow once you break it down piece by piece.

Top comments (0)