Sql Join
Sql Join
• INNER JOIN
• LEFT JOIN
• RIGHT JOIN
• FULL JOIN
• Natural Join
SQL INNER JOIN
• The INNER JOIN keyword selects all rows from both the
tables as long as the condition is satisfied.
• Syntax
• SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;
INNER JOIN Example
• Query:
• SELECT StudentCourse.COURSE_ID, Student.NAME, Student.AGE
FROM Student
INNER JOIN StudentCourse
ON Student.ROLL_NO = StudentCourse.ROLL_NO;
SQL LEFT JOIN
• LEFT JOIN returns all the rows of the table on the left side of the join
and matches rows for the table on the right side of the join.
• Syntax
• SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
LEFT JOIN Example
• SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
LEFT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
SQL RIGHT JOIN
• RIGHT JOIN returns all the rows of the table on the right side of the
join and matching rows for the table on the left side of the join.
Syntax
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;
RIGHT JOIN Example
• Query:
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
RIGHT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
SQL FULL JOIN
• FULL JOIN creates the result-set by combining results of both LEFT
JOIN and RIGHT JOIN. The result-set will contain all the rows from
both tables.
Syntax
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;
FULL JOIN Example
• Query:
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
FULL JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
FULL JOIN Example - Output
NAME COURSE_ID
HARSH 1
PRATIK 2
RIYANKA 2
DEEP 3
SAPTARHI 1
DHANRAJ NULL
ROHIT NULL
NIRAJ NULL
NULL 4
NULL 5
NULL 4
SQL Natural Join (?)
• Natural join can join tables based on the common columns in the
tables being joined. A natural join returns all rows by matching values
in common columns having same name and data type of columns and
that column should be present in both tables.
• Both table must have at least one common column with same
column name and same data type.
• The two table are joined using Cross join.
• DBMS will look for a common column with same name and data
type. Tuples having exactly same values in common columns are kept
in result.
Natural join Example
Employee
Department
Emp_na
Emp_id Dept_id Dept_id Dept_name
me
10 IT
1 Ram 10
30 HR
2 Jon 30 40 TIS
3 Bob 50
Problem: Find all Employees and their respective departments.
• Solution Query: (Employee) ? (Department)
1 Ram 10 10 IT
2 Jon 30 30 HR
• A view in SQL is a saved SQL query that acts as a virtual table. It can
fetch data from one or more tables and present it in a customized
format, allowing developers to:
• Simplify Complex Queries
• Enhance Security
• Present Data Flexibly
EXAMPLE
• Student Detail table
EXAMPLE
• Student Marks Table
CREATE A VIEW
Syntax:
CREATE VIEW view_name AS
SELECT column1, column2…..
FROM table_name
WHERE condition;
Parameters:
•view_name: Name for the View
•table_name: Name of the table
•condition: Condition to select rows
Creating View From a Single
Table
• CREATE VIEW DetailsView AS
SELECT NAME, ADDRESS FROM StudentDetails WHERE S_ID < 5;