Learning SQL compared to Excel (5): grouping and subqueries

Today's article is the fifth article in the series of learning SQL compared with Excel. The portals of the previous three articles are here: " Calculated Fields ", " Operations on Rows and Columns ", " Addition , Deletion, and Modification of Libraries/Tables/Records " , " SQL Learning Articles for Xiaobai ".

The data tables to be used in this section: grade table (sc), student basic information table (student1)

1. Grouping

01 Create groups

The group by command is used to create grouping. Grouping is actually quite easy to understand. It is a pivot table in Excel. The summary method is the function of counting, summing or averaging, so it is usually used in combination with aggregation functions.

--语法
select 列,聚合函数 from 表 where子句 group by 列;

Group by should be placed after where, before order by

 

Q1: Count the number of people under each student number (snum)

Use [group by + count] to group and count the snum student number column, which is equivalent to dragging snum to the column label in the Excel pivot table and counting snum at the same time, the result is the same.

select snum, count(*) from sc group by snum;

 

In the same way, you can try to count the results of cnum and score groups.

 

Q2: Count the total grades under each student number (snum)

[group by + sum], combined with the sum function, can count the total score of each student number, and the score is summed in Excel.

select snum,sum(score) from sc group by snum;

 

Q3: Calculate the average grade under each student number and grade number

This is done in the Excel pivot table, drag the snum and cnum to the row label, and the score count item is averaged.

[group by for more than one column], to group more than one column, each column after group by should be separated by commas. As shown in the figure below, it means that the snum is grouped first, then the cnum is grouped, and the values ​​obtained after grouping are averaged. The result is the same as the Excel pivot table.

select snum,cnum,avg(score) from sc group by snum,cnum;

 

02 Filter after grouping

After using group by to create groups, if you want to keep which groups and which groups to exclude, you need to use the having command.

--语法
select 列,聚合函数 from 表 where子句 group by 列 having 聚合函数 操作符 值;

A website user login browsing record table is as follows, a line record indicates the time period in which the user logged in once, the singin field indicates the number of times the user clicks, and counts the number of times each user logs in to the website (review the group by just now)

Q4: Who are the users who log in to the website more than or equal to 2 times?

select name,count(*) from employee_tb1 group by name having count(*) >= 2;

It is equivalent to doing another screening after perspective

 

The difference with where

Both where and having are filtering commands, but because where cannot filter aggregate functions, having is used. The difference between them is:

  • Where filters rows, and having filters groups;

  • Where is used before grouping, and having is used after grouping

[Use with where at the same time]

Q5: Count the users who have logged in to the website more than or equal to 2 times among the users who have clicked more than or equal to 2 times.

select name,count(*) from employee_tb1 where singin>=2 group by name having count(*) >=2;

 

grouping and sorting

The order after grouping is not necessarily in ascending or descending order. The following is the difference between using order by and not using it, which can be clearly seen. Grouping is generally combined with sorting, and order by is written after group by.

 

Two, sub query

A subquery is to nest a query inside a query. The subquery is also easy to understand, similar to the concept of IF function nesting in Excel.

The use of subqueries in where is to connect another query with operators (>, <, =, in) after the where statement.

--语法
select 列 from 表 where 操作符 (select 列 from 表)

Q6: Find out the information of the students whose grades are greater than the grades of the student whose student number is 7

select snum.cnum,score from sc where score > (select score from sc where snum = 7);

This is equivalent to doing two operations:

First find out the grade of the student whose student number is 7, which is 89

 

Then query the student information with a score greater than 89:

 

The result is consistent with the result of the subquery. Note that when using a subquery in where, the result of the subquery should be one or several values, not a data table.

 

Subqueries can be used in the same table, such as the above sc table query example, can also be used in different tables, as in the following example

Q7: Query the basic information of students whose grades are greater than 90

Here is the grade table (sc) and the student basic information table (student1). The two tables have the same field student number (snum). The student number of the student whose grade is greater than 90 is extracted from the grade table and nested into the basic information table .

select * from student1 where snum in (select snum from sc where score> 90);

 

This is the end of the sharing of related subqueries. For specific exercises, you can go to the subquery module of SQLZOO for practice.


Provide learning route planning for entry-level data analysis, and share dry goods from Excel to statistics. Data analysis is a skill, and everyone is expected to be able to analyze data.

related information:

SQL Learning: Getting Started with MySQL  |  Addition , deletion, and modification of libraries/tables/records

Excel analysis methods: time series analysis  |  regression analysis  |  descriptive statistics analysis  |  correlation analysis 

Excel Charts: Data Maps  |  Pivot Tables  |  5 Basic Charts  |  13 Advanced Charts  |  Histograms  |  Control Charts  |  Arrangement Charts

Excel function: date text function  |  find reference function  |  if function  |  statistical function

Data Analysis with Excel: Data Acquisition  |  Data Processing 

Methodology: How to systematically learn Excel  |  Learning Data Analysis  |  Skills of Excel 

Code words are not easy, click on the lower right corner  to see  , add chicken legs to the little sister~

Guess you like

Origin blog.csdn.net/data_cola/article/details/116026169