SQL ORDER BY

SQL ORDER BY 

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.

Student table:

Regno

Name

Dob

Contact

state

1001

funda

20-june-2000

9696985457

karnataka

1003

web

12-march-1998

8256968475

karnataka

1002

 of

18-may-2003

8585969674

haryana

1004

it

24-nov-1994

9785956748

goa


Syntax:

SELECT column1, column2, 

FROM table_name

ORDER BY column1, column2,  ASC|DESC;

Example : 

SELECT * FROM students

ORDER BY regno;

The OUTPUT for the above query:

Regno

Name

Dob

Contact

state

1001

om

20-june-2000

8596874858

karnataka

1002

of

18-march-2003

8585969674

haryana

1003

web

12-may-1998

8256968475

karnataka

1004

it

24-nov-1994

9785956748

goa


ORDER BY DESC Example : 

SELECT * FROM students

ORDER BY regno DESC;

The OUTPUT for the above query:

Regno

Name

Dob

Contact

state

1004

it

20-nov-1994

9785956748


goa

1003

web

12-may-1998

8256968475

karnataka

1002

of

18-march-2003

8585969674

haryana

1001

om

20-june-2000

8596874858

karnataka