-
Notifications
You must be signed in to change notification settings - Fork 48
/
joins.sql
58 lines (46 loc) · 1.83 KB
/
joins.sql
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
-- creating basic stuff to work on them
create database cheatsheet;
use cheatsheet;
create table employees
(
eid int primary key,
fname varchar(20),
lname varchar(20),
salary real,
joindate date,
dept varchar(10),
gender char
);
create table project
(
pid int primary key,
eid int,
pname varchar(20)
);
alter table project add foreign key(eid) references employees(eid);
set foreign_key_checks = 0;
insert into employees values (1, "Vikas", "Ahlawat", 600000, "2013-02-15", "IT", "M");
insert into employees values (2, "nikita", "Jain", 530000, "2014-01-09", "HR", "F");
insert into employees values (3, "Ashish", "Kumar", 1000000, "2014-01-09", "IT", "M");
insert into employees values (4, "Nikhil", "Sharma", 480000, "2014-01-09", "HR", "M");
insert into employees values (5, "anish", "kadian", 500000, "2014-01-09", "Payroll", "M");
insert into project values (1, 1, "Task Track");
insert into project values (2, 1, "CLP");
insert into project values (3, 1, "Survey Management");
insert into project values (4, 2, "HR Management");
insert into project values (5, 3, "Task Track");
insert into project values (6, 3, "GRS");
insert into project values (7, 3, "DDS");
insert into project values (8, 4, "HR Management");
insert into project values (9, 6, "GL Management");
set foreign_key_checks = 1;
-- inner join
select e.fname, p.pname from employees as e inner join project as p on e.eid = p.eid;
-- outer join
select e.fname, p.pname from employees as e left outer join project as p on e.eid = p.eid
union
select e.fname, p.pname from employees as e right outer join project as p on e.eid = p.eid;
-- left outer join
select e.fname, p.pname from employees as e left outer join project as p on e.eid = p.eid;
-- right outer join
select e.fname, p.pname from employees as e right outer join project as p on e.eid = p.eid;