Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
114 views
in Technique[技术] by (71.8m points)

sql - Rewriting a query in SQLite

Schema:

student: sid
course: cid
prerequisite: cid, pre_cid
record: sid, cid

I wanted to list all students who have taken all the prerequisites for PSY100. I only need to display sid (student id).

The records table may have multiple entries from the same sid if that student has taken multiple classes (cid).

I have the following query, however I wanna know if I can rewrite this without the use of DISTINCT and still only using NOT EXIST.

SELECT DISTINCT r.sid
FROM record r
WHERE NOT EXISTS (
  SELECT * 
  FROM prerequisite p
  WHERE p.cid = 'PSY100' 
  AND NOT EXISTS (
    SELECT * 
    FROM record r2
    WHERE r2.sid=r.sid AND r2.cid=p.pre_cid
)
);

Thank you!

question from:https://stackoverflow.com/questions/65948150/rewriting-a-query-in-sqlite

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...