Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select columns with an even ID number in SQL?

Tags:

sql

oracle

I'm trying to solve the Hackerrank problem Weather Observation Station 3 which is stated below:

enter image description here

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?

like image 832
Kurt Peek Avatar asked Sep 12 '16 08:09

Kurt Peek


People also ask

How do you select even ID numbers in SQL?

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.

Can you select a column by number in SQL?

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.

How can I get odd ID in SQL?

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.


2 Answers

SELECT DISTINCT CITY
FROM STATION
WHERE MOD(ID, 2) = 0
like image 158
Ted Avatar answered Sep 20 '22 14:09

Ted


You can try this query:

SELECT DISTINCT CITY FROM STATION WHERE (ID % 2) = 0 
like image 26
Vishal Prajapati Avatar answered Sep 22 '22 14:09

Vishal Prajapati