-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day_11 - Window Function.sql
126 lines (113 loc) · 2.99 KB
/
Day_11 - Window Function.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
# Window Functions (Used to find Best in category values)
# Any function used with Over Clause
# Aggregate Functions (Sum,Avg,Count,Max,Min)
# Ranking Functions (Row_number,Rank,Dense_Rank)
# Analytical Functions (Lead,Lag,first_value)
Select
concat(First,' ',Last) as FullName,
Department,
Salary,
Row_number() over (Order by Salary desc) as rw
from employees.employees ;
with dmax as
(
Select
concat(First,' ',Last) as FullName,
Department,
Salary,
Row_number() over (Partition by Department Order by Salary desc) as rw
from employees.employees
)
Select FullName,Department, Salary
from dmax
where rw = 1 ;
with ds as
(
Select
concat(First,' ',Last) as FullName,
Department,
Status,
Salary,
Row_number() over (Partition by Department,Status Order by Salary desc) as rw
from employees.employees
)
Select *
from ds
where rw = 1 ;
-- ------------------------------------------------------------------------------------------
# 1. Display the highest grossing movie of every Director along with its title
with dmax as
(
Select
FullName,
title,
Boxofficedollars,
Row_number() over (Partition by fullname order by Boxofficedollars desc) as Rw
from movies.director d inner join movies.film f on d.directorID = f.directorID
)
Select FullName,
title,
Boxofficedollars
from dmax
where rw = 1
Order by BoxofficeDollars desc ;
# 2. Display the highest and lowest salaried employee details of each Department
with dr as
(
Select
concat(first," ",last) as FullName,
Department,
Salary,
Row_number() over (Partition by Department order by Salary desc) as HS,
Row_number() over (Partition by Department Order by Salary asc) as LS
from employees.employees
)
Select
FullName,
Department,
Salary
from dr
where HS = 1 or LS = 1 ;
# 3. Display the youngest player from each country in IPL
with cy as
(
Select p.player_name,
p.DOB as Youngest,
c.Country_name,
Row_number() over (Partition by Country_Name order by DOB desc) as RY
from ipl.player p inner join ipl.country c on p.country_name = c.Country_ID
)
Select *
from cy
where ry = 1 ;
# 4. Display 3 longest runtime films in every genre
with DRRTM as
(
Select
Genre,
Title,
Runtimeminutes,
Row_number() over (Partition by Genre order by Runtimeminutes desc) as Rw
from movies.film f inner join movies.genre g on f.genreID = g.GenreID
)
Select * from DRRTM Where Rw <=3 ;
# 5. Display Top 3 highest runscoring players from every country in IPL
with nt as
(
Select
Player_Name,
c.Country_Name ,
Sum(Runs_scored) as TotalRuns,
Row_number() over (PARTITION BY c.Country_Name Order by Sum(Runs_Scored) desc) as rw
from ipl.ball_by_ball b inner join ipl.batsman_scored bs on
b.match_id = bs.match_id and b.over_id = bs.over_id and
b.innings_no = bs.innings_no and b.ball_id = bs.ball_id
inner join ipl.player p on b.striker = p.player_id
inner join ipl.Country c on p.Country_Name = c.Country_Id
Group by Player_Name,c.Country_Name
)
Select *
from nt
where rw <=3 ;
# PlayerName, TotalRuns, CountryName
-- ================================================= THE END =================================================