SQL Solutions for HackerRank - I

Chaitanya Sharma
4 min readDec 10, 2020

SQL Solutions from HackerRank — I

Hello Techies,

Most of you guys might be looking for the SQL and Questions and answers. In this series, I’ll cover SQL Problems and Solutions to it, and might come across one of the most popular coding and learning platform called Hackerank.

This is Part 1 of the series for solutions to SQL on the HackerRank.

Good Luck!

1. Revising the Select Query 1

Query all columns for all American cities in CITY with populations larger than 100000. The CountryCode for America is USA.

Input Format

The CITY table is described as follows:

SELECT * FROM CITY WHERE COUNTRYCODE = ‘USA’ AND POPULATION > 100000;

II. Revising the Select Query 2

Query the names of all American cities in CITY with populations larger than 120000. The CountryCode for America is USA.

Input Format

The CITY table is described as follows:

SELECT NAME FROM CITY WHERE COUNTRYCODE = ‘USA’ AND POPULATION > 120000;

III. Select All

Query all columns (attributes) for every row in the CITY table.

Input Format

SELECT * FROM CITY;

IV. Select By ID

Query all columns for a city in CITY with the ID 1661.

Input Format

SELECT * FROM CITY WHERE ID = 1661;

V. Japanese Cities’ Attributes

Query all attributes of every Japanese city in the CITY table. The COUNTRYCODE for Japan is JPN.

Input Format

SELECT * FROM CITY WHERE COUNTRYCODE = ‘JPN’;

VI. Japanese Cities’ Names

Query the names of all the Japanese cities in the CITY table. The COUNTRYCODE for Japan is JPN.

Input Format

SELECT NAME FROM CITY WHERE COUNTRYCODE = ‘JPN’;

VII. Weather Observation Station 1

Query a list of CITY and STATE from the STATION table.

Input Format

The STATION table is described as follows:

where LAT_N is the northern latitude and LONG_W is the western longitude.

SELECT CITY, STATE FROM STATION;

VIII. Weather Observation Station 3

Query a list of CITY names from STATION with even ID numbers only. You may print the results in any order but must exclude duplicates from your answer.

Input Format

The STATION table is described as follows:

where LAT_N is the northern latitude and LONG_W is the western longitude.

SELECT DISTINCT CITY FROM STATION WHERE MOD(ID, 2) = 0;

IX. Weather Observation Station 4

Let N be the number of CITY entries in STATION, and let N’ be the number of distinct CITY names in STATION; query the value of N-N’ from STATION. In other words, find the difference between the total number of CITY entries in the table and the number of distinct CITY entries in the table.

Input Format

The STATION table is described as follows:

where LAT_N is the northern latitude and LONG_W is the western longitude.

SELECT COUNT(CITY) — COUNT(DISTINCT CITY) FROM STATION ;

X. Weather Observation Station 5

Query the two cities in STATION with the shortest and longest CITY names, as well as their respective lengths (i.e.: number of characters in the name). If there is more than one smallest or largest city, choose the one that comes first when ordered alphabetically.

Input Format

The STATION table is described as follows:

where LAT_N is the northern latitude and LONG_W is the western longitude.

SELECT * FROM (SELECT DISTINCT city, LENGTH(city) FROM station ORDER BY LENGTH(city) ASC, city ASC) WHERE ROWNUM = 1

UNION

SELECT * FROM (SELECT DISTINCT city, LENGTH(city) FROM station ORDER BY LENGTH(city) DESC, city ASC) WHERE ROWNUM = 1;

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response