Querying cities without repetition and with "WHERE" different

0

When trying to resolve a platform bank issue hackerrank I could not understand the answer after having found it in the discussion wing.

The question says:

  

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

     
    

Make a query for the names of cities in the STATION table with only ID numbers. You can show the result in any order, but exclude duplicate responses.

  
  

Description of the STATION table

     
    

  

The answer marked as certain would be

SELECT DISTINCT city FROM station WHERE (id % 2) = 0;

My question is about WHERE (id % 2 ) = 0 , I did not understand the answer, at first only DISTINCT solves the question, can anyone explain?

Is there another way to do it in MySQL?

    
asked by anonymous 01.12.2017 / 03:05

1 answer

1

There was a translation error there. It wants distinct results that have non-odd IDs. To find out whether an integer is even, the remainder of the division by 2 must be zero. You can represent it in SQL like this:

inteiro % 2 = 0

So the query looks like this:

-- 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.
-- https://www.hackerrank.com/challenges/weather-observation-station-3/problem

SELECT DISTINCT City
FROM Station
WHERE Id % 2 = 0;

(I have a repository on GitHub with some solutions from the HackerRank SQL track, if you want to take a look and contribute , feel free to)

    
01.12.2017 / 04:44