sql查询/子查询,用于查找已注册特定课程的学生
我正在研究介绍SQL查询问题。我已经创建了表。该表显示完美,但我需要帮助检索以下内容:您能否指导我今后如何解决此类问题sql查询/子查询,用于查找已注册特定课程的学生,sql,subquery,many-to-many,inner-join,Sql,Subquery,Many To Many,Inner Join,我正在研究介绍SQL查询问题。我已经创建了表。该表显示完美,但我需要帮助检索以下内容:您能否指导我今后如何解决此类问题 查找注册“ITEC”课程的学生姓名 查找已注册至少一门课程的学生的姓名 这就是我所拥有的 /* Create a table called NAMES */ CREATE TABLE Student(sid integer PRIMARY KEY, sname text, yofb integer); /* Create few records in this table *
/* Create a table called NAMES */
CREATE TABLE Student(sid integer PRIMARY KEY, sname text, yofb integer);
/* Create few records in this table */
INSERT INTO Student VALUES(50,'Dave', 1984);
INSERT INTO Student VALUES(23,'Jones', 1993);
INSERT INTO Student VALUES(53,'Smith', 1993);
INSERT INTO Student VALUES(55,'Green', 1995);
INSERT INTO Student VALUES(54,'Webb', 1980);
INSERT INTO Student VALUES(57,'Brown', 1981);
INSERT INTO Student VALUES(60,'Smith', 1979);
INSERT INTO Student VALUES(62,'Moor', 1976);
INSERT INTO Student VALUES(64,'Wolf', 1978);
INSERT INTO Student VALUES(58,'Potter', 1983);
/* Create a table called NAMES */
CREATE TABLE Transcript(sid integer, cid text, mark integer, PRIMARY KEY (sid, cid));
/* Create few records in this table */
INSERT INTO Transcript VALUES(50,'IT1010', 86);
INSERT INTO Transcript VALUES(53,'IT1010', 45);
INSERT INTO Transcript VALUES(53,'CS3421', 89);
INSERT INTO Transcript VALUES(53,'IT1011', 91);
INSERT INTO Transcript VALUES(60,'IT1010', 85);
INSERT INTO Transcript VALUES(60,'BA2430', 86);
INSERT INTO Transcript VALUES(57,'BA2430', 45);
INSERT INTO Transcript VALUES(58,'CS4411', 89);
INSERT INTO Transcript VALUES(59,'IT1010', 91);
INSERT INTO Transcript VALUES(57,'IT1010', 85);
/* Create a table called NAMES */
CREATE TABLE Course(cid text, ctitle text, pcode text, PRIMARY KEY (cid));
/* Create few records in this table */
INSERT INTO Course VALUES('IT1010', 'Class 1', 'ITEC');
INSERT INTO Course VALUES('CS4411', 'Class 3', 'COSC');
INSERT INTO Course VALUES('IT1011', 'Class 2', 'ITEC');
INSERT INTO Course VALUES('CS3421', 'Class 3', 'COSC');
INSERT INTO Course VALUES('BA2430', 'Class 4', 'BART');
INSERT INTO Course VALUES('CS5511', 'Class 2', 'COSC');
INSERT INTO Course VALUES('MA3210', 'Class 3', 'MATH');
INSERT INTO Course VALUES('IT3210', 'Class 4', 'ITEC');
SELECT
s.sid, s.sname
FROM
Student s
WHERE
EXISTS (SELECT c.cid FROM Course c
WHERE c.pcode = 'ITEC' AND c.cid = s.sid)
它并没有真正返回学生的名字。我想知道我该如何处理“至少1个、多于1个或少于1个”的问题
谢谢。您的尝试是正确的-但需要使用桥牌
成绩单
来参考课程
:
SELECT s.sid, s.sname
FROM Student s
WHERE EXISTS (
SELECT 1
FROM Transcript t
INNER JOIN Course c ON c.cid = t.cid
WHERE c.pcode = 'ITEC' AND t.sid = s.sid
)