-
Notifications
You must be signed in to change notification settings - Fork 0
/
Aliasing_PartitionBy_IntermediateSQL.sql
55 lines (43 loc) · 1.34 KB
/
Aliasing_PartitionBy_IntermediateSQL.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
/* Aliasing
*/
SELECT *
FROM EmployeeDemographics
SELECT FirstName AS Fname
FROM EmployeeDemographics
-- ADDING TWO COLUMNS TOGETHER
SELECT FirstName + ' ' + LastName AS FullName
FROM EmployeeDemographics
SELECT AVG(Age) AS AvgAge
FROM EmployeeDemographics
-- WITH JOINS
SELECT Demo.EmployeeID
FROM EmployeeDemographics Demo
JOIN EmployeeSalary Sal
ON Demo.EmployeeID = Sal.EmployeeID
-- WITH DOUBLE/ NESTED JOINS
SELECT Demo.EmployeeID, Demo.FirstName, Demo.LastName, 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, Age, Gender,
COUNT(Gender) OVER (PARTITION BY Gender) AS GenderCount
FROM EmployeeDemographics Demo
JOIN EmployeeSalary Sal
ON Demo.EmployeeID = Sal.EmployeeID
SELECT *
FROM EmployeeDemographics
SELECT *
FROM EmployeeSalary
SELECT FirstName, LastName, Gender, Salary
, COUNT(Gender) OVER (PARTITION BY Gender) as TotalGender
FROM EmployeeDemographics Dem
JOIN EmployeeSalary Sal
ON Dem.EmployeeID = Sal.EmployeeID
SELECT Gender, COUNT(Gender) AS GenderCount
FROM EmployeeDemographics Dem
JOIN EmployeeSalary Sal
ON Dem.EmployeeID = Sal.EmployeeID
GROUP BY Gender