Italian Trulli

SQL Order By - Order By in SQL With Example

Test Tribe
.

SQL Order By Keyword - How To Use Order By in SQL?


SQL Order By
In SQL order by command is used to sort the fetch data in ascending or descending order, based on one or more columns.
  • By default, The ORDER BY keyword sorts the records in ascending order.
  • We use Keyword DESC to sort the data in descending order and the keyword ASC to sort the data in ascending order.
Syntax of order by
Syntax of order by keyword is given below 

SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ... ASC|DESC;

Order By Example
An example of order by keyword is given below.

SELECT * FROM Employee ORDER BY Country;


The above SQL statement selects all the columns from the "Employee" table, sorted in ascending order by the "Country" column.


Types Of Order By In SQL
There are two types of order in SQL 
  • DESC 
  • ASC
DESC 
In SQL DESC is used to sort the fetch data in descending order.

Syntax of order by (DESC)
Syntax of order by (DESC) keyword is given below. 

SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ... DESC;

Example of DESC keyword
An example of the order by (DESC) keyword is given below.

SELECT * FROM Employee ORDER BY Country DESC;

The above SQL statement selects all the columns from the "Employee" table, sorted in descending order by the "Country" column.


ASC
In SQL DESC is used to sort the fetch data in descending order.

Syntax of order by (ASC)
Syntax of order by (ASC) keyword is given below. 

SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ... ASC;

Example of DESC keyword
An example of an order by (ASC) keyword is given below.

SELECT * FROM Employee ORDER BY Country ASC;

The above SQL statement selects all the columns from the "Employee" table, sorted in ascending order by the "Country" column.


FAQ:

Q: How to use both ASC and DESC commands in the same SQL query.

SELECT * FROM Employee ORDER BY Country ASC, Employee name DESC;


Q: What happens if we do not use ASC and DESC commands.

SELECT * FROM Employee ORDER BY Country, Employee name;

The above SQL Statement has an example, which would sort the result in ascending order by Country and Employee Name. 


Hope!!! The above tutorial on SQL Order By is helpful for you...

Team,

QA acharya

Order by in SQL With Example

Post a Comment

0 Comments