Recent Posts

HackerRank: [SQL Basic Select] (18/20) Higher Than 75 Marks | right, order by in SQL

HackerRank: [Basic Select - 18/20] Higher Than 75 Marks |  RIGHT, ORDER BY in SQL
I started studying SQL from a very famous site - HackerRank. Here I will try to provide multiple approaches & solutions to the same problem. It will help you learn and understand SQL in a better way.

Please make use of my blog posts for learning purpose only and feel free to ask your questions in the comment box below in case of any doubt.

Click Here for the previous blog-post in the series.


SQL Problem Statement:

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.



Input Format:

The STUDENTS table is described as follows:

STUDENTS columns

The Name column only contains uppercase (A - Z) and lowercase (a - z) letters.

Sample Input:
STUDENTS sample data

Sample Output:
Ashley
Julia
Belvet

Explanation:
Only Ashley, Julia, and Belvet have Marks > 75. If you look at the last three characters of each of their names, there are no duplicates and 'ley' < 'lia' < 'vet'.



Solution: (MySQL Query):

SELECT NAME
FROM STUDENTS
WHERE MARKS > 75
ORDER BY RIGHT(NAME,3) ASC, ID ASC;

NOTE: 
  1. The RIGHT function is used to take a substring of CITY starting from the right, and "3" is passed as an argument to select the only 3 characters from the right.


Sample Output:

Stuart
Kristeen
Christene
Amina
Aamina
Priya
Heraldo
Scarlet
Julia
Salma
Britney
Priyanka
Samantha



--------------------------------------------------------------------------------
Click here to see solutions for all Machine Learning Coursera Assignments.
&
Click here to see more codes for Raspberry Pi 3 and similar Family.
&
Click here to see more codes for NodeMCU ESP8266 and similar Family.
&
Click here to see more codes for Arduino Mega (ATMega 2560) and similar Family.
Feel free to ask doubts in the comment section. I will try my best to answer it.
If you find this helpful by any mean like, comment and share the post.
This is the simplest way to encourage me to keep doing such work.

Thanks & Regards,
-Akshay P Daga

No comments