-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLQueryLab11.sql
140 lines (122 loc) · 3.2 KB
/
SQLQueryLab11.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
CREATE TABLE Stu_Info (
Rno INT ,
Name VARCHAR(50),
Branch VARCHAR(50)
);
INSERT INTO Stu_Info (Rno, Name, Branch)
VALUES
(101, 'Raju', 'CE'),
(102, 'Amit', 'CE'),
(103, 'Sanjay', 'ME'),
(104, 'Neha', 'EC'),
(105, 'Meera', 'EE'),
(106, 'Mahesh', 'ME');
select * from Stu_Info;
CREATE TABLE Result (
RNO INT,
SPI DECIMAL(4, 2),
);
INSERT INTO Result (Rno, SPI)
VALUES
(101, 8.8),
(102, 9.2),
(103, 7.6),
(104, 8.2),
(105, 7.0),
(107, 8.9);
select * from Result;
CREATE TABLE EMPLOYEE_MASTER (
EmployeeNo VARCHAR(50) ,
Name VARCHAR(50),
ManagerNo VARCHAR(50)
);
INSERT INTO Employee_Master(EmployeeNo, Name, ManagerNo)
VALUES
('E01', 'Tarun', NULL),
('E02', 'Rohan', 'E02'),
('E03', 'Priya', 'E01'),
('E04', 'Milan', 'E03'),
('E05', 'Jay', 'E01'),
('E06', 'Anjana', 'E04');
select * from Employee_Master;
--Part – A:
--1. Combine information from student and result table using cross join or Cartesian product.
select * from STU_INFO
cross Join Result;
--2. Perform inner join on Student and Result tables.
select * from STU_INFO S
inner Join Result R
on S.RNo = R.RNo;
--3. Perform the left outer join on Student and Result tables.
select * from STU_INFO S
left outer Join Result R
on S.RNo = R.RNo;
--4. Perform the right outer join on Student and Result tables.
select * from STU_INFO S
right outer Join Result R
on S.RNo = R.RNo;
--5. Perform the full outer join on Student and Result tables.
select * from STU_INFO S
full outer Join Result R
on S.RNo = R.RNo;
--6. Display Rno, Name, Branch and SPI of all students.
select
S.RNo,S.Name,S.Branch,R.SPI
from STU_INFO S
left outer Join Result R
on S.RNo = R.RNo;
--7. Display Rno, Name, Branch and SPI of CE branch’s student only.
select
S.RNo,S.Name,S.Branch,R.SPI
from STU_INFO S
left outer Join Result R
on S.RNo = R.RNo
where S.Branch='CE';
--8. Display Rno, Name, Branch and SPI of other than EC branch’s student only.
select
S.RNo,S.Name,S.Branch,R.SPI
from STU_INFO S
left outer Join Result R
on S.RNo = R.RNo
where S.Branch != 'EC';
--9. Display average result of each branch.
select
S.Branch,avg(R.SPI)
from STU_INFO S
Join Result R
on S.RNo = R.RNo
Group by S.Branch;
--10. Display average result of CE and ME branch.
select
S.Branch,avg(R.SPI)
AS AVG_SPI
from STU_INFO S
Join Result R
on S.RNo = R.RNo
Group by S.Branch
having S.Branch ='CE' or S.Branch ='ME';
--Part – B:
--1. Display average result of each branch and sort them in ascending order by SPI.
select
S.Branch,avg(R.SPI)
from STU_INFO S
Join Result R
on S.RNo = R.RNo
Group by S.Branch
order by avg(R.SPI) Asc;
--2. Display highest SPI from each branch and sort them in descending order.
select
S.Branch,max(R.SPI)
from STU_INFO S
Join Result R
on S.RNo = R.RNo
Group by S.Branch
order by max(R.SPI) Desc;
--Part – C:
--1. Retrieve the names of employee along with their manager’s name from the Employee table.
select
A.Name as 'EMP_Name',
B.Name as 'Manager_Name'
from EMPLOYEE_MASTER A
inner join EMPLOYEE_MASTER B
on A.EmployeeNo = B.ManagerNo;