-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdvanced.sql
More file actions
211 lines (158 loc) · 5.05 KB
/
Advanced.sql
File metadata and controls
211 lines (158 loc) · 5.05 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
/*CTE: act as subquery but doen't store in memory*/
WITH CTE_Employee AS (
SELECT FirstName, LastName, Gender, Salary
, COUNT(Gender) OVER (PARTITION BY Gender) AS TotalGender
, AVG(Salary) OVER (PARTITION BY Gender) AS AvgSalary
FROM SQLTutorial..Employee dem
JOIN SQLTutorial..EmployeeSalary sal
ON dem.EmployeeID=sal.EmployeeID
WHERE Salary > '45000'
)
SELECT FirstName, AvgSalary
FROM CTE_Employee
--we need to run with the entire cte everytime, and must be directly used after the cte is created.
/*
Temp table=temporary table but we can use it multiple times
*/
CREATE TABLE #temp_employee
(
EmployeeID int,
JobTitle varchar(100),
Salary int
)
Select * From #temp_employee--empty
INSERT INTO #temp_employee VALUES(
'1001', 'HR', '45000'
)
--or insert a whole table right away
INSERT INTO #temp_employee
SELECT * From SQLTutorial..EmployeeSalary
Select * From #temp_employee
DROP TABLE IF EXISTS #temp_employee2--to avoid we run into error after we created the table already
Create table #temp_employee2 (
JobTitle varchar(100),
EmployeesPerJob int ,
AvgAge int,
AvgSalary int
)
Insert into #temp_employee2
SELECT JobTitle, COUNT(JobTitle), AVG(Age), AVG(salary)
FROM SQLTutorial..Employee emp
JOIN SQLTutorial..EmployeeSalary sal
ON emp.EmployeeID = sal.EmployeeID
group by JobTitle
Select *
From #temp_employee2
--by placing the table in the temp table, we could save lots of computing time
SELECT AvgAge, AvgSalary
from #temp_employee2
/*String function: TRIM, LTRIM, RTRIM, Replace, Substring, UPPER, LOWER*/
--Drop Table EmployeeErrors;
CREATE TABLE EmployeeErrors (
EmployeeID varchar(50)
,FirstName varchar(50)
,LastName varchar(50)
)
Insert into EmployeeErrors Values
('1001 ', 'Jimbo', 'Halbert')
,(' 1002', 'Pamela', 'Beasely')
,('1005', 'TOby', 'Flenderson - Fired')
Select *
From EmployeeErrors
--we see that there's some whitespace or naming error
/*Using
Trim: remove all whitespace,
LTRIM: remove whitespace in the left side only,
RTRIM: remove whitespace in the right side only*/
Select EmployeeID, TRIM(employeeID) AS IDTRIM --so that we could compare the difference
FROM EmployeeErrors
Select EmployeeID, RTRIM(employeeID) as IDRTRIM
FROM EmployeeErrors
Select EmployeeID, LTRIM(employeeID) as IDLTRIM
FROM EmployeeErrors
/*Replace*/
Select LastName, REPLACE(LastName, '- Fired', '') as LastNameFixed--replace the -Fired to empty
FROM EmployeeErrors
/*Substring*/
Select Substring(err.FirstName,1,3), Substring(dem.FirstName,1,3), Substring(err.LastName,1,3), Substring(dem.LastName,1,3)
--select the first digit, and in total 3 digit
FROM EmployeeErrors err
JOIN EmployeeDemographics dem
on Substring(err.FirstName,1,3) = Substring(dem.FirstName,1,3)
and Substring(err.LastName,1,3) = Substring(dem.LastName,1,3)
/*UPPER and lower*/
Select firstname, LOWER(firstname)
from EmployeeErrors
Select Firstname, UPPER(FirstName)
from EmployeeErrors
/*
Stored Procedures
*/
CREATE PROCEDURE Temp_Employee
AS
DROP TABLE IF EXISTS #temp_employee
Create table #temp_employee (
JobTitle varchar(100),
EmployeesPerJob int ,
AvgAge int,
AvgSalary int
)
Insert into #temp_employee
SELECT JobTitle, Count(JobTitle), Avg(Age), AVG(salary)
FROM SQLTutorial..EmployeeDemographics emp
JOIN SQLTutorial..EmployeeSalary sal
ON emp.EmployeeID = sal.EmployeeID
group by JobTitle
Select *
From #temp_employee
EXEC Temp_Employee --the #temp_employee table will be returned
/*Modify stored procedure*/
ALTER PROCEDURE dbo.Temp_Employee
@JobTitle nvarchar(100)
AS
--DROP TABLE IF EXISTS #temp_employee
Create table #temp_employee (
JobTitle varchar(100),
EmployeesPerJob int ,
AvgAge int,
AvgSalary int
)
Insert into #temp_employee
SELECT JobTitle, Count(JobTitle), AVG(Age), AVG(salary)
FROM SQLTutorial..Employee emp
JOIN SQLTutorial..EmployeeSalary sal
ON emp.EmployeeID = sal.EmployeeID
where JobTitle = @JobTitle --- make sure to change this in this script from original above
--you can put as much parameters as you want
group by JobTitle
Select *
From #temp_employee3
exec Temp_Employee @jobtitle = 'Salesman'
exec Temp_Employee @jobtitle = 'Accountant'
/*
Subqueries: can use it everywhere, but here will be in the Select, From, and Where Statement
*/
Select EmployeeID, JobTitle, Salary
From EmployeeSalary
-- Subquery in Select
Select EmployeeID, Salary, (Select AVG(Salary) From EmployeeSalary) as AllAvgSalary
From EmployeeSalary
-- another way of doing it: with Partition By
Select EmployeeID, Salary, AVG(Salary) over () as AllAvgSalary
From EmployeeSalary
-- Why Group By doesn't work
Select EmployeeID, Salary, AVG(Salary) as AllAvgSalary
From EmployeeSalary
Group By EmployeeID, Salary
order by 1,2
-- Subquery in From: like cte or temp table, but subquery is slower so it's not recommended
Select a.EmployeeID, AllAvgSalary
From (Select EmployeeID, Salary, AVG(Salary) over () as AllAvgSalary From EmployeeSalary) a
Order by a.EmployeeID
-- Subquery in Where
Select EmployeeID, JobTitle, Salary
From EmployeeSalary
where EmployeeID in (--create subquery from here
Select EmployeeID
From Employee
where Age > 30)--but if you want to return age column, you need to join these 2 tables