Some error occured while loading page for you. Please try again.
Sort 339 Discussions, By:
Please Login in order to post a comment
Crappy description, Author must try and be more clear about using even number as ID Column.
Thank you! :)
select distinct city from station where (id % 2) = 0;
hi what does the "= 0 do" ????
It is the qualifier for the WHERE statement.
(Id % 2) tells SQL to divide the Id by 2 and return the remainder. Since we're looking for only even values we set this to "= 0", as 2%2=0, 4%2=0, etc.
If we were looking for odd-numbered Ids, we'd use "= 1" instead, since there's always a remainder of 1 when dividing odd numbers by 2.
Hope this helps!
Thank you, that was helpful
Nice expaination. Thank you.
It was Helpful:)
WELL PARANTHESIS ARE NOT NECASSRY THOUGH CODE WORK FINE WITH THEM
THIS MEANT THE SAME THING BECAUSE OF OPERATOR PRECEDENCE
BUT ITS A GOOD HABIT TO USE PARANTHESIS.
I JUST WANTED NEWBIES TO KNOW THE BASIC POINT
it's showing error
can i get an alternative to it.
WE CAN ALSO WRITE MOD(ID,2)=0, BOTH ARE SAME BUT WE CAN WRITE LIKE THIS ALSO.
ya worked well...Thanks
yeah you are right but it is not take mod(id,2)=0
That's not working for me.
This is not working (for oracle or DB2 ).. I used below command which worked ...
select distinct city from station where Mod(ID,2)=0;
This is working with Oracle.
This is the first time I heard of Mod() is it just for Oracle or something?
I TRIED FOR IT..... BUT IAM NOT GETTING IT
Select distinct CITY from STATION
where (ID%2) = 0;
not working for even mysql
will work with MYSQL but not ORACLE
Not working for oracle.
select distinct(city)from station where MOD( id, 2 ) = 0;
mod(id,2)=0 will come. i checked in google search. Still the query is not working for the given question.
i tried - select distinct CITY from STATION where (ID % 2) = 0; but doesn't work. Tell me why?
try this in oracle- select distinct(CITY) from STATION where MOD(id,2)=0;
you are right, but it works with MySQL only. Oracle gives error with this syntax for "%".
why did you write distinct city rather than only city?
Hi, This is bcz in question it is clearly written that "must exclude duplicate in ur ans" .If u write ur query by using "distinct " then duplicate city names will not be in ur answr.
tried this didnt work but worked with MOD(ID,2)=0
For some reason using % is giving me an error of invalid character. Any idea why?
This is correct query and can also be write as [select distinct city from station where (id % 2) = 0;]
but answer is showing wrong output.
% doesn't work here, better use MOD
MOD() function works in Oracle while % works in MySQL.
You are right Manish
output throws error stating invalid output
This works for Oracle:
select distinct city from station where (mod(id, 2) = 0);
Its perfect thank you!
Great code works with all SQL languages.
WHY USE DISTINCT ? NOT UNDERSTANDING THE MEANING OF YOUR STATMENT .
to avoid the duplicates
The problem statement states that print all data, excluding duplicate value. So the distinct keyword in SQL allows to fecth only distinct data values.
So distinct keyword is used.
this was not work for me ,help me to solve this
Perfect!! 'Distinct' eliminates duplicate values as asked in the question.
could explain me the (id%2)=0;
It's evaluating if the remaining of the division(id/2) is equal to 0.
It's a way to discover if a number is even or odd.
Полностью согласен, блять
I think he was very clear. Alot of people kind of overlooked the part where they said "no duplicates" (including myself) which would have meant using SELECT DISTINCT
work for Oracle, but it took me a long time to understand what the author really wanted.
select distinct(city) from station where mod(id,2)='0';
In this particular query, you don't need to use DISTINCT to avoid duplicates. For some reason, you do need to use distinct to have it marked as a correct answer. However, in reality, it doesn't make a difference in this query. There are cases in other types of queries when you can end up with duplicates (especially from joins and such) but in this case distinct should not be required.
Correct anwser on MYSQL:
SELECT DISTINCT a.CITY as CITY from (SELECT ID, CITY FROM STATION WHERE MOD(ID,2)=0) as a;
yes i agree , it is not clear
Yes, I agree with you. Atleast he must describe how to use even-odd mathematic in SQL?
I tried this "select distinct CITY from STATION where (ID % 2) = 0;" but didn't work.
Agree!!! After I tried 5 times, I still don't know what's wrong with the query.... then I found my misunderstanding led by the ambiguous description....
Here is Oracle solution from my HackerrankPractice repository:
SELECT DISTINCT CITY
WHERE MOD(ID, 2) = 0;
Feel free to ask if you have any questions :)
for ORACLE => SELECT DISTINCT(CITY) FROM STATION WHERE MOD(ID,2)=0 ;
for MySQL => SELECT DISTINCT(CITY) FROM STATION WHERE (ID%2)=0 ;
Solution for oracle RDBMS
where MOD(id,2) = 0;
SELECT DISTINCT City
WHERE MOD(Id,2) = 0
ORDER BY City;
select DISTINCT CITY from STATION WHERE (ID % 2) = 0;
I am not sure why this query is not working in Oracle environment. Though it works fine with MYSQL and MS SQL Server. Any idea?
//Below should work in Oracle Env:
select distinct city
it passed ..
No it not passed it
Yep! It didn't passed for me too... :|
What means (id % 2) = 0 ??
Sorry for stupic question. I'm just learning SQL.
% gives the remainder when id is divided by 2.
To identify the even id
MAY BE % IS LIKE OPERATOR
This will be work for this problem, Thanks
-----select distinct city from station where(id%2=0)
select distinct CITY from STATION where (ID % 2) = 0
These query works in MySql
select distinct city
It is showing error in select keyword