-
Notifications
You must be signed in to change notification settings - Fork 0
/
Advanced SQL Tutorial.sql
163 lines (113 loc) · 2.97 KB
/
Advanced SQL Tutorial.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
-- CTE (Common Table Expression)
WITH
CTE_Emplyee
AS
(
SELECT FirstName, LastName, JobTitle, Salary,
AVG(Salary) OVER (PARTITION BY Salary) AS AvgSalary,
COUNT(Gender) OVER (PARTITION BY Gender) AS CountGender
FROM EmployeeDemographics emp
JOIN EmployeeSalary sal
ON emp.EmployeeID = sal.EmployeeID
WHERE Salary >30000
)
SELECT AvgSalary
FROM CTE_Emplyee
-- Temp Tables
DROP TABLE IF EXISTS #temp_Employee
CREATE TABLE #temp_Employee
(
EmplyeeID INT,
JobTitle VARCHAR(50),
Salary INT,
)
INSERT INTO #temp_Employee
VALUES
(
10001, 'HR', 3000
)
INSERT INTO #temp_Employee
SELECT *
FROM EmployeeSalary
SELECT *
FROM #temp_Employee
-- String Functions + Use Cases
/*
Today's Topic: String Functions - 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
-- Using Trim, LTRIM, RTRIM
Select EmployeeID, TRIM(employeeID) AS IDTRIM
FROM EmployeeErrors
Select EmployeeID, RTRIM(employeeID) as IDRTRIM
FROM EmployeeErrors
Select EmployeeID, LTRIM(employeeID) as IDLTRIM
FROM EmployeeErrors
-- Using Replace
Select LastName, REPLACE(LastName, '- Fired', '') as LastNameFixed
FROM EmployeeErrors
-- Using Substring
Select Substring(err.FirstName,1,3), Substring(dem.FirstName,1,3), Substring(err.LastName,1,3), Substring(dem.LastName,1,3)
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)
-- Using UPPER and lower
Select firstname, LOWER(firstname)
from EmployeeErrors
Select Firstname, UPPER(FirstName)
from EmployeeErrors
-- Stored Procedures + Use Cases
-- CREATE PROCEDURE TEST
-- AS
-- SELECT *
-- FROM EmployeeDemographics
-- EXEC TEST
-- Subqueries
/*
Today's Topic: Subqueries (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
-- How to do 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 EmployeeID
-- Subquery in From
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 (
Select EmployeeID
From EmployeeDemographics
where Age > 30)