Queries using order by
keyword:
The ORDER BY keyword is used to sort the result-set in ascending
or descending order. The ORDER BY statement in sql is used to sort the fetched data in
either ascending or descending according to one or more columns.
- By
default ORDER BY sorts the data in ascending order.
- The
keyword DESC to sort the data in descending order and the keyword ASC to
sort in ascending order.
ORDER BY
Syntax:
SELECT column1, column2,
... FROM table_name ORDER BY column1, column2,
... ASC|DESC;
Example:
1. Select
* from student order by age;
The above
statement gives a result set ordering the records in ascending order on field
age.
2. Select
* from student order by age desc;
The above
statement gives a result set ordering the records in descending order on field
age.
3. SELECT
* FROM Student ORDER BY Age ASC , ROLL_NO DESC;
The above
query fetch all data from the table Student and then sort the
result in ascending order first according to the column Age. and
then in descending order according to the column ROLL_NO.
Student table:
Output to the example 3 query is:
No comments:
Post a Comment