0
Is there a way to condition an if statement in SQL
This question is on hackerrank. Query the Name of any student in STUDENTS who scored higher than Marks. Order your output by the last three characters of each name. If two or more students both have names ending in the same last three characters (i.e.: Bobby, Robby, etc.), secondary sort them by ascending ID. So my question is how can we condition an if statement in an ORDER BY expression. Like I used "ORDER BY Name " then what to do to to sort again by ID when the last three characters are same
11 Réponses
+ 3
you can just do after the where clause:
ORDER BY SUBSTRING(name, -3, LENGTH(name)), ID ASC
+ 1
Мг. Кнап🌠 Yeah mahn looks like the system did not show me your ans before. I swear it wasn't there before. And yep I implemented it and got it correct thanks to you.
0
In SQL will be that called WHERE. More you can see in the Sololearn Tutorial as follows:
https://www.sololearn.com/learn/SQL/1856/
0
Hima he needs all the output, like operator is used with where clause to filter out the result..
But here the requirements is to order by last three characters, and by I'd whilst showing all the students name who scored higher than a give marks
0
JaScript Lol dude I know how to use where clause....my doubt is that ki how can we condition an if statement in an ORDER BY expression.
Just for you to know I wrote
SELECT Name FROM Students WHERE Marks> 75 ORDER BY Name LIKE '%___'
After this how should I put the condition that if there are duplicate items while ordering (having last three characters same) then how do I query that
0
Hima Dude ready question again
0
Мг. Кнап🌠 Any idea?
0
Hrithik Gautam I have already posted the answer, you don't need the if else logic, although we can implement that by creating custom variable and using @IF() function,
But here it is not required
You just need to apply this
SELECT name from students
WHERE marks > 75
ORDER BY SUBSTRING(name, -3, LENGTH(name)), ID ASC
0
Мг. Кнап🌠 Btw, this was a query on hackerrank
0
It's ok, it was helpful, that only should matter 🙂🙂
0
Ya I had read your question actually three times