Interview frequently asked SQL window function

picture

interview questions

There is a "student grade table", which contains 4 fields: class id, student id, course id, grades.

insert image description here

Question 1: Find the three records with the highest grades for each student

Problem 2: Find the students whose grades are higher than the class average in each course

technology upgrade

Technology must learn to share and communicate, and it is not recommended to work behind closed doors. A person can go fast, a group of people can go farther.

Here I am sorting out a collection of data analysis interview questions. In addition, the source code, information, data, and technical exchange improvements in the article can be obtained by adding the knowledge planet exchange group. The group members have more than 2,000 people. When adding, remember to add notes: Source + interest direction, easy to find like-minded friends.

Method ①, add WeChat account: pythoner666, remarks: from CSDN + interview questions
Method ②, WeChat search official account: Python learning and data mining, background reply: add group

【Problem solving steps】

1. topN problem

Question 1 is a common ranking problem (topN problem), and it is necessary to think of using SQL window functions to solve such business problems.

There are three kinds of order obtained by window function: rank(), dense_rank() and row_number().

It is also sorted by "value" from small to large. The differences between the three are as follows:

insert image description here

According to the description of the problem, we should use the dense_rank window function

select *
from (
select *,
       dense_rank() over (partition by 班级id,学生id 
       order by 成绩 desc) as 顺序
from 学生成绩表
) t1
where 顺序 <= 3;

search result:

insert image description here

2. Summary analysis

Question 2 asks to find out the students whose scores of each course are higher than the class average, which can be broken down into the following questions:

1) Find the average score of each class and each course

2) Subtract the student's grade of each course from the average score of the corresponding course in the class, if the result is greater than 0, it means that the student's grade in this course is higher than the average score of the course

3) "Find out the students whose course scores are higher than the class average" means that for students, the smallest "subtraction result" is greater than 0

First, use summary analysis to find the average score of each class and each course.

select 班级id,课程id,avg(成绩) as 课程平均分
from 学生成绩表
group by 班级id,课程id;

search result:

insert image description here

3. Multi-table join

When multi-table queries are involved, multi-table joins are required.

The purpose here is to "subtract the student's grade for each course from the average score of the corresponding course in the class".

Therefore, it is to join the original "student grade table" with the "average grade of the class".

In order to keep all the data in the left table "student grade table", the grades of all students are subtracted from the "course average" x, so choose "left join (left join)".

insert image description here

select t1.班级id,t1.学生id,t1.课程id,t1.成绩,
       t1.成绩 - t2.课程平均分 as 相减结果
from 学生成绩表 t1
left join (
select 班级id,课程id,avg(成绩) as 课程平均分
from 学生成绩表
group by 班级id,课程id
) t2 on t1.班级id = t2.班级id and t1.课程id = t2.课程id;

insert image description here

Finally, use the group summary and combine the having condition to filter out the students whose "minimum value of the subtraction result is greater than 0".

select 班级id,学生id
from (
select t1.班级id,t1.学生id,t1.课程id,t1.成绩,
       t1.成绩 - t2.课程平均分 as 相减结果
from 学生成绩表 as t1
left join (
select 班级id,课程id,avg(成绩) as 课程平均分
from 学生成绩表
group by 班级id,课程id
) as t2 on t1.班级id = t2.班级id and t1.课程id = t2.课程id
) as tmp
group by 班级id,学生id
having min(相减结果) > 0;

insert image description here

Test points for this question

1. Examine the understanding of grouping and summarization, and use it flexibly to solve business problems;

2. Examine the understanding of multi-table joins, and use them flexibly to solve business problems;

3) Examine the understanding of window functions. There are only a few classic problems solved by window functions. If you write them down, you can solve 99% of business problems.

Guess you like

Origin blog.csdn.net/m0_59596937/article/details/130298507