Structured Query Language is one of the most important languages used in the industry. It’s one of the most sought languages desired by the employers as the volume of data is increasing, in order to access the humongous data from respective databases, it is important to know this skill which would help you retrieve, update and manipulate data.

In this post, we will be covering all the solutions to **SQL on the HackerRank **platform. HackerRank is a platform for competitive coding. It is very important that you all first give it a try & brainstorm yourselves before having a look at the solutions. Let us code and find answers to our given problems.

I. 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;

XI. Weather Observation Station 6

Query the list of CITY names starting with vowels (i.e., aeio, or u) from STATION. Your result cannot contain duplicates.

#data-analysis #sql #technology #data-science #programming #data analysis

SQL HackerRank Solutions
92.50 GEEK