1. Questions: Now the operation wants to check the details of all user question answers from Zhejiang University, please take out the corresponding data
question_practice_detail Answer details table
user_profile user table
Desired result:
Linked list query: Use question_practice_detail as the basic table, perform left join on user_profile, and set the university field of the user table to Zhejiang University.
select
qpd.device_id,
qpd.question_id,
qpd.result
from
question_practice_detail as qpd
left join user_profile as up on qpd.device_id = up.device_id
where
up.university = '浙江大学'
subquery
select
qpd.device_id,
qpd.question_id,
qpd.result
from
question_practice_detail as qpd
where
device_id in(
select
device_id
from
user_profile
where university='浙江大学'
)
First find the device_id of Zhejiang University in the user_profile, and then check the result of the device_id found in the where condition query in the question_practice_detail table.
2. Count the average number of questions answered by users who have answered questions in each school
user_profile user table
question_practice_detail Answer details table
Desired result:
Analysis: The average answering result of each school is the total number of answering questions of each school, divided by the number of answering users of each school. Since the number of answering users can be identified by a special device_id, because the total number is to be counted, it is necessary to use count function.
Since each school is to be counted, it is necessary to use group by to group, and the result needs to retain 4 decimal places, so the round function needs to be used.
Join the answer table with the user table:
select * from question_practice_detail left join user_profile on question_practice_detail.device_id = user_profile.device_id;
select university, count(question_id) from question_practice_detail left join user_profile on question_practice_detail.device_id = user_profile.device_id group by university;
Count the total number of answers for each school:
Final Results
select
university,
count(question_id) / count(distinct question_practice_detail.device_id) as avg_answer_cnt
from
question_practice_detail
left join user_profile on question_practice_detail.device_id = user_profile.device_id
group by
university;
3. Statistics of the average number of questions completed by users of each difficulty level in each school
user_profile user table
question_practice_detail Answer details table
question_detail Question difficulty level table
Analysis: Since there are three tables, the user table, the user table, the user answer table, and the topic difficulty level table, since the topic requires the level of users of each difficulty and school, it is necessary to query the three tables together, and use group by school difficulty level grouping, because it is an answer The average number of passed questions, so you need to use count statistics
First join the three tables
select * from question_practice_detail left join user_profile on question_practice_detail.device_id = user_profile.device_id left join question_detail on question_practice_detail.question_id = question_detail.question_id;
result:
select
university,
difficult_level,
round(
count(qpd.question_id) / count(distinct qpd.device_id),
4
) as avg_answer_cnt
from
question_practice_detail as qpd
left join user_profile as up on qpd.device_id = up.device_id
left join question_detail as qd on qpd.question_id = qd.question_id
group by
university,
difficult_level;