SQL for Beginners Series - Session 12
JOINS in SQL
Please click here for you tube video Answer
Exercise:
For this exercise, we are considering 2 tables
tblEmployee
tblDept
Deptid is the joining column between these 2 tables.
In Relational databases, all the data is not loaded in a single table, but it will be loaded in multiple tables and those tables will have relations. This process of reducing the redundancy is called Normalization.
These tables can be joined using JOINS in SQL.
There are 4 types of join in SQL
INNER JOIN or JOIN
LEFT JOIN or LEFT OUTER JOIN
RIGHT JOIN or RIGHT OUTER JOIN
FULL JOIN or FULL OUTER JOIN
Scripts Used:
CREATE TABLE tblEmployee (Empid INT NULL
,EmpName Varchar(50) NULL
,Deptid INT NULL
)
CREATE TABLE tblDept (Deptid INT NULL
,DeptName Varchar(50) NULL
)
INSERT tblEmployee Values (1,'Emp5',NULL), (2,'Emp2',2),(3,'Emp3',3)
INSERT tblDept Values (1, 'HR'),(2, 'Finance'),(3,'IT'),(NULL,'Sales')
--INNER JOIN – Returns the common records between two tables
--LEFT JOIN - Returns all the rows from Left table and matching rows between two tables
--LEFT JOIN = INNER JOIN + Records from Left table
--RIGHT JOIN – Returns all the rows from Left table and matching rows
--between two tables
--RIGHT JOIN = INNER JOIN + Records from Right table
--FULL JOIN - Returns all the rows from two tables