Student (s #, sname, sage, ssex) Student table
Course (C #, cname, T #) Curriculum
SC (s #, C #, score) Orders table
Teacher (T #, tname) Instructor table
Problem:
1. query the student IDs of all students whose "course scores are higher than" course scores;
Select a. s # From (select s #, score from SC where C # = '001'),
(Select s #, score from SC where C # = '002 ') B
Where a. score> B. score and a. s # = B. S #;
2. query the student ID and average score of students whose average score is greater than the score;
Select s #, AVG (score)
From SC
Group by S # Having AVG (score)> 60;
3. query the student ID, name, number of course selections, and total score of all students;
SELECT Student. S #, Student. Sname, count (SC. C #), sum (score)
FROM Student left Outer join SC on Student. S # = SC. S #
Group by Student. S #, Sname
4. query the number of teachers surnamed "Li;
SELECT count (distinct (Tname ))
FROM Teacher
WHERE Tname like 'Li % ';
5. query the student ID and name of the student who has not learned the course "ye ping;
SELECT Student. S #, Student. Sname
FROM Student
Where s # Not in (select distinct (SC. s #) from SC, course, teacher where SC. C # = course. C # and teacher. T # = course. T # and teacher. tname = 'peiping ');
6. query the student ID and name of the student who has learned the course "" and has also learned the course number;
Select student. s #, student. sname from student, SC where student. s # = SC. s # And SC. C # = '001' and exists (select * from SC as SC _2 where SC _2.s # = SC. s # And SC _2.c # = '002 ');
7. query the student ID and name of all students who have learned the course taught by instructor ye Ping;
Select s #, sname
From student
Where s # In (select s # from SC, course, teacher where SC. C # = course. C # and teacher. T # = course. T # and teacher. tname = 'peiping 'group by S # Having count (SC. C #) = (select count (C #) from course, teacher where teacher. T # = course. T # And tname = 'peiping '));
8. query the student ID and name of all students whose score is lower than the course number;
Select s #, Sname FROM (SELECT Student. S #, Student. sname, score, (SELECT score from SC SC _2 WHERE SC _2.S # = Student. S # AND SC _2.C # = '002 ') score2
FROM Student, SC where Student. S # = SC. S # AND C # = '001') S_2 WHERE score2 <score;
9. query the student ID and name of all students whose scores are less than the score;
Select s #, Sname
FROM Student
Where s # not in (SELECT Student. S # FROM Student, SC where S.S # = SC. S # AND score> 60 );
10. query the student ID and name of the student who has not completed all the courses;
SELECT Student. S #, Student. Sname
FROM Student, SC
WHERE Student. S # = SC. S # GROUP BY Student. S #, Student. Sname having count (C #) <(SELECT count (C #) FROM Course );
11. query the student ID and name of at least one course and the student whose student ID is;
Select s #, Sname FROM Student, SC where Student. S # = SC. S # and c # in select c # from SC WHERE S # = '123 ';
12. query the student ID and name of at least one student whose student ID is;
SELECT distinct SC. S #, Sname
FROM Student, SC
WHERE Student. S # = SC. S # and c # in (select c # from SC WHERE S # = '001 ');
13. Change the average score of the Course taught by instructor ye Ping in the SC table;
Update SC set score = (SELECT avg (SC _2.score)
From SC SC _2
WHERE SC _2.C # = SC. C #) FROM Course, Teacher WHERE Course. C # = SC. C # AND Course. T # = Teacher. T # AND Teacher. tname = 'peiping ');
14. query the student ID and name of other students whose courses are exactly the same as those;
Select s # from SC where c # in (select c # from SC WHERE S # = '123 ')
Group by s # having count (*) = (SELECT count (*) from SC WHERE S # = '123 ');
15. Delete the SC table record for learning "ye ping;
Delect SC
FROM course, Teacher
WHERE Course. C # = SC. C # AND Course. T # = Teacher. T # AND Tname = 'peiping ';
16. insert some records into the SC table. These records must meet the following requirements: Student ID ,,
Average score of course no;
Insert SC select s #, '002 ', (select AVG (score)
From SC where C # = '002') from student where s # Not in (select s # from SC where C # = '002 ');
17. Show the "Database", "enterprise management", and "English" course scores of all students based on average scores in the following format: Student ID ,, database, enterprise management, English, number of valid courses, average effective score
Select s # As student ID
, (Select score from SC where SC. s # = T. S # And C # = '004 ') as database
, (Select score from SC where SC. s # = T. S # And C # = '001') as Enterprise Management
, (SELECT score from SC where SC. S # = t. S # AND C # = '006 ') AS English
, COUNT (*) AS valid course COUNT, AVG (t. score) AS average score
From SC AS t
Group by s #
Order by avg (t. score)
18. query the highest score and lowest score of each subject: displayed as follows: course ID, highest score, lowest score
Select l. C # As course ID, L. score AS highest score, R. score AS lowest score
From SC l, SC AS R
Where l. C # = R.C # AND
L. score = (select max (IL. score)
From SC AS IL, Student AS IM
Where l. C # = IL. C # and im. S # = IL. S #
Group by il. C #)
AND
R. Score = (select min (IR. score)
FROM SC AS IR
WHERE R.C # = IR. C #
Group by ir. C #
);
19. The average score of each subject ranges from high to high and the pass rate.
SELECT t. C # AS course number, max (course. Cname) AS course name, isnull (AVG (score), 0) AS average score
, 100 * SUM (case when isnull (score, 0)> = 60 THEN 1 ELSE 0 END)/COUNT (*) AS pass percentage
From SC T, Course
WHERE t. C # = course. C #
Group by t.c #
Order by 100 * SUM (case when isnull (score, 0)> = 60 THEN 1 ELSE 0 END)/COUNT (*) DESC
20. query the average score and pass percentage of the following courses (shown in "1 line"): Enterprise Management (), Marx (), OO & UML (), Database ()
Select sum (case when c # = '001' THEN score ELSE 0 END)/SUM (case c # WHEN '001' THEN 1 ELSE 0 END) AS average score of Enterprise Management
, 100 * SUM (case when c # = '001' AND score> = 60 THEN 1 ELSE 0 END) /SUM (case when c # = '001' THEN 1 ELSE 0 END) AS enterprise management pass percentage
, SUM (case when c # = '002' THEN score ELSE 0 END)/SUM (case c # WHEN '002' THEN 1 ELSE 0 END) AS Marx average score
, 100 * SUM (case when c # = '002 'AND score> = 60 THEN 1 ELSE 0 END) /SUM (case when c # = '002' THEN 1 ELSE 0 END) AS Marx pass percentage
, SUM (case when c # = '003 'THEN score ELSE 0 END)/SUM (case c # WHEN '003' THEN 1 ELSE 0 END) as uml average score
, 100 * SUM (case when c # = '003 'AND score> = 60 THEN 1 ELSE 0 END) /SUM (case when c # = '003 'THEN 1 ELSE 0 END) as uml pass percentage
, SUM (case when c # = '004 'then score ELSE 0 END)/SUM (case c # WHEN '004 'then 1 ELSE 0 END) AS database average score
, 100 * SUM (case when c # = '004 'AND score> = 60 THEN 1 ELSE 0 END) /SUM (case when c # = '004 'THEN 1 ELSE 0 END) AS database pass percentage
FROM SC
21. query the average scores of different courses taught by different teachers from high to low.
SELECT max (Z. T #) AS instructor ID, MAX (Z. tname) AS instructor name, C. C # AS course ID, MAX (C. cname) AS course name, AVG (Score) AS average Score
From SC AS T, Course AS C, Teacher AS Z
WHERE T.C # = C. C # and c. T # = Z. T #
Group by c. C #
Order by avg (Score) DESC
22. query the transcript of the following course scores of 3rd to 6th students: Enterprise Management (), Marx (), UML (), Database ()
[Student ID], [Student name], enterprise management, Marx, UML, database, average score
Select distinct top 3
SC. S # As student ID,
Student. Sname AS Student name,
T1.score AS enterprise management,
T2.score AS Marx,
T3.score as uml,
T4.score AS database,
Isnull (t1.score, 0) + isnull (t2.score, 0) + isnull (t3.score, 0) + isnull (t4.score, 0) as total score
From student, SC left join SC as T1
On SC. s # = t1.s # And t1.c # = '001'
Left join SC as T2
On SC. s # = t2.s # And t2.c # = '002'
Left join SC as T3
On SC. s # = t3.s # And t3.c # = '003'
Left join SC as T4
On SC. s # = t4.s # And t4.c # = '004'
Where student. s # = SC. s # And
Isnull (t1.score, 0) + isnull (t2.score, 0) + isnull (t3.score, 0) + isnull (t4.score, 0)
Not in
(Select
Distinct
Top 15 with ties
Isnull (t1.score, 0) + isnull (t2.score, 0) + isnull (t3.score, 0) + isnull (t4.score, 0)
From SC
Left join SC as T1
On SC. s # = t1.s # And t1.c # = 'k1'
Left join SC as T2
On SC. s # = t2.s # And t2.c # = 'k2'
Left join SC as T3
On SC. s # = t3.s # And t3.c # = 'k3'
Left join SC as T4
On SC. s # = t4.s # And t4.c # = 'k4'
Order by isnull (T1.score, 0) + ISNULL (T2.score, 0) + ISNULL (T3.score, 0) + ISNULL (T4.score, 0) DESC );
23. Print the score of each subject in Statistics. Number of students in each score segment: course ID, course name, [100-85], [85-70], [70-60], [<60]
Select SC. C # as course ID, Cname as course name
, SUM (case when score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS [100-85]
, SUM (case when score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) AS [85-70]
, SUM (case when score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS [70-60]
, SUM (case when score <60 THEN 1 ELSE 0 END) AS [60-]
From SC, Course
Where SC. C # = Course. C #
Group by SC. C #, Cname;
24. query the average score and rankings of Students
SELECT 1 + (select count (distinct average score)
FROM (select s #, AVG (score) AS average score
FROM SC
Group by s #
) AS T1
WHERE average score> T2. average score) as ranking,
S # as student ID, average score
FROM (select s #, AVG (score) average score
FROM SC
Group by s #
) AS T2
Order by average score desc;
25. query the top three records of each subject's score (excluding the parallel score)
SELECT t1.S # as student ID, t1.C # as course ID, Score as Score
From SC t1
WHERE score IN (select top 3 score
FROM SC
WHERE t1.C # = C #
Order by score DESC
)
Order by t1.C #;
26. query the number of students selected for each course
SELECT c #, count (S #) FROM SC GROUP BY C #;
27. Check the student ID and name of all students who have selected only one course.
Select SC. S #, Student. Sname, count (C #) AS Course selections
From SC, Student
Where SC. S # = Student. S # group by SC. S #, Student. Sname having count (C #) = 1;
28. query the number of boys and girls
SELECT count (Ssex) as boys FROM Student group by Ssex having Ssex = 'male ';
SELECT count (Ssex) as number of girls FROM Student group by Ssex having Ssex = 'female ';
29. query the student list with the last name "Zhang"
SELECT Sname FROM Student WHERE Sname like 'sheet % ';
30. query the list of same-name students and count the number of students with the same name
SELECT Sname, count (*) FROM Student group by Sname having count (*)> 1 ;;
31. List of students born on the Year (note: the type of the Sage column in Student table is datetime)
SELECT Sname, CONVERT (char (11), DATEPART (year, Sage) as age
FROM student
Where convert (char (11), DATEPART (year, Sage) = '123 ';
32. query the average scores of each course. The results are sorted in ascending order based on the average scores. The average scores are the same and the course numbers are sorted in descending order.
Select c #, Avg (score) from SC GROUP BY C # ORDER BY Avg (score), C # DESC;
33. query the student ID, name, and average score of all students whose average score is greater
SELECT Sname, SC. S #, avg (score)
FROM Student, SC
WHERE Student. S # = SC. S # group by SC. S #, Sname having avg (score)> 85;
34. query the names and scores of students whose course names are "databases" and whose scores are lower
SELECT Sname, isnull (score, 0)
FROM Student, SC, Course
Where SC. S # = Student. S # and SC. C # = Course. C # AND Course. Cname = 'database' and score <60;
35. query the Course selections of all students;
Select SC. s #, SC. C #, sname, cname
From SC, student, Course
Where SC. s # = student. s # And SC. C # = course. C #;
36. query the name, course name, and score of any course score plus the score;
Select distinct student. s #, student. sname, SC. C #, SC. Score
From student, SC
Where SC. score> = 70 and SC. s # = student. s #;
37. query failed courses and arrange them in ascending order by course number
Select C # from SC where SCOR e <60 order by C #;
38. query the student ID and name of the student whose course number is higher than the score;
Select SC. S #, Student. Sname from SC, Student where SC. S # = Student. S # AND Score> 80 AND C # = '003 ';
39. Number of students selected for the course
SELECT count (*) FROM SC;
40. query the names of the students with the highest scores and their scores of the students who take the courses taught by "ye ping ".
SELECT Student. Sname, score
FROM Student, SC, Course C, Teacher
WHERE Student. S # = SC. S # and SC. C # = C. C # and c. T # = Teacher. T # AND Teacher. tname = 'peiping 'and SC. score = (SELECT max (score) from SC WHERE C # = C. C #);
41. query each course and the number of optional students
SELECT count (*) FROM SC GROUP BY C #;
42. query the student's student ID, course number, and student score with the same course score
SELECT distinct A.S #, B. score from SC a, SC B where a. Score = B. Score and a. C # <> B .C #;
43. query the first two of the best scores for each course
SELECT t1.S # as student ID, t1.C # as course ID, Score as Score
From SC t1
WHERE score IN (select top 2 score
FROM SC
WHERE t1.C # = C #
Order by score DESC
)
Order by t1.C #;
44. count the number of students in each course (only when the number of students exceeds the number of students ). The course number and number of electives must be output. The query results are sorted in descending order of the number of students. If the number of students is the same, the query results are sorted in ascending order of the course number.
Select c # as course number, count (*) as students
FROM SC
Group by C #
Order by count (*) desc, c #
45. Search for student IDs that take at least two courses
Select s #
FROM SC
Group by s #
Having count (*)> = 2
46. query the course number and name of all optional courses for all students
Select c #, Cname
FROM Course
Where c # in (SELECT c # FROM SC group by c #)
47. query the names of students who have not completed any course taught by instructor ye Ping.
SELECT Sname FROM Student where s # not in (select s # FROM Course, Teacher, SC WHERE Course. T # = Teacher. T # and SC. C # = course. C # AND Tname = 'peiping ');
48. query the student ID and average score of two or more failed courses
Select s #, avg (isnull (score, 0) from SC where s # in (select s # from SC WHERE score <60 GROUP BY S # having count (*)> 2) group by s #;
49. Search for students whose scores are less than "" and sorted in descending order by scores
Select s # from SC WHERE C # = '004 'and score <60 ORDER BY score desc;
50. Delete the score of "" course of "" student"
Delete FROM SC WHERE S # = '001' and C # = '001 ';