Create Database xuexiao; use xuexiao; create table sc (sno varchar(20), cno varchar(20), grade int); create table course (cno varchar(20), cname varchar(20), hours int); create table student (sno varchar(20), sname varchar(20), Ssex Char ( L0 ), sage int, sdept varchar(20));
INSERT INTO Student values ( " 9,512,101 ", "Li 1", "M", 19 "computer system"), ( " 9512102 ", "Liu Chen", "male", 20 "computer system"), ( " 9512103 ", "King 2", "women", 20 "computer system"), ( " 9512103 ", "Min", "female", 20 "computer system"), ( " 9521101 ", "Zhang 3", "men", 22 , "information system"), ( " 9521102 ", "Wu Bin," "female," 21 , "information system"), ( " 9521103 ," "Zhang 4," "male", 20 , "information system") ( " 9531101 ", "small money power", "female", 18 "mathematics"), ( " 9531102 ", "Wang", "men", 19 , "Department of Mathematics");
INSERT INTO Course, values ( "C01", "computer science culture", 70 ), ( "c02", "VB", 90 ), ( "C03", "computer network", 80 ), ( "C04", "Database base", 108 ), ( "C05", "mathematics", 180 [ ), ( "C06", "data structure", 72 ); insert into sc values("9512101","c01",90),("9512101","c02",86), ("9512101","c06",null),("9512102","c02",78), ("9512102","c04",66),("9521102","c01",82), ("9521102","c02",75),("9521102","c04",92), ("9521102","c05",50),("9521103","c02",68), ("9521103","c06",null),("9531101","c01",80), ("9531101","c05",95),("9531102","c05",85);
# Query scores highest number C01 course grade
select max(grade) from sc where cno="c01";
# Inquiry which students are enrolled in courses required courses listed No.
The SELECT the Cname the AS students take a class, Cno the AS course number the FROM Course the WHERE Cno the IN ( the SELECT the DISTINCT Cno the FROM SC); #DISTINCT used deduplication
# Statistics for each course and the number of Course exams highest score
The SELECT course.cno, CNAME, COUNT (SnO) the AS number of such course, MAX (Grade) the AS highest score the FROM Course the LEFT the JOIN SC the ON course.cno = sc.cno the GROUP BY sc.cno the ORDER BY course.cno;
# Number of statistics for each student elective door, according to ascending order of the number of elective door display results
SELECT student.sno, sname, COUNT (CNO) AS elective number of gates from Student left the Join SC ON student.sno = sc.sno Group by student.sno Order by elective number of gates;
# Queries grade point average and the number of elective elective door gate count of more than two students
SELECT student.sno, sname, AVG (Grade) AS grade point average, COUNT (CNO) AS elective number of gates from Student left the Join SC ON student.sno = sc.sno Group by student.sno HAVING elective gate count > 2 ;
# Query score 80 points more than the student's name, course number and grades, according to the results of the results in descending order
The SELECT Sname student's name, SC.Cno course number, SC.Grade score the FROM Student left the Join SC ON student.Sno = SC.Sno # left (inside) connection table SC query the WHERE SC.Grade > 80 the ORDER BY SC.Grade DESC ;
# Respectively query information system and computer science student's name, gender, Course Name, Course grades,
# and requires the two results merged into one result set,
# and department name to, name, sex, Course name, Course results appear in the order of the columns
The SELECT Sdept department name, Sname name, Ssex sex, course.Cname Course name, SC.Grade Course score the FROM Student Inner the Join SC ON student.Sno = SC.Sno Inner the Join Course, ON course.Cno = SC.Cno the WHERE Sdept = ' information system ' UNION the SELECT Sdept department name, Sname name, Ssex sex, course.Cname Course name, SC.Grade Course score the FROM Student Inner the Join SC ON student.Sno = SC.Sno Inner the Join Course, ON course.Cno= SC.Cno the WHERE Sdept = ' computer system ' ; select sdept,sname,ssex,course.cname,sc.grade from student left join sc on student.sno=sc.sno left join course on sc.cno=course.cno where sdept in ("信息系","计算机系") order by sdept;