MySQL Query practice

student table

 

 teacher table

 

 course table

 

 score table

 

 Four of these tables some practice.

1: Query all student records in the table.

      select *from student;

2: Query student table name / sex / classid these columns.

      select  name,sex,classid from student;

3: Query teachers do not repeat units department.

      select distinct department from teacher;

      distinct column (s) are not duplicate records acquired

4: Query re-recorded between 60 and 80 score table results.

      select *from score where degree between 60 and 80;

5: Query score table record score of 85,86,88.

     select *from score where degree in (86,85,88);

6: Query student table for 95,031 class or gender girls.

      select *from student where classid=95031 or sex='女';

7: classid column to query all the records in descending student table.

     select *from student order by desc classid ;

8: courseid descending to ascending degree score output all records in the table.

    select *from score order by courseid asc, degree desc;

9: Query number 95031 class.

     select count(id) from student where classid=95031;

10: Query table record score in the highest score of.

       select *from score where degree=(select max(degree) from score);

      或: select id,courseid,degree order by degree desc limit 0,1;

11: The average number of queries for each course.

    select courseid,avg(degree) from score group by courseid;

12: Query score table at least two students to take an average of 3 points to the beginning of the course.

      select courseid, avg(degree) as average from score group by courseid having count(courseid)>1 and courseid like '3%';

13: id column query score larger than the score of 70 is less than 90.

        select  *from score where degree between 70 and 90;

14: Query student's name and degree columns and columns classid column (multi-table queries).

        select name,degree,classid from student,score where student.id=score.id

15: Query student's name and degree courses Course id columns (multi-table queries).

         select coursename,degree from score,course where course.courseid=score.courseid;

16: Query student's name and the corresponding results course name. (Three tables query).

       select  name,degree,coursename from student,course,score where score.id=student.id and score.courseid=course.courseid;

17: Query grade point average (subquery) 95 031 classes of students for each course.

        select avg(degree) from score where id in(select id from student where classid=95031) group by courseid;

18: Query student records elective courses than 109 classmates 3105 3105 course grade.

         select *from score where courseid=3105 and degree>(select degree from score where id=109 and courseid=3105);

19: Query scored higher than 109 school course record number of 3105 students.

       select *from score where  degree>(select degree from score where id=109 and courseid=3105);

20: number and school records check 101 students and 108 students were born in the same year.

       select name, brithday,classid from student where year(brithday) in (select year(brithday) from student where id=108 or id=101);

21: Query Zhang Xu teacher of the course student achievement.

        select *from score where courseid in (select courseid from course where teacherid=(select id from teacher where name='张旭'));

22: Query elective teacher excess of five records.

       select *from teacher where id in (select teacherid from course where courseid in (select courseid from score group by courseid having count(courseid)>5));

23: Query Record 95033 and 95031 of classmates

       select *from student where classid in (95033,95031);

24: Query score 88 points or more courses name.

       select coursename from course where courseid in(select courseid from score where degree>88);

25: Course student achievement query taught computer science teacher.

       select *from score where courseid in(select courseid from course where teacherid in(select id from teacher where department='计算机系'));

26: Query teachers of different titles of Computer Science and Electronic Engineering Record.

        select * from teacher where department = 'computer system' and professional not in (select professional from teacher where department = ' Electronic Engineering')
    -> Union
    -> select * from teacher where department = 'Electronic Engineering' and professional not in (select professional from teacher where department = ' computer system');

27: Query elective number for recording the results of the 3245 portion of elective courses students scored higher than 3105 courses at least.

        select *from score where courseid=3105  and degree> any(select degree from score where courseid=3245);

28: No. 3105 Query elective course grade is higher than the record results of 3245 all elective courses students.

        select *from score where courseid=3105  and degree> all(select degree from score where courseid=3245);

29: Query all teachers and students name, sex, brithday.

         select name,sex,brithday from student
    -> union
    -> select name,sex,brithday from teacher;

30: Query name sex brithday female teachers and students.

      select name,sex,brithday from student where sex='女'
    -> union
    ->  select name,sex,brithday from teacher where sex='女';

31: the result inquiry score lower than the average of the course students.

        select *from score a where degree<(select avg(degree) from score b where a.courseid=b.courseid);

32: Query classroom teacher's name and department.

         select *from teacher where id in(select teacherid from course where courseid in(select courseid from score group by courseid));

33: Query class in at least two boys in the class.

       select classid from student where sex='男' group by classid having count(classid)>1;

34: Query class is not surnamed Wang students.

         select *from student where name not like '王%';

35: Query all the student's name and age.

        select name,year(now())-year(brithday) as age from student;

36: Query the oldest and youngest students in the data.

        select max(year(now())-year(brithday)),min(year(now())-year(brithday)) from student;

37: The number and age classes in descending order of student queries all records in the table.

       select *from student order by classid,year(now())-year(brithday);

38: Query on the course by male teachers.

       select coursename from course where teacherid in (select  id from teacher where sex='男');

39: Query highest score information students.

      select *from score where degree =(select max(degree) from score);

40: Query and third place all students of the same gender.

      select *from student where sex=(select sex from student where name='季军');

41: information inquiry and runner-up of the same sex and the same class of students.

        select *from student where sex=(select sex from student where name='季军') and classid=(select classid from student where name='季军');

42: Query boys of all elective Introduction to Computer achievements.

       select *from score where courseid=(select courseid from course where coursename='计算机导论') and id in(select id from student where sex='男' );

 

 

       

 

Guess you like

Origin www.cnblogs.com/zhangyang4674/p/11604017.html