sql interview questions-handwritten sql practice case (1)

1. Seeking: Find out students whose scores in all subjects are greater than the average score in a certain subject

uid  subject_id score
1001 01 90
1001 02 90
1001 03 90
1002 01 85
1002 02 85
1002 03 70
1003 01 70
1003 02 70
1003 03 85

Create the table first

create table score(
    uid string,
    subject_id string,
    score int)
row format delimited fields terminated by '\t'; 

 1.1 Find the average grade of each subject

select
    uid,
    score,
    avg(score) over(partition by subject_id) avg_score
from
    score;t1

1.2 Record the flag according to whether it is greater than the average score, if it is greater than the average score, it is recorded as 0, otherwise it is recorded as 1.

select
    uid,
    if(score>avg_score,0,1) flag
from
    t1;t2

1.3 The sum of flags is grouped and counted according to the student's id, and if the sum is 0, all subjects are greater than the average score

select
    uid
from
    t2
group by
    uid
having
    sum(flag)=0;

1.4 Final SQL

select
    uid
from
    (select
    uid,
    if(score>avg_score,0,1) flag
from
    (select
    uid,
    score,
    avg(score) over(partition by subject_id) avg_score
from
    score)t1)t2
group by
    uid
having
    sum(flag)=0;

2. It is required to use SQL to count the cumulative access times of each user

data:

User id (userId) Month (visitDate) Count (visitCount)
u01 2020-10-28 5
u02 2020-9-27 6
u03   2020-10-16 8
u04 2020-10-13 3
u01 2020-10-24 6
u01 2020-10-09 8
u02 2020-10-25 6
u01 2020-9-18 4

Create table

create table visit
(userId string,
visitDate string,
visitCount int) 
row format delimited fields terminated by "\t";

2.1 Calculate the monthly visits per person

select
    userId,
    visitDate,
    sum(visitCount) mn_count
from
    visit
group by
    userId,visitDate;t1

2.2 Cumulative monthly visits

select
    userId,
    visitDate,
    mn_count,
    sum(mn_count) over(partition by userId order by visitDate)
from t1;

2.3 Final SQL

select
     userId,
     visitDate,
     mn_count,
     sum(mn_count) over(partition by userId order by visitDate)
from
  (select
     userId,
     visitDate,
     sum(visitCount) mn_count
from visit
group by userId,visitDate)t1;
  
    

 

Guess you like

Origin blog.csdn.net/Poolweet_/article/details/109596120