0% found this document useful (0 votes)
33 views12 pages

Intersect Except Query: by Neil A. Basabe

The document discusses the INTERSECT and EXCEPT queries in SQL. INTERSECT returns rows that are identical in two SELECT statements, while EXCEPT returns rows that are in the first statement but not the second. Examples are provided to demonstrate the syntax and output of INTERSECT and EXCEPT queries between two SELECT statements on employee data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views12 pages

Intersect Except Query: by Neil A. Basabe

The document discusses the INTERSECT and EXCEPT queries in SQL. INTERSECT returns rows that are identical in two SELECT statements, while EXCEPT returns rows that are in the first statement but not the second. Examples are provided to demonstrate the syntax and output of INTERSECT and EXCEPT queries between two SELECT statements on employee data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

INTERSECT EXCEPT QUERY

By
Neil A. Basabe

1
INTERSECT
INTERSECT QUERY is used to combine two or
more CURSORS|SELECT statements, but returns
rows only from the first CURSOR that are
identical to the rows in the second CURSOR|
SELECT statement.

2
Syntax
SELECT column list
FROM table-name
WHERE condition

INTERSECT

SELECT column list


FROM table-name
WHERE condition
ORDER BY order-list
3
INTERSECT - Example # 1
SELECT LASTNAME, EDLEVEL, JOB
FROM EMPLOYEE
WHERE JOB='ANALYST'

INTERSECT

SELECT LASTNAME, EDLEVEL, JOB


FROM EMPLOYEE
WHERE EDLEVEL=18
4
OUTPUT

5
INTERSECT – Example # 2
SELECT e.empno, e.lastname, e.job, e.workdept, p.projno, p.projname
FROM EMPLOYEE e JOIN PROJECT p
ON e.empno=p.respemp
WHERE e.workdept BETWEEN 'A00' and 'D11'

INTERSECT

SELECT e.empno, e.lastname, e.job, e.workdept, p.projno, p.projname


FROM EMPLOYEE e JOIN PROJECT p
ON e.empno=p.respemp
WHERE e.job NOT IN ('PRES','MANAGER')

6
EXCEPT
EXCEPT QUERY is used to combine two or more
CURSORS|SELECT statements, but returns rows
from the first CURSOR that are not found in the
rows of the second CURSOR|SELECT statement.

7
Syntax
SELECT column list
FROM table-name
WHERE condition

EXCEPT

SELECT column list


FROM table-name
WHERE condition
ORDER BY order-list
8
EXCEPT – Example # 1
SELECT LASTNAME, EDLEVEL, JOB
FROM EMPLOYEE
WHERE JOB='ANALYST'

EXCEPT

SELECT LASTNAME, EDLEVEL, JOB


FROM EMPLOYEE
WHERE EDLEVEL=18
9
OUTPUT

10
EXCEPT – Example # 2
SELECT e.empno, e.lastname, e.job, e.workdept, p.projno, p.projname
FROM EMPLOYEE e JOIN PROJECT p
ON e.empno=p.respemp
WHERE e.workdept BETWEEN 'A00' and 'D11'

EXCEPT

SELECT e.empno, e.lastname, e.job, e.workdept, p.projno, p.projname


FROM EMPLOYEE e JOIN PROJECT p
ON e.empno=p.respemp
WHERE e.job NOT IN ('PRES','MANAGER')

11
Laboratory Exercises

12

You might also like