-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15 View, Saving Table and Transections Questions.sql
More file actions
207 lines (153 loc) · 4.67 KB
/
15 View, Saving Table and Transections Questions.sql
File metadata and controls
207 lines (153 loc) · 4.67 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
CREATE DATABASE CompanyDB;
USE CompanyDB;
-- Employees Table
CREATE TABLE Employees (
EmpID INT PRIMARY KEY,
EmpName VARCHAR(50),
DeptID INT,
Salary DECIMAL(10, 2),
JoinDate DATE
);
drop table Employees;
-- Departments Table
CREATE TABLE Departments (
DeptID INT PRIMARY KEY,
DeptName VARCHAR(50),
ManagerID INT
);
-- Transactions Table
CREATE TABLE Transactions (
TransID INT PRIMARY KEY,
EmpID INT,
Amount DECIMAL(10, 2),
TransDate DATE,
FOREIGN KEY (EmpID) REFERENCES Employees(EmpID)
);
-- Insert Sample Data
INSERT INTO Employees VALUES
(1, 'Alice', 101, 75000, '2020-01-15'),
(2, 'Bob', 102, 55000, '2019-05-20'),
(3, 'Charlie', 101, 60000, '2021-03-10'),
(4, 'Diana', 103, 80000, '2022-07-18'),
(5, 'Eve', 102, 90000, '2018-11-01');
INSERT INTO Departments VALUES
(101, 'HR', 1),
(102, 'Finance', 2),
(103, 'Engineering', 4);
INSERT INTO Transactions VALUES
(1, 1, 5000, '2023-01-01'),
(2, 2, 7000, '2023-02-10'),
(3, 3, 6000, '2023-03-05'),
(4, 4, 8000, '2023-04-12'),
(5, 5, 4500, '2023-05-20');
-- BASIC QUESTIONS--
-- 1.Create a View for High-Earning Employees (Salary > 70,000).
create View HighEarningEmployees as
select EmpID, EmpName, Salary
from Employees
where Salary > 70000;
-- 2.Select Data from the View.
select * from HighEarningEmployees;
-- 3.Create a View Showing Each Department’s Total Salary.
create View DeptSalary as
select DeptID, sum(Salary) as Total_Salary
from Employees
group by DeptID;
select * from DeptSalary;
-- 4.Update a View’s Data and Reflect Changes in the Underlying Table.
create View FinanceDept as
select EmpID, EmpName, Salary
from Employees
where DeptID = 102;
Update FinanceDept
Set Salary = 95000
Where EmpName = 'Bob';
select * from FinanceDept;
-- 5.Check All Views in the Database.
select Table_Name
from Information_Schema.Views
where Table_Schema = 'CompanyDB';
-- INTERMEDIATE QUESTIONS--
-- 6.Create a Transaction to Update Multiple Records in One Go.
Start transaction;
update Employees
Set Salary = Salary + 5000
where DeptId = 101;
Commit;
select * from Employees;
-- 7.Rollback a Transaction If a Condition Fails.
Start transaction;
Update Employees
Set Salary = Salary - 10000
where EmpName = 'Diana';
IF (SELECT Salary FROM Employees WHERE EmpName = 'Diana') < 70000 THEN
ROLLBACK;
ELSE
COMMIT;
END IF;
-- 8. Save a Table’s Data into Another Table for Archiving.
create Table ArchivedEmployees as
Select * from Employees where JoinDate < '2022-01-01';
select * from ArchivedEmployees;
-- 9.Create a Temporary Table for Staging Data.
create temporary Table TempTransaction as
select * from Transactions where TransDate > '2023-01-01';
select * from TempTransaction;
-- 10.Calculate Running Total of Transaction Amounts for Each Employee Using a View.
create View RunningTotalTransections as
select EmpID, TransDate, Amount,
Sum(Amount) over (partition by EmpID order by TransDate) as RunningTotal
from Transactions;
select * from RunningTotalTransections;
-- ADVANCED QUESTIONS--
-- 11.Use Nested Transactions with Savepoints.
Start transaction;
Savepoint BeforeUpdate;
Update Employees Set Salary = Salary + 10000 where DeptId = 101;
RollBack To Savepoint BeforeUpdate;
Commit;
select * from Employees;
-- 12.Optimize Performance with Indexed Views.
create view IndexedDeptSalary as
select DeptId, sum(Salary) as TotalSalary
from Employees
group by DeptID;
select * from IndexedDeptSalary;
-- 13.Log All Transactions into an Audit Table Using Triggers.
create table TransactionAudit(
AuditID int auto_increment primary key,
EmpID int,
Amount decimal(10, 2),
TransDate date,
AuditTime timestamp default current_timestamp
);
Delimiter &&
create Trigger AfterTransactionInsert
After Insert on Transactions
for each row
Begin
insert into TransactionAudit (EmpID, Amount, TransDate)
values (New.EmpID, 'Insert', now()),
values (New.Amount, 'insert', now()),
values (New.TransDate, 'insert', now());
END
Delimiter &&;
select * from TransactionAudit;
-- 14.Find Gaps in Employee IDs Using a View.
create view GapInEmpID as
select EmpID, EmpID - Row_Number() over (order by EmpID) as GapGroup
from employees;
select * from GapInEmpID;
-- 15.Perform a Transaction Across Multiple Tables (Atomicity).
start Transaction;
update Employees
set Salary = Salary - 4500
where EmpID = 5;
insert into Transactions (EmpID, Amount, TransDate)
Values (5, -4500, curdate());
RollBack;
Commit;
alter table Transactions
modify column TransID int auto_increment;
SELECT * FROM Employees WHERE EmpID = 5;
SELECT * FROM Transactions;