#创建表
CREATE TABLE Student #学生表
(
Sno varchar () NOT NULL primary key,
Sname varchar () NOT NULL,
Ssex varchar () NOT NULL,
Sbirthday datetime,
Class varchar (20)
);
CREATE TABLE Teacher #教师表
(
Tno varchar () NOT NULL primary key,
Tname varchar () NOT NULL,
Tsex varchar () NOT NULL,
Tbirthday datetime,
Prof varchar (20),
Depart varchar () NOT NULL
);
CREATE TABLE Course #课程表
(
Cno varchar () NOT NULL primary key,
Cname varchar () NOT NULL,
Tno varchar () NOT NULL,
Foreign KEY (Tno) references Teacher (Tno)
);
CREATE TABLE Score #成绩表
(
IDS int Auto_increment primary KEY,
Sno varchar () NOT NULL,
Foreign KEY (Sno) references Student (Sno),
Cno varchar () NOT NULL,
Foreign KEY (Cno) references Course (Cno),
Degree Decimal (4,1)
);
#添加数据
#Student学生表
INSERT into Student values (' 108 ', ' Zeng Hwa ', ' Male ', ' 1977-09-01 ', ' 95033 ');
INSERT into Student values (' 105 ', ' Kuanming ', ' Male ', ' 1975-10-02 ', ' 95031 ');
INSERT into Student values (' 107 ', ' Wang Li ', ' female ', ' 1976-01-23 ', ' 95033 ');
INSERT into Student values (' 101 ', ' Li June ', ' male ', ' 1976-02-20 ', ' 95033 ');
INSERT into Student values (' 109 ', ' Wang Fang ', ' female ', ' 1975-02-10 ', ' 95031 ');
INSERT into Student values (' 103 ', ' contacts ', ' Male ', ' 1974-06-03 ', ' 95031 ');
#Teacher教师表
INSERT into Teacher values (' 804 ', ' sung ', ' Male ', ' 1958-12-02 ', ' associate Professor ', ' computer Department ');
INSERT into Teacher values (' 856 ', ' Zhang Xu ', ' Male ', ' 1969-03-12 ', ' lecturer ', ' Electronic Engineering Department ');
INSERT into Teacher values (' 825 ', ' Wang Ping ', ' female ', ' 1972-05-05 ', ' ta ', ' computer Department ');
INSERT into Teacher values (' 831 ', ' Liu Bing ', ' female ', ' 1977-08-14 ', ' ta ', ' Electronic Engineering Department ');
#Course课程表
INSERT into Course values (' 3-105 ', ' Introduction to Computer ', ' 825 ');
INSERT into Course values (' 3-245 ', ' operating system ', ' 804 ');
INSERT into Course values (' 6-166 ', ' digital circuit ', ' 856 ');
INSERT into Course values (' 9-888 ', ' Advanced math ', ' 831 ');
#Score成绩表
INSERT into score values (' ', ' 103 ', ' 3-245 ', ' 86 ');
INSERT into score values (' ', ' 105 ', ' 3-245 ', ' 75 ');
INSERT into score values (' ', ' 109 ', ' 3-245 ', ' 68 ');
INSERT into score values (' ', ' 103 ', ' 3-105 ', ' 92 ');
INSERT into score values (' ', ' 105 ', ' 3-105 ', ' 88 ');
INSERT into score values (' ', ' 109 ', ' 3-105 ', ' 76 ');
INSERT into score values (' ', ' 101 ', ' 3-105 ', ' 64 ');
INSERT into score values (' ', ' 107 ', ' 3-105 ', ' 91 ');
INSERT into score values (' ', ' 108 ', ' 3-105 ', ' 78 ');
INSERT into score values (' ', ' 101 ', ' 6-166 ', ' 85 ');
INSERT into score values (' ', ' 107 ', ' 6-166 ', ' 79 ');
INSERT into score values (' ', ' 108 ', ' 6-166 ', ' 81 ');
#查询
1. Query the sname, Ssex, and class columns of all records in the student table.
Select Sname,ssex,class from Student
2, the inquiry teacher all units namely does not duplicate depart column.
Select distinct depart from Teacher
3. Query all records of student table.
SELECT * FROM Student
4. Query all records from 60 to 80 of the scores in the score table.
SELECT * from score where degree between and 80
5. Check the record of 85, 86 or 88 in the score table.
SELECT * from score where degree in (85,86,88)
6, inquires the student table "95031" class or the sex is "the female" the classmate record.
SELECT * from Student where class= ' 95031 ' or ssex= ' female '
7. Query the student table for all records in descending order of class.
SELECT * from Student ORDER BY Class Desc
8, in CNO Ascending, Degree descending query score all records of the table.
SELECT * FROM score ORDER BY cno,degree Desc
9. Check the number of students in "95031" class.
Select SUM (class=95031) from Student
10. Check the student number and course number of the highest score in the score table. (sub-query or sort)
Select Sno,cno from score where degree= (select Max (degree) from score)
11. Check the average score of each course.
Select AVG (degree) from score Group by Cno;
12. Check the average score of the course with at least 5 students enrolled in the score table and begin with 3.
Select AVG (degree) from score Group by Cno have Count (*) >=5 and Cno like ' 3% '
13, the query score is greater than 70, less than 90 of the SNO column.
Select Sno from score where degree>70 and degree<90
14. Sname, CNO and degree columns for all students are queried.
Select Sname from Student
Union
Select Cno from Course
Union
Select degree from Score
15. Check the SNO, CNAME and degree columns of all students.
Select Sno from Student
Union
Select Cname from Course
Union
Select degree from Score
16. Check the sname, CNAME and degree columns of all students.
Select Sname from Student
Union
Select Cname from Course
Union
Select degree from Score
17. Check the average score of "95033" class students.
Select AVG (degree) from score Group by cno=95033;
18. Assume that 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 ');
The SNO, CNO and rank columns of all students are now queried.
Select Sno from Student
Union
Select Cno from Course
Union
Select rank from grade
19, the query elective "3-105" course performance is higher than the "109" student scores of all the students record.
SELECT * FROM score where cno= ' 3-105 ' and degree> (select Max (degree) from score where sno= ' 109 ')
20, the inquiry score to choose to learn many courses of the students score is the record of non-highest scores.
21, the results of the inquiry is higher than the number of "109", the course number is "3-105" of all records.
SELECT * FROM score where cno= ' 3-105 ' and degree> (select Max (degree) from score where sno= ' 109 ')
22. The SNO, sname and Sbirthday of all students who were born in the same year were queried and studied for 108.
Select Sno,sname,sbirthday from Student where sbirthday= (select Sbirthday from Student where sno= ' 108 ')
23, inquires "Zhang Xu" the Teacher classroom student achievement.
Select degree from score where cno= (select Cno from Course where tno= (select Tno from Teacher where Tname= ' Zhang Xu '))
24. The name of the teacher who has more than 5 students who have enrolled in a course.
Select Tname from teacher where tno= (select Tno from Course where cno= (select Cno from Score GROUP by Cno have count (* ) >5))
25. Check the records of all students in class 95033 and 95031.
SELECT * from Student where class= "95033" or 95031
26, the inquiry existence has 85 points above the course CNO.
Select Cno from score where degree>85
27. Find out the results table of the "computer Department" teacher teaching course.
Select degree from Teacher,course,score where teacher.depart= ' computer system ' and Teacher.tno=course.tno and Course.cno=score.cno
28, query "computer department" and "Electronic Engineering department" different titles of teachers Tname and Prof.
29, the inquiry elective number is "3-105" course and the result is at least higher than the elective number "3-245" classmate's CNO, Sno and degree, and according to degree from high to low order.
Select Cno,sno,degree from score where cno= ' 3-105 ' and degree> (select Max (degree) from score where cno= ' 3-245 ') Order B Y degree desc
30, inquires the elective number is "3-105" and the result is higher than the elective number is "3-245" the course schoolmate's CNO, Sno and degree.
Select Cno,sno,degree from score where cno= ' 3-105 ' and degree> (select Max (degree) from score where cno= ' 3-245 ')
31. Check the name, sex and birthday of all teachers and classmates.
Select Sname,ssex,sbirthday from Student
Union
Select Tname,tsex,tbirthday from Teacher
32, query All "female" teacher and "female" classmate's name, Sex and birthday.
Select sname,ssex,sbirthday from Student where ssex= ' woman '
Union
Select tname,tsex,tbirthday from Teacher where tsex= ' woman '
33. Check the scores of students who have a lower average score than the course.
Select degree from score where degree< (select AVG (degree) from score)
34. Check the Tname and depart of all classroom teachers.
Select Tname,depart from Teacher where Tno in (select Tno from Course where Cno in (select Cno from Score Group by Cno))
35. Inquire about the Tname and depart of all teachers who have not lectured.
Select Tname,depart from Teacher where Tno in (select Tno from Course where Cno not in (select Cno from Score Group by Cno))
36. Check the class number of at least 2 boys.
Select class from Student where ssex= ' Man ' GROUP by class has count (*) >=2
37, inquires the student table the surname "the king" the classmate record.
SELECT * from Student where Sname don't like ' King% '
38. Check the name and age of each student in the student table.
Select Sname,sbirthday from Student
39. Query the maximum and minimum sbirthday date values in the student table.
Select min (sbirthday), Min (sbirthday) from Student
40. Check all records in the student table in order of class number and age from large to small.
SELECT * from Student ORDER BY sbirthday,class Desc
41. Query "male" teachers and their courses.
Select Cno,tname from Course,teacher where tsex= ' man '
42. Check the SNO, CNO and degree columns of the students with the highest score.
Select Sno,cno,degree from score where degree= (select Max (degree) from score)
43, inquiries and "Li June" with the gender of all students sname.
Select Sname from Student where ssex= (select Ssex from Student where Sname= ' Li June ')
44, inquiries and "Li June" with the same sex and classmates sname.
Select Sname from Student where ssex= (select Ssex from Student where Sname= ' Li June ') and class= (select class from Student wher E sname= ' Li June ')
45. Check the scores of all the "male" students who have enrolled in the "Introduction to Computer" course.
Select degree from score where cno= (select Cno from Course where cname= ' Introduction to Computers ') and Sno in (select Sno from Student where ssex= ' Male ')
45 to database query questions