Pages

Monday 20 September 2021

Relational Set Operators

Set Operators:

The SQL Set operation is used to combine the two or more SQL SELECT statements.

Types of Set Operation
Union
Union All
Intersect
Minus

consider table1 (std1):
SQL> select * from std11;

       SNO SNAME
---------- -----------
        11 raju
        22 rani
        33 vani

consider table2 (std2):
SQL> select * from std22;

       SNO SNAME
---------- -----------
        33 vani
        44 suresh
        55 mahesh

Union:
SQL> select * from std11 union select * from std22;

       SNO SNAME
---------- -----------
        11 raju
        22 rani
        33 vani
        44 suresh
        55 mahesh

Union all:
SQL> select * from std11 union all select * from std22;

       SNO SNAME
---------- -----------
        11 raju
        22 rani
        33 vani
        33 vani
        44 suresh
        55 mahesh

Intersect:
SQL> select * from std11 intersect select * from std22;

       SNO SNAME
---------- -----------
        33 vani

Minus:
SQL> select * from std11 minus select * from std22;

       SNO SNAME
---------- -----------
        11 raju
        22 rani

SQL> select * from std22 minus select * from std11;

       SNO SNAME
---------- -----------
        44 suresh
        55 mahesh

Relational Set Operators:
A query language is a language in which user request information from the  database  that  can  be  categorized  as  either  procedural  or  non-procedural .

Procedural Language:(Pl/sql)

      The user instructs the system to do a sequence of operations on a  database  to  compute  the  desired  result .

Non-Procedural Language:(sql)

       The  user  describes  the  desire  information  without  giving  any  specific  procedure  for  obtaining  the  information  in  a  database .

Relational algebra: 

     Relational  algebra  is   a  procedural  query  language  which  consist  of  set  of  operations  that  take  one  or  two  relations  as  input  and  produces  a  new  relation  as  an  output . 

Table 1:

SQL>create table course(cid number(2) primary key,cname varchar(5));

SQL> select * from course ;
       CID CNAME
---------- -----
        11 mca
        22 bca
        33 mpcs
        44 mecs
        55 mscs

Table 2:

SQL>create table std(sno number(3),sname varchar(11),city varchar(10),cid number(2),foreign key(cid) references course(cid));

SQL> select * from std ;
       SNO SNAME       CITY              CID
---------- ----------- ---------- ----------
       111 raju        vsp                   11
       222 rani        hyd                   11
       333 vani        vzm                  22
       444 kani        rjm                   33
       555 poni        anp                   33


1.Union :This  operator  combines  all rows  of  one  table  with  all  the rows  of  another  table  except  for   duplicate  tuples .

Syntax : Select<statement1> Union Select <statement2>;

Example :

SQL> select cid from course  union  select cid from std;
       CID
----------
        11
        22
        33
        44
        55

2.Union all :  It  accepts  all  the  duplicate  values

Syntax : Select<statement1> Union all Select <statement2>;

Example :

SQL> select cid from course  union all select cid from std;
       CID
----------
        11
        22
        33
        44
        55
        11
        11
        22
        33
        33

3.Intersect :  This  operator  takes  two  tables  and  combines  only  the  tuples  that  appear  in  both  tables .

Syntax :  select <statement 1> intersect select <statement 2>;

Example :

SQL> select cid from course  intersect select cid from std;
       CID
----------
        11
        22
        33

4.Difference :  This  operator  gets  all  rows  in  one  table  that  are  not  found  in  another  table 

Syntax :  select <statement 1> minus select <statement 2>;

Example :
SQL> select cid from course  minus select cid from std;
       CID
----------
        44
        55

No comments:

Post a Comment

Conflict Serializability

Find out conflict serializability for the given transactions T1 T2 T3 R(X)     ...