-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_Practice_Questions.sql
More file actions
622 lines (483 loc) · 10.7 KB
/
Copy path02_Practice_Questions.sql
File metadata and controls
622 lines (483 loc) · 10.7 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
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/*
1. Produce a report showing every employee in the company.
*/
SELECT *
FROM Employees;
/*
2. Show only each employee's first and last names.
*/
SELECT
FirstName,
LastName
FROM Employees;
/*
3. Display the names and salaries of employees.
*/
SELECT
FirstName,
LastName,
Salary
FROM Employees;
/*
4. Find every employee who works in Lagos.
*/
SELECT
FirstName,
LastName,
City
FROM Employees
WHERE City = 'Lagos';
/*
5. Identify employees who earn more than 5000.
*/
SELECT
FirstName,
LastName,
Salary
FROM Employees
WHERE Salary > 5000;
/*
6. Display employees from the youngest to the oldest.
*/
SELECT
FirstName,
LastName,
Age
FROM Employees
ORDER BY Age ASC;
/*
7. Arrange employees by salary from highest to lowest.
*/
SELECT *
FROM Employees
ORDER BY Salary DESC;
/*
8. Show employees who have worked for more than five years.
*/
SELECT
FirstName,
LastName,
YearsAtCompany
FROM Employees
WHERE YearsAtCompany > 5;
/*
9. Display all employees in the Finance department.
*/
SELECT
FirstName,
LastName,
Department
FROM Employees
WHERE Department = 'Finance';
/*
10. Find employees whose performance rating is 5.
*/
SELECT *
FROM Employees
WHERE PerformanceRating = 5;
/*
11. Produce a report showing the total payroll for each department.
*/
SELECT
Department,
SUM(Salary) AS TotalSalary
FROM Employees
GROUP BY Department;
/*
12. Determine the average salary in every city.
*/
SELECT
City,
AVG(Salary) AS AverageSalary
FROM Employees
GROUP BY City;
/*
13. Find the highest salary in each department.
*/
SELECT
Department,
MAX(Salary) AS MaxSalary
FROM employees
GROUP BY Department;
/*
14. Find the youngest employee in every department.
*/
SELECT
Department,
MIN(Age) AS LowestAge
FROM employees
GROUP BY Department;
/*
15. Count how many employees work in each city.
*/
SELECT
City,
COUNT(EmployeeID) AS EmployeesByCity
FROM Employees
GROUP BY City;
/*
16. Determine the total number of years employees have spent in each department.
*/
SELECT
Department,
SUM(YearsAtCompany) AS TotalYearsInDepartment
FROM Employees
GROUP BY Department;
/*
17. Display only the different departments in the company.
*/
SELECT DISTINCT
Department
FROM Employees;
/*
18. Produce a list of all cities represented in the company without duplicates.
*/
SELECT DISTINCT
City
FROM Employees;
/*
19. Display the different job titles available in the company.
*/
SELECT DISTINCT
JobTitle
FROM Employees;
/*
20. Show the first five employees in the table.
*/
SELECT TOP 5
*
FROM Employees;
/*
21. Display the five highest-paid employees.
*/
SELECT TOP 5
*
FROM Employees
ORDER BY Salary DESC;
/*
22. Show the three youngest employees.
*/
SELECT TOP 3
*
FROM Employees
ORDER BY Age ASC;
/*
23. Display the ten employees who have been with the company the longest.
*/
SELECT TOP 10
*
FROM Employees
ORDER BY YearsAtCompany DESC;
/*
24. Determine which departments have more than four employees.
*/
SELECT
Department,
COUNT(EmployeeID) AS EmployeesCount
FROM Employees
GROUP BY Department
HAVING COUNT(EmployeeID) > 4;
/*
25. Find cities whose average salary is greater than 4500.
*/
SELECT
City,
AVG(Salary) AS AvergeSalary
FROM Employees
GROUP BY City
HAVING AVG(Salary) > 4500;
/*
26. Identify departments whose total payroll exceeds 20000.
*/
SELECT
Department,
SUM(Salary) AS TotalSalary
FROM Employees
GROUP BY Department
HAVING SUM(Salary) > 20000;
/*
27. Determine which performance ratings have more than three employees.
*/
SELECT
PerformanceRating,
COUNT(EmployeeID) AS NumberOfEmployees
FROM Employees
GROUP BY PerformanceRating
HAVING COUNT(EmployeeID) > 3;
/*
28. Find cities that have fewer than six employees.
*/
SELECT
City,
COUNT(EmployeeID) AS TotalEmployees
FROM Employees
GROUP BY City
HAVING COUNT(EmployeeID) < 6;
/*
29. Produce a report showing departments with an average employee age greater than 33.
*/
SELECT
Department,
AVG(Age) AS AverageAge
FROM Employees
GROUP BY Department
HAVING AVG(Age) > 33;
/*
30. Create a new table called Departments.
31. Design the Departments table to store a department ID, department name, and office location.
*/
CREATE TABLE Departments (
DepartmentID INT PRIMARY KEY,
DepartmentName VARCHAR(50) NOT NULL,
OfficeLocation VARCHAR(50) NOT NULL
);
/*
32. Create a table named Projects with columns suitable for storing project information.
*/
CREATE TABLE Projects (
id INT NOT NULL
);
/*
33. Add a column named Email to the Employees table.
*/
ALTER TABLE Employees
ADD Email VARCHAR(30);
/*
34. Add a column named DateHired to the Employees table.
*/
ALTER TABLE Employees
ADD DateHired DATE;
/*
35. Change the length of the JobTitle column so it can store longer job titles.
*/
ALTER TABLE Employees
ALTER COLUMN JobTitle VARCHAR (100);
/*
36. Remove the Email column from the Employees table.
*/
ALTER TABLE Employees
DROP COLUMN Email;
SELECT *
FROM Employees
/*
37. Insert a new employee into the Employees table.
*/
INSERT INTO Employees (EmployeeID, FirstName, LastName, Department, JobTitle, City, Salary, Age, YearsAtCompany, PerformanceRating, DateHired)
VALUES
(26, 'Joe', 'Patrick', 'Finance', 'Financial Analyst', 'Lagos', 2300, 23, 1, 2, '2025-07-22');
/*
38. Insert three new employees in a single statement.
*/
INSERT INTO Employees
VALUES
(27, 'Aaron', 'Chidi', 'IT', 'Network Engineer', 'Port Harcourt', 6500, 45, 12, 5, '2012-07-23'),
(28, 'Peter', 'Nnamdi', 'Sales', 'Sales Executive', 'Port Harcourt', 4500, 35, 6, 3, '2020-07-23'),
(29, 'Sharon', 'Letila', 'IT', 'Software Engineer', 'Abuja', 4500, 30, 4, 5, '2022-07-23');
/*
39. Add two new departments to the Departments table.
*/
INSERT INTO Departments (DepartmentID, DepartmentName, OfficeLocation)
VALUES
(30, 'Sports', 'Abuja'),
(31, 'Entertainment', 'Lagos');
/*
40. Correct the salary of John Smith to 4200.
*/
UPDATE Employees
SET Salary = 4200
WHERE EmployeeID = 1;
/*
41. Increase every employee's salary by 500.
*/
UPDATE Employees
SET Salary = Salary + 500
WHERE EmployeeID BETWEEN 1 AND 29;
/*
42. Change the city of every employee currently in Kano to Abuja.
*/
UPDATE Employees
SET CITY = 'Abuja'
WHERE CITY = 'Kano';
/*
43. Update the performance rating of all employees in the IT department to 5.
*/
UPDATE Employees
SET PerformanceRating = 5
WHERE Department = 'IT';
/*
44. Rename the Marketing department to Business Development.
*/
UPDATE Employees
SET DEPARTMENT = 'Business Development'
WHERE DEPARTMENT = 'Marketing';
/*
45. Delete the employee whose EmployeeID is 25.
*/
DELETE FROM Employees
WHERE EmployeeID = 25;
/*
46. Remove all employees working in the HR department.
*/
DELETE FROM Employees
WHERE Department = 'HR';
/*
47. Delete employees who have worked for less than two years.
*/
DELETE FROM Employees
WHERE YearsAtCompany < 2;
/*
48. Remove every employee with a performance rating of 2.
*/
DELETE FROM Employees
WHERE PerformanceRating = 2;
/*
49. Empty all records from the Projects table without deleting the table itself.
*/
TRUNCATE TABLE Projects;
/*
50. Remove every record from the Departments table while keeping its structure.
*/
TRUNCATE TABLE Departments;
/*
51. Permanently remove the Projects table from the database.
*/
DROP TABLE IF EXISTS Projects;
/*
52. Permanently remove the Departments table.
*/
DROP TABLE IF EXISTS Departments;
/*
53. Create a new database named HRManagement.
*/
CREATE DATABASE HRManagement;
/*
54. Switch to the HRManagement database.
*/
USE HRManagement;
/*
55. Create an Employees table inside the HRManagement database with columns similar to those in the practice database.
*/
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Age INT NOT NULL,
Firstname VARCHAR(50) NOT NULL,
Lastname VARCHAR(50) NOT NULL
);
/*
56. Delete the HRManagement database.
*/
USE master
DROP DATABASE HRManagement;
/*
57. Display all employees whose salary is between 4000 and 5500.
*/
SELECT *
FROM Employees
WHERE Salary BETWEEN 4000 AND 5500;
/*
58. Find employees whose first name starts with the letter J.
*/
SELECT *
FROM Employees
WHERE FirstName LIKE 'J%';
/*
59. Display employees whose last name ends with son.
*/
SELECT *
FROM Employees
WHERE LastName LIKE '%son';
/*
60. Show employees who work in either Lagos or Abuja.
*/
SELECT
FirstName,
LastName,
City
FROM Employees
WHERE City = 'Lagos' OR City = 'Abuja';
/*
61. Produce a report showing the average salary for each department, beginning with the highest average salary.
*/
SELECT
Department,
AVG(Salary) AS AverageSalary
FROM Employees
GROUP BY Department
ORDER BY AVG(Salary) DESC;
/*
62. Count how many employees have each performance rating and display the ratings from most common to least common.
*/
SELECT
PerformanceRating,
COUNT(PerformanceRating) AS NoOfOccurence
FROM EMPLOYEES
GROUP BY PerformanceRating
ORDER BY NoOfOccurence DESC;
/*
63. Find the department that pays the highest individual salary.
*/
SELECT TOP 1
Department,
Salary
FROM EMPLOYEES
ORDER BY Salary DESC;
/*
64. Determine which city has the largest payroll.
*/
SELECT TOP 1
city,
SUM(Salary) AS TotalSalary
FROM Employees
GROUP BY City
ORDER BY TotalSalary DESC;
/*
65. Display the oldest five employees.
*/
SELECT TOP 5
FirstName,
LastName,
Age
FROM employees
ORDER BY AGE DESC;
/*
66. Produce a report showing every department and its highest performance rating.
*/
SELECT
Department,
MAX(PerformanceRating) AS HighestRating
FROM Employees
GROUP BY Department;
/*
67. Find departments where the oldest employee is at least 40 years old.
*/
SELECT
Department,
MAX(Age) AS OldestAge
FROM Employees
GROUP BY Department
HAVING MAX(Age) >= 40;
/*
68. Determine which cities have an average employee age below 30.
*/
SELECT
City,
AVG(Age)
FROM Employees
GROUP BY City
HAVING AVG(Age) < 30;
/*
69. Display the departments in alphabetical order without duplicates.
*/
SELECT DISTINCT
Department
FROM Employees
ORDER BY Department ASC;
/*
70. Produce a report showing the total number of employees in each department, beginning with the department that has the most employees.
*/
SELECT
Department,
COUNT(EmployeeID) AS TotalEmployees
FROM Employees
GROUP BY Department
ORDER BY TotalEmployees DESC;