Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexTheAnalyst authored Apr 19, 2021
1 parent cf34511 commit 36bd27c
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Subquery Tutorial.sql
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)

0 comments on commit 36bd27c

Please sign in to comment.