-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow functions.sql
More file actions
54 lines (38 loc) · 1.62 KB
/
window functions.sql
File metadata and controls
54 lines (38 loc) · 1.62 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
-- DML
-- delete is a DML operation
use regex1;
drop table actor_cp;
create table actor_cp as select first_name
as fname ,last_name as last from sakila.actor
where actor_id between 10 and 14;
select * from actor_cp;
update actor_cp set last = "goyal" where fname = "zero";
delete from actor_cp;
select * from actor_cp;
-- truncate is a DDL statement : in truncate we don't provide any condition
-- don't revert (rollback/undo)
-- delete : we provide condition
-- in delete we can rollback
-- in case if you have any executed ddl statement then you can't rollback
-- object means a structure to manage , store or refer the data
-- windows function:
-- is used to perform the calculation on set of rows
-- are used to apply with reference to current row
-- window functions are majorly have 3 parts:
-- 1. over clause : to apply the function over a window
-- we apply aggregate function in each row
select * from sakila.actor;
select * from sakila.payment;
use world;
select code, name, continent, population, (select sum(population) from country) from country;
select sum(population) from country;
select code, name, continent, population , sum(population) over() from country;
select code, name, continent, population , sum(population) over() ,
avg(population) over() from country;
-- 2. partition by : divides the rows into groups
select continent, sum(population) from country group by continent;
select code, name, continent, population,
sum(population) over(partition by continent) from country;
-- running sum, cummulative sum
select code, name, continent, population,
sum(population) over(order by population) from country;