Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - What's the difference between order by and group by?
What's the difference between order by and group by?
The difference between sorting basis and grouping basis:

1, order by is understood from English as the sorting method of rows, and the default is ascending order. Order by must be followed by a sorted field name, which can be multiple field names.

2, group by is understood as grouping in English. You must have an "aggregate function" to use it, and you need at least one group flag field when using it.

3. In the statement using group by, only columns (expressions) used for classifying or aggregating functions can be selected.

Where condition is used before group by, and having is used after group by to filter the results.

Extended data:

Usage of order by: sort query, ascending asc, descending desc.

Example:

1, select *?

Step 2 Sort by age

3. Query the student table information, arrange and display it in ascending order by age (default, default, from low to high), or sort by multiple conditions, such as desc by age and grade.

4. In ascending order of age, and then in descending order of achievement.

Second, group according to usage:? Group query and having can only be used in group by clauses and groups, and having conditional clauses can be directly followed by function expressions. Query statements that use the group by clause need to use aggregate functions.

Example:

1. Select the student number from the course selection table. SUM (grade) is grouped by student number? Grouped by student number, query the total score of each student number.

2. choose the student number AVG (grade) from the course selection table?

3. Group by student number

4. Have AVG > (select AVG from the course selection table, where the course number ='00 1')

5. AVG·desc's order

6. Query the student numbers with average scores greater than 00 1, and arrange them in descending order of average scores.

References:

Baidu Encyclopedia-Structured Query Language