Recall & Review
beginner
What does the
PARTITION BY clause do in a window function?It divides the rows into groups (partitions) so the window function is applied separately to each group, similar to grouping but without collapsing rows.
Click to reveal answer
beginner
How is
PARTITION BY different from GROUP BY?GROUP BY collapses rows into one row per group, while PARTITION BY keeps all rows but calculates window functions within each group.Click to reveal answer
intermediate
Write a simple example of a window function using
PARTITION BY.Example: <br>
SELECT department, employee, salary, RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS rank FROM employees;<br>This ranks employees by salary within each department.Click to reveal answer
intermediate
Can you use multiple columns in
PARTITION BY? How?Yes, separate columns by commas. For example:
PARTITION BY department, job_title groups rows by both department and job title.Click to reveal answer
beginner
What happens if you omit
PARTITION BY in a window function?The window function treats all rows as a single group and calculates over the entire result set.
Click to reveal answer
What is the main purpose of
PARTITION BY in window functions?✗ Incorrect
PARTITION BY divides rows into groups for window functions but keeps all rows visible.Which of the following is a valid use of
PARTITION BY?✗ Incorrect
Option A correctly uses
PARTITION BY inside a window function.If you want to rank employees by salary within each department and job title, how do you write the
PARTITION BY clause?✗ Incorrect
Use commas to separate columns in
PARTITION BY for multiple grouping columns.What will happen if you omit
PARTITION BY in a window function?✗ Incorrect
Without
PARTITION BY, the window function treats all rows as one group.Which SQL clause is similar in concept to
PARTITION BY but collapses rows?✗ Incorrect
GROUP BY groups rows and collapses them into one row per group.Explain in your own words how
PARTITION BY works in window functions and how it differs from GROUP BY.Think about how results look with and without grouping.
You got /4 concepts.
Describe a real-life example where using
PARTITION BY in a window function would be helpful.Consider ranking employees by department or calculating running sales totals per region.
You got /3 concepts.