Sunday, February 3, 2013

INTERSECT Set Operator

INTERSECT returns only the rows retrieved by both component queries. Compare this with UNION, which returns the rows retrieved by any of the component queries.

If UNION acts like 'OR', INTERSECT acts like 'AND'.

The Syntax of INTERSECT

<Component Query>
INTERSECT
<Component Query>

For example:

SELECT CUST_NBR, NAME FROM CUSTOMER
WHERE REGION_ID = 5
INTERSECT


SELECT C.CUST_NBR, C.NAME FROM CUSTOMER C WHERE C.CUST_NBR IN (SELECT O.CUST_NBR FROM CUST_ORDER O, EMPLOYEE E  WHERE O.SALES_EMP_ID = E.EMP_ID   AND E.LNAME = 'MARTIN');

No comments:

Post a Comment