Experiment 7 Dbms
Experiment 7 Dbms
Theory:
NESTED QUERIES:
A subquery in MySQL is a query, which is nested into another SQL query and
embedded with SELECT, INSERT, UPDATE or DELETE statement along with the
various operators. We can also nest the subquery with another subquery. A subquery
is known as the inner query, and the query that contains subquery is known as the
outer query. The inner query executed first gives the result to the outer query, and then
the main/outer query will be performed. MySQL allows us to use subquery anywhere,
but it must be closed within parenthesis. All subquery forms and operations
supported by the SQL standard will be supported in MySQL also.
o The subqueries make the queries in a structured form that allows us to isolate each
part of a statement.
o The subqueries provide alternative ways to query the data from the table; otherwise,
we need to use complex joins and unions.
o The subqueries are more readable than complex join or union statements.
o
Syntax :
25
SET Operator in MySQL :
Union
MySQL Union is an operator that allows us to combine two or more results from
multiple SELECT queries into a single result set. It comes with a default
feature that removes the duplicate rows from the result set. MySQL always uses the
name of the column in the first SELECT statement will be the column names of the
result set(output).
o The number and order of the columns should be the same in all tables that you are
going to use.
o The data type must be compatible with the corresponding positions of each
select query. o The column name selected in the different SELECT queries must be
in the same order.
Syntax :
INTERSECT
The INTERSECT operator is a kind of SET operation in SQL that includes UNION,
UNION ALL, MINUS, and INTERSECT. The INTERSECT operator returns the distinct
(common) elements in two sets or common records from two or more tables.
In other words, it compares the result obtained by two queries and produces
unique rows, which are the result returned by both queries.
Since MySQL does not provide support for the INTERSECT operator. However, we
can use the INNER JOIN and IN clause to emulate this operator.
MINUS
The MINUS operator is a kind of SET operation in SQL which also includes
INTERSECT, UNION, and UNION ALL. The MINUS operator returns the unique element
from the first table/set, which is not found in the second table/set. In other words, it
will compare the results of two queries and produces the resultant row from the result
set obtained by the first query and not found in the result set obtained by the second
query.
Since MySQL does not provide support for MINUS operator. However, we can use a
LEFT JOIN clause to simulate this operator.
25
Application of Nested and Set operations on Specified Case study with
Input & Output:
NESTED QUERIES:
ANS:
Ans :