Explore fundamental concepts of window functions and advanced query techniques in SQLite with this beginner-friendly quiz. Understand partitioning, ranking, aggregation, and practical uses of window functions crucial for efficient data analysis.
Which keyword is required to define a window function in an SQLite SELECT statement?
Explanation: The 'OVER' keyword specifies the window for a window function in SQLite, allowing you to perform calculations across a set of rows. 'WITHIN' and 'AROUND' are not recognized keywords in this context, and 'USING' is unrelated to window function declarations. Without 'OVER', the engine cannot apply the window function properly.
In the statement SELECT SUM(amount) OVER (), what distinguishes the use of SUM as a window function compared to a regular aggregate?
Explanation: When used as a window function, SUM(amount) OVER () computes the sum but returns it for every row in the result set. Regular aggregates like SUM without 'OVER' return only one value for the whole group. Ignoring NULLs and sorting rows are not unique to window functions, and removing duplicates is not their primary purpose.
Given the query SELECT employee, department, RANK() OVER (PARTITION BY department ORDER BY salary DESC) FROM staff, what does the PARTITION BY clause do?
Explanation: The 'PARTITION BY' clause logically groups data by department, so each department gets its own ranking order. It does not remove duplicates, sort results, or ignore NULL values. Those distracted options mix up concepts unrelated to the partitioning mechanism.
What does the ROW_NUMBER() window function return when applied with ORDER BY transaction_date?
Explanation: ROW_NUMBER() assigns a distinct number to each row according to the specified order, such as transaction_date. It does not sum values, return the highest date, nor does it assign duplicate numbers when values match. Each row receives a unique identifier regardless of duplicates in the ordered column.
If two salespeople have the same sales figure and RANK() OVER (ORDER BY sales DESC) is used, what rank will they both receive?
Explanation: RANK() assigns the same rank to tied values, so both salespeople with the same sales figure receive the same rank. They do not get consecutive, different ranks as ROW_NUMBER() would. RANK() does not favor lower values, and it never omits ranking for tied entries.
What does LEAD(sale, 1) OVER (ORDER BY date) return for each row in a sales table?
Explanation: LEAD provides access to subsequent row values, hence it retrieves the sale value from the next row based on the specified order. The previous value would need LAG, not LEAD. It does not find the highest sale or aggregate totals, which are unrelated behaviors.
Why is the ORDER BY clause important inside window function definitions in SQLite?
Explanation: ORDER BY within a window function specifies the exact order for evaluating windowed calculations such as ranks and cumulative sums. Filtering and merging duplicates are not direct impacts of ORDER BY in this scenario, and while ordering can affect query plans, its central role here is on calculation sequence, not speed.
When specifying ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING in a window function, what is the function's window for each row?
Explanation: ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING defines a window that spans the prior row, the current one, and the following row for each calculation. Using only current row would need 'CURRENT ROW', not this frame. Summing all previous rows or only first and last are different window definitions.
Which window function setup calculates a running total of 'points' in order of game_date?
Explanation: A cumulative or running total is calculated using SUM as a window function with ORDER BY on the relevant column, in this case, game_date. Aggregates like AVG or MAX do not produce running totals unless combined with proper window frames. Ranking functions do not sum values.
What should you use to select only the top performer in each group after ranking with RANK() OVER (PARTITION BY group_id ORDER BY score DESC)?
Explanation: Only a subquery or Common Table Expression allows you to refer to calculated windowed ranks and filter where rank equals 1 for each group. The HAVING clause cannot reference window function output directly, ORDER BY merely sorts, and LIMIT cannot be used inside the OVER() clause itself.