Is there a way to condition an if statement in SQL | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

27th Aug 2020, 3:33 PM
Hrithik Gautam
Hrithik Gautam - avatar
11 Answers
+ 3
you can just do after the where clause: ORDER BY SUBSTRING(name, -3, LENGTH(name)), ID ASC
27th Aug 2020, 3:40 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
+ 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.
27th Aug 2020, 5:44 PM
Hrithik Gautam
Hrithik Gautam - avatar
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/
27th Aug 2020, 3:43 PM
JaScript
JaScript - avatar
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
27th Aug 2020, 3:50 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
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
27th Aug 2020, 4:11 PM
Hrithik Gautam
Hrithik Gautam - avatar
0
Hima Dude ready question again
27th Aug 2020, 4:12 PM
Hrithik Gautam
Hrithik Gautam - avatar
27th Aug 2020, 4:12 PM
Hrithik Gautam
Hrithik Gautam - avatar
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
27th Aug 2020, 4:18 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
0
Мг. Кнап🌠 Btw, this was a query on hackerrank
27th Aug 2020, 5:45 PM
Hrithik Gautam
Hrithik Gautam - avatar
0
It's ok, it was helpful, that only should matter 🙂🙂
27th Aug 2020, 5:45 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
0
Ya I had read your question actually three times
27th Aug 2020, 5:46 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar