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 columnNameFROM tableNameORDER 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 StudentVALUES (89237,'Kris',16,75);INSERT INTO StudentVALUES (87496,'Kelly',15,82);INSERT INTO StudentVALUES (83927,'Bob',17,84);INSERT INTO StudentVALUES (83216,'Lisa',16,69);INSERT INTO StudentVALUES (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 StudentORDER BY s_marks DESC;