0% found this document useful (0 votes)
3 views

SQL - JOINS

Uploaded by

Aparna Panya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

SQL - JOINS

Uploaded by

Aparna Panya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

/*

*****************************************************************************************
******************
Data Combining - Data Joining - Data Merging
*****************************************************************************************
******************
Data Combining is the process bringing data from two or more tables.
There are two different types of Data Combining
1. Horizontal Combining(Merging)
2. Vertical Combining(Appending)

Note: If the data is coming from more than one table, then make sure there is Data
Combining

Horizontal Combining(Merging): If the two tables are having different structure and
different kind
of data, then we can use Horizontal Combining(Merging)

Note: There should be common column between two tables

The following are the different types of joins in Horizontal Combining(Merging)


1. Inner Join: It is used to get only matching records between two tables
2. Left Join: It is used to get all the records from left side table, and matching
records from other table
3. Right Join: It is used to get all the records from right side table, and matching
records from other table
4. Full Join: It is used to get all the record from both of the tables

syn:
SELECT t1.*,t2.*
FROM table1 AS t1 <type of join> table2 AS t2 ON t1.commonColumn=t2.commonColumn
*/
USE TARGET
DROP TABLE department
DROP TABLE employee

--Creating employee and department tables in the TARGET (If doesn't exist, create
database) database
CREATE TABLE employee(
empid INT,
ename VARCHAR(50),
salary MONEY,
gender VARCHAR(30),
dno INT
)

CREATE TABLE department(


dno INT,
dname VARCHAR(50),
location VARCHAR(50)
)

SELECT * FROM employee


SELECT * FROM department
--Feeding data into employee and department tables
INSERT INTO employee VALUES
(101,'bhaskar',5000,'male',10),
(102,'siri',4000,'female',20),
(103,'raja',1000,'male',30),
(104,'sandy',1000,'female',50),
(105,'hari',3000,'male',60)

INSERT INTO department VALUES


(10,'Sales','Hyderabad'),
(20,'HR','Pune'),
(30,'Finance','Chennai'),
(40,'Research','Bangalore')

/*
syn:
SELECT t1.*, t2.*
FROM <table1> AS t1 <Join Type> <table2> AS t2 ON t1.commonColumn=t2.commonColumn
*/

--Inner Join
SELECT
e.*,
d.*
FROM employee AS e INNER JOIN department AS d ON e.dno=d.dno

--Left Join
SELECT
e.*,
d.*
FROM employee AS e LEFT JOIN department AS d ON e.dno=d.dno

--Right Join
SELECT
e.*,
d.*
FROM employee AS e RIGHT JOIN department AS d ON e.dno=d.dno

--Full Join
SELECT
e.*,
d.*
FROM employee AS e FULL JOIN department AS d ON e.dno=d.dno

-----------------
--Write a SQL Statement to get employee details along with Department details
--EmployeeNo, Ename, Job, DepartmentNo, Dname, Location, Salary, Commission
USE DATASTORE
SELECT
e.EmployeeNo,
e.Ename,
e.Job,
e.DepartmentNo,
d.Dname,
d.Location,
e.Salary,
e.Commission
FROM employee AS e INNER JOIN department AS d ON e.DepartmentNo=d.DepartmentNo

You might also like