-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay16.sql
481 lines (373 loc) · 14.3 KB
/
Day16.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
use zoom;
-- Window Functions
/*
Definition:
Window functions are a type of SQL function that perform calculations across a set of table rows
that are somehow related to the current row. Unlike regular aggregate functions, which return a single
value for a group of rows, window functions return a value for each row in the result set.
Key Characteristics:
1. They do not collapse rows; each row retains its identity.
2. They are often used for running totals, moving averages, ranking, and other calculations
that require access to multiple rows.
Types of Window Functions :
1. Aggregate Functions: These functions perform calculations across a set of rows and return a single value.
Examples include SUM(), AVG(), COUNT(), etc.
2. Ranking Functions: These functions assign a rank to each row within a partition of a result set.
Examples include ROW_NUMBER(), RANK(), and DENSE_RANK().
3. Value Functions: These functions return a value from a specific row in the result set.
Examples include LEAD(), LAG(), FIRST_VALUE(), and LAST_VALUE().
4. Analytic Functions: These functions perform calculations across a set of rows related to the current row,
similar to aggregate functions but without collapsing the result set. Examples include NTILE().
The general syntax for a window function is as follows:
function_name(column_name) OVER (
[PARTITION BY partition_expression]
[ORDER BY order_expression]
[ROWS or RANGE frame_specification]
)
Note ---
Window functions are powerful tools in SQL that enable advanced data analysis without losing
the context of individual rows. They are particularly useful in reporting and analytical queries.
*/
-- Step 1: Create the emp11 Table
CREATE TABLE emp11 (
employee_id INT AUTO_INCREMENT PRIMARY KEY,
employee_name VARCHAR(100),
department VARCHAR(50),
salary DECIMAL(10, 2)
);
-- Step 2: Insert 10 Records into emp11
INSERT INTO emp11 (employee_name, department, salary) VALUES
('Alice Johnson', 'HR', 60000.00),
('Bob Smith', 'IT', 75000.00),
('Charlie Brown', 'IT', 80000.00),
('David Wilson', 'Finance', 70000.00),
('Eva Green', 'HR', 65000.00),
('Frank White', 'Finance', 72000.00),
('Grace Lee', 'IT', 78000.00),
('Hannah Scott', 'Marketing', 50000.00),
('Ian Black', 'Marketing', 52000.00),
('Jack Brown', 'Finance', 71000.00);
select * from emp11;
-- Step 3: Write Queries Using Window Functions
-- Example 1: Using ROW_NUMBER()
-- Assign a unique sequential integer to each row within a partition of a result set.
SELECT
employee_id,
employee_name,
department,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS ranks
FROM
emp11;
-- Example 2: Using RANK()
-- Ranks employees within each department based on their salary.
-- If two employees have the same salary, they receive the same rank, and the next rank is skipped.
SELECT
employee_id,
employee_name,
department,
salary,
RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS salary_rank
FROM
emp11;
-- Example 3: Using DENSE_RANK()
-- Similar to RANK(), but without gaps in the ranking.
SELECT
employee_id,
employee_name,
department,
salary,
DENSE_RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS dense_salary_rank
FROM
emp11;
-- Example 4: Using LAG()
-- Get the salary of the previous employee in the same department.
SELECT
employee_id,
employee_name,
department,
salary,
LAG(salary, 1) OVER (PARTITION BY department ORDER BY salary) AS previous_salary
FROM
emp11;
-- Example 5: Using LEAD()
-- Get the salary of the next employee in the same department.
SELECT
employee_id,
employee_name,
department,
salary,
LEAD(salary, 1) OVER (PARTITION BY department ORDER BY salary) AS next_salary
FROM
emp11;
-- Example 6: Using SUM()
-- Calculate a running total of salaries within each department.
SELECT
employee_id,
employee_name,
department,
salary,
SUM(salary) OVER (PARTITION BY department ORDER BY employee_id) AS running_total
FROM
emp11;
-- Example 7: Using AVG()
-- Calculate the average salary within each department.
SELECT
employee_id,
employee_name,
department,
salary,
AVG(salary) OVER (PARTITION BY department) AS average_salary
FROM
emp11;
-- Example 8: Using NTILE()
-- Divide employees into quartiles based on their salary.
SELECT
employee_id,
employee_name,
department,
salary,
NTILE(4) OVER (ORDER BY salary) AS salary_quartile
FROM
emp11;
-- Example 9: Using FIRST_VALUE()
-- Retrieve the first salary in each department.
SELECT
employee_id,
employee_name,
department,
salary,
FIRST_VALUE(salary) OVER (PARTITION BY department ORDER BY salary) AS first_salary
FROM
emp11;
-- Example 10: Using LAST_VALUE()
-- Retrieve the last salary in each department.
SELECT
employee_id,
employee_name,
department,
salary,
LAST_VALUE(salary) OVER (PARTITION BY department ORDER BY salary
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS last_salary
FROM
emp11;
-- Example 11: Using SUM() with a Frame Specification
-- Calculate a moving average of salaries over the last 3 employees in each department.
SELECT
employee_id,
employee_name,
salary,
AVG(salary) OVER (PARTITION BY department ORDER BY employee_id
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS moving_avg_salary
FROM
emp11;
-- Example 12: Using COUNT() for Cumulative Count
-- Count the number of employees in each department cumulatively.
SELECT
employee_id,
employee_name,
department,
COUNT(employee_id) OVER (PARTITION BY department ORDER BY employee_id
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cumulative_count
FROM
emp11;
-- types of cascades in mysql
-- ER-Diagram
-- ------------------------ More Examples to understand DCL Properly -----------------------
-- Table Setup
-- Step 1: Create the Employee1 table
CREATE TABLE Employee1 (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
position VARCHAR(100) NOT NULL,
salary DECIMAL(10, 2) NOT NULL,
department_id INT,
FOREIGN KEY (department_id) REFERENCES Departments1(id)
);
-- Step 2: Create the Departments1 table
CREATE TABLE Departments1 (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
-- Step 3: Create the Project1 table
CREATE TABLE Project1 (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
budget DECIMAL(10, 2) NOT NULL,
department_id INT,
FOREIGN KEY (department_id) REFERENCES Departments1(id)
);
-- Step 4: Create the Event1 table
CREATE TABLE Event1 (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
date DATE NOT NULL,
location VARCHAR(100) NOT NULL
);
-- Step 5: Create the Clients table
CREATE TABLE Clients (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
contact_info VARCHAR(100) NOT NULL
);
-- Step 6: Insert Sample Data into Departments1 Table
INSERT INTO Departments1 (name) VALUES
('Finance'),
('Sales'),
('IT'),
('Customer Support'),
('Legal');
-- Step 7: Insert Sample Data into Employee1 Table
INSERT INTO Employee1 (name, position, salary, department_id) VALUES
('Rajesh Kumar', 'Finance Manager', 90000.00, 1),
('Sneha Patel', 'Sales Executive', 50000.00, 2),
('Amit Sharma', 'Software Engineer', 75000.00, 3),
('Neha Singh', 'Customer Support Specialist', 40000.00, 4),
('Vikram Mehta', 'Legal Advisor', 85000.00, 5),
('Ravi Verma', 'Sales Manager', 95000.00, 2),
('Pooja Rani', 'HR Executive', 45000.00, 1),
('Karan Bhatia', 'IT Support', 55000.00, 3),
('Deepika Joshi', 'Marketing Manager', 70000.00, 3),
('Suresh Nair', 'Finance Analyst', 60000.00, 1);
-- Step 8: Insert Sample Data into Project1 Table
INSERT INTO Project1 (name, budget, department_id) VALUES
('Project C', 150000.00, 3), -- IT
('Project D', 200000.00, 1), -- Finance
('Project E', 75000.00, 2); -- Sales
-- Step 9: Insert Sample Data into Event1 Table
INSERT INTO Event1 (name, date, location) VALUES
('Quarterly Review', '2023-10-15', 'Conference Room B'),
('Diwali Celebration', '2023-11-12', 'Rooftop Garden'),
('Annual Picnic', '2023-12-20', 'City Park');
-- Step 10: Insert Sample Data into Clients Table
INSERT INTO Clients (name, contact_info) VALUES
('Client A', '[email protected]'),
('Client B', '[email protected]'),
('Client C', '[email protected]'),
('Client D', '[email protected]'),
('Client E', '[email protected]');
-- Example 1: Granting Permissions to Users
-- Step 1: Create new users
CREATE USER 'hr_user'@'localhost' IDENTIFIED BY 'hr_pass';
CREATE USER 'dev_user'@'localhost' IDENTIFIED BY 'dev_pass';
CREATE USER 'marketing_user'@'localhost' IDENTIFIED BY 'marketing_pass';
CREATE USER 'client_user'@'localhost' IDENTIFIED BY 'client_pass';
-- Step 2: Grant Permissions
GRANT SELECT, INSERT, UPDATE, DELETE ON Employee1 TO 'hr_user'@'localhost';
GRANT SELECT, INSERT, UPDATE, DELETE ON Departments1 TO 'hr_user'@'localhost';
GRANT SELECT, INSERT, UPDATE ON Project1 TO 'dev_user'@'localhost';
GRANT SELECT ON Employee1 TO 'dev_user'@'localhost';
GRANT SELECT, INSERT ON Event1 TO 'marketing_user'@'localhost';
GRANT SELECT ON Clients TO 'marketing_user'@'localhost';
GRANT SELECT ON Clients TO 'client_user'@'localhost';
-- Step 3: Verify Permissions
SHOW GRANTS FOR 'hr_user'@'localhost';
SHOW GRANTS FOR 'dev_user'@'localhost';
SHOW GRANTS FOR 'marketing_user'@'localhost';
SHOW GRANTS FOR 'client_user'@'localhost';
-- Example 2: Using Roles for Group Permissions
-- Step 1: Create Roles
CREATE ROLE 'HR_role';
CREATE ROLE 'Dev_role';
CREATE ROLE 'Marketing_role';
-- Step 2: Grant Permissions to Roles
GRANT SELECT, INSERT, UPDATE, DELETE ON Employee1 TO 'HR_role';
GRANT SELECT, INSERT, UPDATE ON Project1 TO 'Dev_role';
GRANT SELECT, INSERT ON Event1 TO 'Marketing_role';
-- Step 3: Assign Roles to Users
GRANT 'HR_role' TO 'hr_user'@'localhost';
GRANT 'Dev_role' TO 'dev_user'@'localhost';
GRANT 'Marketing_role' TO 'marketing_user'@'localhost';
-- Step 4: Verify Role Grants
SHOW GRANTS FOR 'hr_user'@'localhost';
SHOW GRANTS FOR 'dev_user'@'localhost';
SHOW GRANTS FOR 'marketing_user'@'localhost';
-- Example 3: Revoking All Permissions
-- Step 1: Revoke All Permissions from a User
REVOKE ALL PRIVILEGES ON Clients FROM 'client_user'@'localhost';
-- Step 2: Verify Revoked Permissions
SHOW GRANTS FOR 'client_user'@'localhost';
-- Example 4: Granting Permissions with Conditions
-- Hypothetical syntax for granting permissions with conditions (not supported in MySQL)
GRANT SELECT ON Employee1 TO 'hr_user'@'localhost' WHERE department_id = 1;
-- Example 5: Dropping Roles and Users
-- Step 1: Drop Roles
DROP ROLE 'HR_role';
DROP ROLE 'Dev_role';
DROP ROLE 'Marketing_role';
-- Step 2: Drop Users
DROP USER 'hr_user'@'localhost';
DROP USER 'dev_user'@'localhost';
DROP USER 'marketing_user'@'localhost';
DROP USER 'client_user'@'localhost';
-- Example 6: Creating Views for Restricted Access
-- Create a view for HR to see employee details without salary
CREATE VIEW EmployeeView AS SELECT id, name, position, department_id FROM Employee1;
-- Grant HR user access to the view
GRANT SELECT ON EmployeeView TO 'hr_user'@'localhost';
-- Example 7: Granting Permissions on Multiple Tables
-- Grant SELECT permission on multiple tables to the developer user
GRANT SELECT ON Employee1 TO 'dev_user'@'localhost';
GRANT SELECT ON Project1 TO 'dev_user'@'localhost';
GRANT SELECT ON Event1 TO 'dev_user'@'localhost';
-- Example 8: Revoking Permissions from Multiple Tables
-- Revoke SELECT permission from the developer user on the Event1 table
REVOKE SELECT ON Event1 FROM 'dev_user'@'localhost';
-- Example 9: Granting Permissions to a Group of Users
-- Create a new user for the marketing team
CREATE USER 'marketing_team_user'@'localhost' IDENTIFIED BY 'team_pass';
-- Grant the same permissions as the marketing user to the marketing team user
GRANT SELECT, INSERT ON Event1 TO 'marketing_team_user'@'localhost';
GRANT SELECT ON Clients TO 'marketing_team_user'@'localhost';
-- Example 10: Checking User Privileges
-- Check all privileges for a specific user
SHOW GRANTS FOR 'marketing_team_user'@'localhost';
-- Example 11: Using Stored Procedures for Permission Management
-- Create a stored procedure to grant permissions
DELIMITER //
CREATE PROCEDURE GrantPermissions(IN username VARCHAR(100))
BEGIN
SET @sql = CONCAT('GRANT SELECT ON Employee1 TO ', username, '@'localhost'');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
// DELIMITER ;
-- Call the stored procedure to grant permissions
CALL GrantPermissions('dev_user');
-- Example 12: Auditing Permissions
-- Create a table to log permission changes
CREATE TABLE PermissionAudit (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(100),
action VARCHAR(100), table_name VARCHAR(100),
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
);
-- Example of logging a permission change
INSERT INTO PermissionAudit (username, action, table_name) VALUES ('hr_user', 'GRANT', 'Employee1');
-- Example 13: Revoking All Permissions from a Role
-- Revoke all permissions from the marketing role
REVOKE ALL PRIVILEGES ON Event1 FROM 'Marketing_role';
-- Example 14: Granting Permissions with Time Constraints (Hypothetical)
-- Grant SELECT permission with a condition (hypothetical)
GRANT SELECT ON Employee1 TO 'hr_user'@'localhost' WHERE department_id = 1;
-- Example 16: Creating a Role Hierarchy
-- Step 1: Create Roles
CREATE ROLE 'Manager_role';
CREATE ROLE 'Employee_role';
-- Step 2: Grant Permissions to Roles
GRANT SELECT, INSERT, UPDATE ON Employee1 TO 'Employee_role';
GRANT SELECT, INSERT, UPDATE, DELETE ON Departments1 TO 'Manager_role';
GRANT 'Employee_role' TO 'Manager_role'; -- Manager inherits Employee permissions
-- Step 3: Assign Roles to Users
CREATE USER 'manager_user'@'localhost' IDENTIFIED BY 'manager_pass';
CREATE USER 'employee_user'@'localhost' IDENTIFIED BY 'employee_pass';
GRANT 'Manager_role' TO 'manager_user'@'localhost';
GRANT 'Employee_role' TO 'employee_user'@'localhost';
-- Step 4: Verify Role Grants
SHOW GRANTS FOR 'manager_user'@'localhost';
SHOW GRANTS FOR 'employee_user'@'localhost';
-- Example 17: Revoking Specific Permissions
-- Step 1: Revoke INSERT permission from the HR user
REVOKE INSERT ON Employee1 FROM 'hr_user'@'localhost';
-- Step 2: Verify Permissions
SHOW GRANTS FOR 'hr_user'@'localhost';