Join, Subquery & View
Join, Subquery & View
Sub-Query and
View.
MySQL Joins
MySQL Inner JOIN (Simple Join/Natural Join)
• The MySQL INNER JOIN is used to return all rows from
multiple tables where the join condition is satisfied. It is the
most common type of join.
• Syntax 1:
SELECT columns
FROM table1 ,table2
Where table1.column = table2.column;
• Syntax 2:
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
MySQL Inner JOIN Example
Stud_Info Table Stud_Marks Table
SELECT Stud_Info.Rno,Name,Dbms,Toc
FROM Stud_Info,Stud_Marks
Where Stud_Info.RNo= Stud_Marks.RNo
RNo Name Dbms Toc
O/P 1 Abhay 50 45
2 Sarika 67 65
3 Riya 76 55
MySQL Left Outer Join
• The LEFT OUTER JOIN returns all rows from the left hand table
specified in the ON condition and only those rows from the
other table where the join condition is fulfilled.
• Syntax:
SELECT columns
FROM table1
LEFT [OUTER] JOIN table2
ON table1.column = table2.column;
MySQL Left Outer Join Example
Stud_Info Table Stud_Marks Table
SELECT Stud_Info.Rno,Name,Dbms,Toc
FROM Stud_Info LEFT JOIN Stud_Marks
ON Stud_Info.RNo= Stud_Marks.RNo
• Syntax:
SELECT columns
FROM table1
RIGHT [OUTER] JOIN table2
ON table1.column = table2.column;
MySQL Right Outer Join Example
Stud_Info Table Stud_Marks Table
SELECT Stud_Info.Rno,Name,Dbms,Toc
FROM Stud_Info RIGHT JOIN Stud_Marks
ON Stud_Info.RNo= Stud_Marks.RNo
SELECT Stud_Marks.Rno,Name,Dbms,Toc
FROM Stud_Info RIGHT JOIN Stud_Marks
ON Stud_Info.RNo= Stud_Marks.RNo
• Syntax
SELECT Attr_list FROM table_A CROSS JOIN table_B;
Types of Subqueries
Single Row Sub Query: Sub query which returns single row
output. They mark the usage of single row comparison
operators, when used in WHERE conditions.
Operator Description
A subquery can be used
before or after any of the
= Equal to
comparison operators.
> Greater than
>= Greater than or equal to
< Less than The subquery can return at
most one value.
<= Less than or equal to
!= Not equal to
<> Not equal to The value can be the result
NULL-safe equal to of an arithmetic expression
<=> or a column function.
operator
MySQL Subqueries- Single Row
Examples on Emp Table
Emp-id Ename City Post Salary
1 John Nashik Clerk 5000
2 Seema Aurangabad Developer 20000
3 Amita Nagar Manager 70000
4 Rakesh Pune Analyst 50000
5 Samata Nashik Tester 35000
6 Ankita Chandwad Developer 30000
7 Bhavika Pune Team-LR 50000
8 Deepa Mumbai CEO 90000
9 Nitin Nagpur Clerk 8000
10 Pooja Pune Analyst 45000
• Display the information of employees, paid more than
‘pooja' from emp table
Select *
from emp 45000
where salary >
(select salary
from emp
where name=‘Pooja’) ;
Select *
from emp Pune
where city =
(select city
from emp
where name=‘Rakesh’) ;
Select *
from emp 40300
where salary <
(select avg(salary)
from emp ) ;
Output of
Above Query
Select *
from emp 90000
where salary =
(select max(salary)
from emp );
Output of
Above Query
[> ALL] More than the highest value returned by the subquery
[< ALL] Less than the lowest value returned by the subquery
[< ANY] Less than the highest value returned by the subquery
[> ANY] More than the lowest value returned by the subquery