I'm trying to solve the Hackerrank problem Weather Observation Station 3 which is stated below:
I came up with the following solution:
SELECT CITY
FROM STATION
WHERE MOD(ID, 2) = 0
but when I try to run it I get the compiler message "Wrong answer". I don't see what's wrong with this query?
How do you check if a number is even in SQL Server? 10 Answers Use the modulus operator n % 2 . It returns 0 if the number is even, and 1 if the number is odd.
You can only refer to columns by column number in the ORDER BY clause or using the positional notation when using the DBMS_SQL package to execute the statement. (or if you're using . NET or some other 3rd party language that uses positional notation). In a regular SQL statement you can't use such notation.
To find and return the records with the odd or even values, the most simple way is to check the remainder when we divide the column value by 2. When the remainder is 0, that's an even number, otherwise, that's an odd number.
SELECT DISTINCT CITY
FROM STATION
WHERE MOD(ID, 2) = 0
You can try this query:
SELECT DISTINCT CITY FROM STATION WHERE (ID % 2) = 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With