EXPERIMENT NO:-06
Aim: Implement various Join operations.
Theory:
Inner Join: The INNER JOIN keyword selects records that have matching values in both tables.
SELECT column-name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
Left Join: The LEFT JOIN keyword returns all records from the left table (table1), and the
matched records from the right table (table2). The result is NULL from the right side, if there is no
match.
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name
21
Right Join:
The RIGHT JOIN keyword returns all records from the right table (table2), and the matched
records from the left table (table1). The result is NULL from the left side, when there is no match.
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
FULL outer join:
The FULL OUTER JOIN keyword returns all records when there is a match in left (table1) or right
(table2) table records.
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name;
22
Self Join : When table is joined with itself.
Syntax:
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
Application of Join Operations on Specified Case study with Input & Output:
Inner Join:
Q63 Retrieve Customer Details along with their Orders:
Ans : SELECT Customers.Customer_ID, Customers.Name, Orders.Order_ID, Orders.Total_Amount
FROM Customers
INNER JOIN Orders ON Customers.Customer_ID = Orders.Order_CustomerID;
Left Join:
Q64 : Retrieve All Customers and Their Orders (Including Customers Who Haven't Placed an
Order).
Ans : SELECT Customers.Customer_ID, Customers.Name, Orders.Order_ID, Orders.Total_Amount
FROM Customers
LEFT JOIN Orders ON Customers.Customer_ID = Orders.Order_CustomerID;
23
Right Join:
Q 65: 1. Retrieve All Orders and Associated Customers (Including Orders without Matching
Customers):
Ans : SELECT Orders.Order_ID, Orders.Total_Amount, Customers.Customer_ID,
Customers.Name
FROM Orders
RIGHT JOIN Customers ON Orders.Order_CustomerID = Customers.Customer_ID;
FULL outer join:
Q66 : Retrieve all Customers and Orders (including unmatched):
Ans :, SELECT Customers.Customer_ID, Customers.Name, Orders.Order_ID,
Orders.Total_Amount
FROM Customers
LEFT JOIN Orders ON Customers.Customer_ID = Orders.Order_CustomerID
UNION
SELECT Customers.Customer_ID, Customers.Name, Orders.Order_ID,
Orders.Total_Amount
FROM Orders
RIGHT JOIN Customers ON Orders.Order_CustomerID = Customers.Customer_ID;
24
Self Join : When table is joined with itself.
Syntax: SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
Q 67: Find Customers from the Same City:
Ans :; SELECT A.Customer_ID AS Customer1_ID, A.Name AS Customer1_Name,
B.Customer_ID AS Customer2_ID, B.Name AS Customer2_Name, A.City
FROM Customers A
JOIN Customers B ON A.City = B.City AND A.Customer_ID < B.Customer_ID;
Conclusion: We successfully implemented various JOIN operationsin MySQL
R1 R2 R3 R4 Total Sign with Date
(3) (5) (4) (3) (15)
25
EXPERIMENT NO:-07
Aim: Perform Nested and Complex
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.
The following are the rules to use subqueries:
o Subqueries should always use in parentheses.
o If the main query does not have multiple columns for subquery, then a subquery can have only
one column in the SELECT command.
o We can use various comparison operators with the subquery, such as >, <, =, IN, ANY, SOME,
and ALL. A multiple-row operator is very useful when the subquery returns more than one
row.
o We cannot use the ORDER BY clause in a subquery, although it can be used inside the main
query.
o If we use a subquery in a set function, it cannot be immediately enclosed in a set function.
The following are the advantages of using subqueries:
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 :
SELECT column_list (s) FROM table_name
WHERE column_name OPERATOR
(SELECT column_list (s) FROM table_name [ WHERE ])
21
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).
MySQL Union must follow these basic rules:
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 :
SELECT column_list FROM table1
UNION
SELECT column_list FROM table2;
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.
22
Application of Nested and Set operations on Specified Case study with Input
& Output:
NESTED QUERIES:
Q 68: Common categories between Seller1 and Seller2:
ANS: SELECT DISTINCT p1.Category
FROM Products p1
INNER JOIN Products p2 ON p1.Category = p2.Category
WHERE p1.Seller_ID = 1 AND p2.Seller_ID = 2;
1. Q 69 : UNION - Retrieve distinct product categories sold by Seller1 and Seller2:
Ans : SELECT Category
FROM Products
WHERE Seller_ID = 1
UNION
SELECT Category
FROM Products
WHERE Seller_ID = 2;
23
Q 71: EXCEPT (Using NOT IN simulation) - Categories sold by Seller1 but not by Seller2:
Ans: SELECT DISTINCT Category
FROM Products
WHERE Seller_ID = 1
AND Category NOT IN (SELECT DISTINCT Category FROM Products WHERE Seller_ID = 2);
Conclusion:
R1 R2 R3 R4 Total Sign with Date
(3) (5) (4) (3) (15)
24