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

Practicle 10

Uploaded by

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

Practicle 10

Uploaded by

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

Experiment No 10

XI. Exercise

1. Consider following Schema : emp1(empno,ename,deptno)


emp2(empno,ename,deptno)
Ans –
i) Create emp1
CREATE TABLE emp1 ( empno INT PRIMARY KEY, ename VARCHAR(50), deptno
INT );
Insert Sample Data into emp1
a)
INSERT INTO emp1 (empno, ename, deptno) VALUES (1, 'John', 10);
b)
INSERT INTO emp1 (empno, ename, deptno) VALUES (2, 'Mike', 20);
c)
INSERT INTO emp1 (empno, ename, deptno) VALUES (3, 'Sara', 10);
d)
INSERT INTO emp1 (empno, ename, deptno) VALUES (4, 'Tom', 30);
e)
INSERT INTO emp1 (empno, ename, deptno) VALUES (5, 'Jane', NULL);
To display a output
SELECT * FROM emp1;
ii) Create emp2 Tables
CREATE TABLE emp2 ( empno INT PRIMARY KEY, ename VARCHAR(50), deptno
INT );
Insert Sample Data into emp2 Tables
a)
INSERT INTO emp2 (empno, ename, deptno) VALUES (6, 'Sara', 10);
b)
INSERT INTO emp2 (empno, ename, deptno) VALUES (7, 'Emily', 40);
c)
INSERT INTO emp2 (empno, ename, deptno) VALUES (8, 'John', 10);
d)
INSERT INTO emp2 (empno, ename, deptno) VALUES (9, 'Tom', NULL);

e)
INSERT INTO emp2 (empno, ename, deptno) VALUES (10, 'Adam', 50);
To display a output
SELECT * FROM emp2;

Write SQL commands for the following statements.


1. Display the names of employees including duplicate employee names.
Ans –
Query
SELECT ename FROM emp1 UNION ALL SELECT ename FROM emp2;
Output:

ename
John
Mike
Sara
Tom
Jane
Sara
Emily
John
Tom
Adam

2. Display the names of employees excluding duplicate employee names.


Ans –
Query
SELECT ename FROM emp1 UNION SELECT ename FROM emp2;
Output:

ename
John
Mike
Sara
Tom
Jane
Emily
Adam

3. Display the common employee names from both the tables.


Ans –
Query
SELECT ename FROM emp1 INTERSECT SELECT ename FROM emp2;

Output:

ename
Sara
John
Tom

4. List employees who are not assigned to any department?


Ans –
Query
SELECT ename FROM emp1 WHERE deptno IS NULL UNION SELECT ename
FROM emp2 WHERE deptno IS NULL;

Output:

ename
Jane
Tom
X. Practical related questions
1. Explain the need of set operators in SQL
Ans –

Set operators in SQL allow us to combine the results of two or more SELECT queries into a
single result set. These operators are crucial for performing operations that involve
comparisons and combinations of data across multiple tables or query results. The main set
operators in SQL are:

1. UNION
2. UNION ALL
3. INTERSECT
4. EXCEPT (or MINUS in some databases)

set operators need.

1. Combining Results from Multiple Queries


o Sometimes, data is spread across multiple tables, or multiple queries need to
be run to fetch a complete dataset. Set operators allow us to combine these
result sets into one without needing to write complex JOIN queries. For
example, fetching all employees from two different tables (emp1 and emp2),
as shown in the previous example.
2. Removing Duplicates or Including Duplicates
o When combining data from multiple queries, you may want to either include
duplicates (UNION ALL) or remove them (UNION). Set operators provide
flexibility in deciding how to handle duplicates.
o For example:
▪ UNION returns a distinct set of records (removing duplicates).
▪ UNION ALL returns all records, including duplicates.
3. Identifying Common Data
o Set operators help identify common data between two result sets. For example,
you may want to know which records appear in both tables. The
INTERSECT operator is used to find common rows between two or more
result sets.
o Example: If you want to know which employees work in both departments
(represented by two tables), you can use INTERSECT to retrieve only the
common employees.
4. Excluding Specific Data
o The EXCEPT or MINUS operator allows you to exclude rows found in one
result set but not in another. This is useful when you want to filter out certain
data. For example, you may want to find employees who are in emp1 but not
in emp2.
o Example: Using EXCEPT to find employees from one department but not
from another.
5. Simplification of Complex Queries
o Without set operators, some operations would require nested subqueries or
complex joins. Set operators simplify this by letting us directly combine the
results of different queries without needing to rewrite the logic into a single
query. This enhances readability and maintainability.
o For example, if you wanted to retrieve employees from multiple tables but
didn’t want to write a complex JOIN query, you could use UNION or UNION
ALL to simplify the process.
6. Querying Across Different Structures
o Set operators allow combining data across tables with different structures (as
long as the selected columns are of the same data types). This flexibility is
useful when combining data from different sources or systems.
o For instance, you might want to combine data from an orders table and a
shipments table where both have different columns but share common data
types for specific columns.

2. Describe various set operators in SQL


Ans –

SQL provides set operators to combine the results of two or more queries. These set
operators treat the results as mathematical sets and allow us to combine, intersect, or subtract
them. The primary set operators in SQL are:

1. UNION
2. UNION ALL
3. INTERSECT
4. EXCEPT (also known as MINUS in some databases)

Each operator works on multiple result sets produced by SELECT statements and requires
the result sets to have the same number of columns and compatible data types.

1. UNION

The UNION operator is used to combine the results of two or more SELECT queries and
removes duplicate rows. This operator returns only distinct values from the combined result
sets.

Syntax:
SELECT column_list FROM table1 UNION SELECT column_list FROM table2;

2. UNION ALL

The UNION ALL operator is similar to UNION, but it does not remove duplicate rows. It
returns all rows from both result sets, including duplicates.

Syntax:
SELECT column_list FROM table1 UNION ALL SELECT column_list FROM table2;

3. INTERSECT
The INTERSECT operator returns only the rows that are common to both result sets. In other
words, it returns the intersection of the two sets.

Syntax:
SELECT column_list FROM table1 INTERSECT SELECT column_list FROM table2;

4. EXCEPT (or MINUS)

The EXCEPT operator (known as MINUS in some databases like Oracle) returns the rows
that are present in the first result set but not in the second. It is used to subtract one result
set from another.

Syntax:
SELECT column_list FROM table1 EXCEPT SELECT column_list FROM table2;
a)

SELECT ename FROM emp1 UNION ALL SELECT ename FROM emp2;

b)

SELECT ename FROM emp1 UNION SELECT ename FROM emp2;

c)

SELECT ename FROM emp1 INTERSECT SELECT ename FROM emp2;

d)

SELECT ename FROM emp1 WHERE deptno IS NULL UNION SELECT ename FROM emp2 WHERE
deptno IS NULL;

You might also like