Quantcast
Channel: Planet MySQL
Viewing all articles
Browse latest Browse all 18787

SQL ORDER BY CLAUSE Example | SQL Order By Tutorial

$
0
0
SQL ORDER BY CLAUSE Example | SQL Order By Tutorial

SQL ORDER BY CLAUSE Example | SQL Order By Tutorial is today’s topic. An ORDER BY keyword is used to sort the result in ascending or descending order. An ORDER BY keyword sorts the records in ascending order by default. We have already covered Group By Clause in SQL.

What is the use of SQL ORDER BY Clause

SQL order by clause is used to sort the data in ascending or descending order according to one or more columns. It is used as a conjunction with the Select query. Sorting merely are re-arranging our query results in a specified way. Sorting can be done on the number, strings as well as date data types.

  • By default, this clause sorts the data in ascending order.
  • To sort data in descending order use, DESC keyword and ASC keyword is used for sorting data manually in ascending order.

The SQL ORDER BY syntax

For sorting according to Single Columns.

Select column1,column2,.. 
from Table_name 
ORDER BY column1 DESC|ASC;

For sorting according to Multiple Columns.

Select column1,column2,.. 
from Table_name 
ORDER BY column1,column2,… DESC|ASC;

PARAMETERS

  1. Columns are the name which you want to display and sort accordingly.
  2. Table_name is the name of the table.

SQL ORDER BY CLAUSE Example

Consider a SQL Table.

CUSTOMER:

ID NAME AGE CITY
1 Rohit 20 Patna
2 Shivam 18 Jalandhar
3 Pranav 19 Dharamshala

 

STATEMENTS: (For Single Columns)

If you want to sort a customer table according to the name of the employee.

select * from customer order by name;

OUTPUT

SQL ORDER BY CLAUSE Example

 

EXPLANATION

As you can see names are sorted in ascending order as no ASC | DESC is mentioned. By default, ASC is executed.

select * from customer order by name desc;

OUTPUT

What is the use of SQL ORDER BY Clause

 

EXPLANATION

It is clear from the sentence that names are arranged in descending order.

STATEMENTS: (FOR Multiple Columns)

select * from customer order by age ASC, name DESC;

OUTPUT

SQL Order By Tutorial

 

Explanation

It is clear from the statements that, according to age and name constraints data is displayed. Age is displayed in ascending order, and the name is displayed in descending order.  If ASC was not mentioned after age, then also the same result will be generated.

If Age was to be displayed in descending order and name in ascending order, then query for this is the following.

Select * from customer order by age DESC, name ASC;

The columns specified in the order by clause should be one of the columns selected in the SELECT column list. We can also sort the columns values by determining the position of the column instead of writing the names.

See the query given below.

Select * from customer order by 2;

Output

 

SQL ORDER BY CLAUSE Example Tutorial

Using Expressions in the ORDER BY Clause

Suppose in the above customer table if we want to display the records of the customer above the age of 18 and display the final records in ascending order, then the following queries are used.

Select * from customer where age > 18 order by age;

Output

 

Using Expressions in the ORDER BY Clause

Explanation

As you can see above that two customer names where having age greater than 18 and is displayed in ascending order.

We can also use the alias in order by clause.

See the below query.

Select * from customer AS new_customer where age > 18 order by age;

So, the SQL ORDER BY statement is used to sort the fetched data in either an ascending or descending according to one or more columns.

The Order By clause is used with SELECT statement for arranging retrieved data in sorted order. An Order by clause by default sorts the retrieved data in the ascending order. If you want to sort the data in descending order DESC keyword is used with Order by clause.

Finally, SQL ORDER BY CLAUSE Example | SQL Order By Tutorial article is over.

The post SQL ORDER BY CLAUSE Example | SQL Order By Tutorial appeared first on AppDividend.


Viewing all articles
Browse latest Browse all 18787

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>