- SQL - Discussion
- SQL - Useful Resources
- SQL - Useful Functions
- SQL - Quick Guide
- SQL - Questions and Answers
- SQL - Datatype Functions
- SQL - Conversion Functions
- SQL - JSON Functions
- SQL - Cursor Functions
- SQL - Logical Functions
- SQL - Statistical Functions
- SQL - Text & Image Functions
- SQL - Numeric Functions
- SQL - Aggregate Functions
- SQL - String Functions
- SQL - Date Functions
- SQL - Database Tuning
- SQL - IN vs EXISTS
- SQL - Group By vs Order By
- SQL - Common Table Expression
- SQL - Cursors
- SQL - Date & Time
- SQL - Auto Increment
- SQL - Using Sequences
- SQL - Handling Duplicates
- SQL - Sub Queries
- SQL - Transactions
- SQL - NULL Values
- SQL - Stored Procedures
- SQL - Default Constraint
- SQL - Check Constraint
- SQL - Null Functions
- SQL - Min & Max
- SQL - Hosting
- SQL - Injection
- SQL - Comments
- SQL - Wildcards
- SQL - Non-Clustered Index
- SQL - Clustered Index
- SQL - Unique Index
- SQL - Primary Key
- - 工会诉Join
- SQL - Inner Join
- SQL - Using Joins
- SQL - Aliases
- SQL - EXCEPT Operator
- SQL - INTERSECT Operator
- SQL - UNION vs UNION ALL
- SQL - UNION Operator
- SQL - BETWEEN Operator
- SQL - NOT NULL
- SQL - IS NOT NULL
- SQL - IS NULL
- SQL - NOT EQUAL
- SQL - NOT Operator
- SQL - CASE
- SQL - EXISTS Operator
- SQL - ANY, ALL Operators
- SQL - IN Operator
- SQL - LIKE Operator
- SQL - BOOLEAN (BIT) Operator
- SQL - AND & OR
- SQL - Having Clause
- SQL - Group By Clause
- SQL - Order By Clause
- SQL - Distinct Clause
- SQL - Top Clause
- SQL - Where Clause
- SQL - Rename Views
- SQL - Drop Views
- SQL - Update Views
- SQL - Create Views
- SQL - Sorting Results
- SQL - Delete Query
- SQL - Update Query
- SQL - Insert Into Select
- SQL - Select Into
- SQL - Select Query
- SQL - Insert Query
- SQL - Constraints
- SQL - Delete Table
- SQL - Drop Table
- SQL - Alter Tables
- SQL - Temporary Tables
- SQL - Clone Tables
- SQL - Truncate Table
- SQL - Rename Table
- SQL - Show Tables
- SQL - Create Table
- SQL - Backup Database
- SQL - Show Database
- SQL - Rename Database
- SQL - Select Database
- SQL - Drop Database
- SQL - Create Database
- SQL - Expressions
- SQL - Operators
- SQL - Data Types
- SQL - Syntax
- SQL - Databases
- SQL - RDBMS Concepts
- SQL - Overview
- SQL - Home
5. 图瓦卢
- 页: 1
- 页: 1
- 结构-创建指数
- 页: 1
- 页: 1
- 页: 1
- SQL - Foreign Key
- 文 件
- ∗ E/CN.6/2009/1。
- 页: 1
- 页: 1
- 文 件
- 页: 1
- 页: 1
- 文 件
- 页: 1
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
SQL - ORDER BY Clause
The SQL ORDER BY clause is used to sort the data in either ascending or descending order, based on one or more columns. This clause can sort data by a single column or by multiple columns. Sorting by multiple columns can be helpful when you need to sort data hierarchically, such as sorting by state, city, and then by the person s name.
ORDER BY is used with the SQL SELECT statement and is usually specified after the WHERE, HAVING, and GROUP BY clauses, if present in the query.
Note −
Some databases sort the query results in an ascending order by default.
To sort the data in ascending order, we use the keyword ASC.
To sort the data in descending order, we use the keyword DESC.
Syntax
The basic syntax of the ORDER BY clause is as follows −
SELECT column-pst FROM table_name [ORDER BY column1, column2, .. columnN] [ASC | DESC];
Where, column-pst are the columns we want to retrieve from the table_name, and ASC or DESC specifies whether the sorting should be in ascending or descending order, respectively.
Note − You can use more than one column in the ORDER BY clause. Make sure whatever column you are using to sort that column, should be in the column-pst.
ORDER BY with ASC
We can use the ORDER BY clause with the ASC keyword to sort the result set of a query in ascending order based on one or more columns. When using the ORDER BY clause, if you do not exppcitly specify the sort order, ASC is used by default.
Example
Assume we have created a table with name CUSTOMERS in SQL database using CREATE TABLE statement as shown below −
CREATE TABLE CUSTOMERS ( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );
Following query inserts values into this table using the INSERT statement −
insert INTO CUSTOMERS VALUES(1, Ramesh , 32, Ahmedabad , 2000.00); insert INTO CUSTOMERS VALUES(2, Khilan , 25, Delhi , 1500.00); insert INTO CUSTOMERS VALUES(3, kaushik , 23, Kota , 2000.00); insert INTO CUSTOMERS VALUES(4, Chaitap , 25, Mumbai , 6500.00); insert INTO CUSTOMERS VALUES(5, Hardik , 27, Bhopal , 8500.00); insert INTO CUSTOMERS VALUES(6, Komal , 22, MP , 4500.00); insert INTO CUSTOMERS VALUES(7, Muffy , 24, Indore , 10000.00);
If we verify the contents of the CUSTOMERS table using the SELECT statement, we can observe the inserted records as shown below −
SELECT * from CUSTOMERS; +----+----------+-----+-----------+----------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+-----------+----------+ | 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 2 | Khilan | 25 | Delhi | 1500.00 | | 3 | kaushik | 23 | Kota | 2000.00 | | 4 | Chaitap | 25 | Mumbai | 6500.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 | | 6 | Komal | 22 | MP | 4500.00 | | 7 | Muffy | 24 | Indore | 10000.00 | +----+----------+-----+-----------+----------+
In the following query we are trying to sort the result in an ascending order by the name and the salary of the customers −
SELECT * FROM CUSTOMERS ORDER BY NAME ASC
Output
This would produce the following result −
+----+----------+-----+-----------+----------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+-----------+----------+ | 4 | Chaitap | 25 | Mumbai | 6500.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 | | 3 | kaushik | 23 | Kota | 2000.00 | | 2 | Khilan | 25 | Delhi | 1500.00 | | 6 | Komal | 22 | MP | 4500.00 | | 7 | Muffy | 24 | Indore | 10000.00 | | 1 | Ramesh | 32 | Ahmedabad | 2000.00 | +----+----------+-----+-----------+----------+
ORDER BY with DESC
We can also use the ORDER BY clause with the DESC keyword to sort the result set of a query in descending order based on one or more columns.
Example
The following query sorts the result in the descending order by the name of the customers −
SELECT * FROM CUSTOMERS ORDER BY NAME DESC;
Output
This would produce the result as follows −
+----+----------+-----+-----------+----------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+-----------+----------+ | 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 7 | Muffy | 24 | Indore | 10000.00 | | 6 | Komal | 22 | MP | 4500.00 | | 2 | Khilan | 25 | Delhi | 1500.00 | | 3 | kaushik | 23 | Kota | 2000.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 | | 4 | Chaitap | 25 | Mumbai | 6500.00 | +----+----------+-----+-----------+----------+
ORDER BY Multiple Columns
We can use the ORDER BY clause to sort the result set of a query by multiple (more than one) columns. When sorting by multiple columns, the sorting is done in the order that is specified in the ORDER BY clause. In other words, the table will be sorted first by sorting the first column specified, then the second column, and so on.
Example
In the following query, we are trying to retrieve all records from the CUSTOMERS table and sorts them first by their address in ascending order, and then by their salary in descending order −
SELECT * FROM CUSTOMERS ORDER BY AGE ASC, SALARY DESC
Output
Following is the result produced −
+----+----------+-----+-----------+----------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+-----------+----------+ | 6 | Komal | 22 | MP | 4500.00 | | 3 | kaushik | 23 | Kota | 2000.00 | | 7 | Muffy | 24 | Indore | 10000.00 | | 4 | Chaitap | 25 | Mumbai | 6500.00 | | 2 | Khilan | 25 | Delhi | 1500.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 | | 1 | Ramesh | 32 | Ahmedabad | 2000.00 | +----+----------+-----+-----------+----------+
ORDER BY with WHERE clause
In SQL, we can use the WHERE clause with the ORDER BY clause to sort only the rows that meet certain specified conditions. This can be useful when we want to sort a subset of the data in a table based on the specified criteria.
Example
Now, we are trying to retrieve all records from the CUSTOMERS table where the age of the customer is 25 and sort them in descending order based on their name −
SELECT * FROM CUSTOMERS WHERE AGE = 25 ORDER BY NAME DESC;
Output
Following is the output of the above query −
+----+----------+-----+-----------+----------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+-----------+----------+ | 2 | Khilan | 25 | Delhi | 1500.00 | | 4 | Chaitap | 25 | Mumbai | 6500.00 | +----+----------+-----+-----------+----------+
ORDER BY with TOP clause
We can use the TOP clause with ORDER BY clause to pmit the specified number of rows by sorting it in either ascending or descending order.
Syntax
Following is the syntax of using the TOP clause with the ORDER BY clause in SQL −
SELECT TOP N column1, column2, ... FROM table_name ORDER BY column_name1 [ASC | DESC], column_name2 [ASC | DESC], ...
Example
In here, we are trying to retrieve the top 4 records from the CUSTOMERS table on the basis of their salary, and sort them in ascending order based on their name −
SELECT TOP 4 SALARY FROM CUSTOMERS ORDER BY NAME;
Output
Following is the output of the above query −
+----------+ | SALARY | +----------+ | 6500.00 | | 8500.00 | | 2000.00 | | 1500.00 | +----------+Advertisements