0% found this document useful (0 votes)
22 views

Set Operators

This document describes different types of set operators and nested queries that can be used in SQL. It defines the UNION, INTERSECT, and MINUS operators for combining result sets from multiple queries and provides syntax examples. It also explains single row and multiple row nested subqueries that can be used with operators like IN, ANY, and ALL to filter rows based on results from a subquery.

Uploaded by

VinaySaini
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Set Operators

This document describes different types of set operators and nested queries that can be used in SQL. It defines the UNION, INTERSECT, and MINUS operators for combining result sets from multiple queries and provides syntax examples. It also explains single row and multiple row nested subqueries that can be used with operators like IN, ANY, and ALL to filter rows based on results from a subquery.

Uploaded by

VinaySaini
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

SET OPERATORS

UNION
SYNTAX: Select Field1, Field2 From Table1 UNION Select Field1, Field2 From Table2
where condition;
select empno,ename from emp where deptno=20 UNION select empno,ename
from emp where deptno=30;


INTERSECTION
SYNTAX: Select Field1, Field2 From Table1 INTERSECT Select
Field1, Field2 From Table2 where condition;
select job from emp where deptno=20 INTERSECT select job from emp where
deptno=30;

MINUS
SYNTAX: Select Field1, Field2 From Table1 MINUS Select Field1,
Field2 From Table2 where condition;
select empno,ename from emp where deptno=20 MINUS select empno,ename
from emp where deptno=30;


NESTED QUERIES

SINGLE ROW SUB QUERY
SYNTAX: Select Field1, Field2, Field3 From Tablename Where
Field1=(Select Field1 From Tablename Where Condition) And Field2>(Select
Field2 From Tablename Where Condition);
select empno,ename,job from emp where deptno=(select deptno from emp
where ename='vinay');




MULTIPLE ROW SUB QUERY


a. USING IN OPERATOR
Syntax: Select Field1, Field2, Field3 From Tablename Where Field1 In(Select
Min(Field1) From Tablename Group By Field2);
EXAMPLE: select empno,ename,sal,deptno from emp where sal IN(select min(sal)
from emp group by deptno );



b. USING ANY OPERATOR
Syntax: Select Field1, Field2, Field3 From Tablename Where Field1<Any
(Select Field1 From Tablename Where Codition);
EXAMPLE: select empno,ename,sal,job from emp where sal>ANY(select sal from
emp where deptno=20);




c. USING All OPERATOR
Syntax: Select Field1, Field2, Field3 From Tablename Where Field1>Any
(Select Avg(Field1) From Tablename Group By Field2);
EXAMPLE: select ename,id,job from employee where salary > ALL(select salary
from employee where job = 'doctor');

You might also like