SQL Server Exists,not exists usage _mssql

Source: Internet
Author: User
Student table: CREATE TABLE Student
(
ID Number (8) primary key,
Name VARCHAR2, deptment number (8)
)
Selected timetable: 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)
)
Timetable: CREATE TABLE COURSE
(
ID Number (8) is not NULL,
C_name VARCHAR2 (20),
C_no VARCHAR2 (10)
)
 Student table data: 
        ID name             deptment_id
------------------------------------
          1 echo                    1000
         2 spring                 
          3 smith                   1000
         4 liter                  
Data for Course tables:
ID C_name C_no
---------- -------------------- --------
1 Database Data1
2 Mathematical Month1
3 English ENGLISH1
Data for Select_course tables:
ID student_id course_id
---------- ---------- ----------
1 1 1
2 1 2
3 1 3
4 2 1
5 2 2
6 3 2
1. Enquiries took the student ID of all courses, name: (that is, this student does not have a course he did not choose.) )
and sc.course_id=c.id exist null,
This indicates that (2) The query result in SELECT * from course C does not exist (in 1 queries), and the result of the query returns the course not selected.
The not exists, after the select * from T_student ts, evaluates to FALSE and does not execute the query.
NOT EXISTS
NOT EXISTS
ID NAME deptment_id
---------- --------------- -----------
1 Echo 1000
2. The query does not select all students who have not selected all courses, that is, students who are not fully elected. (There is a student who has at least one course not selected),
Analysis: As long as there is a door not selected, that is, select * from Select_course SC where student_id=t_student.id and course_id
=course.id has a null, that is, not exists null is true, when the SELECT * from course has the query result (ID is the course.id in the subquery),
Therefore, the select Id,name from t_student executes the query (the ID is t_student.id in the subquery).
Exists
	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. A student who is not selected for a course. (without such a student, he has at least one elective course),
Analysis: If he elective a select * From course result set is not NULL, not exists evaluates to false;
The select Id,name from T_student does not execute the query.
NOT EXISTS
	Exists
		(SELECT * from Select_course SC where student_id=t_student.id and course_id=course.id));
ID NAME
---------- ---------------
4 liter
4. Consult a student who has selected at least one course.
Exists
	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

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.