Skip to content

Commit 203269c

Browse files
authored
Update Advanced.sql
1 parent b7f07a6 commit 203269c

File tree

1 file changed

+93
-3
lines changed

1 file changed

+93
-3
lines changed

Advanced.sql

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ from #temp_employee2
6464

6565

6666

67-
/*String function*/
67+
/*String function: TRIM, LTRIM, RTRIM, Replace, Substring, UPPER, LOWER*/
6868

6969
--Drop Table EmployeeErrors;
7070
CREATE TABLE EmployeeErrors (
@@ -95,12 +95,10 @@ FROM EmployeeErrors
9595
Select EmployeeID, LTRIM(employeeID) as IDLTRIM
9696
FROM EmployeeErrors
9797

98-
9998
/*Replace*/
10099
Select LastName, REPLACE(LastName, '- Fired', '') as LastNameFixed--replace the -Fired to empty
101100
FROM EmployeeErrors
102101

103-
104102
/*Substring*/
105103
Select Substring(err.FirstName,1,3), Substring(dem.FirstName,1,3), Substring(err.LastName,1,3), Substring(dem.LastName,1,3)
106104
--select the first digit, and in total 3 digit
@@ -118,4 +116,96 @@ from EmployeeErrors
118116

119117

120118

119+
/*
120+
Stored Procedures
121+
*/
122+
123+
CREATE PROCEDURE Temp_Employee
124+
AS
125+
DROP TABLE IF EXISTS #temp_employee
126+
Create table #temp_employee (
127+
JobTitle varchar(100),
128+
EmployeesPerJob int ,
129+
AvgAge int,
130+
AvgSalary int
131+
)
132+
133+
Insert into #temp_employee
134+
SELECT JobTitle, Count(JobTitle), Avg(Age), AVG(salary)
135+
FROM SQLTutorial..EmployeeDemographics emp
136+
JOIN SQLTutorial..EmployeeSalary sal
137+
ON emp.EmployeeID = sal.EmployeeID
138+
group by JobTitle
139+
140+
Select *
141+
From #temp_employee
142+
143+
EXEC Temp_Employee --the #temp_employee table will be returned
144+
145+
146+
147+
/*Modify stored procedure*/
148+
ALTER PROCEDURE dbo.Temp_Employee
149+
@JobTitle nvarchar(100)
150+
AS
151+
--DROP TABLE IF EXISTS #temp_employee
152+
Create table #temp_employee (
153+
JobTitle varchar(100),
154+
EmployeesPerJob int ,
155+
AvgAge int,
156+
AvgSalary int
157+
)
158+
159+
Insert into #temp_employee
160+
SELECT JobTitle, Count(JobTitle), AVG(Age), AVG(salary)
161+
FROM SQLTutorial..Employee emp
162+
JOIN SQLTutorial..EmployeeSalary sal
163+
ON emp.EmployeeID = sal.EmployeeID
164+
where JobTitle = @JobTitle --- make sure to change this in this script from original above
165+
--you can put as much parameters as you want
166+
group by JobTitle
167+
168+
Select *
169+
From #temp_employee3
170+
171+
exec Temp_Employee @jobtitle = 'Salesman'
172+
exec Temp_Employee @jobtitle = 'Accountant'
173+
174+
175+
176+
177+
178+
/*
179+
Subqueries: can use it everywhere, but here will be in the Select, From, and Where Statement
180+
*/
181+
Select EmployeeID, JobTitle, Salary
182+
From EmployeeSalary
183+
184+
-- Subquery in Select
185+
Select EmployeeID, Salary, (Select AVG(Salary) From EmployeeSalary) as AllAvgSalary
186+
From EmployeeSalary
187+
188+
-- another way of doing it: with Partition By
189+
Select EmployeeID, Salary, AVG(Salary) over () as AllAvgSalary
190+
From EmployeeSalary
191+
192+
-- Why Group By doesn't work
193+
Select EmployeeID, Salary, AVG(Salary) as AllAvgSalary
194+
From EmployeeSalary
195+
Group By EmployeeID, Salary
196+
order by 1,2
197+
198+
199+
-- Subquery in From: like cte or temp table, but subquery is slower so it's not recommended
200+
Select a.EmployeeID, AllAvgSalary
201+
From (Select EmployeeID, Salary, AVG(Salary) over () as AllAvgSalary From EmployeeSalary) a
202+
Order by a.EmployeeID
203+
121204

205+
-- Subquery in Where
206+
Select EmployeeID, JobTitle, Salary
207+
From EmployeeSalary
208+
where EmployeeID in (--create subquery from here
209+
Select EmployeeID
210+
From Employee
211+
where Age > 30)--but if you want to return age column, you need to join these 2 tables

0 commit comments

Comments
 (0)