Sqlserver exists, not exists usage

Source: Internet
Author: User

Student table: create table student
(
Id number (8) primary key,
Name varchar2 (10), deptment number (8)
)
Course Selection table: create table select_course
(
Id number (8) primary key,
STUDENT_ID NUMBER (8) foreign key (COURSE_ID) references course (ID ),
COURSE_ID NUMBER (8) foreign key (STUDENT_ID) references student (ID)
)
COURSE schedule: create table COURSE
(
Id number (8) not null,
C_NAME VARCHAR2 (20 ),
C_NO VARCHAR2 (10)
)
Student table data:
Id name DEPTMENT_ID
------------------------------------
1 echo 1000
2 spring 2000
3. smith 1000
4 liter 2000
Course table data:
ID C_NAME C_NO
--------------------------------------
1 Database data1
2. month1
3 English english1
Data in the select_course table:
ID STUDENT_ID COURSE_ID
------------------------------
1 1 1
2 1 2
3 1 3
4 2 1
5 2 2
6 3 2
1. query the student id and name that have taken all courses: (that is, this student does not have a course that he has not selected .)
Analysis: If no course is selected, select * from select_course SC where SC. student_id = ts. id
And SC. course_id = c. id null,
This indicates that (2) the query result of select * from course c does not exist (1 query), and the query result returns the course that is not selected,
In this case, the not exists after select * from t_student ts determines that the result is false and no query is executed.
SQL> select * from t_student ts where not exists
 (select * from course c where not exists
  (select * from select_course sc where sc.student_id=ts.id and sc.course_id=c.id));       
        ID NAME            DEPTMENT_ID
---------- --------------- -----------
         1 echo                   1000
2. query the students who have not selected all courses. (If such a student exists, he does not select at least one course ),
Analysis: select * from select_course SC where student_id = t_student.id and course_id
= Either course. id is null, that is, not exists null is true. In this case, select * from course has the query result (id is the course. id in the subquery ),
Therefore, select id, name from t_student will execute the query (id is t_student.id in the subquery ).
SQL> select id,name from t_student where exists
(select * from course where not exists
(select * from select_course sc where student_id=t_student.id and course_id=course.id));
        ID NAME
---------- ---------------
         2 spring
         3 smith
         4 liter
3. query the students who have not selected a course. (If such a student is not saved, he must take at least one course ),
Analysis: if he chooses a select * from course result set that is not empty, not exists determines that the result is false;
Select id, name from t_student does not execute the query.
SQL> select id,name from t_student where not exists
(select * from course where exists
(select * from select_course sc where student_id=t_student.id and course_id=course.id));
        ID NAME
---------- ---------------
         4 liter
4. query the students who take at least one course.
SQL> select id, name from t_student whereExists
(select * from course where  exists
(select * from select_course sc where student_id=t_student.id and course_id=course.id));
        ID NAME
---------- ---------------
         1 echo
         2 spring
         3 smith

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.