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