-
Notifications
You must be signed in to change notification settings - Fork 0
/
Practicing SQL.sql
187 lines (141 loc) · 4.48 KB
/
Practicing SQL.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
/*
SQL Practice
*/
------------------------------------------------------------------------------------------------------------------------
-- Using the 'Having' clauses
SELECT JobTitle,
COUNT(JobTitle)
FROM EmployeeDemographics a
JOIN EmployeeSalary b
ON a.EmployeeID = b.EmployeeID
GROUP BY JobTitle
HAVING COUNT(JobTitle) > 1
SELECT JobTitle,
AVG(Salary)
FROM EmployeeDemographics a
JOIN EmployeeSalary b
ON a.EmployeeID = b.EmployeeID
GROUP BY JobTitle
HAVING AVG(Salary) > 44000
ORDER BY AVG(Salary)
------------------------------------------------------------------------------------------------------------------------
-- Using the 'Case' statement
SELECT FirstName,
LastName,
Age,
CASE WHEN Age > 30 THEN 'Old'
WHEN Age BETWEEN 27 AND 30 THEN 'Young'
END
FROM EmployeeDemographics
WHERE Age IS NOT NULL
ORDER BY Age
SELECT FirstName,
LastName,
JobTitle,
Salary,
CASE WHEN JobTitle = 'Salesman' THEN Salary + (Salary * 0.10)
WHEN JobTitle = 'Accountant' THEN Salary + (Salary * 0.05)
WHEN JobTitle = 'HR' THEN Salary + (Salary * 0.02)
ELSE Salary + (Salary * 0.03)
END As NewSalary
FROM EmployeeDemographics a
JOIN EmployeeSalary b
ON a.EmployeeID = b.EmployeeID
------------------------------------------------------------------------------------------------------------------------
-- The Partition By statement
SELECT FirstName,
LastName,
Gender,
Salary,
COUNT(Gender) OVER (PARTITION BY Gender) as TotalGender,
AVG(SALARY) OVER (PARTITION BY GENDER) AS AverageSalary
FROM EmployeeDemograghics a
JOIN EmployeeSalary b
ON a.EmployeeID = b.EmployeeID
WHERE Age > 31
------------------------------------------------------------------------------------------------------------------------
-- How to write subqueries
-- Subquery in Select statement
SELECT EmployeeID, Salary,
(SELECT AVG(Salary) FROM EmployeeSalary) AS AvgSalary
FROM EmployeeSalary;
-- Perform a subquery with partition by
SELECT EmployeeID, Salary, AVG(Salary) OVER () AS AvgSalary
FROM EmployeeSalary;
-- Subquery in the from statement
SELECT *
FROM (SELECT EmployeeID, Salary, AVG(Salary) OVER () AS AvgSalary
FROM EmployeeSalary) a
-- Subquery in the where statement
SELECT EmployeeID, JobTitle, Salary
FROM EmployeeSalary
WHERE Salary < 45000 And EmployeeID IN (
SELECT EmployeeID
FROM EmployeeDemograghics
WHERE Age > 30);
------------------------------------------------------------------------------------------------------------------------
-- Creating Temp Tables
CREATE TABLE #temp_employee
(
EmployeeID INT,
JobTitle VARCHAR(255),
Salary INT
)
INSERT INTO #temp_employee
SELECT *
FROM EmployeeSalary
CREATE TABLE #TempEmployee
(
JobTitle VARCHAR(50),
EmployeesPerJob INT,
AvgAge INT,
AvgSalary INT
)
INSERT INTO #TempEmployee
SELECT JobTitle, COUNT(JobTitle), AVG(Age), AVG(Salary)
FROM EmployeeDemograghics a
JOIN EmployeeSalary b
ON a.EmployeeID = b.EmployeeID
GROUP BY JobTitle
------------------------------------------------------------------------------------------------------------------------
-- Stored Procedures In SQL
CREATE PROCEDURE EmployeeData
AS
SELECT CONCAT(FirstName, ' ', LastName) AS EmployeeName, Age, JobTitle, Salary
FROM EmployeeDemograghics a
JOIN EmployeeSalary b
ON a.EmployeeID = b.EmployeeID;
-- Test the stored procedure
EXEC EmployeeData;
CREATE PROCEDURE Temp_Employee
AS
CREATE TABLE #temp_employee (JobTitle VARCHAR(255), EmployeesPerJob INT, AvgAge INT, AvgSalary INT)
INSERT INTO #temp_employee
SELECT JobTitle, COUNT(JobTitle), AVG(Age), AVG(Salary)
FROM EmployeeDemograghics a
JOIN EmployeeSalary b
ON a.EmployeeID = b.EmployeeID
GROUP BY JobTitle
SELECT *
FROM #temp_employee;
-- Test the Procedure
EXEC Temp_Employee
------------------------------------------------------------------------------------------------------------------------
-- How to use CTEs
WITH Employees AS (
SELECT FirstName,
LastName,
Gender,
Salary,
COUNT(Gender) OVER (PARTITION BY Gender) as TotalGender,
AVG(SALARY) OVER (PARTITION BY GENDER) AS AverageSalary
FROM EmployeeDemograghics a
JOIN EmployeeSalary b
ON a.EmployeeID = b.EmployeeID
WHERE Salary > 45000
)
SELECT LastName,
Gender,
Salary
FROM employees
------------------------------------------------------------------------------------------------------------------------