HackerRank SQL Preparation: Weather Observation Station 3(MySQL)
Problem Statement: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.
Link: HackerRank - Weather Observation Station 3
Solution:
SELECT DISTINCT CITY FROM STATION WHERE ID % 2 = 0;
Explanation:
SELECT DISTINCT CITY
: This part of the query specifies that you want to retrieve theCITY
column from the STATION table, andDISTINCT
ensures that each city name is unique, eliminating any duplicates from the result.FROM STATION
: Indicates that you are selecting data from the STATION table.WHERE ID % 2 = 0
: This condition filters the rows to include only those cities where theID
is an even number. The modulo operation (% 2 = 0
) checks if theID
is divisible by 2, meaning it's even.
This query will return a list of unique city names from the STATION table where the city has an even ID
number. The result will exclude any duplicate city names.