Query to order by the last three characters of a column

how to get last 2 digits of a number in sql
order by right
order by right('sql)
sql substring right of character
right in sql
right(name 3) in sql
secondary sort sql
substring in sql

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.

STUDENTS table has following columns:

ID , NAME , MARKS

Sample input:

id         name     marks
1          ashley   81
2          samantha 75
3          julia    76
4          belvet   84

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'.

this is correct output:

select name from students where marks>75

order by substr(name,-3,3),id;

Try with this for MySQL:

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

SQL Notes: Hackerrank Higher Than 75 Marks, 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. The Name column only contains uppercase (A-Z) and lowercase (a-z) letters. This will do it, very simply selecting the right-most 3 characters and ordering by that value ascending. SELECT * FROM table_name ORDER BY RIGHT(name, 3) ASC; It should be added that as your data grows, this will become an inefficient solution.

You can try this command:

SELECT * FROM students ORDER BY RIGHT(name, 3), ID

Let me know. ;-).

Higher Than 75 Marks Discussions | SQL, Query the names of students scoring higher than 75 Marks. Sort the output by the LAST three characters of each name. https://dba.stackexchange.com/questions​/34951/order-by-clause-is-allowed-over-column-that-is-not-in-select-list. 0|. First, the database system sorts the result set by the first name in ascending order, then it sorts the sorted result set by the last name in descending order. Notice the change in position of two employees: Alexander Khoo and Alexander Hunold. 3) Using SQL ORDER BY clause to sort values in a numeric column example

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. Thats why ORDER BY ID is needed

For Mysql

SELECT Name FROM STUDENTS WHERE Marks>75 ORDER By SUBSTRING(Name,-3,LENGTH(Name)),ID

ref:- http://www.w3resource.com/mysql/string-functions/mysql-substring-function.php

For Oracle

SELECT Name FROM Students WHERE Marks>75 ORDER BY substr(Name,-3),ID;

hacker-rank/more_than_75_marks.sql at master · shanesatterfield , 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. Column | Type |. extact last 3 characters from a string – Learn more on the SQLServerCentral forums don't use the variable, it was only for the demo. Use your column name instead, and select it directly from

DISTINCT should be removed, otherwise multiple students with the same NAME would be ignored.

SELECT NAME
FROM STUDENTS
WHERE MARKS > 75
ORDER BY SUBSTRING(NAME, LEN(NAME)-2, 3), ID;

SQL Server RIGHT() Function, Example. Extract 3 characters from a string (starting from right): Example. Extract 5 characters from the text in the "CustomerName" column (starting from right):. 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. The Name column only contains uppercase (A-Z)

You can try below query:

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

NOTE: This one will work for MySQL

MySQL SUBSTRING() Function, Extract a substring from a string (start at position 5, extract 3 characters):. SELECT SUBSTRING("SQL Tutorial", 5, 3) AS ExtractString;. Try it Yourself » Example. Extract a substring from the text in a column (start at position 2, extract 5 characters): Google Maps · Range Sliders · Tooltips · Slideshow · Filter List · Sort List  Select the cells that you will sort by last character, and click Kutools > Text > Reverse Text Order. 2. In the opening Reverse Text dialog box, check Noting option and click OK button.

SQL Server ORDER BY clause By Practical Examples, When you use the SELECT statement to query data from a table, the order of If you specify multiple columns, the result set is sorted by the first column and then that has an ORDER BY clause, the ORDER BY clause is the very last clause to be processed. The LEN() function returns the number of characters of a string. The ORDER BY clause allows you to sort the result set of the specified SELECT statement by one or more columns and also provides the option of specifying an ascending or descending sort order for each column. The only columns you can use in the ORDER BY clause are those that are currently listed in the SELECT clause.

MySQL RIGHT() function, MySQL RIGHT() extracts a specified number of characters from the right side of a string. Web Design tutorial Zurb Foundation 3 tutorials Pure CSS HTML5 Canvas Second column shows a string containg last seven characters of the $username, $password); foreach($dbh->query('SELECT aut_name  Basic SELECT statement: Get the first three characters of first name of all employees Last update on February 26 2020 08:09:45 (UTC/GMT +8 hours) MySQL Basic Select Statement: Exercise-12 with Solution

RIGHT, The following query returns the last three characters of the string 'vertica': The following query queries date column date_ordered from table store.​store_orders_fact . 5)) MonthDays FROM store.store_orders_fact ORDER BY MonthDays;  The SQL ORDER BY Keyword. The ORDER BY keyword is used to sort the result-set in ascending or descending order. The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword. SELECT column1, column2, FROM table_name. ORDER BY column1, column2,

Comments
  • Mysql and oracle are 2 different products. Pls remove the unnecessary product tag!
  • i see this is a question on hackerrank.com You should try it and ask a question if you are facing a problem.
  • What did you try?
  • SELECT distinct NAME FROM STUDENTS WHERE MARKS > 75 ORDER BY RIGHT(NAME,3),ID; but its not working
  • "It's not working" is not very helpful. RIGHT() is a MySQL function but not an Oracle RDBMS function. Maybe that's why it's not working. So (1) please clarify your question's tags and (2) explain how the query is not working, for instance by posting the error message you get or by describing the observed behaviour
  • SELECT distinct NAME FROM STUDENTS WHERE MARKS > 75 ORDER BY RIGHT(NAME,3),ID; but its not working
  • This solution seems identical to many of the other existing answers.
  • Please try to avoid code only answers by adding some explanation of why your code is the solution to the question. Remember this answer could help someone else in the future and the more information the better.