Window Functions Flashcards
What are the various window aggregate functions that can be used?
- SUM
- COUNT
- AVG
- MIN
- MAX
SQL Server 70-461 05-03
What are the three main parts in the over clause?
- Partitioning
- Ordering
- Framing
SQL Server 70-461 05-03
When using OVER() with none of the three clauses it can take, what does it represent?
All of the rows in the query. You could use it for a grand total, for example.
SQL Server 70-461 05-03
What do UNBOUNDED PRECEEDING and UNBOUND FOLLOWING stand for?
- UNBOUNDED PRECEEDING: Beginning of the partition
- UNBOUNDED FOLLOWING: End of the partition
SQL Server 70-461 05-03
What does <n> ROWS PRECEEDING and <n> ROWS FOLLOWING stand for?
- <n> ROWS PRECEEDING: <n> rows before the current row
- <n> ROWS FOLLOWING: <n> rows after the current row
SQL Server 70-461 05-03
How does using window aggregate functions for running totals compare to using joins, subqueries or group aggregate functions in terms of performance?
It will perform better than the other three, especially when using UNBOUNDED PRECEEDING as the first delimiter.
SQL Server 70-461 05-03
What clause is required to use a framing clause?
An ORDER BY clause in the OVER clause
SQL Server 70-461 05-03
What clause is mandatory when using a window ranking function?
ORDER BY within the OVER clause
SQL Server 70-461 05-03
What cluase that can be used in OVER cannot be used when you use a ranking function?
A frame clause
SQL Server 70-461 05-03
What does the NTILE ranking function do?
Allows you to arrange rows within the partition into a requested number of tiles. The NTILE column will have the tile number for the particular row.
Ex.
830 rows total. You specify 100 tiles. Since 100 does not divide equally into 830, the first 30 tiles will have an extra row and the remaining tiles will have 8 rows.
SQL Server 70-461 05-03
General form when using NTILE function.
NTILE(n) OVER(ORDER BY val) AS NTILE
The NTILE function goes before the OVER clause
n=the number of tiles you want
SQL Server 70-461 05-03
What could you do if you need to refer to a Window Aggregate function in a WHERE clause?
Create a CTE that creates the desired column using a window aggregate function. Refer to that column from the outer query.
SQL Server 70-461 05-03
What is the logical query processing order?
- FROM
- WHERE
- GROUP BY
- HAVING
- SELECT
- ORDER BY
SQL Server 70-461 05-03
If you use the LAG or LEAD functions and there is no row available for the offset you specified, what value is returned?
Say you specified LAG(val,2) but there is no row two rows before the current row, a null will be returned.
SQL Server 70-461 05-03
In the framing clause, is it better to use RANGE or ROWS performance wise?
- ROWS
- Example: ROWS BETWEEN UNBOUNDED PRECEEDING AND CURRENT ROW
SQL Server 70-461 05-03
What main query clauses can a window function be in?
- SELECT
- ORDER BY
SQL Server 70-461 05-03
What is one difference between grouped queries and windowed queries with respect to how rows are treated?
- Grouped queries arrange rows in groups
- Windowed queries may apply a function to a group of rows but each individual row is still returned in the result set.
SQL Server 70-461 05-03
When using LEAD or LAG, if you don’t specify the # of rows to offset, what is the default?
It will offset 1 row
SQL Server 70-461 05-03
When a window frame is applicable to a function but you don’t include an explicit window frame clause, what is the default?
RANGE BETWEEN UNBOUNDED PRECEEDING AND CURRENT ROW
SQL Server 70-461 05-03
Why is it important to explicitly define a window frame clause if one is applicable to a function?
If you don’t and you’re using the function LAST_VALUE, for example, to get the last row in the partition you won’t get the result you’re expecting since the default is RANGE BETWEEN UNBOUNDED PRECEEDING AND CURRENT ROW. Bottom line, you may get unexpected results.
Also, for performance it is preferred to use ROWS instead of RANGE.
SQL Server 70-461 05-03
What is the general structure of the OVER clause?
Aggregate Function(col_name) OVER ( PARTITION BY col_name ORDER BY col_name ROWS BETWEEN top delimiter AND bottom delimiter )
- Partitioning PARTITION BY col_name
- Ordering ORDER BY col_name
-
Framing ROWS BETWEEN top delimiter
AND bottom delimiter - NOTE: col_name does not have to be the same in aggregate function, partition by and order by.
SQL Server 70-461 05-03
Real example of OVER clause based on test data from the book
SELECT empid, ordermonth, val, SUM(val) OVER (PARTITION BY empid ORDER BY ordermonth ROWS BETWEEN UNBOUNDED PRECEEDING AND CURRENT ROW ) AS runval
- Partitioning PARTITION BY empid
- Ordering ORDER BY ordermonth
- Framing ROWS BETWEEN UNBOUNDED PRECEEDING AND CURRENT ROW
SQL Server 70-461 05-03
What does the ORDER BY clause within the OVER clause do?
Orders data only for the sake of the computation, not for how the data result will be presented when it is done.
SQL Server 70-461 05-03
What does the framing clause do?
It filters a subset of rows within the partition.
SQL Server 70-461 05-03
General form when using RANK
- It goes before an OVER clause
- Example: RANK() OVER(ORDER BY val) AS rnk
SQL Server 70-461 05-03
What does the DENSE_RANK ranking function do?
Returns the number of distinct ordering values that are lower than the current row plus 1.
Example: So if there were 9 rows with a value lower than the current row but the value in two of them was the same you would have 8 distinct values plus 1 for the DENSE_RANK of the current row.
SQL Server 70-461 05-03
General form when using DENSE_RANK function
- It goes before OVER clause
~~~
DENSE_RANK OVER(ORDER BY val) AS densernk
~~~
SQL Server 70-461 05-03
What clauses of the main query clauses are window function allowed in?
- SELECT
- ORDER BY
SQL Server 70-461 05-03
What clause within the OVER clause can you not use with LAG and LEAD?
- Cannot be used: You cannot use the framing clause
- Can be used: You can use the partitioning and ordering clauses
SQL Server 70-461 05-03
How do you define the rows a window aggregate function will be applied to
User the OVER clause
SQL Server 70-461 05-03
What does the partitioning clause do?
SUM(val) OVER(PARTITION BY empid) AS totalval
- It tells you what rows the function should act on based on the current row
- So in the above example, if the current row has empid of 1, then the sum of val for that row will only be the sum of the rows that also have an empid of 1.
SQL Server 70-461 05-03
What is the general form of the framing clause?
ROWS BETWEEN <top delimiter> AND <bottom delimiter>
SQL Server 70-461 05-03
What are the three delimiters that can be used in the framing clause?
- UNBOUNDED PRECEEDING OR UNBOUNDED FOLLOWING
- Current Row
- <n> ROWS PRECEEDING OR ROWS FOLLOWING
SQL Server 70-461 05-03
What is a window ranking function?
It allows you to rank rows within a partition based on specified ordering.
SQL Server 70-461 05-03
Do you have to include a partition clause when using a ranking function?
No. If you don’t indicate a partition, the entire result set of the query will be considered a partition.
SQL Server 70-461 05-03
What are the four ranking functions?
- ROW_NUMBER
- RANK
- DENSE_RANK
- NTILE
SQL Server 70-461 05-03
What does the ROW_NUMBER ranking function do?
It gives a unique sequential integer to each row in the specified partition based on the ordering in the over clause. Always starts with 1.
If no partition is indicated, the entire query result is considered one partition.
SQL Server 70-461 05-03
General form when using ROW_NUMBER
- It goes before the OVER clause
- Example: ROW_NUMBER() OVER(ORDER BY val) AS rownum
SQL Server 70-461 05-03
What does the RANK ranking function do?
- For the current row, it returns the number of rows in the specified partition that have a lower ordering value plus 1.
- Example: If the current row order by column has $45.00 and there are 9 rows that have a value less than $45.00, the RANK for the current row is 10.
SQL Server 70-461 05-03
What are the 4 window offset functions?
- LAG
- LEAD
- FIRST_VALUE
- LAST_VALUE
SQL Server 70-461 05-03
What clauses within the OVER clause can you use with FIRST_VALUE and LAST_VALUE?
- Partitioning
- Ordering
- Framing
SQL Server 70-461 05-03
How does a window offset function work?
It returns an element from a single row that is
1. offset a specified number of rows from the current row
or
2. the first or last row in a window fram
SQL Server 70-461 05-03
How do the LAG and LEAD functions work?
- LAG: returns an element from the row in the current partition that is a requested number of rows before the current row
- LEAD: returns an element from the row in the current partition that is a requested number of rows after the current row
SQL Server 70-461 05-03
What is the general form of the LAG and LEAD functions?
LAG
LAG(val) OVER(PARTITION BY custid
ORDER BY orderdate,
orderid) AS prev_val
LEAD
LEAD(val) OVER(PARTITION BY custid
ORDER BY orderdate,
orderid) AS prev_val
SQL Server 70-461 05-03
What are the three arguments the LEAD and LAG functions accept?
- element to return
- # rows to offset
- value to assign if null
LAG(element to return, # rows to offset, value to assign if null)
Example: LAG(val, 6, 0)
or
Example: LEAD(val, 6, 0)
SQL Server 70-461 05-03
How do the FIRST_VALUE and LAST_VALUE functions work?
- They return a value from the last or first row of a window frame.
SQL Server 70-461 05-03
What is the general form when using FIRST_VALUE and LAST_VALUE
FIRST_VALUE
FIRST_VALUE(val) OVER(PARTITION BY custid,
ORDER BY orderdate, orderid
ROWS BETWEEN UNBOUNDED PRECEEDING AND CURRENT ROW
) AS first_val
LAST_VALUE
LAST_VALUE(val) OVER(PARTITION BY custid,
ORDER BY orderdate, orderid
ROWS BETWEEN UNBOUNDED PRECEEDING AND CURRENT ROW
) AS last_val
SQL Server 70-461 05-03