-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTempTables_AdvancedSQL.sql
More file actions
47 lines (37 loc) · 1.03 KB
/
TempTables_AdvancedSQL.sql
File metadata and controls
47 lines (37 loc) · 1.03 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
/* Temp Tables
*/
CREATE TABLE #temp_Employee (
EmployeeID int,
JobTitle varchar(100),
Salary int
)
SELECT *
FROM #temp_Employee
INSERT INTO #temp_Employee VALUES (
1001,'HR', 45000
)
--- adding a values into the temp table from an actual table, so like subset of the table
INSERT INTO #temp_Employee
SELECT *
FROM EmployeeSalary
--- Abit advanced way
DROP TABLE IF EXISTS #Temp_Employee2
CREATE TABLE #Temp_Employee2(
JobTitle varchar (50),
EmployeesPerJob int,
AvgAge int,
AvgSalary int)
INSERT INTO #Temp_Employee2
SELECT sal.JobTitle,COUNT(JobTitle), AVG(Age), AVG(Salary)
FROM EmployeeDemographics dem
JOIN EmployeeSalary sal
ON dem.EmployeeID = sal.EmployeeID
GROUP BY JobTitle
SELECT *
FROM #Temp_Employee2
/* Stored Procedures:-
- DROP TABLE IF EXISTS #Temp_Employee2
- when you are running a stored procedures, it will look at it and see if the table
exists then it deletes it and allows you to create it again
- so when you apply it you can run it as much as you want and smoothly
*/