Initial table structure and content
Contains three tables of Student, Course and SC (student information table, course information table and course selection result table respectively)
1.1 Student table structure
1.2 Course table structure
1.3 SC table structure
2.1 Student content
2.2 Course content
SELECT * FROM Course ORDER BY cast(Cno as int);
2.3 SC content
Insert data
INSERT INTO Student VALUES('201212126','WANGHUA','MALE',18,'CS');
delete data
DELETE FROM Course WHERE Cno = '11';
update data
UPDATE Course SET Cno = '8' WHERE Cno = '11';
Find data
1. Single table query
SELECT Sno, Sname FROM Student;
SELECT Sname, 2021-Sage FROM Student;
SELECT Sname NAME,
'Year of Birth:' BIRTH,
2021-Sage BIRTHDAY,
LOWER(Sdept) DEPARTMENT
FROM Student;
SELECT Sno FROM SC;
SELECT DISTINCT Sno FROM SC;
SELECT Sname FROM Student WHERE Sdept = 'CS';
SELECT Sname, Sage FROM Student WHERE Sage<20;
SELECT Sname, Sdept, Sage FROM Student WHERE Sage (NOT) BETWEEN 19 AND 20;
SELECT Sname, Sdept, Sage FROM Student WHERE Sage BETWEEN 19 AND 20;
(NOT)
SELECT Sname, Ssex FROM Student WHERE Sdept IN('CS','IS');
Fuzzy query
wildcard: _%
SELECT * FROM Student WHERE Sno LIKE '201212121';
SELECT * FROM Course WHERE Cno = '2';
SELECT * FROM Course WHERE Cno LIKE '2';
SELECT * FROM Course WHERE Cno LIKE '2%';
SELECT Sname FROM Student WHERE Sname LIKE 'LIU%';
SELECT Sname FROM Student WHERE Sname LIKE '___CHEN%';
INSERT INTO Course VALUES('9','DBADESIGN','',4);
SELECT * FROM Course WHERE Cname LIKE 'DB_DESIGN%';
SELECT * FROM Course WHERE Cname LIKE 'DB\_DESIGN%' ESCAPE'\';
Aggregate function
Total number of students
SELECT COUNT(*) FROM Student;
Average grade for elective 1
SELECT AVG(Grade) FROM SC WHERE Cno = '1';
Student's total credits for 201212121 elective courses
SELECT SUM(Ccredit) FROM SC,Course
WHERE Sno = '201212121' AND SC.Cno = Course.Cno;
GROUP BY
Each course number and corresponding number of courses
SELECT Cno, COUNT(Sno) FROM SC GROUP BY Cno;
Link query
The situation of each student and their elective courses
SELECT Student.*, SC.* FROM Student,SC WHERE Student.Sno = SC.Sno;
Natural connection
SELECT Student.Sno, Sname, Ssex, Sage, Sdept, Cno, Grade
FROM Student, SC WHERE Student.Sno = SC.Sno;
Student ID and name of students who take No. 2 course and have scores below 90 points
SELECT Student.Sno, Sname FROM Student, SC
WHERE Student.Sno = SC.Sno AND SC.Cno = '2' AND SC.Grade<=90;
Self-connection
Query the prerequisites of each course (comparison)
SELECT FIRST.Cno,SECOND.Cpno FROM Course FIRST, Course SECOND
WHERE FIRST.Cpno = SECOND.Cno;
SELECT Cno,Cpno FROM Course WHERE Cpno = Cno;
SELECT FIRST.Cno,FIRST.Cpno FROM Course FIRST,Course
WHERE FIRST.Cpno = FIRST.Cno;
Outer join
SELECT Student.Sno, Sname, Sname, Ssex, Sage, Sdept,Cno,Grade
FROM Student LEFT OUTER JOIN SC ON (Student.Sno = SC.Sno);
Multi-table join
Query each student's student ID, name, elective courses and grades
SELECT Student.Sno, Sname, Cname, Grade FROM Student,SC,Course
WHERE Student.Sno = SC.Sno AND SC.Cno = Course.Cno;
Nested query
The name of the student who selected course 2
SELECT Sname FROM Student WHERE Sno IN
(SELECT Sno FROM SC WHERE Cno = '2');
The number of the course for each student that exceeds the average grade of his own elective course
SELECT Sno, Cno FROM SC x
WHERE Grade >=(SELECT AVG(Grade) FROM SC y WHERE y.Sno = x.Sno);
Collection query
Computer science students and students younger than 19 years old
SELECT * FROM Student WHERE Sdept = 'CS'
UNION SELECT * FROM Student WHERE Sage<=19;
The intersection of computer science students and age 19 or younger
SELECT * FROM Student WHERE Sdept = 'CS'
INTERSECT SELECT * FROM Student WHERE Sage<=19;
The difference between a student in the Department of Computer Science and Technology and a student younger than 19 years old
SELECT * FROM Student WHERE Sdept = 'CS'
EXCEPT SELECT * FROM Student WHERE Sage<=19;
Query based on derived table
Find out the course number of each student that exceeds his own elective course average
SELECT Sno, Cno
FROM SC, (SELECT Sno, Avg(Grade) FROM SC GROUP BY Sno)
AS Avg_sc(avg_sno,avg_grade)
WHERE SC.Sno = Avg_sc.avg_sno AND SC.Grade >= Avg_sc.avg_grade;