SQL database query exercises, after-school exercises

topic:

There is a database, including four tables: student table (Student), curriculum table (Course), grade table (Score) and teacher information table (Teacher)

Table (1) Student (student table)

attribute name type of data Can it be empty meaning
Sno varchar(20) no Student number (master code)
It takes off varchar(20) no student name
Ssex varchar(20) no student gender
Sbirthday datetime Can student's date of birth
Class varchar(20) Can student's class

Table (2) Course

attribute name type of data Can it be empty meaning
Cno varchar(20) no Course number (master code)
Cname varchar(20) no Course Title
Tno varchar(20) no Faculty number (external code)

Table (3) Score (score table)

attribute name type of data Can it be empty meaning
Sno varchar(20) no Student number (master code)
Cno varchar(20) no Course number (external code)
Degree Decimal Can score

Table (4) Student (teacher table)

attribute name type of data Can it be empty meaning
Tno varchar(20) no Faculty number (master code)
Tname varchar(20) no Faculty name
Czech varchar(20) no Faculty gender
Tbirthday datetime Can Faculty's date of birth
Prof varchar(20) Can job title
Depart varchar(20) no Faculty's department
  • 1. Query the Sname, Ssex and Class columns of all records in the Student table
   select Sname,Ssex,Class 
   from Student;
  • 2. Query all the units of the teacher, that is, the non-repeated Depart column
   select distinct Depart 
   from teacher;
  • 3. Query all records of the Student table
   select * 
   from Student; 
  • 4. Query all records with scores between 60 and 80 in the Score table
   select * 
   from Score 
   where Degree>60 and Degree<80;
  • 5. Query the records with scores of 85, 86 or 88 in the Score table
   select * 
   from score 
   where Degree=85 or Degree=86 or Degree=88;
  • 6. Query the "95031" class or the student records whose gender is "female" in the Student table
   select * 
   from student 
   where Class=95031 or Ssex='女';         
  • 7. Query all records of 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 Cno and descending order of Degree
   select * 
   from score 
   order by Cno asc ,Degree desc;
  • 9. Query the number of students in class "95031"
   select count(Class) as NumberofStudent 
   from student 
   where Class='95031';
  • 10. Query the student number and course number of the student with the highest score in the Score table. (subquery or sort)
   select Sno,Cno 
   from score 
   where Degree=(select max(Degree) from score);
  • 11. Query the average grade of each course
   select Cno,avg(Degree) 
   from score 
   group by Cno;     
  • 12. Query the average score of courses that have at least 5 students in the Score table and start with 3
   select Cno,avg(Degree) 
   from score 
   where Cno like '3%'group by Cno having count(Cno)>5;
  • 13. Query the Sno column whose score is greater than 70 and less than 90
   select Sno 
   from score 
   where degree between 70 and 90;
  • 14. Query the Sname, Cno and Degree columns of all students
   select Sname,Cno,Degree 
   from student 
   join score on student.Sno=score.Sno;
  • 15. Query the Sno, Cname and Degree columns of all students
   select Sno,Cname,Degree 
   from score 
   join course on course.Cno=score.Cno;
  • 16. Query the Sname, Cname and Degree columns of all students
   select Sname,Cname,Degree 
   from student 
   join score on student.Sno=score.Sno 
   join course on score.Cno=course.Cno;
  • 17. Query the average score of the students in class "95033"
   select avg(Degree) 
   from score 
   join student on student.Sno=score.Sno 
   group by Class having Class='95033';
  • 18. Suppose a grade table is created using the following command:
    create table grade(low int(3),upp int(3),rank char(1))
    insert into grade values(90,100,'A')
    insert into grade values( 80,89,'B')
    insert into grade values(70,79,'C')
    insert into grade values(60,69,'D')
    insert into grade values(0,59,'E')
    now query all Classmate's Sno, Cno and rank columns
   select Sno,Cno,`rank` 
   from grade 
   join score on score.Degree between grade.low and grade.upp;
  • 19. Query the records of all the students who have taken the "3-105" course and their grades are higher than those of the "109" students
   select * 
   from student,score 
   where  score.Cno='3-105'and student.Sno=Score.Sno 
   and score.Degree>(select Degree from score where Sno='109' 
   and score.Cno='3-105');

For the full version, please pay attention to the WeChat public account: coding gas station, get

  • 20. Query the records of the scores of the students who have chosen to take multiple courses in the score, which are not the highest score

  • 21. Query all records whose grades are higher than those whose student number is "109" and course number is "3-105"

  • 22. Query the Sno, Sname and Birthday columns of all students born in the same year as the student number 108

  • 23. Query the grades of students taught by teacher "Zhang Xu"

  • 24. Query the name of the teacher who has more than 5 students taking a course

  • 25. Query the records of all students in class 95033 and class 95031

  • 26. Query the Cno of courses with a score of 85 or more

  • 27. Check out the grades of the courses taught by the teachers of the "Computer Department"

  • 28. Query the Tname and Prof of teachers with different professional titles in "Department of Computer Science" and "Department of Electronic Engineering"

  • 29. Query the Cno, Sno, and Degree of the students whose elective number is "3-105" and whose grades are at least higher than the elective number "3-245", and sort by Degree from high to low

    1. Query the Cno, Sno and Degree of the students whose elective number is "3-105" and whose grades are higher than the elective number "3-245".
  • 31. Query the name, sex and birthday of all teachers and classmates

  • 32. Query the name, sex and birthday of all "female" teachers and "female" classmates

  • 33. Query the transcripts of students whose grades are lower than the average grade of the course

  • 34. Query the Tname and Depart of all teachers

  • 35. Query the Tname and Depart of all teachers who have not lectured

  • 36. Query the class number with at least 2 boys

  • 37. Query the records of students whose surname is not "Wang" in the Student table

  • 38. Query the name and age of each student in the Student table

  • 39. Query the maximum and minimum Sbirthday date values ​​in the Student table

  • 40. Query all records in the Student table in descending order of class number and age

  • 41. Query "male" teachers and their courses

  • 42. Query the Sno, Cno and Degree columns of the students with the highest scores

  • 43. Query the Sname of all students with the same gender as "Li Jun"

  • 44. Query the classmate Sname of the same gender and class as "Li Jun"

  • 45. Query the transcripts of all "male" students who have taken the "Introduction to Computer" course

Guess you like

Origin blog.csdn.net/pandas23/article/details/127280632