SQL Basic – Hacker Rank Solution

Revising the Select Query I & II

Problem 1 : Query all columns for all American cities in the CITY table with populations larger than 100000. The CountryCode for America is USA.

The CITY table is described as follows:

Solution :  MySQL
SELECT * FROM CITY WHERE COUNTRYCODE = 'USA' AND POPULATION > 100000;

Problem 2 : Query the NAME field for all American cities in the CITY table with populations larger than 120000. The CountryCode for America is USA.

The CITY table is described as follows:

Solution : MySQL
SELECT name FROM CITY WHERE COUNTRYCODE = 'USA' AND POPULATION > 120000;

Select All

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

The CITY table is described as follows:

Solution : MySQL
select * from city ;

Select By ID

Problem : Query all columns for a city in CITY with thID 1661.

The CITY table is described as follows:

Solution : MySQL
select * from city where id = 1661;

Japanese Cities’ Attributes

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

The CITY table is described as follows:

Solution : MySQL
select * from city where countrycode = 'JPN' ;

Japanese Cities’ Names

Problem : Query the names of all the Japanese cities in the CITY table. The COUNTRYCODE for Japan is JPN.
The CITY table is described as follows:

Solution : MySQL
select name from city where countrycode = 'JPN'

Weather Observation Station

Problem 1 : Query a list of CITY and STATE from the STATION table.
The STATION table is described as follows:

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

Solution : MySQL
select city , state from station ;

Problem 2 : Query a list of CITY names from STATION for cities that have an even ID number. Print the results in any order, but exclude duplicates from the answer.
The STATION table is described as follows:

Solution : MySQL
select distinct city from station where MOD (id,2)=0 ORDER BY CITY ;

Problem 3 : Find the difference between the total number of CITY entries in the table and the number of distinct CITY entries in the table.
The STATION table is described as follows:

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

For example, if there are three records in the table with CITY values ‘New York’, ‘New York’, ‘Bengalaru’, there are 2 different city names: ‘New York’ and ‘Bengalaru’. The query returns 1.

Solution : MySQL 
select count(CITY)- count(distinct CITY) from STATION

Problem 4 : 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.
The STATION table is described as follows:

Solution : MySQL
select CITY,LENGTH(CITY) from STATION order by Length(CITY) asc, CITY limit 1; 
select CITY,LENGTH(CITY) from STATION order by Length(CITY) desc, CITY limit 1; 

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

Input Format

The STATION table is described as follows:

Solution : MySQL  
select city from station where city REGEXP "^[aeiou].*" order by city;

Disclaimer : ऊपर दी गयी problems को Hacker Rank दवारा generate किया गया है। लेकिन इन problems के solution आपको हमारे दवारा दिए गए है।

One Reply to “SQL Basic – Hacker Rank Solution”

Comments are closed.