20200819-sql statement basic exercises

1. Create a table:

CREATE DATABASE MyHomework;
CREATE TABLE tb_class(
cid INT(4) PRIMARY KEY AUTO_INCREMENT,
cname VARCHAR(10)
)
INSERT INTO tb_class VALUES(1,‘kb01’),(2,‘kb01’),(3,‘kb01’),(4,‘kb01’);

DROP TABLE tb_student;
CREATE TABLE tb_student(
sid INT(4) PRIMARY KEY AUTO_INCREMENT,
sname VARCHAR(10),
gender VARCHAR(2),
class_id INT(4),
age INT(100)

)

INSERT INTO tb_student(sname,gender,class_id,age) VALUES
(' Cro ','Male',1,18),
('Messi','Male',1,19),
('Beyonce', 'Female',2,20),
('ysm','女',1,18),
('hly','女',2,18),
('rqm','女',2,20) ,
('lx','Male',3,21),
('pq','Male',3,19),
('wr','Male',3,17);

DROP TABLE tb_teacher;
CREATE TABLE tb_teacher(
tid INT(4) PRIMARY KEY AUTO_INCREMENT,
tname VARCHAR(10),
gender VARCHAR(2)
);
INSERT INTO tb_teacher(tname,gender) VALUES(‘波多’,‘女’),(‘苍空’,‘男’),(‘饭岛’,‘男’);

CREATE TABLE tb_course(
tid INT(4) PRIMARY KEY AUTO_INCREMENT,
cname VARCHAR(10),
teacher_id INT(4)
);
INSERT INTO tb_course(cname,teacher_id)
VALUES(‘java’,1),(‘java web’,1),(‘mysql’,2);

CREATE TABLE tb_score(
sid INT(4) PRIMARY KEY AUTO_INCREMENT,
stu_id INT(4),
corse_id INT(4),
scores INT(100)
);
INSERT INTO tb_score(stu_id,corse_id,scores)
VALUES(1,1,60),(1,2,85),(1,2,100);

2. Insert data:

#1. Make up 3 students in each class
INSERT INTO tb_student(sname,gender,class_id) VALUES
('ysm','女',1),
('hly','nv',2),
('rqm ','Female',2),
('lx','Male',3),
('pq','Male',3),
('wr','Male',3);

INSERT INTO tb_course(cname,teacher_id)
VALUES(‘python’,2),(‘hive’,3),(‘linux’,3);

TRUNCATE TABLE tb_score;

INSERT INTO tb_score(stu_id,corse_id,scores)
VALUES(1,1,60),(1,2,85),(1,3,100),(1,4,60),(1,5,85),(1,6,100),
(2,1,100),(2,2,85),(2,3,98),(2,4,60),(2,5,85),(2,6,100),
(3,1,60),(3,2,78),(3,3,98),(3,4,60),(3,5,85),(3,6,100),
(4,1,99),(4,2,98),(4,3,87),(4,4,60),(4,5,85),(4,6,100),
(5,1,60),(5,2,67),(5,3,98),(5,4,60),(5,5,85),(5,6,100),
(6,1,90),(6,2,87),(6,3,98),(6,4,60),(6,5,85),(6,6,100),
(7,1,99),(7,2,98),(7,3,78),(7,4,60),(7,5,85),(7,6,100),
(8,1,60),(8,2,90),(8,3,78),(8,4,60),(8,5,85),(8,6,100),
(9,1,88),(9,2,98),(9,3,79),(9,4,60),(9,5,85),(9,6,100);

3. Exercises:

#第1题:
SELECT tb1.stu_id
FROM tb_score tb1
JOIN tb_score tb2
ON tb1.scores>tb2.scores AND tb1.corse_id=1 AND tb2.corse_id=2
AND tb1.stu_id=tb2.stu_id

#2. Query the student ID and average score of students with an average score greater than 60;
SELECT * FROM
(SELECT st.sid student ID, st.sname name, AVG(so.scores) average score
FROM tb_student st
JOIN tb_score so ON st . sid=so.stu_id
GROUP BY stu_id
) a
HAVING a. average score>80
ORDER BY average score DESC

#3. Query the student ID, name, number of courses and total scores of all students;
SELECT sc.stu_id student ID, s.sname name,
COUNT(stu_id) number of courses, SUM(sc.scores) total score
FROM tb_score sc
JOIN tb_student s ON sc.stu_id = s.sid
GROUP BY stu_id

#4, query the number of teachers with the last name "Li";
SELECT COUNT(tname) number
FROM tb_teacher
WHERE tname LIKE'李%'

#第5题(1):
SELECT c.cname
FROM tb_course c,tb_teacher t
WHERE t.tid IN(SELECT tid FROM tb_teacher WHERE tname = ‘苍空’)
AND t.tid=c.teacher_id

#第5题(2):
SELECT s.sname FROM tb_student s
WHERE sid IN(
SELECT s1.stu_id
FROM tb_score s1
WHERE s1.course_id=2
)

#第5题(3):
SELECT s.sname FROM tb_student s
WHERE s.sname NOT IN(
SELECT s0.sname FROM tb_student s0
WHERE sid IN(
SELECT s1.stu_id
FROM tb_score s1
WHERE s1.course_id=2
)
)

#第6题(1):
SELECT cname
FROM tb_course
WHERE tid = 1 OR tid=2

#8. Query the student ID and name of all students whose grades of course number "002" are lower than course number "001";
SELECT s.sname, s.sid
FROM tb_student s
WHERE sid IN(
SELECT sc1.stu_id
FROM tb_score sc1 ,tb_score sc2
WHERE sc1.stu_id=sc2.stu_id AND sc1. course_id=1 AND sc2. corse_id=2
AND sc1. scores<sc2. scores)

#9. Query the student ID and name of students who have a course score of less than 60 points;
SELECT DISTINCT s.sname, s.sid
FROM tb_student s
WHERE sid IN(
SELECT sc1.stu_id
FROM tb_score sc1
WHERE sc1.stu_id AND sc1. scores<80 )

#10. Query the student ID and name of students who have not studied all courses;
SELECT st.sid student ID, st.sname name
FROM tb_score sc
JOIN tb_student st
ON sc.stu_id = st.sid
GROUP BY stu_id
HAVING COUNT(course_id) <6

方法二:
SELECT COUNT(DISTINCT course_id),stu_id
FROM tb_score
GROUP BY stu_id
HAVING COUNT(DISTINCT course_id)<
(SELECT COUNT(1) FROM tb_course);

#11. Query at least one course with the student ID and name of the student whose student ID is "001";
#(1) Query whether the course selected by the student is in the course of the student whose student ID is 1
SELECT s2 .stu_id
FROM tb_score s1
JOIN tb_score s2
ON s1.course_id = s2.course_id
WHERE s1.stu_id = 1 AND s2.stu_id != 1
GROUP BY stu_id
#having s1.course_id = s2.coutse_id

方法二:
SELECT DISTINCT stu_id,sname
FROM tb_score s
JOIN tb_student st
ON s.stu_id = st.sid
WHERE s.course_id IN
(SELECT DISTINCT course_id
FROM tb_score
WHERE stu_id=1 )
AND stu_id != 1;

#(2) Get the relevant ID and name associated with the student table
SELECT s.sid student ID, s.sname name
FROM tb_student s
WHERE s.sid IN (
SELECT s2.stu_id
FROM tb_score s1
JOIN tb_score s2
ON s1.course_id = s2 .course_id
WHERE s1.stu_id = 1 AND s2.stu_id != 1
GROUP BY stu_id)

#13. Inquire about the student ID and name of the other students who are studying the same course as the classmate of "002";
#002 The student has selected two courses, 1,3;1;2;2,4;
#( 1) The student id with the same number selected by student No. 1
SELECT stu_id
FROM tb_score
GROUP BY stu_id
HAVING stu_id != 1 AND COUNT(stu_id) = (
SELECT COUNT(stu_id) FROM tb_score WHERE stu_id = 1)

#(2)Re-select the student id with the same course name as student No. 1

SELECT DISTINCT a.stu_id,COUNT(a.course_id) ‘相同课程个数’ FROM tb_score a
LEFT JOIN
(SELECT course_id FROM tb_score WHERE stu_id=2) b
ON a.course_id=b.course_id
GROUP BY stu_id
HAVING 相同课程个数=
(SELECT COUNT(course_id) FROM tb_score WHERE stu_id=1) AND stu_id<>1

#16. Display the scores of the three courses of "Chinese", "Mathematics" and "English" for all students from low to high average grades,
# Display in the following format: Student ID, Chinese, Mathematics, English, the number of effective courses, Effective average score; #The
first implementation method
SELECT * FROM
(SELECT sc.stu_id student number, s.sname name, COUNT(course_id) number of courses, AVG(scores) average score
FROM tb_score sc
JOIN tb_student s ON s. sid= sc .stu_id

JOIN tb_course c ON c.cid = sc.course_id
WHERE cname = ‘java’
GROUP BY sc.stu_id
) a
ORDER BY a.平均分 DESC

#第二种实现方式
SELECT s.stu_id,s.scores python,mysql,java,
CASE WHEN s.scores IS NULL THEN 0 ELSE 1 END+
CASE WHEN mysql IS NULL THEN 0 ELSE 1 END+
CASE WHEN java IS NULL THEN 0 ELSE 1 END sub_cnt
FROM tb_score s
LEFT JOIN tb_course c ON cid = course_id
LEFT JOIN
(SELECT s.stu_id,s.scoures mysql
FROM tb_score s
JOIN tb_course c ON s.stu_id = b.stu_id
LEFT JOIN (SELECT s.stu_id, s.scores java
FROM tb_score s JOIN tb_course
ON s.stu_id = d.stu_id WHERE
c.cname = ‘python’) a

#18. Query the highest and lowest scores of each subject: display in the following format: course ID, highest score, lowest score;
#Idea: group by course id, this time will display four rows, and then use the aggregate function max, min to find the maximum and minimum values.
SELECT course_id course number, MAX(scores) highest score, MIN(scores) lowest score
FROM tb_score
GROUP BY course_id

#19. According to the order of the average score of each subject from low to high and the percentage of passing rate from high to low; #New
knowledge point: case when then is equivalent to if judgment

SELECT AVG(scores),course_id,
CONCAT(100*SUM(CASE WHEN scores>60 THEN 1 ELSE 0 END)/COUNT(scores),’%’) passed
FROM tb_score
GROUP BY course_id
ORDER BY AVG(scores)
SUM(CASE WHEN scores>60 THEN 1 ELSE 0 END)/COUNT(scores) DESC;

#20. The average score of the course is displayed from high to low (displaying the teacher);
SELECT s.course_id course number, t.tname teacher, AVG(s.scores) average score
FROM tb_score s
JOIN tb_course c ON c.cid = s. course_id
JOIN tb_teacher t ON t.tid = c.teacher_id
GROUP BY course_id
ORDER BY Average score DESC

#21
SELECT DISTINCT score1,score2,score3,score4,score5,score6 FROM
(SELECT scores ‘score1’ FROM tb_score WHERE course_id=1 ORDER BY scores DESC LIMIT 3) a,
(SELECT scores ‘score2’ FROM tb_score WHERE course_id=1 ORDER BY scores DESC LIMIT 3) b,
(SELECT scores ‘score3’ FROM tb_score WHERE course_id=1 ORDER BY scores DESC LIMIT 3) c,
(SELECT scores ‘score4’ FROM tb_score WHERE course_id=1 ORDER BY scores DESC LIMIT 3) d,
(SELECT scores ‘score5’ FROM tb_score WHERE course_id=1 ORDER BY scores DESC LIMIT 3) e,
(SELECT scores ‘score6’ FROM tb_score WHERE course_id=1 ORDER BY scores DESC LIMIT 3) f;

SELECT scores ‘score1’ @m:=@m+1 FROM tb_score JOIN(SELECT @m:=0) a WHERE course_id=1
ORDER BY score LIMIT 3;

#21
SELECT a.course_id ,a.scores, b.scores
FROM tb_score a
JOIN tb_score b ON a.course_id = b.course_id AND
a.scores<=b.scores
GROUP BY b.course_id,b.scores
HAVING COUNT(DISTINCT a.scores)<=3
ORDER BY a.course_id ,COUNT(DISTINCT b.scores);

#22. Query the number of students selected for each course;
SELECT course_id course number, COUNT(course_id) number of courses selected
FROM tb_score
GROUP BY course_id

#23. Query the student numbers and names of all students who only took one course;
SELECT st.sid student number, st.sname name
FROM tb_score sc
JOIN tb_student st
ON sc.stu_id = st.sid
GROUP BY stu_id
HAVING COUNT (course_id)=2

#24. Query the number of boys and girls;
SELECT gender, COUNT(gender) number
FROM tb_student
GROUP BY gender

#26. Query the list of students with the same name and surname, and count the number of people with the same name;
SELECT s.sname name, COUNT(sname) number
FROM tb_student s
GROUP BY sname
HAVING number! = 1

#27. Query the average score of each course, and the results are arranged in ascending order of average score. When the average score is the same, they are arranged in descending order of course number;
SELECT course_id course number, AVG(scores) average score
FROM tb_score
GROUP BY course_id
ORDER BY average score ASC #, course_id desc

#28. Query the student ID, name and average score of all students whose average score is greater than 85;
SELECT st.sid student ID, st.sname name, AVG(scores) average score
FROM tb_score
JOIN tb_student st
ON stu_id = st.sid
GROUP BY stu_id
HAVING average score>85

#30. Find the number of students who have selected courses
SELECT COUNT(sid) Number of courses selected
FROM tb_student
WHERE sid IN(
SELECT stu_id
FROM tb_score
GROUP BY stu_id
HAVING COUNT(course_id)>0
)

方法二:
SELECT a.sid,sname FROM tb_student a
WHERE a.sid IN(SELECT DISTINCT stu_id FROM tb_score
GROUP BY course_id)

#31
SELECT MAX(scores),sname,tname
FROM tb_teacher
JOIN tb_course ON teacher_id=tid
JOIN tb_score s ON cid=course_id
JOIN tb_student st ON s.stu_id= st.sid
WHERE tname=‘李梅’
GROUP BY tid;

#31. Query the name and grade of the student with the highest score among the students who took the course taught by teacher "Yang Yan";
SELECT s.sid student number, s.sname name, MAX(sc.scores) score
FROM tb_score sc
JOIN tb_student s ON s. sid= sc. stu_id
WHERE course_id IN(SELECT cid FROM tb_course WHERE teacher_id IN( SELECT tid FROM tb_teacher WHERE tname='李梅'))
GROUP BY stu_id
ORDER BY scores DESC
LIMIT 1;

33. Query each course and the corresponding number of electives; the
first
SELECT c.cname course name, c.cid course number, COUNT(course_id) number of elective courses
FROM tb_score sc
JOIN tb_course c ON sc.course_id = c.cid
GROUP BY course_id

34. Query the student ID, course ID, and student score of students with the same score in different courses;
SELECT s1.stu_id student ID, s1.course_id course ID, s1.scores score
FROM tb_score s1
JOIN tb_score s2
ON s1.stu_id = s2. stu_id AND s1.course_id != s2.course_id AND s1.scores = s2.scores

35. Query the top two with the best scores for each course
;
#First , select count(1) indicates the number of eligible rows in the query table; #sc2.scores>=sc1.scores AND sc1.course_id=sc2.course_id Represents the query condition; #The
overall meaning is to query the
number of rows satisfying the #sc2.scores>=sc1.scores AND sc1.course_id=sc2.course_id condition from the table sc2 ; #Combined
with the complete sql statement, this query is The number of rows must be <=2, so "number of rows<=2" is used as the query condition of the previous query statement.
#So, to select 2 people, first group the tables in descending order by courses. In fact, the data of the two tables are the same, so without adding the condition of <=2
# should all rows meet the conditions, so <=2 In fact, the first two lines are selected.

SELECT sc1.*
FROM tb_score sc1
WHERE (SELECT COUNT(scores) FROM tb_score sc2 WHERE
sc2.scores>=sc1.scores AND sc1.course_id=sc2.course_id)<=2
ORDER BY sc1.course_id ;

老师:
SELECT a.course_id, a.scores
FROM tb_score a
JOIN tb_score b
ON a.course_id=b.course_id AND a.scores<=b.scores
GROUP BY b.course_id,a.scores
HAVING COUNT(DISTINCT b.scores)<=2
ORDER BY a.course_id,COUNT(DISTINCT b.scores);

36. Retrieve the student ID of students who have taken at least two courses;
SELECT DISTINCT stu_id student ID, COUNT(stu_id)
FROM tb_score
GROUP BY stu_id
HAVING Course selection >1

37. Query the course number and course name of the courses taken by all students;
SELECT stu_id student number, course_id course number, cname course name
FROM tb_score
JOIN tb_course ON course_id = cid

38. Query the names of students who have not studied any course taught by teacher "Ye Ping"; the
first type

SELECT sname 姓名, sid 学号 FROM tb_student WHERE sname NOT IN (
SELECT DISTINCT sname
FROM tb_student st
JOIN tb_score s ON st.sid = s.stu_id
WHERE course_id IN (SELECT cid FROM tb_course WHERE teacher_id IN(
SELECT tid FROM tb_teacher WHERE tname = ‘罗老师’)
)
)

The second

38. Inquire about the student number and average grade of students who failed more than two courses;

SELECT s1.stu_id ,AVG(scores)
FROM tb_score s1
#join (select avg(scores) from tb_score s2 where scores<=70 group by stu_id) a
WHERE scores <=70
GROUP BY stu_id
HAVING COUNT(stu_id)>=2

39. Retrieve the student ID of the students whose "004" course score is less than 60, in descending order of score;
SELECT stu_id ,scores FROM tb_score
WHERE course_id=4 AND scores<80
ORDER BY scores DESC

40. Delete "002" classmate's "001" course score;
DELETE FROM tb_score
WHERE stu_id = 3 AND course_id = 3

41. Query the name of the student whose score is 85, 86 or 88 in the score table
SELECT sid ,sname FROM tb_student
WHERE sid IN (
SELECT stu_id FROM tb_score WHERE scores = 85 OR scores = 86 OR scores = 88)

42. Query the number of students in class "kb03"
SELECT COUNT(class_id) Number FROM tb_student
WHERE class_id IN(SELECT cid FROM tb_class WHERE cname ='kb03')

43. Query "Zhang Xu" teacher's average score of students in classes
SELECT course_id, AVG(scores)
FROM tb_score WHERE course_id IN (SELECT cid FROM tb_course WHERE teacher_id IN
(SELECT tid FROM tb_teacher WHERE tname ='王老师'))
GROUP BY course_id

44. Query the names of all students with the same age as the student whose student number is 108
SELECT s.sname ,s.sid
FROM tb_student s
JOIN tb_student s1
ON s.age=s1.age AND s1.sid = 4;

45. Query the score table of students whose scores are lower than the average score of the course
SELECT stu_id, a.course_id, scores
FROM tb_score a
JOIN (
SELECT course_id, AVG(scores) average score FROM tb_score GROUP BY course_id) b
ON a.course_id = b .course_id
WHERE a.scores <b. Average Score
#group by a.course_id

46. ​​Query the class number of at least 3 girls
SELECT cid FROM tb_class
WHERE cid IN(SELECT class_id FROM tb_student WHERE gender ='女'
GROUP BY class_id
HAVING COUNT(class_id)>=2)

47. Query "male" teachers and their course transcripts
SELECT cname
FROM tb_course
WHERE teacher_id IN(SELECT tid FROM tb_teacher WHERE gender ='Male')

48. The query displays the average scores of male and female students separately
SELECT gender, AVG(scores)
FROM tb_score a
JOIN tb_student b
ON a.stu_id = b.sid
GROUP BY gender

49. Query the names of teachers with more than 5 students taking a course
SELECT tname FROM tb_teacher
WHERE tid IN (SELECT teacher_id FROM tb_course WHERE cid IN
(SELECT course_id FROM tb_score GROUP BY course_id
HAVING COUNT(course_id)>=5
))

50. Query the average scores of courses that are selected by at least 5 students in the course table and start with java
SELECT AVG(scores)
FROM tb_score
JOIN tb_course
WHERE course_id IN (SELECT cid FROM tb_course WHERE cname LIKE'java%')
GROUP BY course_id
HAVING COUNT(course_id)>3

Guess you like

Origin blog.csdn.net/qq_42005540/article/details/108108118