-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13-AdvancedORintelligentJoin.sql
More file actions
124 lines (105 loc) · 4.14 KB
/
13-AdvancedORintelligentJoin.sql
File metadata and controls
124 lines (105 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Advance or Intelligent joins in SQL Server
* Retrive only the non matching rows from the left table
* Retrive only the non matching rows from the right table
* Retrive only the non matching rows from both the left and right table
*/
--CONSTRAINT FK_PersonOrder
--FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);
create table GenderName
(
genderID int identity(1,1) NOT NULL unique,
gender nvarchar(10) NOT NULL PRIMARY KEY,
)
--Insert into gender Name table
insert into GenderName values('Male');
insert into GenderName values('Female');
insert into GenderName values('Unknown');
--Show data of gender name tabel
select * from GenderName
Create table Department
(
ID int NOT NULL identity(1,1) primary key,
DepartmentName nvarchar(50) NOT NULL DEFAULT('Other Department'),
Location nvarchar(50) NOT NULL,
DepartmentHead nvarchar(50) NOT NULL
)
--insert into Department table
insert into Department(DepartmentName, Location, DepartmentHead) values ('IT', 'London', 'Rick');
insert into Department(DepartmentName, Location, DepartmentHead) values ('Payroll', 'Dellhi', 'Ron');
insert into Department(DepartmentName, Location, DepartmentHead) values ('HR', 'NEW YORK', 'Chistie');
insert into Department(DepartmentName, Location, DepartmentHead) values ('Other Department', 'Sydney', 'Cindrella');
--Show department table data
select *from Department
/*
To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax:
MySQL / SQL Server / Oracle / MS Access:
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
CONSTRAINT FK_PersonOrder FOREIGN KEY (PersonID)
REFERENCES Persons(PersonID)
);
*/
create table ITfarm
(
ID int identity(1,1) NOT NULL PRIMARY key,
Name nvarchar(50) NOT NULL,
Gender nvarchar(10) NOT NULL CONSTRAINT FK_ITfarm_Gender foreign key References GenderName(gender),
Salary int NOT NULL Default(0) check( Salary > 0),
DepartmentID int NULL Constraint FK_IFARM_DepartmentID Foreign key References Department(ID)
)
--Insert into ITfarm
insert into ITfarm(Name, Gender, Salary, DepartmentID) values('Tom', 'Male', 4000,1);
insert into ITfarm(Name, Gender, Salary, DepartmentID) values('Pam', 'Female', 3000,3);
insert into ITfarm(Name, Gender, Salary, DepartmentID) values('John', 'Male', 3500, 1);
insert into ITfarm(Name, Gender, Salary, DepartmentID) values('Sam', 'Male', 4500, 2);
insert into ITfarm(Name, Gender, Salary, DepartmentID) values('Todd', 'Male', 2800, 2);
insert into ITfarm(Name, Gender, Salary, DepartmentID) values('Ben', 'Male', 7000, 1);
insert into ITfarm(Name, Gender, Salary, DepartmentID) values('Sara', 'Female', 4800, 3);
insert into ITfarm(Name, Gender, Salary, DepartmentID) values('Valarie', 'Female', 5500, 1);
insert into ITfarm(Name, Gender, Salary, DepartmentID) values('James', 'Male', 6500, NULL);
insert into ITfarm(Name, Gender, Salary, DepartmentID) values('Russel', 'Male', 8800, NULL);
--Show ITFarm data
select * from ITfarm
--Show department table data
select *from Department
--Show data of gender name tabel
select * from GenderName
/*
Non Matching rows left outer join
*/
select Name, gender, salary, DepartmentName, DepartmentHead, Location
from ITfarm
left outer join Department
ON ITfarm.DepartmentID = Department.ID
where ITfarm.DepartmentID is NULL
/*
James Male 6500 NULL NULL NULL
Russel Male 8800 NULL NULL NULL
*/
/*
Non Matching rows right outer join
*/
select Name, gender, salary, DepartmentName, DepartmentHead, Location
from ITfarm
right outer join Department
ON ITfarm.DepartmentID = Department.ID
where ITfarm.DepartmentID is NULL
--NULL NULL NULL Other Department Cindrella Sydney
/*
Non Matching rows full outer join
*/
select Name, gender, salary, DepartmentName, DepartmentHead, Location
from ITfarm
full outer join Department
ON ITfarm.DepartmentID = Department.ID
where ITfarm.DepartmentID is NULL
OR Department.ID is NULL
/*
James Male 6500 NULL NULL NULL
Russel Male 8800 NULL NULL NULL
NULL NULL NULL Other Department Cindrella Sydney
*/