Chapter 6 - Window Tables and Window Functions in SQL Flashcards

1
Q

What is the idea of Windowed Table functions and how do they compare with traditional set functions?

A
  • Operates on a Window of table, calculating one value for each row based on the other rows in that Window (moving/cumulative aggreegate values) -> tuple-based aggregation.
  • Grouping with set functions -> one result per group
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain the concept of window table, window and window function.

A
  • Table can have multiple independent windows associated
  • Window: defines for each row, a set of related rows used to compute additional atts
  • Window function: applied over each row, together with other rows in the win, returning single value.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the elements of a window definition and how are they expressed in SQL?

A

Window partitioning, window ordering, window frame
- Window partitioning: Forming groups, but rows are retained.
- Window ordering: defines order of rows within partition
- Window frame: Defined relative to each row, further restricts set of rows
SELECT… FUNCTION(column) OVER (PARTITION BY expr1 ORDER BY expr2 (ROWS/RANGE)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does the partitioning clause work?

A
  • Defined like GROUP BY, with list of columns
    -> Each group is a partition
    (Windows are limited by partition boundaries)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Is it possible to apply set functions as window functions?

A
  • Yes: set function + OVER = window function
    • > aggregate value is computed for each row
    • > if no frame specified, value is unique for a whole partition (like GROUP BY)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How does the ordering clause work?

A
  • > Defines ordering with partition (independent from query ORDER BY -> no impact on sort)
    • > List of columns: name (ASC/DESC) (NULLS FIRST/ NULLS LAST)
    • > required for frame specification and ranking function
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the functionality provided by ranking functions?

A

-> provide, based on the ordering, the rank of each row in the partition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Enumerate and explain the different kind of ranking functions.

A

RANK: draws are ranked the same, skipping positions -> 1,2,2,4
DENSE-RANK: draws are ranked the same, without skips -> 1,2,2,3
ROW_NUMBER: draws are ranked differently (non-deterministic) -> 1,2,3,4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do ranking functions and window functions in general relate to other clauses in the query?

A

window functions are computed after FROM, WHERE, GROUP BY, HAVING -> cannot be referenced

  • > possible to apply window function to rows aggregated by GROUP BY
  • > set aggregate functions can be used in ordering clause (groups defined in GROUP BY)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the idea behind cumulative functions?

A

if no frame is specified, the value for each row is computed using all the previous (requires ordering!) rows in the partition -> cumulative effect

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are window frames and their different kinds supported in SQL?

A

refines the set of rows used to apply window function with ranges of values or row count
(ROWS, RANGE) (| BETWEEN AND )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the differences and pros/cons between RANGE and ROWS as frame specifications?

A

RANGE is better suited if duplicate/missing values occur (not dense data)
-> or situations in which groups must be made based on values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can we explicitly define a window?

A

-> Provide names to windows, allowing the same window to be used in different functions
…f(arg) OVER w…
WINDOW w AS (partition by … order by…)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do the OLAP operations fit in the query processing stages?

A

WINDOW functions are processed in SELECT clause = projection

  • > after product, selection, grouping aggregation
  • > allows to create partitions based on groups generated by GROUP BY
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Give an overview of additional OLAP capabilities.

A

-> Hypothetical aggregate functions: RANK (col1, col2, col3)
(how would this tuple rank in this group?)
-> inverse distribution functions: percentile_ (DISC|CONT) (expr)
* return values of expressions in order by clause that correspond to percent value 0…1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly