-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLQuery2.sql
More file actions
159 lines (122 loc) · 4.35 KB
/
SQLQuery2.sql
File metadata and controls
159 lines (122 loc) · 4.35 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
--Inner Join On Both Table
SELECT *
FROM EmployeeDemographics
Inner Join EmployeeSalary
On EmployeeDemographics.EmployeeID = EmployeeSalary.EmployeeID
--Full Outer Join
SELECT *
From EmployeeDemographics
FUll Outer Join EmployeeSalary
On EmployeeDemographics.EmployeeID = EmployeeSalary.EmployeeID
--left outer join
SELECT *
From EmployeeDemographics
Left Outer Join EmployeeSalary
On EmployeeDemographics.EmployeeID = EmployeeSalary.EmployeeID
--Right Outer Join
SELECT *
From EmployeeDemographics
Right Outer Join EmployeeSalary
On EmployeeDemographics.EmployeeID = EmployeeSalary.EmployeeID
--left outer join with specific columns
SELECT EmployeeSalary.EmployeeID, FirstName, LastName,Salary
From EmployeeDemographics
Left Outer Join EmployeeSalary
On EmployeeDemographics.EmployeeID = EmployeeSalary.EmployeeID
/*Use case-1 Company is pressuring Michael to meet his quarterly Quota and michael is almost there and he needs like a thousand more dollars
and he comes up with the genius idea to deduct pay for the highest pay employee at his branch besides himself so how does he go about doing this
identifying the person that makes the moost money*/
SELECT EmployeeDemographics.EmployeeID, FirstName, LastName, Salary
From EmployeeDemographics
Inner Join EmployeeSalary
On EmployeeDemographics.EmployeeID = EmployeeSalary.EmployeeID
Where FirstName <> 'Michael'
Order by Salary Desc
-- Use case-2 accountant makes mistake to calculate Average salary of Salesman Find Avg salary of saleman
Select JobTitle , Avg(Salary) As 'Average salary'
From EmployeeDemographics
Inner Join EmployeeSalary
On EmployeeDemographics.EmployeeID = EmployeeSalary.EmployeeID
Where JobTitle = 'Salesman'
Group by JobTitle
--Union
Create Table WareHouseEmployeeDemographics
(EmployeeID int,
FirstName varchar(50),
LastName varchar(50),
Age int,
Gender varchar(50))
Insert into WareHouseEmployeeDemographics Values
(1013,'Darryl','Philbin',Null,'Male'),
(1050,'Roy','Anderson',31,'Male'),
(1051,'Hidetoshi','Hasagawa',40,'Male'),
(1052,'Val','Johnson',31,'Female')
Select *
From EmployeeDemographics
Union
Select *
From WareHouseEmployeeDemographics
Select *
From EmployeeDemographics
Union All
Select *
From WareHouseEmployeeDemographics
--CASE
SELECT FirstName, LastName ,Age,
CASE
When Age = 38 Then 'Stanley'
When Age > 30 Then 'Old'
Else 'Baby'
End As 'Age group'
From EmployeeDemographics
Where Age is Not Null
Order by Age
-- SalaryRise based on their JobTitle
Select Firstname, Lastname, JobTitle, Salary,
CASE
When JobTitle = 'Salesman' Then Salary + (Salary * 0.10)
When JObTitle = 'Accountant' Then Salary + (Salary * 0.5)
When JobTitle = 'HR' Then Salary + (Salary * 0.2)
Else Salary + (Salary * 0.01)
End As 'SalaryAfterRise'
From EmployeeDemographics
Inner Join EmployeeSalary
On EmployeeDemographics.EmployeeID = EmployeeSalary.EmployeeID
--Having Clause
Select JobTitle, Count(JobTitle)
From EmployeeDemographics
Inner Join EmployeeSalary
On EmployeeDemographics.EmployeeID = EmployeeSalary.EmployeeID
Group By JobTitle
--If We want to add condition on group by function then we Must use Having Clause instead of Where
Select JobTitle, Count(JobTitle)
From EmployeeDemographics
Inner Join EmployeeSalary
On EmployeeDemographics.EmployeeID = EmployeeSalary.EmployeeID
Group By JobTitle
Having Count(JobTitle)>1
--Update and Delete Query
Select * From EmployeeDemographics
Update EmployeeDemographics
Set EmployeeID = 1012
Where FirstName = 'Holly' And LastName = 'Flax'
Update EmployeeDemographics
Set Age = 35, Gender= 'Female'
Where EmployeeID = 1012
Delete From EmployeeDemographics
Where EmployeeID = 1012
--Aliasing
Select FirstName + ' ' + Lastname As 'Full Name'
From EmployeeDemographics
Select Demo.EmployeeID, Demo.FirstName, Demo.LastName, salary.Salary,warehouse.Age
From EmployeeDemographics as Demo
Left Outer Join EmployeeSalary salary
On Demo.EmployeeID = salary.EmployeeID
Left Outer Join WareHouseEmployeeDemographics warehouse
on Demo.EmployeeID = warehouse.EmployeeID
--Partition By
Select EmployeeDemographics.EmployeeID,FirstName,LastName, Age,Gender,
Count(Gender) Over (Partition By Gender) As TotalGender
From EmployeeDemographics
Inner Join EmployeeSalary
On EmployeeDemographics.EmployeeID = EmployeeSalary.EmployeeID