Sort 490 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
Well, you need to add "Distinct" otherwise it will fetch the duplicate data.
Thank you for your kindful answer!!!!!!!
May I know how does sql identifys its ID properly?, because (id%2)=0 , e.g (2%2)=0 -> 0=0 true, there is no ID here
In the paranthesis enclosed (id%2)=0 here we are performing the arithmetic operation i.e %2 on ID .so in this way sql identifies ID .
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?
Ditto, some explantion would be nice. w3 schools is a bit vague as well.
that was so helpful, thanks friend :)
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;
yes man it works and thank you so much
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.
Thats helpful. Thanks!
It is written in the question but it was not mentioned explicitly that duplicates will be there. I am guessing the data itself has repeated entries of the same city.
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?
Maybe because you use Oracle.
Try "MOD (id, 2) = 0" instead "(id % 2) = 0";
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.
its not working in oracle
Could you explain it to me, why "distinct" required for above statement? Thank you..
select distinct city from station where MOD(id,2) = 0;
% 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;
great its working fine.
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....
can't agree more
ohh so i am confuse in this questios
for ORACLE => SELECT DISTINCT(CITY) FROM STATION WHERE MOD(ID,2)=0 ;
for MySQL => SELECT DISTINCT(CITY) FROM STATION WHERE (ID%2)=0 ;
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 :)
SELECT DISTINCT CITY FROM STATION 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 believe using binary & operation is the best solution here.
You have a good point, but some people may need an example to understand what you mean by this.
Modulus division by any non-negative power of 2 can be replaced by bitwise AND operator & with (2 ** n) - 1 as the second operand, so WHERE condition MOD(ID, 2) = 0 may be turned into ID & 1 = 0.
MOD(ID, 2) = 0
ID & 1 = 0
SELECT DISTINCT(city) FROM station
WHERE (id % 2) =0;
Solution for oracle RDBMS
where MOD(id,2) = 0;
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