Your cart is currently empty!
Category: sql
-
Khóa học miễn phí SQL – Order By Clause nhận dự án làm có lương
SQL – ORDER BY Clause
Table of content
The 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.
Following are the important points about ORDER BY Clause −
- 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.
In addition to sorting records in ascending order or descending order, the ORDER BY clause can also sort the data in a database table in a preferred order.
This preferred order may not sort the records of a table in any standard order (like alphabetical or lexicographical), but they could be sorted based on external condition(s).
For instance, in the CUSTOMERS table containing the details of the customers of an organization, the records can be sorted based on the population of the cities they are from. This need not be alphabetically sorted, instead, we need to define the order manually using the statement.
Syntax
The basic syntax of the ORDER BY clause is as follows −
SELECT column-list FROM table_name [ORDER BY column1, column2, .. columnN] [ASC | DESC];
Where, column-list is list of the columns we want to retrieve; and ASC or DESC specifies the sort order.
Note: We can use more than one column in the ORDER BY clause, but we need to make sure that the column we are using to sort is specified in the column-list.
ORDER BY Clause with ASC
We can sort the result-set of a query in ascending order (based on one or more columns) using the SQL ORDER BY clause by specifying ASC as the sort order. ASC is the default sort order for this clause, i.e. while using the ORDER BY clause if you do not explicitly specify the sort order, the data will be sorted in ascending order.
Example
Assume we have created a table with name CUSTOMERS in the MySQL 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), (2, ''Khilan'', 25, ''Delhi'', 1500.00), (3, ''Kaushik'', 23, ''Kota'', 2000.00), (4, ''Chaitali'', 25, ''Mumbai'', 6500.00), (5, ''Hardik'', 27, ''Bhopal'', 8500.00), (6, ''Komal'', 22, ''Hyderabad'', 4500.00), (7, ''Muffy'', 24, ''Indore'', 10000.00);
The table obtained is as shown below −
ID NAME AGE ADDRESS SALARY 1 Ramesh 32 Ahmedabad 2000.00 2 Khilan 25 Delhi 1500.00 3 Kaushik 23 Kota 2000.00 4 Chaitali 25 Mumbai 6500.00 5 Hardik 27 Bhopal 8500.00 6 Komal 22 Hyderabad 4500.00 7 Muffy 24 Indore 10000.00 In the following query, we are sorting the records of the CUSTOMERS table in ascending order based on the column NAME −
SELECT * FROM CUSTOMERS ORDER BY NAME ASC;
Output
This would produce the following result −
ID NAME AGE ADDRESS SALARY 4 Chaitali 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 Hyderabad 4500.00 7 Muffy 24 Indore 10000.00 1 Ramesh 32 Ahmedabad 2000.00 ORDER BY Clause with DESC
To sort the result-set of a query in descending order (based on one or more columns), we need to use the ORDER BY clause by specifying DESC as the sort order.
Example
The following query sorts the records of the CUSTOMER table based on the descending order of 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 Hyderabad 4500.00 2 Khilan 25 Delhi 1500.00 3 Kaushik 23 Kota 2000.00 5 Hardik 27 Bhopal 8500.00 4 Chaitali 25 Mumbai 6500.00 ORDER BY Clause on 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 based on the first column (specified in the query), then the second column, and so on.
Example
In the following query, we are retrieving all records from the CUSTOMERS table and sorting 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 Hyderabad 4500.00 3 Kaushik 23 Kota 2000.00 7 Muffy 24 Indore 10000.00 4 Chaitali 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
We can also use the WHERE clause with the ORDER BY clause to sort the rows that meet certain conditions. This can be useful when we want to sort a subset of the data in a table based on the specific criteria.
Example
Now, we are retrieving all records from the CUSTOMERS table where the age of the customer is 25, and sorting them as per the descending order of their names −
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 Chaitali 25 Mumbai 6500.00 ORDER BY with LIMIT Clause
We can use the LIMIT clause with ORDER BY clause to limit the specified number of rows by sorting them either in ascending or in descending order.
Syntax
Following is the syntax of using the LIMIT clause with the ORDER BY clause in MySQL database −
SELECT column1, column2, ... FROM table_name ORDER BY column_name1 [ASC | DESC], column_name2 [ASC | DESC], ... LIMIT N;
Example
In here, we are retrieving the top 4 records from the CUSTOMERS table based on their salary, and sorting them in ascending order based on their name −
SELECT SALARY FROM CUSTOMERS ORDER BY NAME LIMIT 4;
Output
Following is the output of the above query −
SALARY 6500.00 8500.00 2000.00 1500.00 Sorting Results in a Preferred Order
One can also sort the records of a table in their own preferred order using the CASE statement within the ORDER BY clause. All the values are specified in the clause along with the position they are supposed to be sorted in; if the values are not given any number, they are automatically sorted in ascending order.
Example
To fetch the rows with their own preferred order, the SELECT query used would be as follows −
SELECT * FROM CUSTOMERS ORDER BY ( CASE ADDRESS WHEN ''MUMBAI'' THEN 1 WHEN ''DELHI'' THEN 2 WHEN ''HYDERABAD'' THEN 3 WHEN ''AHMEDABAD'' THEN 4 WHEN ''INDORE'' THEN 5 WHEN ''BHOPAL'' THEN 6 WHEN ''KOTA'' THEN 7 ELSE 100 END );
Output
The above query sorts the CUSTOMERS table based on the custom order defined using the CASE statement. Here, we are sorting the records based on the population of the cities specified in the ADDRESS column.
ID NAME AGE ADDRESS SALARY 4 Chaitali 25 Mumbai 6500.00 2 Khilan 25 Delhi 1500.00 6 Komal 22 Hyderabad 4500.00 1 Ramesh 32 Ahmedabad 2000.00 7 Muffy 24 Indore 10000.00 5 Hardik 27 Bhopal 8500.00 3 Kaushik 23 Kota 2000.00
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc
Khóa học miễn phí SQL – Delete Table nhận dự án làm có lương
SQL – Delete Table
The SQL DELETE is a command of Data Manipulation Language (DML), so it does not delete or modify the table structure but it delete only the data contained within the table. Therefore, any constraints, indexes, or triggers defined in the table will still exist after you delete data from it.
SQL DELETE TABLE Statement
The SQL DELETE TABLE statement is used to delete the existing records from a table in a database. If you wish to delete only the specific number of rows from the table, you can use the WHERE clause with the DELETE statement. If you omit the WHERE clause, all rows in the table will be deleted. The SQL DELETE statement operates on a single table at a time.
Syntax
Following is the basic syntax for using the SQL DELETE command in SQL −
DELETE FROM table_name;
SQL DELETE TABLE with WHERE Clause
We can use the SQL DELETE statement to delete specific rows from a table based on a single condition using the WHERE clause.
Syntax
Following is the syntax for deleting specific rows based on single condition −
DELETE FROM table_name WHERE condition;
Example
Assume we have creating a table named CUSTOMERS, which contains the personal details of customers including their name, age, address and salary etc.
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) );
Now insert values into this table using the INSERT statement as follows −
INSERT INTO CUSTOMERS VALUES (1, ''Ramesh'', 32, ''Ahmedabad'', 2000.00 ), (2, ''Khilan'', 25, ''Delhi'', 1500.00 ), (3, ''Kaushik'', 23, ''Kota'', 2000.00 ), (4, ''Chaitali'', 25, ''Mumbai'', 6500.00 ), (5, ''Hardik'', 27, ''Bhopal'', 8500.00 ), (6, ''Komal'', 22, ''Hyderabad'', 4500.00 ), (7, ''Muffy'', 24, ''Indore'', 10000.00 );
If you retrieve the contents of the above created table using the SELECT * FROM CUSTOMERS statement you will get the following output −
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Ramesh | 32 | Ahmedabad | 2000.00 |
2 | Khilan | 25 | Delhi | 1500.00 |
3 | Kaushik | 23 | Kota | 2000.00 |
4 | Chaitali | 25 | Mumbai | 6500.00 |
5 | Hardik | 27 | Bhopal | 8500.00 |
6 | Komal | 22 | Hyderabad | 4500.00 |
7 | Muffy | 24 | Indore | 10000.00 |
Now, let”s try to delete all the customers with the name ”Hardik” as shown in the query below −
DELETE FROM CUSTOMERS WHERE NAME=''Hardik
Output
We get the following result. We can observe that 1 row has been deleted.
Query OK, 1 row affected (0.05 sec)
Verification
Now if you retrieve the contents of the CUSTOMERS table using the SELECT * FROM CUSTOMERS command you will get the following output −
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Ramesh | 32 | Ahmedabad | 2000.00 |
2 | Khilan | 25 | Delhi | 1500.00 |
3 | Kaushik | 23 | Kota | 2000.00 |
4 | Chaitali | 25 | Mumbai | 6500.00 |
6 | Komal | 22 | Hyderabad | 4500.00 |
7 | Muffy | 24 | Indore | 10000.00 |
Deleting rows based on multiple conditions
We can also use the SQL DELETE TABLE statement to delete specific rows from a table based on multiple conditions using the WHERE clause. This is useful when we want to remove a subset of rows from a table that meet a certain criterion.
When using multiple conditions, we can use the comparison operators such as AND, OR, and NOT to refine our conditions. This way, only rows that satisfy the conditions will be deleted.
Syntax
Following is the basic syntax for deleting specific rows based on multiple conditions which can be connected using either AND or OR operators −
DELETE FROM table_name WHERE condition1 AND condition2 OR ... conditionN;
Here, table_name is the name of the table from which we want to delete rows, and condition1 through conditionN are the conditions that must be met for the rows to be deleted. The AND or OR operators can be used to join the conditions together.
Example
In the following query we are trying to delete all the customers whose name is either ”Komal” or their address is ”Mumbai” −
DELETE FROM CUSTOMERS WHERE NAME=''Komal'' OR ADDRESS=''Mumbai
Output
We get the following result. We can observe that 2 rows has been deleted.
Query OK, 2 rows affected (0.03 sec)
Verification
Now if you retrieve the contents of the CUSTOMERS table using the SELECT * FROM CUSTOMERS command you will get the following output −
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Ramesh | 32 | Ahmedabad | 2000.00 |
2 | Khilan | 25 | Delhi | 1500.00 |
3 | Kaushik | 23 | Kota | 2000.00 |
6 | Komal | 22 | Hyderabad | 4500.00 |
7 | Muffy | 24 | Indore | 10000.00 |
Deleting all the records in a table
We can use the SQL DELETE TABLE statement without a WHERE clause to delete all records in a table in SQL. This statement will remove all the rows from the specified table, effectively resetting the table to its original state (containing only the structure and its constraints).
However, it”s important to note that this operation cannot be undone, and all the data in the table will be permanently deleted.
Example
In here, we are trying to delete all the records from the CUSTOMERS table −
DELETE FROM CUSTOMERS;
Output
Following is the result produced by executing the above query −
Query OK, 4 rows affected (0.13 sec)
Verification
Now, if you retrieve the contents of the CUSTOMERS table using the SELECT * FROM CUSTOMERS statement you will get the following result −
Empty set (0.00 sec)
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc