Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Set operations (SQL)

From Wikipedia, the free encyclopedia
This article includes a list ofgeneral references, butit lacks sufficient correspondinginline citations. Please help toimprove this article byintroducing more precise citations.(July 2022) (Learn how and when to remove this message)
Type of operation in SQL

Set operations inSQL is a type of operations which allow the results of multiple queries to be combined into a singleresult set.[1]

Set operators in SQL includeUNION,INTERSECT, andEXCEPT, whichmathematically correspond to the concepts ofunion,intersection andset difference.

UNION operator

[edit]

InSQL theUNION clause combines the results of two SQL queries into a singletable of all matchingrows. The two queries must result in the same number ofcolumns and compatibledata types in order to unite. Any duplicate records are automatically removed unlessUNION ALL is used.

UNION can be useful indata warehouse applications where tables are not perfectlynormalized.[2] A simple example would be a database having tablessales2005 andsales2006 that have identical structures but are separated because of performance considerations. AUNION query could combine results from both tables.

Note thatUNION ALL does not guarantee the order of rows. Rows from the second operand may appear before, after, or mixed with rows from the first operand. In situations where a specific order is desired,ORDER BY must be used.

Note thatUNION ALL may be much faster than plainUNION.

Examples

[edit]

Given these two tables:

sales2005
personamount
Joe1000
Alex2000
Bob5000
sales2006
personamount
Joe2000
Alex2000
Zach35000

Executing this statement:

SELECT*FROMsales2005UNIONSELECT*FROMsales2006;

yields this result set, though the order of the rows can vary because noORDER BY clause was supplied:

personamount
Joe1000
Alex2000
Bob5000
Joe2000
Zach35000

Note that there are two rows for Joe because those rows are distinct across their columns. There is only one row for Alex because those rows are not distinct for both columns.

UNION ALL gives different results, because it will not eliminate duplicates. Executing this statement:

SELECT*FROMsales2005UNIONALLSELECT*FROMsales2006;

would give these results, again allowing variance for the lack of anORDER BY statement:

personamount
Joe1000
Joe2000
Alex2000
Alex2000
Bob5000
Zach35000

The discussion offull outer joins also has an example that usesUNION.

INTERSECT operator

[edit]

The SQLINTERSECT operator takes the results of two queries and returns only rows that appear in both result sets. For purposes of duplicate removal theINTERSECT operator does not distinguish betweenNULLs. TheINTERSECT operator removes duplicate rows from the final result set. TheINTERSECT ALL operator does not remove duplicate rows from the final result set, but if a row appears X times in the first query and Y times in the second, it will appearmin(X,Y){\displaystyle \min(X,Y)} times in the result set.

Example

[edit]

The following exampleINTERSECT query returns all rows from the Orders table where Quantity is between 50 and 100.

SELECT*FROMOrdersWHEREQuantityBETWEEN1AND100INTERSECTSELECT*FROMOrdersWHEREQuantityBETWEEN50AND200;

EXCEPT operator

[edit]

The SQLEXCEPT operator takes the distinct rows of one query and returns the rows that do not appear in a second result set. For purposes of row elimination and duplicate removal, theEXCEPT operator does not distinguish betweenNULLs. TheEXCEPT ALL operator does not remove duplicates, but if a row appears X times in the first query and Y times in the second, it will appearmax(XY,0){\displaystyle \max(X-Y,0)} times in the result set.

Notably, the Oracle platform provides aMINUS operator which is functionally equivalent to theSQL standardEXCEPT DISTINCT operator.[3]

Example

[edit]

The following exampleEXCEPT query returns all rows from the Orders table where Quantity is between 1 and 49, and those with a Quantity between 76 and 100.

Worded another way; the query returns all rows where the Quantity is between 1 and 100, apart from rows where the quantity is between 50 and 75.

SELECT*FROMOrdersWHEREQuantityBETWEEN1AND100EXCEPTSELECT*FROMOrdersWHEREQuantityBETWEEN50AND75;

Example

[edit]

The following example is equivalent to the above example but without using theEXCEPT operator.

SELECTo1.*FROM(SELECT*FROMOrdersWHEREQuantityBETWEEN1AND100)o1LEFTJOIN(SELECT*FROMOrdersWHEREQuantityBETWEEN50AND75)o2ONo1.id=o2.idWHEREo2.idISNULL

See also

[edit]

References

[edit]
  1. ^"The UNION [ALL], INTERSECT, MINUS Operators". Oracle. Retrieved14 July 2016.
  2. ^"aUNION ALL views technique for managing maintenance and performance in your large data warehouse environment ... ThisUNION ALL technique has saved many of my clients with issues related to time-sensitive database designs. These databases usually have an extremely volatile current timeframe, month, or day portion and the older data is rarely updated. Using different container DASD allocations, tablespaces, tables, and index definitions, the settings can be tuned for the specific performance considerations for these different volatility levels and update frequency situations."Terabyte Data Warehouse Table Design Choices - Part 2 (accessed on July 25, 2006)
  3. ^"E071-03,EXCEPT DISTINCT table operator: UseMINUS instead ofEXCEPT DISTINCT""Oracle Compliance To Core SQL:2003".Docs.oracle.com. Retrieved7 July 2022.

External links

[edit]
Versions
Keywords
Related
ISO/IEC SQL parts
Retrieved from "https://en.wikipedia.org/w/index.php?title=Set_operations_(SQL)&oldid=1166298573"
Category:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp