SQL lesson learned 1

Andrea Allen
2 min readSep 2, 2021

--

Today I was working on Hackerrank questions for SQL practice. The problem was to:

Query the Name of any student in STUDENTS who scored higher than 75 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.

My original answer was:

And returned results:

I realized this answer was wrong because SUBSTRING looks for the name column then finds the third character and counts the next three characters. I noticed this because the name Evil was the second output. Per the instructions given, Evil should be towards the bottom. The query as written ordered Evil by ‘il’ instead of the desired output of ‘vil’.

The correct solution:

Results:

The RIGHT function looks at every name in the name column and pulls the last three letters. For example, the new query looks at Stuart and focuses on ‘art’ and puts it in the result set based on those letters.

--

--

No responses yet