10-5 查询全部学生都选修的课程的课程号与课程名

查询全部学生都选修的课程的课程号与课程名

提示:请使用SELECT语句作答。

表结构:

CREATE TABLE STUDENT
( SNO CHAR(5) PRIMARY KEY,
  SNAME VARCHAR(20),
  SAGE SMALLINT CHECK(SAGE>=15 AND SAGE<=45),
  SSEX CHAR(2) DEFAULT '男' CHECK(SSEX='男' OR SSEX='女'),
  SDEPT CHAR(2));
CREATE TABLE COURSE
( CNO CHAR(2) PRIMARY KEY,
  CNAME VARCHAR(20),
  CPNO CHAR(2),
  CCREDIT SMALLINT);
CREATE TABLE SC
( SNO CHAR(5) NOT NULL,
  CNO CHAR(2) NOT NULL,
  GRADE SMALLINT CHECK((GRADE IS NULL) OR (GRADE BETWEEN 0 AND 100)),
  PRIMARY KEY(SNO,CNO),
  FOREIGN KEY(SNO) REFERENCES STUDENT(SNO),
  FOREIGN KEY(CNO) REFERENCES COURSE(CNO)
);


代码如下:

SELECT COURSE.CNO,COURSE.CNAME
FROM COURSE
WHERE CNO 
IN(
    SELECT DISTINCT CNO
    FROM SC
    GROUP BY CNO
    HAVING COUNT(SNO)=(SELECT COUNT(SNO) FROM STUDENT)
)

 

你可能感兴趣的:(数据库,sql)