0% found this document useful (0 votes)
8 views4 pages

Practice06 Sol

Uploaded by

littlen991
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Practice06 Sol

Uploaded by

littlen991
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CS3402 Practice 6:

1. Query the name of students who have studied ‘English’ course.


Answer:
SELECT
s_name
FROM
Student
WHERE
s_id IN (
SELECT
s_id
FROM
Sc S, Course C
WHERE
S.c_id = C.c_id and C.c_name = 'English'
);

2. Query the information of students who have studied the course with id ‘01’ but have not
studied the course with id ‘02’.
Answer:
SELECT
*
FROM
Student
WHERE
s_id IN (
SELECT
s_id
FROM
Sc
WHERE
c_id = '01'
)
AND s_id NOT IN (
SELECT
s_id
FROM
Sc
WHERE
c_id = '02'
);

3. Query the names of students who have not studied any course taught by teacher ‘ZHANG
San’.
Answer:

SELECT S.s_name
FROM Student S
WHERE
NOT EXISTS (
SELECT c_id
FROM Sc
WHERE Sc.s_id=S.s_id

INTERSECT

SELECT c_id
FROM Course C, Teacher T
WHERE C.t_id = T.t_id AND t_name = 'ZHANG San'
);

4. Query course ids and the number of students enrolling for each course.
Answer:
SELECT
c_id, COUNT(s_id)
FROM
Sc
GROUP BY
c_id;

5. Query the average score of each course, and sort the results in descending order of the
average score.
Answer:
SELECT
c_id, AVG(s_Sc)
FROM
Sc
GROUP BY
c_id
ORDER BY
AVG(s_Sc) DESC;

6. Query id and name of students who enroll only 2 courses.


Answer:
SELECT
s_id, s_name
FROM
Student
WHERE
s_id IN (
SELECT
s_id
FROM
Sc
GROUP BY
s_id
HAVING
COUNT(c_id) = 2
);

7. Query information of students who have taken all courses.


Answer:
SELECT
*
FROM
Student
WHERE
s_id IN (
SELECT
s_id
FROM
Sc
GROUP BY
s_id
HAVING
COUNT(*) = (
SELECT
COUNT(*)
FROM
Course
)
);

8. Display student id and average grades for each student in descending order of average
grades.
Answer:
SELECT
Sc.s_id, AVG(s_Sc)
FROM
Sc
GROUP BY
Sc.s_id
ORDER BY
AVG(s_Sc) DESC;

9. Query the information of other students who took at least one same course as the student
with id ‘01’.
Answer:
SELECT
DISTINCT Student.*
FROM
Student, SC
WHERE
Student.s_id=SC.s_id
AND SC.c_id IN (
SELECT
a.c_id
FROM
Sc a
WHERE
a.s_id = '01'
)
AND Student.s_id != '01';

10. Query the information of the students who got highest score in the ‘Math’ course.
Answer:
SELECT
*
FROM
Student
WHERE
Student.s_id IN (
SELECT s_id
FROM Sc
WHERE (Sc.c_id, Sc.s_sc) = (
SELECT Sc.c_id, MAX(Sc.s_sc)
FROM Sc, Course
WHERE Sc.c_id = Course.c_id
AND Course.c_name = 'Math'
Group BY Sc.c_id
)
);

You might also like