SQL Order by
SQL Order by
Topperworld.in
ORDER BY
• Whenever we want to sort the records based on the columns stored in the
tables of the SQL database, then we consider using the ORDER BY clause in
SQL.
• The ORDER BY clause in SQL will help us to sort the records based on the
specific column of a table. This means that all the values stored in the column
on which we are applying ORDER BY clause will be sorted, and the
corresponding column values will be displayed in the sequence in which we
have obtained the values in the earlier step.
©Topperworld
SQL
Let us explore more on this topic with the help of examples. We will use the
MySQL database for writing the queries in examples.
©Topperworld
SQL
Example :
Write a query to sort the records in the ascending order of the customer names
stored in the customers table.
Query:
Output:
©Topperworld
SQL
All the records present in the customers table are displayed in the ascending
order of the customer's name.
• The ORDER BY clause in SQL helps us sort the records based on a table's
specific column. This means that initially, all the values stored in the
column on which we are applying the ORDER BY clause will be sorted.
Then the corresponding column values will be displayed in the same
sequence in which the values we have obtained in the earlier step.
©Topperworld
SQL
clause. Whereas, DESC keyword will sort the records in descending order.
If no keyword is specified after the column based on which we have to
sort the records, then in that case, the sorting will be done by default in
the ascending order.
Before writing the queries for sorting the records, let us understand the syntax.
Let us explore more on this topic with the help of examples. We will use the
MySQL database for writing the queries in examples.
©Topperworld
SQL
Example :
Write a query to sort the records in the ascending order of the customer names
stored in the customers table.
Query:
©Topperworld
SQL
Output:
All the records present in the customers table are displayed in the ascending
order of the customer's name.
©Topperworld
SQL
• The ORDER BY clause in SQL helps us to sort the records based on the
specific column of a table. This means that initially, all the values stored
in the column on which we are applying the ORDER BY clause will be
sorted. Then the corresponding column values will be displayed in the
same sequence in which the values we have obtained in the earlier step.
Before writing the queries for sorting the records, let us understand the syntax.
Let us explore more on this topic with the help of examples. We will use the
MySQL database for writing the queries in examples.
©Topperworld
SQL
Example :
Write a query to sort the records in the descending order of the customer names
stored in the customers table.
©Topperworld
SQL
Query:
Output:
©Topperworld
SQL
All the records present in the customers table are displayed in the descending
order of the customer's name.
We can retrieve limited rows from the database. I can be used in pagination
where are forced to show only limited records like 10, 50, 100 etc.
If you want to use LIMIT clause with SQL, you have to use ROWNUM queries
because it is used after result are selected.
FROM
FROM
©Topperworld
SQL
Let's take an example of customer table which has many columns, the following
SQL statement selects all customers from the table named "customer", stored
by the "country" and "Customer-Name" columns:
©Topperworld