-
Notifications
You must be signed in to change notification settings - Fork 0
/
Intermediate SQL Tutorial.sql
137 lines (105 loc) · 3.17 KB
/
Intermediate 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
-- Inner/Outer Joins | Use Cases
SELECT *
FROM EmployeeDemographics
SELECT *
FROM EmployeeSalary
SELECT *
FROM EmployeeDemographics
INNER JOIN EmployeeSalary
ON EmployeeDemographics.EmployeeID = EmployeeSalary.EmployeeID
SELECT *
FROM EmployeeDemographics
FULL OUTER JOIN EmployeeSalary
ON EmployeeDemographics.EmployeeID = EmployeeSalary.EmployeeID
SELECT *
FROM EmployeeDemographics
LEFT OUTER JOIN EmployeeSalary
ON EmployeeDemographics.EmployeeID = EmployeeSalary.EmployeeID
SELECT *
FROM EmployeeDemographics
RIGHT OUTER JOIN EmployeeSalary
ON EmployeeDemographics.EmployeeID = EmployeeSalary.EmployeeID
SELECT EmployeeDemographics.EmployeeID, FirstName, LastName, JobTitle, Salary
-- we need spcify what is EmployeeID and from where???
FROM EmployeeDemographics
INNER JOIN EmployeeSalary
ON EmployeeDemographics.EmployeeID = EmployeeSalary.EmployeeID
WHERE FirstName <> 'Shatha'
ORDER BY Salary DESC
SELECT JobTitle, AVG(Salary) AS AVGSALARY
FROM EmployeeDemographics
INNER JOIN EmployeeSalary
ON EmployeeDemographics.EmployeeID = EmployeeSalary.EmployeeID
WHERE JobTitle = 'HR'
GROUP BY JobTitle
-- Unions | Union Operator
SELECT *
FROM EmployeeDemographics
UNION
SELECT *
FROM WareHouseEmployeeDemographics
SELECT *
FROM EmployeeDemographics
UNION ALL
SELECT *
FROM WareHouseEmployeeDemographics
ORDER BY EmployeeID
SELECT EmployeeID, FirstName, Age
-- That's why dataType from tables is same
FROM EmployeeDemographics
UNION ALL
SELECT EmployeeID, JobTitle, Salary
FROM EmployeeSalary
-- Case Statement | Use Cases
SELECT EmployeeID, FirstName, Age,
CASE
WHEN Age > 30 THEN 'Old'
WHEN Age BETWEEN 20 AND 30 THEN 'Young'
ELSE 'Baby'
END
FROM EmployeeDemographics
WHERE Age IS NOT NULL
ORDER BY Age
SELECT FirstName, LastName, JobTitle, Salary,
CASE
WHEN JobTitle = 'HR' THEN Salary +(Salary *0.001)
WHEN JobTitle = 'SalesMan' THEN Salary +(Salary *0.02)
WHEN JobTitle = 'Manger' THEN Salary +(Salary *0.04)
ELSE Salary+(Salary*0.03)
END AS SalaryAfterRais
FROM EmployeeDemographics
JOIN EmployeeSalary
ON EmployeeDemographics.EmployeeID = EmployeeSalary.EmployeeID
-- Having Clause
SELECT JobTitle, COUNT(JobTitle)
FROM EmployeeSalary
WHERE JobTitle IS NOT NULL
GROUP BY JobTitle
HAVING COUNT(JobTitle) >1
-- Updating/Deleting Data
SELECT *
FROM EmployeeDemographics
UPDATE EmployeeDemographics
SET FirstName = 'DevShatha'
WHERE EmployeeID = 1001
DELETE FROM EmployeeDemographics
WHERE EmployeeID IS NULL
-- Aliasing
SELECT Demo.EmployeeID, Demo.FirstName, Sal.JobTitle, Ware.Age
FROM EmployeeDemographics Demo
LEFT JOIN EmployeeSalary Sal
ON Demo.EmployeeID = Sal.EmployeeID
LEFT JOIN WareHouseEmployeeDemographics Ware
ON Demo.EmployeeID = Ware.EmployeeID
-- Partition By
SELECT FirstName, LastName, Gender, Salary,
COUNT(Gender) OVER (PARTITION By Gender) AS TotalGender
FROM EmployeeDemographics demo
JOIN EmployeeSalary sal
ON demo.EmployeeID = sal.EmployeeID
-- The prevoius code is same next :)
SELECT Gender, COUNT(Gender)
FROM EmployeeDemographics demo
JOIN EmployeeSalary sal
ON demo.EmployeeID = sal.EmployeeID
GROUP BY Gender