0% found this document useful (0 votes)
253 views11 pages

Practical No:-4 Aim: - Set Operators, Nested Queries, Joins, Sequences Set operators:-UNION Operation: UNION Is Used To Combine The Results of Two or

This document discusses various SQL concepts including set operators, nested queries, joins, and sequences. It provides examples of UNION, UNION ALL, INTERSECT, MINUS, independent nested queries using IN and NOT IN operators, correlated nested queries, and different types of joins like INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN using sample tables. It also explains what sequences are in databases and provides a syntax example to create a sequence that cycles and increments values.

Uploaded by

Rohit Rana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
253 views11 pages

Practical No:-4 Aim: - Set Operators, Nested Queries, Joins, Sequences Set operators:-UNION Operation: UNION Is Used To Combine The Results of Two or

This document discusses various SQL concepts including set operators, nested queries, joins, and sequences. It provides examples of UNION, UNION ALL, INTERSECT, MINUS, independent nested queries using IN and NOT IN operators, correlated nested queries, and different types of joins like INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN using sample tables. It also explains what sequences are in databases and provides a syntax example to create a sequence that cycles and increments values.

Uploaded by

Rohit Rana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Practical no:-4

Aim: - Set Operators, Nested Queries, Joins, Sequences


Set operators:-
UNION Operation: UNION is used to combine the results of two or
more SELECT statements. However it will eliminate duplicate rows from
its resultset. In case of union, number of columns and Datatype must
be same in both the tables, on which UNION operation is being applied.

Example:  We have two tables dept and Teacher info .


 SELECT * FROM teacher_info UNION SELECT * FROM Dept;
UNION ALL: This operation is similar to Union. But it also shows the
duplicate rows.
Example: 
 SELECT * FROM teacher_info UNION ALL  SELECT * FROM Dept;

INTERSECT: Intersect operation is used to combine


two SELECT statements, but it only retuns the records which are
common from both SELECT statements. In case of Intersect the number
of columns and datatype must be same.
NOTE: MySQL does not support INTERSECT operator.
Example: 
The First table,
I NAM
D E

1 abhi

ada
2
m
The Second table,
I NAM
D E

2 adam

Chest
3
er
 SELECT * FROM First INTERSECT SELECT * FROM Second;
Output:  
I NAM
D E

ada
2
m

MINUS: The Minus operation combines results of


two SELECT statements and return only those in the final result, which
belongs to the first set of the result.
Example:
SELECT * FROM First  MINUS SELECT * FROM Second;
Output: 
I NAM
D E

1 abhi

Nested Queries:-
In nested queries, a query is written inside a query. The
result of inner query is used in execution of outer query.
We will use STUDENT, COURSE, S_ID C_ID
STUDENT_COURSE tables for understanding nested
queries. S1 C1
 
S1 C3

S2 C1
STUDENT
S3 C2
S_ID S_NAME S_ADDRESS S_PHONE S_AGE
S4 C2
S1 RAM DELHI 9455123451 18
S4 C3
S2 RAMESH GURGAON 9652431543 18

S3 SUJIT ROHTAK 9156253131 20

S4 SURESH DELHI 9156768971 18


 
COURSE

C_ID C_NAME

C1 DSA

C2 Programming

C3 DBMS
 
STUDENT_COURSE
 
There are mainly two types of nested queries:
 Independent Nested Queries: In independent nested queries,
query execution starts from innermost query to outermost queries.
The execution of inner query is independent of outer query, but the
result of inner query is used in execution of outer query. Various
operators like IN, NOT IN, ANY, ALL etc are used in writing
independent nested queries.

IN: If we want to find out S_ID who are enrolled in C_NAME ‘DSA’


or ‘DBMS’, we can write it with the help of independent nested
query and IN operator. From COURSE table, we can find
out C_ID for C_NAME ‘DSA’ or DBMS’ and we can use these C_IDs
for finding S_IDs from STUDENT_COURSE TABLE.
 
STEP 1: Finding C_ID for C_NAME =’DSA’ or ‘DBMS’
Select C_ID from COURSE where C_NAME = ‘DSA’ or C_NAME =
‘DBMS’
 
STEP 2: Using C_ID of step 1 for finding S_ID
Select S_ID from STUDENT_COURSE where C_ID IN
(SELECT C_ID from COURSE where C_NAME = ‘DSA’
or C_NAME=’DBMS’);
 
The inner query will return a set with members C1 and C3 and outer
query will return those S_IDs for which C_ID is equal to any
member of set (C1 and C3 in this case). So, it will return S1, S2 and
S4.
 
NOT IN: If we want to find out S_IDs of STUDENTs who have neither
enrolled in ‘DSA’ nor in ‘DBMS’, it can be done as:
Select S_ID from STUDENT where S_ID NOT IN
(Select S_ID from STUDENT_COURSE where C_ID IN
(SELECT C_ID from COURSE where C_NAME=’DSA’
or C_NAME=’DBMS’));
 
The innermost query will return a set with members C1 and C3.
Second inner query will return those S_IDs for which C_ID is equal
to any member of set (C1 and C3 in this case) which are S1, S2 and
S4. The outermost query will return those S_IDs where S_ID is not a
member of set (S1, S2 and S4). So it will return S3.
 
 Co-related Nested Queries: In co-related nested queries, the
output of inner query depends on the row which is being currently
executed in outer query. e.g.; If we want to find
out S_NAME of STUDENTs who are enrolled in C_ID ‘C1’, it can be
done with the help of co-related nested query as:
Select S_NAME from STUDENT S where EXISTS
( select * from STUDENT_COURSE SC where S.S_ID=SC.S_ID and
SC.C_ID=’C1’);
 
For each row of STUDENT S, it will find the rows
from STUDENT_COURSE where S.S_ID = SC.S_ID and SC.C_ID=’C1’.
If for a S_ID from STUDENT S, atleast a row exists
in STUDENT_COURSE SC with C_ID=’C1’, then inner query will
return true and corresponding S_ID will be returned as output.
JOINS:-  join is used to combine columns from one (self-join) or more
tables based on the values of the common columns between the
tables. A JOIN is a means for combining fields from two tables by using
values common to each.There are four types of joins :-
 INNER JOIN
 LEFT JOIN
 RIGHT JOIN
 FULL JOIN

Sample tables :Suppose we have two tables


called basket_a and basket_b that stores fruits:

CREATE TABLE basket_a (id INT PRIMARY KEY, fruit VARCHAR (10
0) NOT NUL); 
INSERT INTO basket_a (id, fruit)VALUES(1, 'Apple'),
INSERT INTO basket_a (id, fruit)VALUES (2, 'Orange'),
INSERT INTO basket_a (id, fruit)VALUES(3, 'Banana'),
INSERT INTO basket_a (id, fruit)VALUES (4, 'Cucumber');
CREATE TABLE basket_b (id INT PRIMARY KEY,fruit VARCHAR (100
) NOT NUL);
INSERT INTO basket_b (id, fruit)VALUES(1, 'Orange'),
INSERT INTO basket_b (id, fruit)VALUES(2, 'Apple'),
INSERT INTO basket_b (id, fruit)VALUES(3, 'Watermelon'),
INSERT INTO basket_b (id, fruit)VALUES(4, 'Pear');

INNER JOIN: This type of join returns those records which have


matching values in both tables.
SELECT a.id id_a, a.fruit fruit_a, b.id id_b, b.fruit fruit_b FROM bas
ket_a a INNER JOIN basket_b b ON a.fruit = b.fruit;

Output: 
id_a fruit_a id_b fruit_b

1 apple  2 apple

2 orange 1 orange

LEFT JOIN:  returns all rows from the left table, even if there are no
matches in the right table.
SELECT a.id id_a, a.fruit fruit_a, b.id id_b, b.fruit fruit_b
FROM  basket_a a LEFT JOIN basket_b b ON a.fruit = b.fruit;

Output: 
id_ id_ fruit_
fruit_a
a b b

1 apple 2 apple
id_ id_ fruit_
fruit_a
a b b

orang
2 orange 1
e

3 banana null null

cucumbe
4 null null
r

RIGHT JOIN: returns all rows from the right table, even if there are no
matches in the left table.
SELECT a.id id_a, a.fruit fruit_a,  b.id id_b,  b.fruit fruit_b FROM b
asket_a a RIGHT JOIN basket_b b ON a.fruit = b.fruit;

Output: 
id_ fruit_ id_
fruit_b
a a b

orang
2 1 orange
e

1 apple 2 apple

watermelo
null null 3
n

null null 4 pear


FULL JOIN: returns rows when there is a match in one of the tables.
SELECT a.id id_a, a.fruit fruit_a, b.id id_b, b.fruit fruit_b FROM bas
ket_a a FULL
JOIN basket_b b ON a.fruit = b.fruit WHERE a.id IS NULL OR b.id IS 
NULL;
Output: 
id_ id_
fruit_a fruit_b
a b

3 banana null null

4 cucumber null null

null null 3 watermelon

null null 4 pear


Sequence: -
Sequence is a set of integers 1, 2, 3, … that are generated and
supported by some database systems to produce unique values on
demand.
 A sequence is a user defined schema bound object that
generates a sequence of numeric values.
 Sequences are frequently used in many databases because many
applications require each row in a table to contain a unique value
and sequences provides an easy way to generate them.
 The sequence of numeric values is generated in an ascending or
descending order at defined intervals and can be configured to
restart when exceeds max_value.
Syntax:
CREATE SEQUENCE sequence_name
START WITH initial_value
INCREMENT BY increment_value
MINVALUE minimum value
MAXVALUE maximum value
CYCLE|NOCYCLE ;

For Example :
CREATE SEQUENCE sequence_1
start with 1
increment by 1
minvalue 0
maxvalue 100
cycle;
Above query will create a sequence named sequence_1.Sequence will
start from 1 and will be incremented by 1 having maximum value 100.
Sequence will repeat itself from start value after exceeding 100.

You might also like