-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cf34511
commit 36bd27c
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
Today's Topic: Subqueries (in the Select, From, and Where Statement) | ||
*/ | ||
|
||
Select EmployeeID, JobTitle, Salary | ||
From EmployeeSalary | ||
|
||
-- Subquery in Select | ||
|
||
Select EmployeeID, Salary, (Select AVG(Salary) From EmployeeSalary) as AllAvgSalary | ||
From EmployeeSalary | ||
|
||
-- How to do it with Partition By | ||
Select EmployeeID, Salary, AVG(Salary) over () as AllAvgSalary | ||
From EmployeeSalary | ||
|
||
-- Why Group By doesn't work | ||
Select EmployeeID, Salary, AVG(Salary) as AllAvgSalary | ||
From EmployeeSalary | ||
Group By EmployeeID, Salary | ||
order by EmployeeID | ||
|
||
|
||
-- Subquery in From | ||
|
||
Select a.EmployeeID, AllAvgSalary | ||
From | ||
(Select EmployeeID, Salary, AVG(Salary) over () as AllAvgSalary | ||
From EmployeeSalary) a | ||
Order by a.EmployeeID | ||
|
||
|
||
-- Subquery in Where | ||
|
||
|
||
Select EmployeeID, JobTitle, Salary | ||
From EmployeeSalary | ||
where EmployeeID in ( | ||
Select EmployeeID | ||
From EmployeeDemographics | ||
where Age > 30) |