SQL subqueries EXISTS and NOT EXISTS

The content comes from the Internet: https://blog.csdn.net/qq_27571221/article/details/53090467

 

 

 

Put the data of the main query into the sub-query for conditional verification, and determine whether the data results of the main query are retained according to the verification result (TRUE or FALSE).

Below are examples of three tables

We first introduce the 3 data tables used:

student data table:

sno student number take off ssex sage
20161181 Altair male 20
20161182 Desmond male 18
20161183 Ezio male 22
20161184 Christina Female 19

Course data table:

cno course number cname course name
1 C language
2 data structure
3 Signals and Systems
4 analog electronics
5 High number

sc datasheet:

sno student number cno course number grades
20161181 1 99
20161182 2 98
20161181 2 97
20161181 3 95
20161184 3 92
20161181 4 90
20161181 5 88
20161183 5 58

EXISTS

EXISTS stands for the existential quantifier ∃. A subquery with an EXISTS predicate returns no data, only the logically true value "true" or the logically false value "false".

An example 1.1:

Requirement: Query students who have taken the course "Signal and System"

SELECT s.Sname FROM student s
WHERE EXISTS  
(SELECT * FROM sc, course c WHERE sc.sno = s.sno AND sc.cno = c.cno AND c.cname = '信号与系统')

 

After using the existential quantifier EXISTS, if the inner query result is not empty, the outer WHERE clause returns true, otherwise the return value is false.

In this example, the innermost statement is parsed first:

SELECT * FROM sc, course c WHERE sc.sno = s.sno AND sc.cno = c.cno AND c.cname = '信号与系统'
  • 1

The query condition of the subquery in this example depends on an attribute value of the outer parent query (in this example, the Sno value of Student). The processing process of this correlated subquery is:

First, take the first tuple of the (student) table in the outer query, and process the inner query according to its attribute value (Sno value) related to the inner query. If the outer WHERE returns true, then take the outer query Put the Sname of the tuple in the result table;

Then take the next group of the (student) table, and repeat this process until the outer (Student) table is all checked.

Query result table:

Take off
Altair
Christina

NOT EXISTS

The opposite of the EXISTS predicate is the NOT EXISTS predicate. After using the existential quantifier NOT EXISTS, if the corresponding query result is empty, the outer WHERE sub-statement returns a true value, otherwise it returns a false value.

Example 2.1:
Requirement: Query students who do not have the elective course "Signal and System"

SELECT s.Sname FROM student s
WHERE NOT EXISTS  
(SELECT * FROM sc, course c WHERE sc.sno = s.sno AND sc.cno = c.cno AND c.cname = '信号与系统')

 

After using NOT EXISTS, if the inner query result is not empty, the corresponding NOT EXISTS does not hold, so the corresponding WHERE statement does not hold either.

In Example 1.1, the record corresponding to Li Yong matches the inner select statement, so the record data is returned, but the corresponding NOT EXISTS and WHERE statements are not valid, indicating that this is not the data we want to query.

Query result table:

Take off
Desmond
Ezio

Example 2.2 (this is an example of a universal quantifier with NOT EXISTS):

Requirements: Query the names of students who have taken all courses.

SQL statement:

SELECT Sname  
FROM Student   
WHERE NOT EXISTS  
(SELECT * FROM Course WHERE NOT EXISTS  
     (SELECT * FROM SC WHERE Sno=Student.Sno AND Cno=Course.Cno)  
);  

 

This is a relatively complex sql statement, two EXISTS and three WHERE.

This SQL statement can be divided into three layers, the outermost statement, the innermost statement, and the middle layer statement.

We are very concerned about the outermost statement, because the data in the result table is the data in the outermost query table, and we are more concerned about the innermost data, because the innermost data contains all the judgment statements, decide The record in the student table is the record we query.

We analyze from the inside out:

The first record in the outermost student table is the record corresponding to Altair, and then the first record in the middle course table is the record corresponding to the database, and then the data is judged (the innermost WHERE statement) , the result returns true, then the NOT EXISTS of the inner layer is false,
and then continues to judge the next record in the course table, and the value of the returned NOT EXISTS is also false, until all the data in the course table is traversed, the inner layer The value of NOT EXISTS is always false, so the value of the WHERE statement in the middle layer is always false.
Corresponding to the Altair record of the student, the return value of the middle layer corresponding to all the records in the course table is false, so the value corresponding to the outermost NOT EXISTS is true, and the value of the outermost WHERE is also true, then Altair corresponds to The records that meet the query conditions are loaded into the result table.
Then continue to judge the next record in the student table until all the data in the student table has been traversed.

The following is my own interpretation of this sql:

First take a student record, enter the middle layer, then take a course record, enter the inner layer, at this time, the student record and the course record are used as the conditions for the inner layer judgment. For example, the first record I take at this time is Altair, then My Sql can be written as

SELECT * FROM Course WHERE NOT EXISTS  
     (SELECT * FROM SC WHERE Sno = '20161181' AND Cno=Course.Cno)  
)

 

Here sno 20161181 is Altair's student number. This sql means to select courses that have not been selected by Altair. If it does not exist, return false, and then associate it with the outermost NOT EXISTS. Negative and negative are positive. The meaning of each cycle means that for each student screened out, there is no course that has not been selected by him, that is, all courses are selected.

Final query result:

Take off
Altair

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325218826&siteId=291194637