-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntermediate.sql
More file actions
167 lines (131 loc) · 5.08 KB
/
Intermediate.sql
File metadata and controls
167 lines (131 loc) · 5.08 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
/*JOIN is a way to combine multiple tables to a single output
INNER JOIN: only return rows that are in common
FULL OUTER JOIN: join all rows in both tables, some column may be in NULL
LEFT OUTER JOIN: everything in the left table, though there may be no much in the right table, but discard everything in the right but not in the left table
(vs. RIGHT OUTER JOIN)
*/
SELECT *
FROM SQLTutorial.dbo.city
INNER JOIN SQLTutorial.dbo.company
ON city.EmployeeID = comapny.EmployeeID
SELECT *
FROM SQLTutorial.dbo.city
FULL OUTER JOIN SQLTutorial.dbo.company
ON city.EmployeeID = comapny.EmployeeID
SELECT *
FROM SQLTutorial.dbo.city
LEFT OUTER JOIN SQLTutorial.dbo.company
ON city.EmployeeID = comapny.EmployeeID
--we need to specify which table to collect overlapping column value from; otherwise, it will return error
--(specify with different table will return different results). But this doesn't apply to INNER JOIN, as INNER JOIN only take data that are in both table
SELECT employee.EmployeeID, FirstName, LastName, JobTitle, Salary
FROM SQLTutorial.dbo.city
RIGHT OUTER JOIN SQLTutorial.dbo.company
ON employee.EmployeeID = comapny.EmployeeID
--add WHERE, GROUP BY, ORDER BY statement
SELECT employee.EmployeeID, FirstName, LastName, JobTitle, Salary
FROM SQLTutorial.dbo.city
INNER JOIN SQLTutorial.dbo.company
ON employee.EmployeeID = comapny.EmployeeID
WHERE FirstName <> 'Jim'
ORDER BY Salary DESC
SELECT JobTitle, AVG(Salary) AS AvgSalary
FROM SQLTutorial.dbo.city
INNER JOIN SQLTutorial.dbo.company
ON employee.EmployeeID = comapny.EmployeeID
WHERE JobTitle = 'Sales'
GROUP BY JobTitle
/*
Union, Union All: Also is to combine different tables, but putting common column into one column, rather than separate ones.
UNION removes duplicates; UNION ALL doesn't remove duplicates
JOING combine tables based on common column (one or many) into separate columns
*/
SELECT *
FROM SQLTutorial.dbo.Employee
UNION--UNION ALL
SELECT *
FROM SQLTutorial.dbo.WareHouseEmployee
SELECT EmployeeID, FirstName, Age
FROM SQLTutorial.dbo.Employee
UNION
SELECT EmployeeID, JobTitle, Salary
FROM SQLTutorial.dbo.EmployeeSalary
ORDER BY EmployeeID
--this is still working even the columns are not in common bc the columns are in the same data types
/*
CASE WHEN: to specify the conditions. We can do many CASE statements.
*/
SELECT FirstName, LastName, Age
CASE
WHEN Age > 30 THEN 'Old'
WHEN Age BETWEEN 27 AND 30 THEN 'Young'
ELSE 'Baby'
END
FROM SQLTutorial.dbo.Employee
WHERE Age IS NOT NULL
ORDER BY Age
--if the conditions have been met, the output will go with the first criteria
SELECT FirstName, LastName, Age
CASE
WHEN Age > 30 THEN 'Old'
WHEN Age = 38 THEN 'Target' --this wont return, unless we move this criteria before above 30
WHEN Age BETWEEN 27 AND 30 THEN 'Young'
ELSE 'Baby'
END
FROM SQLTutorial.dbo.Employee
WHERE Age IS NOT NULL
ORDER BY Age
SELECT FirstName, LastName, JobTitle, Salary
CASE
WHEN JobTitle = 'Sales' THEN Salary + (Salary * .10)
WHEN JobTitle 'Accountant' THEN Salary + (Salary * .05)
WHEN JobTitle 'HR' THEN Salary + (Salary * .000001)
ELSE Salary + (Salary * .03)
END AS SalaryAfterRaise
FROM SQLTutorial.dbo.Employee
JOIN SQLTutorial.dbo.EmployeeSalary
ON Employee.EmployeeID = EmployeeSalary.EmployeeID
/*HAVING*/
SELECT JobTitle, COUNT(JobTitle)
FROM SQLTutorial.dbo.Employee
JOIN SQLTutorial.dbo.EmployeeSalary
ON Employee.EmployeeID = EmployeeSalary.EmployeeID
--aggregate first before we add the condition.
GROUP BY JobTitle
HAVING AVG(Salary) >45000 --we cannot use aggregation in WHERE clause
ORDER BY AVG(Salary)
/*Update Delete data
UPDATE SET is to change existing data, while INSERT VALUES is to insert a new row of value
DELETE: delete an entire row from the data
*/
SELECT *
FROM SQLTutorial.dbo.Employee
UPDATE SQLTutorial.dbo.Employee
SET EmployeeID = 1012, Gender = 'Female', Age= 31
WHERE FirstName = 'Jim' AND LastName = 'Chen'
--SELECT * --to ensure that you are not removing the wrong data as no where to reverse once done
DELETE FROM SQLTutorial.dbo.Employee
WHERE EmployeeID = 1005
/* Aliasing: Change column name in the script to increase readability*/
SELECT FirstName + ' ' + LaseName AS FullName--FirstName and LastName values will be joined
from SQLTutorial.dbo.Employee
--give alias to table
SELECT Demo.EmployeeID, Sal.Salary
FROM SQLTutorial.dbo.Employee AS Demo
JOIN SQLTutorial.dbo.EmployeeSalary AS Sal
ON Demo.EmployeeID = Sal.EmployeeID
/*
Partition By: group those row of values into partition and change how the window partition; dont actually change the number of row returned in the output.
Group By: reduce the number of rows in output by covering the aggregation of each group
*/
SELECT FirstName, LastName, Gender, Salary, COUNT(Gender) OVER (PARTITION BY Gender) AS TotalGender
FROM SQLTutorial..Employee dem
JOIN SQLTutorial..EmployeeSalary sal
ON dem.EmployeeID=sal.EmployeeID
--every rows will return but with a new column as TotalGender with COUNT(Gender) of fixed number for Female and Male
--cf.GROUP BY
SELECT Gender, COUNT(Gender)
FROM SQLTutorial..Employee dem
JOIN SQLTutorial..EmployeeSalary sal
ON dem.EmployeeID=sal.EmployeeID
GROUP BY Gender