0% found this document useful (0 votes)
39 views7 pages

What Are Joins in SQL Server

The document discusses different types of joins in SQL Server including inner, left, right, full, and self joins. It provides the syntax and examples of each join type. Tables called Employee and Department are created and sample data is inserted to demonstrate inner, left, right, and full outer joins.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views7 pages

What Are Joins in SQL Server

The document discusses different types of joins in SQL Server including inner, left, right, full, and self joins. It provides the syntax and examples of each join type. Tables called Employee and Department are created and sample data is inserted to demonstrate inner, left, right, and full outer joins.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

What Are Joins In SQL Server ?

 Joins are used to retrieve data from two or more tables


based on a logical relationship between tables.
 SQL Server joins are used to combine data of two or more
tables in a single result-set.
 It defines the manner in which two tables are related in a
query by specifying the column from each table to be used
for the join. A typical join specifies a foreign key from one
table and its associated key in the other table
Types Of Joins In SQL Server
1. Inner Join
2. Left Join / Left Outer Join
3. Right Join / Outer Join
4. Full Join / Full Outer Join
5. Self Join
Inner Join In SQL Server
 SQL Server inner join is formed when records from two
tables are combined only if the rows from both the tables
are matched based on a common column.
 Inner join does same thing as we learn in Intersection In
Sets in Mathematics.
Syntax of an inner join is as follows:
SELECT <ColumnName1>, <ColumnName2>...<ColumnNameN> FROM
Table_A AS Table_Alias_A
INNER JOIN
Table_B AS Table_Alias_B
ON
Table_Alias_A.<CommonColumn> = Table_Alias_B.<CommonColumn>

Create And Insert Query for Employee Table


create table Employee
(
EmpId int primary key,
EmpName varchar(50),
Gender varchar(50),
Age int
);

select * from Employee;

insert into Employee values(11,'Ali','Male',23);


insert into Employee values(22,'Zain','Male',25);
insert into Employee values(33,'Osama','Male',33);
insert into Employee values(44,'Anum','Female',24);
insert into Employee values(55,'Umar','Male',27);

Create And Insert Query for Department Table


create table Department
(
Dpt_Id int primary key,
Dpt_Name varchar(50),
Dpt_Salary int,
EmpId int
);

insert into Department values(5001,'Administration',30000,22);


insert into Department values(5002,'Accounts',35000,11);
insert into Department values(5003,'I-T',40000,44);
insert into Department values(5004,'Counselling',38000,33);
insert into Department values(5005,'Sports',48000,88);

Inner Join Query - Example No: 1


select * from Employee as A
inner join Department as B
on A.EmpId = B.EmpId;

Inner Join Query - Example No: 2


select A.EmpName, A.Gender, B.Dpt_Name,B.Dpt_Salary from Employee as A
inner join Department as B
on A.EmpId = B.EmpId;

Left Outer / Left Join In SQL Query


 SQL Server left outer join returns all the records from the
left table and only matching records from the right table.

syntax of an outer join is as follows:


SELECT <ColumnList> FROM
Table_A AS Table_Alias_A
LEFT OUTER JOIN
Table_B AS Table_Alias_B
ON
Table_Alias_A.<CommonColumn> = Table_Alias_B.<CommonColumn>

Left Outer Join Query - Example No: 1


select * from Employee as A
left join Department as B
on A.EmpId = B.EmpId;

Left Outer Join Query - Example No: 2


select A.EmpName, A.Gender, B.Dpt_Name,B.Dpt_Salary from Employee as A
left outer join Department as B
on A.EmpId = B.EmpId;

Right Outer / Right Join In SQL Query


 The right outer join retrieves all the records from the
second table in the join regardless of whether there is
matching data in the first table or not.

The syntax of a right outer join is as follows:


SELECT <ColumnList>
FROM Left_Table_Name
AS
Table_A AS Table_Alias_A
RIGHT OUTER JOIN
Table_B AS Table_Alias_B
ON
Table_Alias_A.<CommonColumn> = Table_Alias_B.<CommonColumn>

Right Outer Join Query - Example No: 1


select * from Employee as A
right join Department as B
on A.EmpId = B.EmpId;
Right Outer Join Query - Example No: 2
select A.EmpName, A.Gender, B.Dpt_Name,B.Dpt_Salary from Employee as A
right join Department as B
on A.EmpId = B.EmpId;

Full Outer Join / Full Join In SQL Query


 SQL Server Full outer join returns all the records from the
left table as well as from the right table and matching
records from both the table.
 Infact, it returns all the data.

Full Outer Join Query - Example


select * from Employee as A
full join Department as B
on A.EmpId = B.EmpId;

SQL Server Joins


Self Join In SQL Server
 In SQL Server, a self JOIN is a regular join, but the table is
joined with itself.
 A self-join is used to find records in a table that are related
to other records in the same table. A table is joined to itself
in a self-join.
Before Self Join
After Self Join

SQL Query For SQL Join


SELECT A.emp_name as Employee, B.emp_name as Manager
from emp_tbl as A
inner join emp_tbl as B
on A.manager_id = B.emp_id;

You might also like