Test your understanding of SQL window functions, their syntax, usage, and key concepts with this beginner-friendly quiz. You'll cover essentials of OVER(), PARTITION BY, RANK, ROW_NUMBER, and practical examples to boost your SQL analytics skills.
Which statement best describes a window function in SQL?
Explanation: A window function performs calculations across a window of rows related to each row, but it doesn't collapse the result, keeping the original rows visible. Summarizing data into a single row is done by aggregate functions, not window functions. Window functions cannot be used in the WHERE clause and are unrelated to triggers.
What is the main role of the OVER() clause in an SQL window function?
Explanation: The OVER() clause specifies the partition and order of rows for the window function. It does not rename columns (that is done by AS), filter results (WHERE does that), or create joins (which use JOIN clauses).
How do window functions differ from using the GROUP BY clause in SQL?
Explanation: Window functions enable calculations over sets of rows related to the current row without eliminating any rows, while GROUP BY aggregates and returns a single row per group. Removing duplicates is done by DISTINCT, and GROUP BY is often used in SELECT too. Both work with various data types.
What happens if you use a window function without a PARTITION BY clause?
Explanation: Without PARTITION BY, window functions treat all rows as a single partition, calculating results across the entire set. This doesn't generate errors, nor does it force grouping by primary keys or columns.
Can you directly use a window function in the WHERE clause of a SQL statement?
Explanation: Window functions are evaluated after WHERE, so their results aren't available there. You must use subqueries or common table expressions. SUM() as a window function is not an exception, and window functions do not replace SELECT.
Which SQL window function pattern is commonly used to calculate running totals within each product category?
Explanation: This sums sales for each category up to the current row, creating a running total. The AVG(sales) GROUP BY version summarizes by group, not as a running total. COUNT(*) counts rows, not sums, and ordering only by category does not yield correct running totals per category.
Which function is suitable to rank employees by their salary within each department?
Explanation: RANK() with PARTITION BY and ORDER BY assigns rankings per department by salary. SUM() produces totals, ROW_COUNT() is not a valid function, and RANK() with GROUP BY does not work in SQL.
How does ROW_NUMBER() differ from RANK() and DENSE_RANK()?
Explanation: ROW_NUMBER() gives unique sequential numbers. RANK() assigns the same rank to ties but skips subsequent ranks, and DENSE_RANK() does not skip ranks. They are not identical, and RANK() does not assign random numbers.
Which query structure helps select the top 3 highest-paid employees in each department with window functions?
Explanation: RANK() with a subquery or common table expression enables ranking and subsequent filtering. MAX() only finds the highest. DISTINCT doesn't limit rows to the top 3 per group, and WHERE salary u003E 3 just filters by salary value.
What is the result of including an ORDER BY clause inside OVER() in a window function?
Explanation: ORDER BY inside OVER() specifies how rows are processed for calculation, influencing results like running totals or ranks. It doesn't merely arrange output or block group operations. Ignoring ORDER BY would be incorrect.
Which of the following is a standard SQL window function?
Explanation: ROW_NUMBER() is a core window function for numbering rows. LEFT() and CONCAT() are string functions and START_WITH() is not standard SQL. Only ROW_NUMBER() can be used as a window function.
Why would you use the PARTITION BY clause within a window function?
Explanation: PARTITION BY splits data into groups for the window function. It does not sort, filter duplicates, or join tables—those are handled by other SQL features.
How would you write a query to display the maximum score in each class, showing all students and their scores?
Explanation: Using MAX() as a window function over PARTITION BY class returns each student's data with the class maximum. GROUP BY collapses results and shows only the max per class, omitting other details. The third option is incorrect syntax, and the last option doesn't partition by class.
Which clause in a window function controls the order rows are evaluated in, while the other splits rows into groups?
Explanation: ORDER BY inside OVER() controls the order of evaluation, affecting calculations. PARTITION BY breaks data into groups for separate calculations. They serve distinct purposes and never swap roles.
If you omit ORDER BY inside the OVER() clause for a window function like SUM(), what is the effect?
Explanation: Without ORDER BY, the window function treats the entire partition as the window for each row, so every row in a partition gets the same value. It does not error or create running totals, and SQL never calculates using a random order.
What is the minimum syntax required to use a window function in SQL?
Explanation: The standard syntax for a window function is FUNCTION_NAME() OVER (). The other options are either invalid syntax or do not define a window function.
If you want to calculate each row's share of total sales, which window function pattern should you use?
Explanation: This pattern gives the grand total in each row, so you can divide individual sales by the total for a percentage. COUNT(sales) is for counts, GROUP BY collapses rows, and AVG with ROWS 1 PRECEDING calculates a small running average, not a ratio.