Category: SQL Queries

Similar to the UNION command, INTERSECT also operates on two SQL statements. The difference is that, while UNION essentially acts as an OR operator (value is selected if it appears in either the first or the second statement), the INTERSECT command acts as an AND operator (value is selected only if it appears in both statements).

 

INTERSECT operator allows you to combine two table expressions into one and return a result set which consists of rows that appear in the results of both table expressions. INTERSECT operator, like UNION operator, removes all duplicated row from the result sets.

 

Syntax:

SQL Statement 1]

INTERSECT

[SQL Statement 2]

SQL> SELECT ENANE, EMPNO FROM EMP WHERE

ENAME LIKE'M%' OR ENAME LIKE 'J%.'

INTERSECT

SELECT ENAME, EMPNO FROM EMP WHERE

ENAME LIKE 'J%' OR ENAME LIKE 'A%';

 

On execution it will return the common rows that come in first select statement and also in second statement.



Dinesh ThakurDinesh Thakur is a Columinist and designer with strong passion and founder of Computer Notes. if you have any ideas or any request please get @me on Google+
linkedin FaceBook Twitter Google Plus