Database query practice case

Class Schedule


– Table structure for course


DROP TABLE IF EXISTS course;
CREATE TABLE course (
id int(11) NOT NULL,
name varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
teacherid int(11) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


– Records of course


BEGIN;
INSERT INTO courseVALUES (105,'Introduction to Computer', 825);
INSERT INTO courseVALUES (166,'Data Circuit', 856);
INSERT INTO courseVALUES (245,'Operating System', 804);
INSERT INTO courseVALUES (888, 'Advanced Mathematics', 100);
COMMIT;

Teacher table

– DROP TABLE IF EXISTS teacher;
– CREATE TABLE teacher (
id int(11) NOT NULL,
name varchar(255) CHARACTER SET utf8 DEFAULT NULL,
gender varchar(255) CHARACTER SET utf8 DEFAULT NULL,
birthday date DEFAULT NULL,
title varchar(255) CHARACTER SET utf8 DEFAULT NULL,
department varchar(255) CHARACTER SET utf8 DEFAULT NULL,
– PRIMARY KEY (id)
– ) ENGINE=InnoDB DEFAULT CHARSET=latin1;


– Records of teacher


– BEGIN;
– INSERT INTO teacherVALUES (804,'毛 Dichen','Male', '1973-12-02','Associate Professor','Computer Department');
– INSERT INTO teacherVALUES (825,'向小芳', ' Female', '1988-05-05','Lecturer','Computer Department');
– INSERT INTO teacherVALUES (831,'Liu Bing','Female', '1977-08-14','Assistant Teacher', ' Department of Electronic Engineering');
– INSERT INTO teacherVALUES (856,'Zhang Xu','Male', '1985-03-12','Lecturer','Electronic Engineering Department');
– COMMIT;

Student table

– DROP TABLE IF EXISTS student;
– CREATE TABLE student (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
gender varchar(4) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
birthday date DEFAULT NULL,
class int(11) DEFAULT NULL,
– PRIMARY KEY (id)
– ) ENGINE=InnoDB AUTO_INCREMENT=110 DEFAULT CHARSET=latin1;


– Records of student


– BEGIN;
– INSERT INTO studentVALUES (101,'Liang Qianlei','Male', '1992-02-20', 95033);
– INSERT INTO studentVALUES (103,'Chen Shengjie','Male', '1995-06-03 ', 95031);
– INSERT INTO studentVALUES (105,'Huang Zhen','Male', '2000-10-02', 95031);
– INSERT INTO studentVALUES (107,'Xu Jinfeng','Male', '1991 -01-23', 95033);
– INSERT INTO studentVALUES (108,'Luo Chang','Male', '1995-09-01', 95033);
– INSERT INTO studentVALUES (109,'Lu Yanming','Female ', '1994-02-10', 95031);
– COMMIT;

Score table


– Table structure for score


– DROP TABLE IF EXISTS score;
– CREATE TABLE score (
studentid int(11) DEFAULT NULL,
courseid int(11) DEFAULT NULL,
grade varchar(255) DEFAULT NULL
– ) ENGINE=InnoDB DEFAULT CHARSET=latin1;


– Records of score


– BEGIN;
– INSERT INTO score VALUES (103, 245, ‘86’);
– INSERT INTO score VALUES (105, 245, ‘75’);
– INSERT INTO score VALUES (109, 245, ‘68’);
– INSERT INTO score VALUES (103, 105, ‘92’);
– INSERT INTO score VALUES (105, 105, ‘88’);
– INSERT INTO score VALUES (109, 105, ‘76’);
– INSERT INTO score VALUES (101, 105, ‘64’);
– INSERT INTO score VALUES (107, 105, ‘91’);
– INSERT INTO score VALUES (108, 105, ‘78’);
– INSERT INTO score VALUES (101, 166, ‘85’);
– INSERT INTO score VALUES (107, 106, ‘79’);
– INSERT INTO score VALUES (108, 166, ‘81’);
– COMMIT;

Enquiry case

– 1. Query the name, gender and Class columns of all records in the Student table.
– Select name ,gender, class from student;

– 2. Query the records of students whose surname is not "Wang" in the Student table.
– Select * from student where name not like'王%';
–% can match any number of characters. _ Self can configure one

– 3. Query all records in the Student table.
– Select * from student;

– 4. Query all records in the Score table whose scores are between 60 and 80.
– Select * from score where grade between 60 and 80;
– select * from score where grade >= 60 and grade <= 80;

– 5. Query the records with scores of 83, 82 or 86 in the Score table.
– Select * from score where grade in (83,82,86);

– 6. Query the records of class "95031" or classmates whose gender is "female" in the Student table.
– Select * from
– student
– where
– class = 95031 or gender ='女';

– 7. Query all records in the Student table in descending order of Class.
– Select * from student ORDER BY class desc;

– 8. Query all records in the Score table in ascending order of courseid and descending grade of grade.
– Select * from score ORDER BY courseid asc, grade desc;

– 9. Query the number of students in the "95031" class.
– Select count(*) from student where class = 95031

– 10. Query the student number and course number of the highest-scoring student in the Score table.
– Select * from score where grade = (select max(grade)from score);

– 11. Query the average score of No. 105 course.
– Select avg(grade) from score where courseid = 105;

– 12. Query the average scores of courses that have at least 5 electives and start with 1 in the Score table.
– Select * from score where courseid like '2%' and (select count(*) from score where courseid like '2%')> 2

– 13. Query the studentid column with scores between 60-85 for each subject.
– Select studentid from score where grade BETWEEN 60 and 85;

– 14. Query the name, courseid and grade columns of all students. (Two tables joint check)
– select st.name, s.courseid, s.grade from student st, score s where st.id=s.courseid;

– 15. Query the id, course name and grade columns of all students.
– Select s.id, c.name,ss.grade
– from
– student s,course c,score ss
– where
– s.id = ss.studentid
– and
– c.id = ss.courseid

– 16. Query the name, course name and grade columns of all students. (Columns with duplicate names)
– select s.name, c.name as aa, g.grade
– from
– student s,course c, score g
– where
– s.id = g.studentid
– and
– c.id = g.courseid

– 17. Query the average score of the selected courses of the "95033" class. The execution order of select and having
– select s.courseid ,avg(s.grade)
– from
– score s
– inner join student stu
– on stu.id = s.studentid
– where stu.class = 95033
– GROUP BY s.courseid

– SELECT ss.courseid ,avg(ss.grade)
– from score ss,student stu
– where stu.id=ss.studentid and stu.class = 95033
– GROUP BY ss.courseid

– 18. Suppose that a gradeRank table is created using the following command: the lowest score, the highest score, and the grade
– create table gradeRank (low int(3),upp int(3),rank char(1));
– insert into gradeRank values (90,100,'A');
– insert into gradeRank values(80,89,'B');
– insert into gradeRank values(70,79,'C');
– insert into gradeRank values(60,69,'D ');
– insert into gradeRank values(0,59,'E');
– Now query the id, course id and rank columns of all students. (Assuming a student's score is 85, then his rank is B)

– select s.Sno,sc.Cno,sc.Degree,
– CASE
– WHEN sc.Degree>=90 and sc.Degree<=100 THEN ‘A’
– WHEN sc.Degree>=80 AND sc.Degree<=89 THEN ‘B’
– WHEN sc.Degree>=70 AND sc.Degree<=79 THEN ‘C’
– WHEN sc.Degree <=60 and sc.Degree<=69 THEN ‘D’
– ELSE ‘E’
– END as “rank”
– from student s
– inner join score sc
– on sc.Sno =s.sno

– 19. Query the records of all students whose grades of elective course 105 are higher than those of student No. 109.
– Select stu.* from
– student stu ,score s
– where
– stu.id = s.studentid and s.courseid =105
– and s.grade> (select grade from score where studentid = 109 and courseid =105)

– 20. Query the record of the scores of students who took more than one course in the score that are not the highest scores.
– Select * from
– score a
– where
– a.studentid in(select studentid from score GROUP BY studentid HAVING count(*)> 1)
– and
– a.grade <(select max(grade) from score b where a.studentid =b.studentid)

– 21. Query all records whose scores are higher than those with student ID "109" and course ID 105.
– Select stu. ,s. from
– student stu,score s
– where
– stu.id = s.studentid
– and s.grade> (select grade from score where studentid=109 and courseid=105)

– 22. The query and student number are the id, name and birthday columns of the 108 students born in the same year.
– Select id,name,birthday from student
– where
– YEAR(birthday) = (select YEAR(birthday) from student where id = 108)
– and
– id != 108;

– 23. Inquire about the grades of the students who are taught by the teacher "Xiang Xiaofang".
– SELECT * FROM score WHERE courseid = (
– select id from course
– WHERE
– teacherid = (
– SELECT id
– from teacher where name='向小芳'))

– 24. Query the names of teachers with more than 5 students taking a course.
– SELECT t. nameFROM teacher t, course c ,score s WHERE t.id=c.teacherid
– and
– c.id = s.courseid
– and
– s.courseid in (
– SELECT courseid FROM score GROUP BY courseid HAVING COUNT( courseid)> 2)
– GROUP BY t.name

– 25. Inquire about the information of all students in classes 95033 and 95031.
– SELECT * FROM student WHERE class=95033 or class = 95031

– 26. Query the course id that has scores of 85 or more. (Query all the course ids with scores greater than 85)
– SELECT courseid FROM score where grade> 85;

– 27. Query the transcripts of the courses taught by the teachers of the Department of Computer Science. –105
– SELECT * FROM score
– WHERE
– courseid
– in(
– SELECT id FROM course WHERE teacherid in (SELECT id FROM teacher where department ='Computer Department'))

– 28. Query the names and titles of teachers with different titles in the "Computer Department" and "Electronic Engineering Department". (I have it in my department, you don’t have it, you have it, I don’t have it)
– SELECT name ,title FROM teacher where department='Computer Department' and title not in (SELECT title FROM teacher WHERE department='Electronic Engineering Department')
– UNION
– SELECT name ,title FROM teacher where department='Electronic Engineering Department' and title not in (SELECT title FROM teacher WHERE department='Computer Department')

– 29. Query the id, course id and grade of the students whose elective number is 105 and whose grades are at least higher than 85, and sort by grade from high to low.
– SELECT * FROM score WHERE courseid = 105 AND grade> 85 ORDER BY grade desc

– 30. Query the name, sex and birthday of all teachers and classmates.
– SELECT name, gender, birthday FROM student
– UNION
– SELECT name, gender, birthday FROM teacher

– 31. Query the name, sex and birthday of all "female" teachers and "female" classmates.
– SELECT name,gender,birthday FROM student WHERE gender='女'
– UNION
– SELECT name,gender,birthday FROM teacher WHERE gender= 'Female'

– 32. Query the transcripts of students whose grades are lower than the average grade of the course.
– SELECT * FROM score ss WHERE grade <(
– SELECT AVG(grade) FROM score s WHERE ss.courseid=s.courseid) ORDER BY grade DESC

– SELECT a.* FROM
– score a,(SELECT AVG(grade) as grades , courseid FROM score GROUP BY courseid) b
– WHERE
– a.courseid = b.courseid
– and a.grade < b.grades
– ORDER BY a.grade DESC

– 33. Query the name and Department of all teachers in the course .
– SELECT t. name,t.department FROM course c ,teacher t
– where c.teacherid=t.id

– 34 Query the name and Department of all teachers who have not lectured.
– SELECT * FROM teacher WHERE id not in (SELECT teacherid FROM course)

– 35. Check the class numbers of at least 2 boys.
– SELECT class FROM student WHERE gender='Male' GROUP BY class HAVING count(gender)> 1

– 36. Inquire about "male" teachers and their courses.
– SELECT t.* FROM teacher t,course c WHERE t.gender='男' and t.id = c.teacherid

– 37. Query all units of the teacher, that is, the unique department column.
– SELECT DISTINCT department FROM teacher

– 38. Query the largest and smallest birthday date values ​​in the Student table.
– SELECT MAX(birthday),MIN(birthday) FROM student

– 39. Query the transcripts of all "male" students who took the "Introduction to Computer" course
– SELECT s.grade
– FROM
– score s, student stu, course c
– WHERE
– stu.id = s.studentid
– and
– c. id = s.courseid
– and
– stu.gender='male'
– and
– c. name='Introduction to Computers'

– 40. Query all records in the Student table in descending order of class number and age. (Note the order of the two sorts)
– SELECT * FROM student ORDER BY class desc, birthday desc

– 41 Query the names of classmates of the same gender as "梁前磊" and in the same class.
SELECT nameFROM student
WHERE
gender=(SELECT gender FROM student WHERE name='梁前磊')
and
class=(SELECT class FROM student WHERE name='梁前磊')
and
name != 'Liang Qianlei'

Guess you like

Origin blog.csdn.net/shuai_ge_feng/article/details/107298983