DEV Community

Manoj Kumar
Manoj Kumar

Posted on

HackerRank SQL — Fetching All Japanese Cities from the CITY Table

Another day, another Basic Select problem on HackerRank. This one asked me to get all the details of every Japanese city from the CITY table. Japan's country code in the table is JPN, so that is what I had to filter on.

Same thinking as before. They said all attributes so I went with SELECT star, no need to type out every column. Then I just had to filter the rows where the country code matches JPN.

Here is what I wrote:

SELECT *
FROM CITY
WHERE COUNTRYCODE = 'JPN';
Enter fullscreen mode Exit fullscreen mode

SELECT star pulls everything, FROM CITY tells it which table, and WHERE COUNTRYCODE = 'JPN' makes sure you only get the Japanese cities and nothing else.

One small thing worth noting here is that JPN is a text value, not a number, so it needs to go inside single quotes. If you skip the quotes the query will throw an error. Small thing but easy to forget when you are just starting out.

That is it. Clean and simple. These problems are short but they slowly make you comfortable with how SQL thinks, and that is what matters at this stage.

Top comments (0)