EXISTS representatives existential quantifier ∃.
With the predicate EXISTS subquery does not return any data, produced only a logical true value "true" or logically false value "false".
If the inner query result is not empty, then the outer layer of the WHERE clause returns true, otherwise returns false.
EXISTS subquery by the leads, with the goal column expression often used *, because the subquery with EXISTS only return true or false value, given the column name moot.
After using the existential quantifier NOT EXISTS, if the inner query result is empty, the outer layer of the WHERE clause returns a true value, otherwise return value.
[3.60] to query all patients enrolled in the 1st course student names.
SELECT Sname
FROM Student
WHERE EXISTS
(SELECT *
FROM SC
WHERE Sno=Student.Sno AND Cno= '1');
Find Principle: Sno value taken by one of the Student, the Sno value SC if present, and which Cno = '1', the result is not empty article, is TRUE, this entry Student.Sname display.
[3.61] Example query student's name is not No. 1 elective courses.
SELECT Sname
FROM Student
WHERE NOT EXISTS(
SELECT *
FROM SC
WHERE Sno = Student.Sno AND Cno='1');
All sub-query with IN predicate, comparison operators, ANY and ALL predicates EXISTS predicate can be used with sub-queries equivalent replacement.
[3.62] patients enrolled in a full course of query names of the students.
SQL language is not universal quantifier, predicate can be converted with the universal quantifier is equivalent with the existential quantifier predicate:
(∀x) P≡¬ (∃x (to p))
because there is no full quantifiers, may be subject meaning converted into an equivalent form of existential quantifiers: query such students, no one elective course he does not. Its SQL statement is as follows:
SELECT Sname
FROM Student
WHERE NOT EXISTS
(SELECT *
FROM Course
WHERE NOT EXISTS(
SELECT *
FROM SC
WHERE Sno= Student.Sno
AND Cno= Course.Cno) );
Thus using EXIST / NOT EXIST to implement the query with a measure word.
[3.63] Example query least number of students enrolled in a full course of 201,215,122 student elective.
SQL language contains no logical operation
can be used to convert the predicate calculus predicate logic implication equivalent to:
P → q≡¬p∨q
SELECT DISTINCT Sno
FROM SC SCX
WHERE NOT EXISTS(
SELECT *
FROM SC SCY
WHERE SCY.Sno = '201215122' AND NOT EXISTS(SELECT * FROM SC SCZ WHERE SCZ.Sno=SCX.Sno AND SCZ.Cno=SCY.Cno));
[3.64] Example queries Department of Computer Science students and age not more than 19-year-old student.
SELECT *
FROM Student
WHERE Sdept= 'CS'
UNION
SELECT *
FROM Student
WHERE Sage<=19;
[3.65] Example 1 query elective courses or elective courses students 2.
SELECT Sno
FROM SC
WHERE Cno='1'
UNION
SELECT Sno
FROM SC
WHERE Cno='2';
[3.66] Example queries Department of Computer Science students age is not more than 19-year-old student intersection.
SELECT *
FROM Student
WHERE Sdept='CS'
INTERSECT
SELECT *
FROM Student
WHERE Sage<=19
[3.67] query both patients enrolled in courses 1 and 2 of elective courses students.
SELECT Sno
FROM SC
WHERE Cno='1'
INTERSECT
SELECT Sno
FROM SC
WHERE Cno='2';
Of course, the solution is more than one nested query can be achieved:
SELECT Sno
FROM SC
WHERE Cno='1' AND Sno IN
(SELECT Sno
FROM SC
WHERE Cno='2');
[3.68] Example queries Department of Computer Science students age is not more than 19-year-old student set difference.
SELECT *
FROM Student
WHERE Sdept='CS'
EXCEPT
SELECT *
FROM Student
WHERE Sage <=19;