Sql 简明教程
SQL - Group By vs Order By
在 SQL 中,我们有两个常用的子句来帮助我们操作数据:Group By 子句和 Order By 子句。
In SQL, we have two commonly used clauses that help us to manipulate data; Group By clause and Order By clause.
Group By 子句用于将相同的数据/记录安排到组中,Order By 子句用于按升序或降序对数据进行排序。
A Group By clause is used to arrange the identical data/records into groups and the Order By clause is used to sort the data in ascending or descending order.
The SQL Group By Clause
使用 GROUP BY 子句,我们可以按组(根据列)组织表中的数据,并对它们执行所需的计算。
Using the GROUP BY clause we can organize the data in a table into groups (based on a column) and perform required calculations on them.
此子句通常与聚合函数如 MIN()、MAX()、SUM()、AVG() 和 COUNT() 等一起使用。
This clause is often used with the aggregate functions such as MIN(), MAX(), SUM(), AVG(), and COUNT() etc.
它通常与 SELECT 语句一起使用,并且放在 WHERE 子句后面或 HAVING 子句之前。如果我们使用 Order By 子句,则 Group By 子句应位于 Order By 子句之前。
It is often used with the SELECT statement, and it is placed after the WHERE clause or before the HAVING clause. If we use the Order By clause, the Group By clause should precede the Order By clause.
Syntax
以下是 SQL Group By 子句的语法:
Following is the syntax of the SQL Group By clause −
SELECT column_name, aggregate_function() FROM table_name
WHERE condition GROUP BY column_name;
aggregate_function() 和 WHERE 子句在上面的语法中是可选的。
The aggregate_function() and the WHERE clause are optional in the above syntax.
Example
假设我们使用以下 CREATE 语句创建了一个名为 CUSTOMERS 的表,其中包含客户记录,如 NAME、AGE、ADDRESS 和 SALARY 等:
Assume we have created a table named CUSTOMERS that contains records of customers such as NAME, AGE, ADDRESS, and SALARY etc.., using the following CREATE statement −
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)
);
现在,我们使用 INSERT 语句将值插入到此表中,如下所示:
Now, we are inserting 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 );
CUSTOMERS 表将创建为 −
The CUSTOMERS table will be created as −
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 |
在下面的 SQL 查询中,我们使用 Group by 子句根据 CUSTOMERS 表中的薪水对行进行分组,并统计每一组中的记录数:
In the SQL query below, we are using the Group by clause to group the rows based on their salaries from the CUSTOMERS table and counting the number of records in each group −
SELECT SALARY, COUNT(SALARY) from CUSTOMERS GROUP BY SALARY;
The SQL Order By Clause
ORDER BY 子句用于对查询结果进行排序。此子句用在 SELECT 语句的最后,跟随 WHERE、HAVING 和 GROUP BY 子句。我们可以通过指定排序顺序分别为 ASC 和 DESC 来按升序或降序对表列进行排序。如果我们不指定任何顺序,则默认为升序。
The ORDER BY clause is used to sort the query results. This clause is used at the end of a SELECT statement, following the WHERE, HAVING and GROUP BY clauses. We can sort the table column in ascending or descending order with the by specifying the sort order as ASC and DESC respectively. If we do not specify any order, it defaults to ascending order.
Syntax
以下是使用 SQL ORDER BY 子句按升序/降序对列值进行排序的语法:
Following is the syntax to sort the column value in ascending/descending order using the SQL ORDER BY clause −
SELECT column_name FROM table_name ORDER BY ASC/DSC;
Example
在下面的查询中,我们从 CUSTOMERS 表中检索 ID 和 NAME,并使用 ORDER BY 子句按升序对名称进行排序。
In the following query, we are retrieving the ID and NAME from the CUSTOMERS table and using the ORDER BY clause, we are sorting the names in ascending order.
SELECT ID, NAME FROM CUSTOMERS ORDER BY NAME;
Output
当我们运行以上查询时,我们可以看到结果表按名称按升序排序。
When we run the above query, we can see that the resultant table is sorted by name in ascending order.
ID |
NAME |
4 |
Chaitali |
5 |
Hardik |
3 |
Kaushik |
2 |
Khilan |
6 |
Komal |
7 |
Muffy |
1 |
Ramesh |
Example
在以下示例中,我们正在检索姓名,计算平均工资,并使用 GROUP BY 子句按姓名对表进行分组。
In the following example, we are retrieving the NAME, calculating the AVG SALARY, and using the GROUP BY clause to group the table by NAME.
SELECT NAME, AVG(SALARY) FROM CUSTOMERS GROUP BY NAME;
Output
当我们运行以上查询时,我们会得到姓名和平均工资。平均工资与实际工资相同,因为没有两条或两条以上具有相同姓名的记录。因此,平均工资与实际工资相同,并且表按名称分组。如下图所示。
When we run the above query, we get the name and average salary. The average salary is the same as the actual salary because there are no two or more than two records with the same name. As a result, the average salary is the same as the actual salary, and the table is grouped by name. as shown in the table below.
NAME |
AVG(SALARY) |
Ramesh |
2000.000000 |
Khilan |
1500.000000 |
Kaushik |
2000.000000 |
Chaitali |
6500.000000 |
Hardik |
8500.000000 |
Komal |
4500.000000 |
Muffy |
10000.000000 |
Group by vs Order by
下表总结了 Group By 子句和 Order By 子句之间的差异:
Following table summarizes the differences between the Group By clause and Order by clause −
S.No. |
Group By |
Order By |
1 |
It is applied to group rows with same values. |
It sorts the columns in either ascending or descending order. |
2 |
It could be allowed in the create view statement. |
It is not allowed to create view statement. |
3 |
The attribute cannot be assigned to the aggregate function in the Group By statement. |
The attribute can be assigned to the aggregate function in the Order By statement. |
4 |
It is always used before the Order by clause in the select statement. |
It is always used after the Group by clause in the select statement. |
5 |
Here grouping is done based on the similarity among the rows attribute value. |
Here, the result-set is sorted based on the columns attribute value either ascending or descending order. |
6 |
It controls the presentation of the row |
It controls the presentation of the column. |
7 |
We can use the aggregate function in the Group by. |
Here its not mandatory to use the aggregate function in the Order by. |