Sort 885 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:)
THANKS MAN !
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 .
thank u so much. it is very helpful. u explained it very nicely and clearly.
THANKS, NICE EXPLANATION
yeah.. this is very nice explanation
Great explanation. Thank you.
it's showing error
can i get an alternative to it.
Not all DB types use the exact same keyword for modular arithmetic. In Oracle its MOD(ID,2).
thank you so much
Thank You So Much!
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
I try this, it shows error. Do you know what's the issue?
y we use distinct here
coz we need no duplicates
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.
select distinct city from station where mod(id,2)=0; -----> select as oracle
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
Yep, this works for me
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.
to avoid duplication..
because DISTINCT remove(discard) the duplicate attributes in city column
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;
nice explaination yes it reallly works...
IT DOES NOT WORK!
I tried it and it works fine. Try to add distinct in order to select distinct elements. I also changed oracle to mysql
thanks!! its working...
IF I AM USING IT IS COMING LIKE THIS
select distinct city from station where (id % 2) = 0
ERROR at line 1:
ORA-00911: invalid character
WHAT I HAVE TO DO FOR THAT.
SELECT DISTINCT CITY FROM STATION WHERE (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
why using mod 2 nd only 2 nd =0
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
YES 👏 I 👏 AGREE👏
SELECT DISTINCT CITY FROM STATION WHERE (ID % 2) = 0;
That's not necessary anymore.
okay, after thinking through the problem, you'ld realize that the problem is about querying a list of city names without DUPLICATES.
This can be achieved by using the 'DISTINCT' keyword after SELECT before CITY.
SELECT DISTINCT CITY
WHERE mod(ID, 2)= 0;
I think the description was clear enough to understand ! What is that you guys found crappy about it, please let me kow ?
SELECT UNIQUE CITY
WHERE MOD(ID, 2) = 0;
I agree, deam, thouht I am stupid or something as I can not solve the task...
Yes totaly agree
Remember even number
e.g: 4 is a even ID number
for ORACLE => SELECT DISTINCT(CITY) FROM STATION WHERE MOD(ID,2)=0 ;
for MySQL => SELECT DISTINCT(CITY) FROM STATION WHERE (ID%2)=0 ;
I went for the %, did'nt know what was wrong until i googled it.
THANK YOU! I was wondering why the % wouldn't work for me on oracle...
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 :)
what is the use of DISTINCT here?
to avoid duplication
WHY WE ARE USING MOD() HERE .
SQL MOD() function is used to retrieve only unique records depending on the specified column or expression .............that's why i did .
Any after mod() we need to set it =0
you're most welcome😊
SELECT DISTINCT CITY FROM STATION WHERE MOD(STATION.ID,2)=0;
Duplicate entries can be removed by using DISTINCT and for checking whether the ID is odd or even , we use MOD() in SQL
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