Express and intersect operations in SQL

Note: All underscore + italic statements are optional statements


SQL statement:

Select…  From…  Where…

Union (union) / Intersect (intersect) / Difference (except)   all

Select…  From…  Where…  


word description:

Suppose the tuple number obtained by the first subquery (SFW) is ①②⑤, and the tuple number obtained by the second subquery (SFW) is ①②④

Then the tuple obtained by the union operation is ①②④⑤, and the tuple obtained by the intersect operation is ①②

The tuple obtained by the union all operation is ①①②②④⑤, that is, there will be repeated tuples


special:

Generally speaking, the union/difference/intersection operations will automatically deduplicate, that is, the default "distinct", and after adding all, it will not

Some DBMSs such as Mysql do not support intersection or difference operations (except)


Example:

①Assume that all students have elective courses, and ask for the student number of students who have not taken course No. 002

Select DISTINCT St From SC
EXCEPT
Select St From SC Where Ct = '002';

or

Select distinct St From SC SC1
Where not exists(Select * From SC
		 Where Ct = '002' and St = SC1.St);


Guess you like

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