Hackerrank SQL Practice questions-P5

Ankit songara
1 min readSep 6, 2020

--

Difficulty : Easy

Problem 5:

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:

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

Solution:

select distinct CITY from STATION where ID%2 = 0

Query Breakdown:

select CITY column without any duplicate values: select distinct CITY
source table name: from STATION
condition: where ID%2 = 0

Note: If you remove distinct from the above query, you’ll get duplicate values as well. e.g Manchester will appear twice.
% is an arithmetic operator which returns the remainder of a number divided by another number. You can use any of the below three for finding modulo of a number.

Using MOD(N,M)

select distinct CITY from STATION where MOD(ID,2) = 0

Using N % M

select distinct CITY from STATION where ID%2 = 0

Using N MOD M

select distinct CITY from STATION where ID MOD 2 = 0

--

--