Search notes:

Oracle SQL: Set operators

The so called set operators allow to combine the resultset of multiple select statements (queries).
The set operators are
Set operators cannot be used on columns whose data type is blob, clob, bfile, varray or nested table.

Precedence

All set operators have equal precedence.
create table tq84_a(val number);
create table tq84_b(val number);
create table tq84_c(val number);
create table tq84_d(val number);

insert into tq84_a values (1);
insert into tq84_a values (2);
insert into tq84_a values (9);

insert into tq84_b values (3);
insert into tq84_b values (4);
insert into tq84_b values (5);
insert into tq84_b values (7);
insert into tq84_a values (6);

insert into tq84_c values (1);
insert into tq84_c values (3);
insert into tq84_c values (5);
insert into tq84_a values (7);

insert into tq84_d values (2);
insert into tq84_d values (4);
insert into tq84_a values (0);

--
--     All set operators have equal precedence,
--     the following select statements are equivalent:
--

select val from tq84_a union
select val from tq84_b minus
select val from tq84_c union
select val from tq84_d;


(((select val from tq84_a   union
   select val from tq84_b)  minus
   select val from tq84_c)  union
   select val from tq84_d) ;

--       This is different...

  (select val from tq84_a  union
  (select val from tq84_b  minus
  (select val from tq84_c  union
   select val from tq84_d))) ;

--       As well as this:

  (select val from tq84_a  union
   select val from tq84_b) minus
  (select val from tq84_c  union
   select val from tq84_d);

--       As well as this:

   select val from tq84_a  union
  (select val from tq84_b  minus
   select val from tq84_c) union
   select val from tq84_d;


drop table tq84_a purge;
drop table tq84_b purge;
drop table tq84_c purge;
drop table tq84_d purge;
Github repository Oracle-Patterns, path: /SQL/select/set_operators/precedence.sql

See also

With a combination of the set operators union all and minus, it is possible to execute an SQL statement that compares the data of two tables returns their difference
Using a set operator in a view prevents the view from being updatable.

Index