Problem description:
Create three tables to manage job business training information:
S (s #, Sn, SD, SA) s #, Sn, SD, and SA represent the student ID, Student name, organization, and student age respectively.
C (C #, CN) C #, CN represents the course number and Course name respectively.
SC (s #, C #, g) s #, C #, and G represent Student IDs, elective course numbers, and academic scores respectively.
1. Use standard SQL nested statements to query the student ID and name of the elective course named 'tax basics'
-- Implementation code:
Select Sn, SD from S
Where [s #] In (
Select [s #] from C, SC
Where C. [C #] = SC. [C #]
And Cn = n' tax basis ')
2. Use standard SQL nested statements to query the name and unit of the student whose elective course number is 'c2'
-- Implementation code:
Select S. Sn, S. SD from S, SC
Where S. [s #] = SC. [s #]
And SC. [C #] = 'c2'
3. Use standard SQL nested statements to query the names and units of students who do not select the course number as 'c5'
-- Implementation code:
Select Sn, SD from S
Where [s #] Not in (
Select [s #] from SC
Where [C #] = 'c5 ')
4. Use standard SQL nested statements to query the names and units of trainees who take all courses
Http://www.ad0.cn/netfetch/
-- Implementation code:
Select SN, SD FROM S
Where [S #] IN (
Select [S #] FROM SC
RIGHT JOIN
C on SC. [C #] = C. [C #] GROUP BY [S #]
Having count (*) = COUNT ([S #])
5. query the number of students who have selected the course
-- Implementation code:
Select students = COUNT (DISTINCT [S #]) FROM SC
6. query the student ID and organization of more than 5 Elective Courses
-- Implementation code:
Select SN, SD FROM S
Where [S #] IN (
Select [S #] FROM SC
Group by [S #]
Having count (DISTINCT [C #])> 5)
Question 2
Problem description:
Known link mode:
S (SNO, SNAME) Student Relationship. SNO is the student ID, and SNAME is the name
C (CNO, CNAME, CTEACHER) Course relationship. CNO is the course number, CNAME is the course name, And CTEACHER is the course teacher
SC (SNO, CNO, SCGRADE) Course Selection relationship. SCGRADE is the score
1. Find out the names of all students who have not taken the course taught by Mr. Li Ming.
-- Implementation code:
Select SNAME FROM S
Where not exists (
Select * from SC, C
Where SC. CNO = C. CNO
And cname = 'lilim'
And SC. SNO = S. SNO)
2. List the names and average scores of students whose two or more courses fail.
-- Implementation code:
Select S. SNO, S. SNAME, AVG_SCGRADE = AVG (SC. SCGRADE)
From s, SC ,(
Select SNO
FROM SC
Where SCGRADE <60
GROUP BY SNO
Having count (distinct cno)> = 2
) A Where S. SNO = A. sno and SC. SNO = A. SNO
Group by s. SNO, S. SNAME
3. List the names of all students who have learned course 1 and course 2
-- Implementation code:
Select S. SNO, S. SNAME
From s ,(
Select SC. SNO
From SC, C
Where SC. CNO = C. CNO
And c. cname in ('1', '2 ')
GROUP BY SNO
Having count (distinct cno) = 2
) SC Where S. SNO = SC. SNO
4. List the student IDs of all students whose scores are higher than those of students whose scores are equal to those of the course No.
-- Implementation code:
Select S. SNO, S. SNAME
From s ,(
Select SC1.SNO
From SC SC1, C C1, SC SC2, C C2
Where SC1.CNO = C1.CNO AND C1.NAME = '1'
AND SC2.CNO = C2.CNO AND C2.NAME = '2'
AND SC1.SCGRADE> SC2.SCGRADE
) SC Where S. SNO = SC. SNO
5. List the student IDs of all students whose scores are higher than those of Lesson 2 and their scores of Lesson 1 and Lesson 2
-- Implementation code:
Select S. SNO, S. SNAME, SC. [score of Lesson 1], SC. [score of Lesson 2]
From s ,(
Select SC1.SNO, [score for Lesson 1] = SC1.SCGRADE, [score for Lesson 2] = SC2.SCGRADE
From SC SC1, C C1, SC SC2, C C2
Where SC1.CNO = C1.CNO AND C1.NAME = '1'
AND SC2.CNO = C2.CNO AND C2.NAME = '2'
AND SC1.SCGRADE> SC2.SCGRADE
) SC Where S. SNO = SC. SNO