当前位置:网站首页>Drink at night, 50 classic SQL questions, really fragrant~

Drink at night, 50 classic SQL questions, really fragrant~

2022-06-11 09:49:00 _ Chenhaha

   In the evening, I heard that our village was about to be unsealed , Home office days are numbered , It will not settle down for a long time ~~

   While crouching in the pit , Found... On the Internet 50 Tao is called classics SQL topic , This is a must-have snack at night ? I picked up my slippers with my feet , Take out half a bottle of coke that has been sealed for a long time from the refrigerator , Open what I haven't seen for days MySQL8, Come on , Let's talk about the questions on the field .

 Insert picture description here

   Now it is 6 month 9 Number 00:15 branch , The flowers are near 3 Hours to finish this little 50 topic , I feel a little sleepy , I don't want to give the so-called standard answer .. There is a tiger in my heart , Why sniff the roses ?

   I will simply send my results first ( I will separate the question from the answer and brush the question with you ), I believe that the students who like to play with me , You can brush it once and compare it with mine SQL, Will find me SQL Problems in , I'll redo the update again . On the whole, the difficulty is medium and low , But if you want to review SQL Aftertaste the original you , It's recommended , After all, aftertaste is a blessing . No more bullshit , On and off .



One 、 Test table data

Student list :student [ Student number , The student's name , date of birth , Gender ]
League tables :score [ Student number , Course no. , achievement ]
The curriculum :course [ Course no. , Course name , Teacher number ]
Teachers list :teacher [ Teacher number , Teacher's name )

 Insert picture description here

Here is the table structure and data , Direct execution ~

-- ----------------------------
-- Table structure for teacher
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher`  (
  `t_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  `t_name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
  PRIMARY KEY (`t_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of teacher
-- ----------------------------
INSERT INTO `teacher` VALUES ('01', ' Math teacher - Jess ');
INSERT INTO `teacher` VALUES ('02', ' Chinese teacher - Blind monk ');
INSERT INTO `teacher` VALUES ('03', ' English teacher - Fiona ');

-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student`  (
  `s_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  `s_name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
  `s_birth` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
  `s_sex` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
  PRIMARY KEY (`s_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('01', ' Wandering mage - ritz ', '1990-01-01', ' male ');
INSERT INTO `student` VALUES ('02', ' Go!Explore -EZ', '1990-12-21', ' male ');
INSERT INTO `student` VALUES ('03', ' The strong wind sword - Arthur ', '1990-05-20', ' male ');
INSERT INTO `student` VALUES ('04', ' Swift scout - Timo ', '1990-08-06', ' male ');
INSERT INTO `student` VALUES ('05', ' The daughter of darkness - Anne ', '1991-12-01', ' Woman ');
INSERT INTO `student` VALUES ('06', ' The goddess of war - Hicville ', '1992-03-01', ' Woman ');
INSERT INTO `student` VALUES ('07', ' Bright girl - Laches ', '1989-07-01', ' Woman ');
INSERT INTO `student` VALUES ('08', ' The blade of exile - Sharp Vivian ', '1990-01-20', ' Woman ');

-- ----------------------------
-- Table structure for score
-- ----------------------------
DROP TABLE IF EXISTS `score`;
CREATE TABLE `score`  (
  `s_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  `c_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  `s_score` int(0) NULL DEFAULT NULL,
  PRIMARY KEY (`s_id`, `c_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of score
-- ----------------------------
INSERT INTO `score` VALUES ('01', '01', 80);
INSERT INTO `score` VALUES ('01', '02', 90);
INSERT INTO `score` VALUES ('01', '03', 99);
INSERT INTO `score` VALUES ('02', '01', 70);
INSERT INTO `score` VALUES ('02', '02', 60);
INSERT INTO `score` VALUES ('02', '03', 80);
INSERT INTO `score` VALUES ('03', '01', 80);
INSERT INTO `score` VALUES ('03', '02', 80);
INSERT INTO `score` VALUES ('03', '03', 80);
INSERT INTO `score` VALUES ('04', '01', 50);
INSERT INTO `score` VALUES ('04', '02', 30);
INSERT INTO `score` VALUES ('04', '03', 20);
INSERT INTO `score` VALUES ('05', '01', 76);
INSERT INTO `score` VALUES ('05', '02', 87);
INSERT INTO `score` VALUES ('06', '01', 31);
INSERT INTO `score` VALUES ('06', '03', 34);
INSERT INTO `score` VALUES ('07', '02', 89);
INSERT INTO `score` VALUES ('07', '03', 98);

-- ----------------------------
-- Table structure for course
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course`  (
  `c_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  `c_name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',
  `t_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  PRIMARY KEY (`c_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of course
-- ----------------------------
INSERT INTO `course` VALUES ('01', ' Chinese language and literature ', '02');
INSERT INTO `course` VALUES ('02', ' mathematics ', '01');
INSERT INTO `course` VALUES ('03', ' English ', '03');

Two 、50 Avenue SQL topic ( No answers ), For self test

Okay , Please open your dear navicat, Shut down Baidu 、 Mobile phones and other obstacles to your progress . Start your show ~

-- 50 Avenue SQL Interview questions 
-- 1、 The course number is “01” The course is better than “02” The student numbers of all students with high grades in the course ( difficult )

-- 2、 Query average score greater than 60 The student number and average grade of the students with a score of 

-- 3、 Check the student numbers of all the students 、 full name 、 Number of courses selected 、 Total score 

-- 4、 Check the last name “ Monkey ” The number of teachers 

-- 5、 I didn't learn how to query “ Math teacher - Jess ” The student number of the teacher's class 、 full name 

-- 6、 I have learned to query “ Math teacher - Jess ” The student number of all the students the teacher taught 、 full name 

-- 7、 Inquiry learned number is “01” And I've learned a number of courses “02” The student number of the student in the course of 、 full name 

-- 8、 The course number is “02” The total score of 

-- 9、 Query all , The course score is less than 60 The student number of the student 、 full name 

-- 10、 Query the student ID of the student who did not learn all the courses 、 full name 

-- 11、 Query at least one course and student number is “01” The student number and name of the same student who studied the same course  ( difficult )

-- 12、 Query and “01” Student number of other students whose courses are exactly the same ( difficult )

-- 13、 I didn't learn how to query " Math teacher - Jess " The name of the student in any course taught by the teacher 

-- 14、 empty 

-- 15、 Check the student number of two or more failed courses , Name and average score 

-- 16、 retrieval "01" The course score is less than 60, Student information in descending order of scores 

-- 17、 Show the grades of all courses and the average grades of all students from high to low ( difficult )

-- 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 

-- 19、 Check the student's total score and rank 

-- 20、 Query the average score of different courses taught by different teachers , Display from high to low 

-- 21、 Check the student's average score and ranking 

-- 22、 Rank according to the results of each subject , And show ranking ( difficult )

-- 23、 Check the names of the top two students with the best scores in each subject 

-- 24、 Check the grades of all courses 2 Name to number 3 Student information and course results 

-- 25、 Check the records of the top three students in each subject ( Don't think about the result juxtaposition )

-- 26、 Use segmentation [100-85],[85-70],[70-60],[<60] To count the results of each subject , Count the number of people in each score segment : Course ID And the name of the course 

-- 27、 Check the number of students selected for each course 

-- 28、 Find out the student number and name of all students in only two courses 

-- 29、 Check the boys 、 Number of girls 

-- 30、 The query name contains " wind " The student information of the word 

-- 31、 Inquire about 1990 List of students born in 

-- 32、 Query average score is greater than or equal to 85 Of all students 、 Name and average score 

-- 33、 Query the average score of each course , The results are sorted in ascending order of average , The average is the same , In descending order of course number 

-- 34、 Query the course name as " mathematics ", And the score is lower than 60 Students' names and scores 

-- 35、 Check all students' courses and scores 

-- 36、 Check the results of any course in 70 Score the above names 、 Course name and score 

-- 37、 Query the failed courses and arrange them in descending order according to the course number 

-- 38、 The course number is 03 And the course results are in 80 Student number and name of the above students 

-- 39、 Ask for the number of students in each course 

-- 40、 Check options “ Math teacher - Jess ” The name and grade of the student with the highest score in the course given by the teacher 

-- 41、 Check the student number of the same student in different courses 、 Course number 、 Student achievement  ( difficult )

-- 42、 Count the number of students in each course ( exceed 5 People's course statistics ). 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 

-- 43、 Search student ID of at least two courses 

-- 44、 Query the information of students who have taken all courses 

-- 45、 Check the age of each student 

-- 46、 Check the student number and average score of students who have failed two or more courses 

-- 47、 Check out the students who have birthdays this month 

-- 48、 Check the students whose birthdays are next month 

3、 ... and 、50 Avenue SQL topic ( With answers ), For reference

– Let's have a look at the full data association

--  Let's have a look at the full data association 
select * from student stu,score sc,course c,teacher t 
where stu.s_id=sc.s_id and sc.c_id =c.c_id and c.t_id = t.t_id;

50 Avenue SQL Interview questions

1、 The course number is “01” The course is better than “02” The student numbers of all students with high grades in the course ( difficult )

-- 1、 The course number is “01” The course is better than “02” The student numbers of all students with high grades in the course ( difficult )
SELECT a.s_id from 
(select * from score sc1 where sc1.c_id = '01') a , 
(select * from score sc2 where sc2.c_id = '02') b 
where a.s_id = b.s_id and a.s_score > b.s_score;

– 2、 Query average score greater than 60 The student number and average grade of the students with a score of

-- 2、 Query average score greater than 60 The student number and average grade of the students with a score of 
select a.s_id,a.avg_score from 
(select stu.s_id,stu.s_name,AVG(sc.s_score) as avg_score from student stu, score sc where stu.s_id = sc.s_id GROUP BY stu.s_id) a 
where a.avg_score > 60 ORDER BY avg_score desc;  

– 3、 Check the student numbers of all the students 、 full name 、 Number of courses selected 、 Total score

-- 3、 Check the student numbers of all the students 、 full name 、 Number of courses selected 、 Total score 
--  I'm here MySQL8 perform sql When there is sql_mode=only_full_group_by error , Execute downlink sql Configure to solve the problem ;
-- set @@global.sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
select stu.s_id,stu.s_name,count(sc.c_id),IFNULL(SUM(sc.s_score),0) from student stu 
LEFT JOIN score sc on stu.s_id = sc.s_id GROUP BY stu.s_id;

– 4、 Check the last name “ Monkey ” The number of teachers

-- 4、 Check the last name “ Monkey ” The number of teachers 
select count(*) from teacher where t_name like ' Monkey %';

– 5、 I didn't learn how to query “ Math teacher - Jess ” The student number of the teacher's class 、 full name

-- 5、 I didn't learn how to query “ Math teacher - Jess ” The student number of the teacher's class 、 full name 
SELECT * from student a where a.s_id not in
 (select stu.s_id from student stu,score sc,course c,teacher t 
where stu.s_id=sc.s_id and sc.c_id =c.c_id and c.t_id = t.t_id and t.t_name =' Math teacher - Jess ')

– 6、 I have learned to query “ Math teacher - Jess ” The student number of all the students the teacher taught 、 full name

-- 6、 I have learned to query “ Math teacher - Jess ” The student number of all the students the teacher taught 、 full name 
select stu.s_id,stu.s_name from 
student stu,score sc,course c,teacher t 
where stu.s_id=sc.s_id and sc.c_id =c.c_id and c.t_id = t.t_id and t.t_name =' Math teacher - Jess ';

– 7、 Inquiry learned number is “01” And I've learned a number of courses “02” The student number of the student in the course of 、 full name

-- 7、 Inquiry learned number is “01” And I've learned a number of courses “02” The student number of the student in the course of 、 full name 
SELECT a.s_id,a.s_name from 
(SELECT stu1.s_id,stu1.s_name from student stu1, score sc1 where stu1.s_id = sc1.s_id and sc1.c_id ='01') a,  
(SELECT stu2.s_id,stu2.s_name from student stu2, score sc2 where stu2.s_id = sc2.s_id and sc2.c_id ='02') b
where a.s_id = b.s_id;

– 8、 The course number is “02” The total score of

-- 8、 The course number is “02” The total score of 
select SUM(sc.s_score) from score sc where c_id = '02';

– 9、 Query all , The course score is less than 60 The student number of the student 、 full name

-- 9、 Query all , The course score is less than 60 The student number of the student 、 full name 
select stu.s_id,stu.s_name from student stu,score sc where stu.s_id = sc.s_id and sc.s_score < 60 GROUP BY stu.s_id;

– 10、 Query the student ID of the student who did not learn all the courses 、 full name

-- 10、 Query the student ID of the student who did not learn all the courses 、 full name 
SELECT stu2.s_id,stu2.s_name from student stu2 ,
(SELECT stu.s_id,stu.s_name,IFNULL(COUNT(c.c_id),0) as c_count from student stu 
LEFT JOIN score sc on stu.s_id = sc.s_id JOIN course c on sc.c_id = c.c_id GROUP BY stu.s_id) a
where stu2.s_id = a.s_id and a.c_count = (SELECT count(*) from course);

– 11、 Query at least one course and student number is “01” The student number and name of the same student who studied the same course ( difficult )

-- 11、 Query at least one course and student number is “01” The student number and name of the same student who studied the same course  ( difficult )
SELECT a.s_id,a.s_name from 
(SELECT stu1.s_id,stu1.s_name,sc1.c_id from student stu1,score sc1 where stu1.s_id = sc1.s_id) a,
(SELECT sc2.c_id from student stu2,score sc2 where stu2.s_id = sc2.s_id and stu2.s_id = '01') b
where a.c_id = b.c_id GROUP BY a.s_id;

– 12、 Query and “01” Student number of other students whose courses are exactly the same ( difficult )

-- 12、 Query and “01” Student number of other students whose courses are exactly the same ( difficult )
select s_id,s_name from student 
where s_id in (
select s_id from score
where s_id <> '01'
group by s_id
having count(*) = (select count(*) from score where s_id = '01'))
and s_id not in(select distinct s_id from score where c_id not in (select c_id from score where s_id = '01'))

– 13、 I didn't learn how to query " Math teacher - Jess " The name of the student in any course taught by the teacher

-- 13、 I didn't learn how to query " Math teacher - Jess " The name of the student in any course taught by the teacher 
SELECT s_name from student stu2 where stu2.s_id not in
(select DISTINCT stu.s_id from student stu,score sc,course c,teacher t 
where stu.s_id=sc.s_id and sc.c_id =c.c_id and c.t_id = t.t_id and t.t_name = ' Math teacher - Jess ') ;

– 15、 Check the student number of two or more failed courses , Name and average score

-- 15、 Check the student number of two or more failed courses , Name and average score 
SELECT a.s_id, a.s_name, avg from student stu2,
(SELECT stu.s_id, stu.s_name, count(*) as count, AVG(sc.s_score) as avg from student stu, score sc 
where stu.s_id = sc.s_id and sc.s_score < 60 GROUP BY stu.s_id) a where stu2.s_id = a.s_id and a.count >= 2;

– 16、 retrieval "01" The course score is less than 60, Student information in descending order of scores

-- 16、 retrieval "01" The course score is less than 60, Student information in descending order of scores 
SELECT stu.s_id, stu.s_name,sc.s_score from student stu, score sc where stu.s_id = sc.s_id and sc.c_id = '01' and sc.s_score < 60 ORDER BY sc.s_score desc;

– 17、 Show the grades of all courses and the average grades of all students from high to low ( difficult )

-- 17、 Show the grades of all courses and the average grades of all students from high to low ( difficult )
--  Full display 
SELECT * from 
(SELECT stu.s_id,stu.s_name,sc.c_id,IFNULL(sc.s_score,0) from student stu LEFT JOIN score sc on stu.s_id = sc.s_id ) a,
(SELECT sc1.s_id,AVG(sc1.s_score) as avg from score sc1 GROUP BY sc1.s_id) b
where a.s_id = b.s_id ORDER BY avg desc;
--  Horizontal display 
select s_id,s_name,
(select s_score from score where score.s_id=student.s_id and c_id='01') as ' Chinese language and literature ',
(select s_score from score where score.s_id=student.s_id and c_id='02') as ' mathematics ',
(select s_score from score where score.s_id=student.s_id and c_id='03') as ' English ',
(select avg(s_score) from score where score.s_id=student.s_id) avg_score
from student 
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

-- 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 
SELECT c.c_id,c.c_name,MAX(sc.s_score),MIN(sc.s_score),AVG(sc.s_score),CONCAT(round(100 * sum(case when sc.s_score>=60 then 1.0 else 0.0 end)/count(*),2),'%') as ' pass rate ' from score sc, course c where sc.c_id = c.c_id GROUP BY c.c_id;

– 19、 Check the student's total score and rank

-- 19、 Check the student's total score and rank 
SELECT stu.s_id,stu.s_name,IFNULL(SUM(sc.s_score),0) as totalCount from student stu LEFT JOIN score sc on stu.s_id = sc.s_id GROUP BY stu.s_id ORDER BY totalCount desc

– 20、 Query the average score of different courses taught by different teachers , Display from high to low

-- 20、 Query the average score of different courses taught by different teachers , Display from high to low 
SELECT t.t_name,c.c_name,AVG(sc.s_score) as avg from score sc, course c, teacher t where sc.c_id = c.c_id and c.t_id = t.t_id GROUP BY t.t_id ORDER BY avg desc;

– 21、 Check the student's average score and ranking

-- 21、 Check the student's average score and ranking 
SELECT stu.s_id,stu.s_name,AVG(sc.s_score) as avg,rank() over(ORDER BY AVG(sc.s_score) desc) as `no` from student stu LEFT JOIN score sc on stu.s_id = sc.s_id GROUP BY stu.s_id ORDER BY avg desc;

– 22、 Rank according to the results of each subject , And show ranking ( difficult )

-- 22、 Rank according to the results of each subject , And show ranking ( difficult )
select c_id,s_id,s_score,rank() over(partition by c_id order by s_score desc) num_row 
from score;

– 23、 Check the names of the top two students with the best scores in each subject

-- 23、 Check the names of the top two students with the best scores in each subject 
select a.s_id,a.s_name,b.num_row 
from student a 
join (select s_id, c_id, row_number() over(partition by c_id order by s_score desc) num_row from score) b on a.s_id=b.s_id 
where b.num_row<=2;

– 24、 Check the grades of all courses 2 Name to number 3 Student information and course results

-- 24、 Check the grades of all courses 2 Name to number 3 Student information and course results 
select a.s_id,a.s_name,b.c_id,b.s_score,b.num_row 
from student a 
,(select 
      s_id,
      c_id,
      s_score,
      row_number() over(partition by c_id order by s_score desc) num_row from score) b 
where a.s_id=b.s_id 
and b.num_row between 2 and 3;

– 25、 Check the records of the top three students in each subject ( Don't think about the result juxtaposition )

-- 25、 Check the records of the top three students in each subject ( Don't think about the result juxtaposition )
select a.s_id,a.s_name,b.c_id,b.s_score,b.num_row 
from student a 
,(select 
      s_id,
      c_id,
      s_score,
      row_number() over(partition by c_id order by s_score desc) num_row from score) b 
where a.s_id=b.s_id 
and b.num_row between 1 and 3;

– 26、 Use segmentation [100-85],[85-70],[70-60],[<60] To count the results of each subject , Count the number of people in each score segment : Course ID And the name of the course

-- 26、 Use segmentation [100-85],[85-70],[70-60],[<60] To count the results of each subject , Count the number of people in each score segment : Course ID And the name of the course 
SELECT c.c_id,c.c_name,
(select count(*) from score sc1 where sc1.c_id=c.c_id and sc1.s_score >= 85) as '[100-85]',
(select count(*) from score sc2 where sc2.c_id=c.c_id and sc2.s_score BETWEEN 70 and 85) as '[85-70]',
(select count(*) from score sc3 where sc3.c_id=c.c_id and sc3.s_score BETWEEN 60 and 70) as '[70-60]',
(select count(*) from score sc4 where sc4.c_id=c.c_id and sc4.s_score < 60) as '[<60]'
from course c 
GROUP BY c.c_id;

– 27、 Check the number of students selected for each course

-- 27、 Check the number of students selected for each course 
select c_id,count(c_id) count from score group by c_id;

– 28、 Find out the student number and name of all students in only two courses

-- 28、 Find out the student number and name of all students in only two courses 
select a.s_id,a.s_name,count(b.s_id) count 
from student a 
join score b on a.s_id=b.s_id 
group by s_id 
having count=2;

– 29、 Check the boys 、 Number of girls

-- 29、 Check the boys 、 Number of girls 
select s_sex,count(s_sex) count_sex from student group by s_sex;

– 30、 The query name contains " wind " The student information of the word

-- 30、 The query name contains " wind " The student information of the word 
select * from student where s_name like '% wind %';

– 31、 Inquire about 1990 List of students born in

-- 31、 Inquire about 1990 List of students born in 
select s_id,s_name from student where year(s_birth)='1990';

– 32、 Query average score is greater than or equal to 85 Of all students 、 Name and average score

-- 32、 Query average score is greater than or equal to 85 Of all students 、 Name and average score 
select a.s_id,a.s_name,avg(b.s_score) avg
from student a 
join score b on a.s_id=b.s_id 
group by b.s_id 
having avg>=85;

– 33、 Query the average score of each course , The results are sorted in ascending order of average , The average is the same , In descending order of course number

-- 33、 Query the average score of each course , The results are sorted in ascending order of average , The average is the same , In descending order of course number 
select c_id, avg(s_score) avg_score from score group by c_id order by avg_score,c_id desc;

– 34、 Query the course name as " mathematics ", And the score is lower than 60 Students' names and scores

-- 34、 Query the course name as " mathematics ", And the score is lower than 60 Students' names and scores 
select sc.c_id,c.c_name,stu.s_id,stu.s_name,sc.s_score
from score sc 
join course c on sc.c_id=c.c_id 
join student stu on sc.s_id=stu.s_id 
where sc.s_score<60 and c.c_name=' mathematics ';

– 35、 Check all students' courses and scores

-- 35、 Check all students' courses and scores 
select stu.s_id,stu.s_name,c.c_id,c.c_name,sc.s_score 
from student stu 
left join score sc on stu.s_id=sc.s_id 
left join course c on sc.c_id=c.c_id ;

– 36、 Check the results of any course in 70 Score the above names 、 Course name and score

-- 36、 Check the results of any course in 70 Score the above names 、 Course name and score 
select c.s_name,b.c_name,a.s_score 
from score a 
join course b on a.c_id=b.c_id 
join student c on a.s_id=c.s_id 
where a.s_score>70;

– 37、 Query the failed courses and arrange them in descending order according to the course number

-- 37、 Query the failed courses and arrange them in descending order according to the course number 
select c_id from score where s_score < 60 GROUP BY c_id order by c_id desc;

– 38、 The course number is 03 And the course results are in 80 Student number and name of the above students

-- 38、 The course number is 03 And the course results are in 80 Student number and name of the above students 
select stu.s_id,stu.s_name 
from student stu, score sc where stu.s_id=sc.s_id 
and sc.s_score>80 and sc.c_id=03;

– 39、 Ask for the number of students in each course

-- 39、 Ask for the number of students in each course 
select c_id, count(*) count from score group by c_id;

– 40、 Check options “ Math teacher - Jess ” The name and grade of the student with the highest score in the course given by the teacher

-- 40、 Check options “ Math teacher - Jess ” The name and grade of the student with the highest score in the course given by the teacher 
select stu.s_id,stu.s_name,sc.s_score from
student stu,score sc,course c,teacher t where stu.s_id=sc.s_id and sc.c_id =c.c_id and c.t_id = t.t_id 
and t.t_name=' Math teacher - Jess ' 
order by sc.s_score desc 
limit 1;

– 41、 Check the student number of the same student in different courses 、 Course number 、 Student achievement ( difficult )

-- 41、 Check the student number of the same student in different courses 、 Course number 、 Student achievement  ( difficult )
select a.s_id,a.c_id,a.s_score 
from score a, score b where a.s_score=b.s_score 
and a.c_id <> b.c_id and a.s_id=b.s_id 
group by a.s_id,a.c_id,a.s_score;

– 42、 Count the number of students in each course ( exceed 5 People's course statistics ). 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

-- 42、 Count the number of students in each course ( exceed 5 People's course statistics ). 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 c_id, count(*) count from score group by c_id order by c_id asc;

– 43、 Search student ID of at least two courses

-- 43、 Search student ID of at least two courses 
select s_id,count(*) count from score group by s_id having count>=2;

– 44、 Query the information of students who have taken all courses

-- 44、 Query the information of students who have taken all courses 
select stu.s_id,stu.s_name,count(*) count from student stu,score sc where stu.s_id = sc.s_id group by sc.s_id having count = 3;

– 45、 Check the age of each student

-- 45、 Check the age of each student 
SELECT * ,(YEAR(CURDATE()) - YEAR(s_birth)) AS age from student; 

– 46、 Check the student number and average score of students who have failed two or more courses

-- 46、 Check the student number and average score of students who have failed two or more courses 
select s_id,avg(s_score) avg
from score 
where s_score < 60 
group by s_id 
having count(*) >= 2;

– 47、 Check out the students who have birthdays this month

-- 47、 Check out the students who have birthdays this month 
select s_id from student where month(s_birth) = month(now());

– 48、 Check the students whose birthdays are next month

-- 48、 Check the students whose birthdays are next month 
select s_id from student where month(s_birth) = month(now())+1;

Okay , Have an early rest!It's clockwise ~~
If there are errors or performance to be optimized SQL, Remember to tell me in the comments section , I'll ask you to give me a kick ~~

 Insert picture description here

原网站

版权声明
本文为[_ Chenhaha]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110933249675.html