当前位置:网站首页>Classic sql50 questions

Classic sql50 questions

2022-07-06 22:07:00 LI_ dreamit

Database creation, table creation and data insertion

#  Building database 
create database practice_sql50;
use practice_sql50;

#  Student list 
create table Student(SId varchar(10),Sname varchar(10),Sage datetime,Ssex varchar(10));
insert into Student values('01' , ' Zhao Lei ' , '1990-01-01' , ' male ');
insert into Student values('02' , ' Qian Dian ' , '1990-12-21' , ' male ');
insert into Student values('03' , ' Sun Feng ' , '1990-12-20' , ' male ');
insert into Student values('04' , ' Li Yun ' , '1990-12-06' , ' male ');
insert into Student values('05' , ' Zhou Mei ' , '1991-12-01' , ' Woman ');
insert into Student values('06' , ' Wu Lan ' , '1992-01-01' , ' Woman ');
insert into Student values('07' , ' Zheng Zhu ' , '1989-01-01' , ' Woman ');
insert into Student values('08' , ' Wangju ' , '1990-01-20' , ' Woman ');
insert into Student values('09' , ' Zhang San ' , '2017-12-20' , ' Woman ');
insert into Student values('10' , ' Li Si ' , '2017-12-25' , ' Woman ');
insert into Student values('11' , ' Li Si ' , '2012-06-06' , ' Woman ');
insert into Student values('12' , ' Zhao Liu ' , '2013-06-13' , ' Woman ');
insert into Student values('13' , ' Sun Qi ' , '2014-06-01' , ' Woman ');


#  Chart of subjects 
create table Course(CId varchar(10),Cname nvarchar(10),TId varchar(10));
insert into Course values('01' , ' Chinese language and literature ' , '02');
insert into Course values('02' , ' mathematics ' , '01');
insert into Course values('03' , ' English ' , '03');


#  Teachers list 
create table Teacher(TId varchar(10),Tname varchar(10));
insert into Teacher values('01' , ' Zhang San ');
insert into Teacher values('02' , ' Li Si ');
insert into Teacher values('03' , ' Wang Wu ');


#  League tables 
create table SC(SId varchar(10),CId varchar(10),score decimal(18,1));
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);

SQL50 topic

# 1  Inquire about " 01 " Course than " 02 " Student number and course score of students with high course scores 
select t1.SId, class1, class2
from (select SId, score as class1 from SC where CId = '01') as t1,
     (select SId, score as class2 from SC where CId = '02') as t2
where t1.SId = t2.SId
  and class1 > class2;

# 2  Queries exist at the same time " 01 " Courses and " 02 " Course situation 
select t1.*, t2.CId, t2.score
from (select * from SC where CId = '01') as t1,
     (select * from SC where CId = '02') as t2
where t1.SId = t2.SId;

# 3  Query exists " 01 " The course may not exist " 02 " Course situation ( If it does not exist, it will be displayed as  null )
select t1.*, t2.CId, t2.score
from (select * from SC where CId = '01') as t1
         left join
         (select * from SC where CId = '02') as t2
         on t1.SId = t2.SId;

# 4  Query does not exist " 01 " Course but there is " 02 " Course situation 
select *
from SC
where SC.SId not in
      (select SId
       from SC
       where SC.CId = '01')
  and SC.CId = '02';

# 5  Query average score is greater than or equal to  60  Student number, student name and average score of each student 
select SId,avg(score) as avg_score
from SC
group by SId;

# 6  The query in  SC  There is student information about the scores in the table 
select *
from Student
where SId in (
    select SId
    from SC
    where score is not null);

# 7  Query student numbers of all students 、 The student's name 、 The total number of selected courses 、 Total grade of all courses ( No results are shown as  null )
select Student.SId,
       Sname,
       CId_count,
       score_sum
from Student
         left join (
    select SId,
           count(CId) as CId_count,
           sum(score) as score_sum
    from SC
    group by SId) t1
                   on Student.SId = t1.SId;

# 8  Check the information of the students who have achievements 
select *
from Student
where SId in (
    select SId
    from SC
    where score is not null);

# 9  Inquire about 「 Li 」 The number of teachers surnamed 
select count(*)
from Teacher
where Tname like ' Li %';

# 10  I have learned to query 「 Zhang San 」 Information of students taught by teachers 
select Student.*
from Student,
     Teacher,
     SC,
     Course
where Student.SId = SC.SId
  and SC.CId = Course.CId
  and Course.TId = Teacher.TId
  and Teacher.Tname = ' Zhang San ';

# 11  Check the information of students who have not learned all the courses 
select *
from Student
where SId not in
      (select SId
       from SC
       group by SId
       having count(CId) = (select count(*) from Course));

# 12  Query at least one course and student number is " 01 " Of the students who learn the same information 
select SId
from SC
where CId in (
    select CId
    from SC
    where SId = '01')
  and SId <> '01'
group by SId;

# 13  Query and " 01 " The course of study of No   Exactly the same information from other students 
select *
from Student
where SId in (select SId #  Query the number of selected courses and 01 Students like students 
              from SC
              where SId <> '01'
              group by SId
              having count(CId) = (select count(CId) from SC where SId = '01')
                 #  Remove the selected course 01 Students don't choose courses 
                 and SId not in (select distinct SId from SC where CId not in (select CId from SC where SId = '01')));

# 14  I didn't learn how to query " Zhang San " The name of the student in any course taught by the teacher 
select Sname
from Student
where SId not in (select SC.SId
                  from SC,
                       Course,
                       Teacher
                  where SC.CId = Course.CId
                    and Course.TId = Teacher.TId
                    and Tname = ' Zhang San ');

# 15  Check the student number of two or more failed courses , Name and average score 
select stu.SId,
       Sname,
       avg(score) as avg_score
from Student stu,
     SC
where stu.SId = SC.SId
  and score < 60
group by stu.SId, Sname
having count(CId) >= 2;

# 16  retrieval " 01 " The course score is less than  60, Student information in descending order of scores 
select stu.*, score
from Student stu,
     SC
where stu.SId = SC.SId
  and CId = '01'
  and score < 60
order by score desc;

# 17  Show the grades of all courses and the average grades of all students from high to low 
select SId,
       sum(case when CId = '01' then score else null end) as '01',
       sum(case when CId = '02' then score else null end) as '02',
       sum(case when CId = '03' then score else null end) as '03',
       avg(score)                                         as avg_score
from SC
group by SId
order by avg_score desc;

/*18  Check the highest scores of all subjects 、 Minimum and average points :
 Show as follows : Course  ID, Course  name, The highest , Lowest score , average , pass rate , Medium rate , Excellent rate , Excellence rate 
 Pass is >=60, The average is :70-80, Good is :80-90, Excellence is :>=90
 Require output of course number and number of electives , The query results are arranged in descending order of the number of people , If the number of people is the same , In ascending order of course number 
 */
select SC.CId,
       Cname,
       max(score)                                                             as max_score,
       min(score)                                                             as min_score,
       avg(score)                                                             as avg_score,
       sum(case when score >= 60 then 1 else 0 end) / count(*)                as ' pass rate ',
       sum(case when score >= 70 and score < 80 then 1 else 0 end) / count(*) as ' Medium rate ',
       sum(case when score >= 80 and score < 90 then 1 else 0 end) / count(*) as ' Excellent rate ',
       sum(case when score >= 90 then 1 else 0 end) / count(*)                as ' Excellence rate '
from SC,
     Course
where SC.CId = Course.CId
group by SC.CId, Cname
order by count(*) desc, CId;

# 19  Rank according to the results of each subject , And show ranking , Score  Merge rank when repeat 
select t1.CId,
       t1.SId,
       t1.score,
       count(t2.score) + 1 as `rank`
from SC t1
         left join SC t2
                   on t1.score < t2.score and t1.CId = t2.CId
group by t1.CId, t1.SId, t1.score
order by t1.CId, `rank`;

# 20  Check the student's total score , And rank , If the total score is repeated, no vacancy will be reserved 
select SId,
       sum(score),
       rank() over (order by sum(score) desc ) as `rank`
from SC
group by SId;

# 21  Count the number of students in each grade section of each subject : Course number , Course name ,[100-85],[85-70],[70-60],[60-0]  And percentage 
select SC.CId,
       C.Cname,
       sum(case when score between 85 and 100 then 1 else 0 end)            as '[100-85] The number of ',
       sum(case when score between 70 and 85 then 1 else 0 end)             as '[85-70] The number of ',
       sum(case when score between 60 and 70 then 1 else 0 end)             as '[70-60] The number of ',
       sum(case when score between 0 and 60 then 1 else 0 end)              as '[60-0] The number of ',
       sum(case when score between 85 and 100 then 1 else 0 end) / count(*) as '[100-85] Percentage of people ',
       sum(case when score between 70 and 85 then 1 else 0 end) / count(*)  as '[85-70] Percentage of people ',
       sum(case when score between 60 and 70 then 1 else 0 end) / count(*)  as '[70-60] Percentage of people ',
       sum(case when score between 0 and 60 then 1 else 0 end) / count(*)   as '[60-0] Percentage of people '
from SC
         left join Course C
                   on SC.CId = C.CId
group by SC.CId, C.Cname;

# 22  Check the records of the top three students in each subject 
select *
from (
         select CId,
                SId,
                score,
                dense_rank() over (partition by CId order by score desc ) as `rank`
         from SC) t1
where t1.`rank` <= 3;

# 23  Check the number of students selected for each course 
select CId,
       count(SId) as ' Number of elective students '
from SC
group by CId;

# 24  Find out the student ID and name of students who only take two courses 
select SId,
       Sname
from Student
where SId in (
    select SId
    from SC
    group by SId
    having count(CId) = 2);

# 25  Check the boys 、 Number of girls 
select Ssex,
       count(*) ' The number of '
from Student
group by Ssex;

# 26  The query name contains 「 wind 」 The student information of the word 
select *
from Student
where Sname like '% wind %';

# 27  Check the list of students with the same surname , And count the number of people with the same name 
select Sname,
       count(SId) - 1 as ' The number of people with the same name '
from Student
group by Sname;

# 28  Inquire about  1990  List of students born in 
select *
from Student
where year(Sage)='1990';

# 29  Query the average score of each course , The results are in ascending order of average score , The average is the same , In descending order of course number 
select CId,
       avg(score) as avg_score
from SC
group by CId
order by avg_score,
         CId desc;

# 30  Query average score is greater than or equal to  85  Of all students 、 Name and average score 
select stu.SId,
       stu.Sname,
       avg(score) as avg_score
from Student stu
         left join SC
                   on stu.SId = SC.SId
group by stu.SId, stu.Sname
having avg_score >= 85;

# 31  Query the course name as 「 mathematics 」, And the score is lower than  60  Students' names and scores 
select Sname,
       score
from Student stu,
     SC,
     Course C
where stu.SId = SC.SId
  and SC.CId = C.CId
  and Cname = ' mathematics '
  and score < 60;

# 32  Check all students' courses and scores ( There are students who have no grades , No class selection )
select SId,
       sum(case when Cname = ' Chinese language and literature ' then score else null end) as ' Chinese achievement ',
       sum(case when Cname = ' mathematics ' then score else null end) as ' Math scores ',
       sum(case when Cname = ' English ' then score else null end) as ' English scores '
from SC
         join Course C
              on C.CId = SC.CId
group by SId;

# 33  Check the results of any course in  70  Score the above names 、 Course name and score 
select Sname,
       Cname,
       score
from Student stu
         left join SC
                   on stu.SId = SC.SId
         left join Course C
                   on SC.CId = C.CId
where score >= 70;

# 34  Query the failed courses and arrange them in descending order according to the course number 
select SC.CId,
       Cname,
       score
from SC
         inner join Course C
                    on SC.CId = C.CId
where score < 60
order by SC.CId desc;

# 35  The course number is  03  And the course results are in  80  Student number and name of the above students 
select stu.SId,
       Sname
from Student stu
         left join SC
                   on stu.SId = SC.SId
where CId = '03'
  and score > 80;

# 36  Ask for the number of students in each course 
select CId,
       count(SId) as ' Number of students '
from SC
group by CId;

# 37  The results are not repeated , Check options 「 Zhang San 」 Among the students who are taught by the teacher , Student information with the highest scores and their scores 
select stu.SId,
       Sname,
       score
from Student stu,
     SC,
     Course C,
     Teacher T
where stu.SId = SC.SId
  and SC.CId = C.CId
  and C.TId = T.TId
  and Tname = ' Zhang San '
order by score desc
limit 1;

# 38  If there is a repetition of the results , Check options 「 Zhang San 」 Among the students who are taught by the teacher , Student information with the highest scores and their scores 
select stu.*,
       score
from Student stu
left join (
    select *,dense_rank() over (order by score desc ) `dense_rank` from SC
    where CId in (select CId from Course where TId in (select TId from Teacher where Tname = ' Zhang San '))) t1
on stu.SId = t1.SId
where t1.`dense_rank` = 1;

# 39  Check the student number of the same student in different courses 、 Course number 、 Student achievement 
select a.SId,
       a.CId,
       b.CId,
       a.score,
       b.score
from SC a,
     SC b
where a.SId = b.SId
  and a.score = b.score
  and a.CId <> b.CId;

# 40  Look up the top two in each subject 
select *
from (
         select *,
                dense_rank() over (partition by CId order by score desc ) as `dense_rank`
         from SC) t1
where t1.`dense_rank` in (1, 2);

# 41  Count the number of students in each course ( exceed  5  People's course statistics )
select *
from (
         select CId,
                count(SId) as num
         from SC
         group by CId) t1
where t1.num >= 5;

# 42  Search student ID of at least two courses 
select *
from (
         select SId,
                count(CId) as num
         from SC
         group by SId) t1
where t1.num >= 2;

# 43  Query the information of students who have taken all courses 
select stu.*
from Student stu
left join (select SId,count(CId) as num from SC group by SId) t1
on stu.SId = t1.SId
where t1.num = (select count(CId) from Course);

# 44  Check the age of each student , Only by year 
select SId,
       Sname,
       (year(now()) - year(Sage)) as ' Age '
from Student;

# 45  According to the date of birth , Current month day  <  The date of birth is , Age minus one 
/*
 TIMESTAMPDIFF function : There are parameter settings , It can be as accurate as the year (YEAR)、 God (DAY)、 Hours (HOUR), minute (MINUTE) And seconds (SECOND),
  Compared with datediff Functions are more flexible . For the two times of comparison , Put the small time in front of you , Put the big time in the back .
datediff function : The return value is the difference in days , Can't locate hours 、 Minutes and seconds .
 */
select SId,
       Sname,
       timestampdiff(year, Sage, now()) as ' Age '
from Student;

# 46  Check out students who have birthdays this week 
/*
week( Time ) The default from the 0 Start , But Sunday is the first day by default , Foreign algorithms 
week( Time ,1) from 1 Start , And Monday is the first day , Domestic algorithms 
 */
select SId,
       Sname
from Student
where week(Sage)=week(now(),1);

# 47  Check out the students who have their birthday next week 
select SId,
       Sname
from Student
where week(Sage)=week(now(),1)+1;

# 48  Check out the students who have birthdays this month 
select SId,
       Sname
from Student
where month(Sage)=month(now());

# 49  Check out the students who have their birthday next month 
select SId,
       Sname
from Student
where month(Sage)=month(now())+1;

# 50  Inquire about "01" Course than "02" Information and course marks for students with high course grades 
select *
from (
         select t1.SId, class1, class2
         from (select SId, score as class1 from SC where CId = '01') as t1,
              (select SId, score as class2 from SC where CId = '02') as t2
         where t1.SId = t2.SId
           and class1 > class2) as t
         left join Student
                   on t.SId = Student.SId;

 

原网站

版权声明
本文为[LI_ dreamit]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131105090903.html