Introduction
In this blog, we will understand what a SQL Join is and how to join two or more SQL tables without using a foreign key. We will look into the various types of join as well.
Step 1
Table-1 Employee
- CREATE TABLE [dbo].[Employee](
- [EmployeeId] [int] IDENTITY(1,1) NOT NULL,
- [Name] [nvarchar](50) NULL,
- [Gender] [char](10) NULL,
- [Position] [nvarchar](50) NULL,
- [Salary] [int] NULL,
- [Department_Id] [int] NULL,
- [Incentive_Id] [int] NULL,
- PRIMARY KEY CLUSTERED
- (
- [EmployeeId] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- GO
Table-2 Department
- CREATE TABLE [dbo].[Department](
- [DepartmentId] [int] IDENTITY(1,1) NOT NULL,
- [DepartmentName] [nvarchar](50) NULL,
- PRIMARY KEY CLUSTERED
- (
- [DepartmentId] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- GO
Table-3 Incentive
- CREATE TABLE [dbo].[Incentive](
- [IncentiveId] [int] IDENTITY(1,1) NOT NULL,
- [IncentiveAmount] [int] NULL,
- PRIMARY KEY CLUSTERED
- (
- [IncentiveId] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- GO
Step 2 - Insert some demo data to all three tables
Insert records into the employee table.
- /*INESRT VALUES IN EMPLOYEE TABLE*/
- insert into Employee values('Anisha Agarwal','Female','Sales Excutive',30000,6,3)
- insert into Employee values('Manish Agarwal','Male','Accountant',40000,1,6)
- insert into Employee values('Fayaz Ansari','Male','UI Developer',50000,3,8)
- insert into Employee values('Rahul Sharma','Male','Software Engineer',45000,3,8)
- insert into Employee values('Abdul Rahim','Male','HR',30000,3,5)
- insert into Employee values('Arvind Kumar','Male','HR',32000,3,5)
- insert into Employee values('Priya Jain','Female','Marketing',25000,4,4)
- insert into Employee values('Zoya','Female','Sales Excutive',30000,6,3)
- insert into Employee values('Monika Agarwal','Female','Marketing',25000,4,4)
- insert into Employee values('Suresh Kumar','Male','Assistant',20000,null,4)




Sreekanth ReddyPosted Jan 17, 2019, 6:05 AM
What is the approach you think, when you join multiple joins ? Do you think any parameters like joining 1st and 2nd next 2nd and 3rd or joining 1st and 3rd next 3rd and 2nd tables (I mean about order if all are having the ralationship) ?