【DAY3|SQL self-study check-in】Advanced query

foreword

The learning platform I use: Niuke Brush Questions Network

Reasons for recommendation : It is very convenient to be able to program online . If you have questions, you can also judge right or wrong , and you can also see the problem-solving ideas of various masters .

SLogan: Take advantage of the limited time, roll up your sleeves and work hard!

       Learning interface: Come and register for learning!
insert image description here

Summary of knowledge points

1. Sorting clause:
ORDER BY <column name> [ASC | DESC ][,<column name> … ]

2, WHERE child clause

3. BETWEEN AND clause

4. Filter null values:
①The statement format for judging that the value is empty is: column name IS NULL
②The format of the statement for judging that the value is not empty is: column name IS NOT NULL

5. WHERE IN and NOT IN:
column name [NOT] IN (constant 1, constant 2,...constant n)

6、AND 和 OR

7. Character matching
column

The above is the learning content of yesterday, today we start to learn advanced query

1. Learning process

1. Calculation function

insert image description here

[SQL16] Find the highest value of GPA
insert image description here
Problem-solving idea:
  First, the idea of ​​solving the problem is to filter out 'Fudan University' first, and then select the highest value of gpa
Here you can use MAX( <column name> ): find the maximum value of the column ;
The code is as follows:

SELECT max(gpa) FROM user_profile
WHERE university='复旦大学'

result:
insert image description here

[SQL17] Calculate the number of boys and the average GPA
insert image description here

Problem-solving idea:
  firstly, looking at the results, it can be seen that the table header needs to be renamed, and the as syntax is used to store the number of boys. Here, counting is needed, and the count() function is used ; secondly, the average value is calculated, using the avg() function . The average value of floating-point numbers may have a lot of decimal places. Follow the example to save one decimal place, and use the round() function
to retain the number of decimal places: round(column name, number of digits)

code show as below:

SELECT 
COUNT(gender) as male_num,ROUND(avg(gpa),1) FROM user_profile
WHERE gender='male'

result:
insert image description here

2. Group query

insert image description here
[SQL18] Group calculation exercises
insert image description here

Problem-solving ideas:
  This is a comprehensive examination question, we use the above knowledge points to solve the problem.
   Grouping: GROUP BY .

SELECT gender,university,
    COUNT(device_id) AS user_num,
    ROUND(avg(active_days_within_30),1) AS avg_active_day,
    ROUND(avg(question_cnt),1) AS avg_question_cnt
FROM user_profile
GROUP BY gender,university

result:
insert image description here

[SQL19] Group filtering exercises
insert image description here

Problem-solving ideas:
grouping: GROUP BY

be careful! ! : Use the HAVING clause
The HAVING clause is used to filter the grouped results.
Its function is a bit like the WHERE clause, but it is used for groups instead of individual records.
Statistical functions can be used in the HAVING clause, but not in the WHERE clause.
HAVING is often used with the GROUP BY clause.

SELECT 
    university,
    round(avg(question_cnt),3) as avg_question_cnt,
    round(avg(answer_cnt),3) as avg_answer_cnt
FROM user_profile
GROUP BY university
HAVING avg_question_cnt<5 OR avg_answer_cnt<20

result:
insert image description here

[SQL20] Grouping and sorting training questions
insert image description here

Problem-solving ideas:
  This is a comprehensive sorting and grouping problem. We use the knowledge points involved before to solve the problem.

SELECT 
    university,
    ROUND(avg(question_cnt),4) AS avg_question_cnt
FROM user_profile
GROUP BY university
ORDER BY avg_question_cnt ASC

result:
insert image description here

Summarize

  The above is today's SQL learning check-in. The last three comprehensive questions are very well integrated with the knowledge points we have learned before. Make persistent efforts, the difficulty will increase little by little, I hope the more you learn, the better!

 I will continue to share my learning process

Friends are welcome to keep checking in with me, and supervise each other in the comment area!

      Niu Ke - See you there or be square!

Guess you like

Origin blog.csdn.net/m0_62279905/article/details/127161796