Structured Query Language (SQL) consists of many functions and operators that allow for flexible queries. One such keyword is ORDER BY
. It is used at the end of the query to sort the result according to the specified manner. It can be sorted in ascending or descending order using the ASC
or DESC
keyword.
SELECT columnName FROM tableName ORDER BY columnName ASC|DESC;
Let’s create a demo table and implement a query that uses ORDER BY
:
CREATE TABLE Student ( s_ID int, s_name varchar(255), s_age int, s_marks int ); INSERT INTO Student VALUES (89237,'Kris',16,75); INSERT INTO Student VALUES (87496,'Kelly',15,82); INSERT INTO Student VALUES (83927,'Bob',17,84); INSERT INTO Student VALUES (83216,'Lisa',16,69); INSERT INTO Student VALUES (82312,'Rob',18,87); SELECT * FROM Student;
To get student data in terms of descending marks, we use ORDER BY
and the DESC
keyword:
SELECT * FROM Student ORDER BY s_marks DESC;
RELATED TAGS
CONTRIBUTOR
View all Courses