Consulting cities without repetition and with different "WHERE"

Asked

Viewed 48 times

0

When trying to resolve a platform bank issue hackerrank I could not understand the answer after having found 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 must exclude Duplicates from your Answer.

Make a query of names of the cities in the table STATION with only identification ID numbers. You can display the result in any order, but delete duplicate answers.


Description of the STATION table

inserir a descrição da imagem aqui


The answer marked as right would be

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

My question is about the WHERE (id % 2 ) = 0, I didn’t understand the answer, at first only the DISTINCT solves the question, someone can explain?

Have another way in Mysql?

  • 1

    Translation error: "with Even ID" translates to "with par ID". Your translation ignored this use of even, which is opposed to odd (unique)

  • 1

    If you had Even number the translation would have been done correctly, thank you for the remark @Jeffersonquesado =) I passed beaten =(

1 answer

1


There was a translation error there. He wants different results that have a non-odd ID. To find out if an integer is even, the rest of the division by 2 must be zero. You can represent in SQL that way:

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 Github repository with some Hackerrank SQL track solutions, if you want to take a look and contribute, feel free)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.