What does the OVER clause with ORDER BY do in SQL?
It allows you to perform calculations across a set of rows related to the current row, ordered in a specific way, without grouping the rows into a single output row.
Click to reveal answer
beginner
How does the ORDER BY inside the OVER clause affect the result?
It defines the order in which rows are processed for the window function, affecting calculations like running totals or ranking.
Click to reveal answer
beginner
Example: What does ROW_NUMBER() OVER (ORDER BY salary DESC) do?
It assigns a unique rank number to each row ordered by salary from highest to lowest, starting at 1 for the highest salary.
Click to reveal answer
intermediate
Can you use PARTITION BY with ORDER BY inside the OVER clause?
Yes, PARTITION BY divides rows into groups, and ORDER BY sorts rows within each group for the window function calculation.
Click to reveal answer
intermediate
Why is the OVER clause with ORDER BY useful compared to GROUP BY?
Because it lets you calculate values like running totals or ranks while still keeping each row separate, unlike GROUP BY which combines rows.
Click to reveal answer
What does SUM(sales) OVER (ORDER BY date) calculate?
ATotal sales for all rows
BRunning total of sales ordered by date
CSales grouped by date
DAverage sales per date
✗ Incorrect
The SUM with OVER and ORDER BY calculates a running total of sales ordered by date.
Which window function assigns a unique rank to each row ordered by a column?
ACOUNT()
BAVG()
CSUM()
DROW_NUMBER()
✗ Incorrect
ROW_NUMBER() assigns a unique rank number to each row based on the ORDER BY inside the OVER clause.
What happens if you omit ORDER BY inside the OVER clause?
ARows are processed in an undefined order
BRows are grouped by default
CThe window function will not work
DIt causes a syntax error
✗ Incorrect
Without ORDER BY, the window function processes rows in an undefined order.
Which clause divides rows into groups before applying ORDER BY in a window function?
AHAVING
BGROUP BY
CPARTITION BY
DWHERE
✗ Incorrect
PARTITION BY divides rows into groups for the window function, then ORDER BY sorts within those groups.
What is the main difference between GROUP BY and OVER with ORDER BY?
AGROUP BY combines rows, OVER keeps rows separate
BOVER combines rows, GROUP BY keeps rows separate
CGROUP BY keeps all rows, OVER groups rows
DThey are the same
✗ Incorrect
GROUP BY combines rows into one per group; OVER with ORDER BY calculates values while keeping each row separate.
Explain how the OVER clause with ORDER BY works in SQL window functions.
Think about how you can calculate running totals or ranks without grouping rows.
You got /4 concepts.
Describe a real-life example where using OVER with ORDER BY is helpful.
Imagine tracking sales over time or ranking students by score.
You got /4 concepts.
Practice
(1/5)
1. What does the OVER clause with ORDER BY do in SQL?
easy
A. It creates a new table sorted by the specified column.
B. It performs calculations on rows in a specific order without grouping them.
C. It filters rows based on order conditions.
D. It groups rows and then orders the groups.
Solution
Step 1: Understand the OVER clause
The OVER clause allows window functions to work on a set of rows related to the current row without grouping them.
Step 2: Role of ORDER BY inside OVER
ORDER BY inside OVER defines the order in which rows are processed for functions like ROW_NUMBER() or SUM() running totals.
Final Answer:
It performs calculations on rows in a specific order without grouping them. -> Option B
Quick Check:
OVER with ORDER BY = ordered calculations without grouping [OK]
Hint: OVER with ORDER BY orders rows for calculations, no grouping needed [OK]
Common Mistakes:
Thinking OVER groups rows like GROUP BY
Confusing ORDER BY inside OVER with filtering
Assuming it creates a new sorted table
2. Which of the following is the correct syntax to assign row numbers ordered by salary using the OVER clause?
easy
A. SELECT ROW_NUMBER(ORDER BY salary) AS rank FROM employees;
B. SELECT ROW_NUMBER() AS rank OVER ORDER BY salary FROM employees;
C. SELECT ROW_NUMBER() ORDER BY salary FROM employees;
D. SELECT ROW_NUMBER() OVER (ORDER BY salary) AS rank FROM employees;
Solution
Step 1: Identify correct ROW_NUMBER() syntax
ROW_NUMBER() requires OVER() with ORDER BY inside parentheses to define ordering.
Step 2: Check each option
SELECT ROW_NUMBER() OVER (ORDER BY salary) AS rank FROM employees; correctly uses ROW_NUMBER() OVER (ORDER BY salary). Others miss parentheses or misplace clauses.
Final Answer:
SELECT ROW_NUMBER() OVER (ORDER BY salary) AS rank FROM employees; -> Option D
Quick Check:
ROW_NUMBER() needs OVER(ORDER BY ...) [OK]
Hint: ROW_NUMBER() must be followed by OVER(ORDER BY column) [OK]
Common Mistakes:
Omitting OVER() parentheses
Placing ORDER BY outside OVER()
Using ROW_NUMBER() without OVER
3. Given the table sales with columns id, amount, what is the output of this query?
SELECT id, amount, SUM(amount) OVER (ORDER BY id) AS running_total FROM sales ORDER BY id;
medium
A. The query returns an error due to missing GROUP BY.
B. Each row shows id, amount, and total sum of all amounts repeated.
C. Each row shows id, amount, and a running total of amounts ordered by id.
D. Rows are grouped by id with sum of amounts per group.
Solution
Step 1: Understand SUM() OVER (ORDER BY id)
This calculates a running total of amount, adding amounts in order of id for each row.
Step 2: Analyze output per row
Each row shows its id, amount, and the sum of amounts from first id up to current id.
Final Answer:
Each row shows id, amount, and a running total of amounts ordered by id. -> Option C
SELECT id, RANK() OVER ORDER BY score FROM players;
medium
A. Missing parentheses after OVER and ORDER BY inside them.
B. RANK() cannot be used with OVER clause.
C. ORDER BY should be outside the OVER clause.
D. The query is correct and will run without errors.
Solution
Step 1: Check syntax of RANK() with OVER
RANK() requires OVER() with parentheses enclosing ORDER BY clause.
Step 2: Identify missing parentheses
The query misses parentheses after OVER and does not put ORDER BY inside them, causing syntax error.
Final Answer:
Missing parentheses after OVER and ORDER BY inside them. -> Option A
Quick Check:
OVER() must have parentheses with ORDER BY inside [OK]
Hint: Always use OVER(...) with parentheses enclosing ORDER BY [OK]
Common Mistakes:
Writing OVER without parentheses
Placing ORDER BY outside OVER()
Assuming RANK() works without OVER
5. You want to assign a rank to employees based on their salary, but if two employees have the same salary, they should get the same rank and the next rank should skip accordingly. Which query correctly uses the OVER clause with ORDER BY to achieve this?
hard
A. SELECT employee_id, salary, RANK() OVER (ORDER BY salary DESC) AS salary_rank FROM employees;
B. SELECT employee_id, salary, ROW_NUMBER() OVER (ORDER BY salary DESC) AS salary_rank FROM employees;
C. SELECT employee_id, salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS salary_rank FROM employees;
D. SELECT employee_id, salary, RANK() OVER ORDER BY salary DESC AS salary_rank FROM employees;
Solution
Step 1: Understand ranking functions
RANK() assigns same rank to ties and skips ranks after ties; DENSE_RANK() assigns same rank but no skips; ROW_NUMBER() assigns unique numbers.
Step 2: Check syntax and behavior
SELECT employee_id, salary, RANK() OVER (ORDER BY salary DESC) AS salary_rank FROM employees; uses RANK() with correct OVER(ORDER BY salary DESC) syntax, matching requirement. SELECT employee_id, salary, RANK() OVER ORDER BY salary DESC AS salary_rank FROM employees; has syntax error. ROW_NUMBER() and DENSE_RANK() do not skip ranks after ties.
Final Answer:
SELECT employee_id, salary, RANK() OVER (ORDER BY salary DESC) AS salary_rank FROM employees; -> Option A
Quick Check:
RANK() with OVER(ORDER BY) skips ranks after ties [OK]
Hint: Use RANK() OVER(ORDER BY) for ranks with gaps on ties [OK]
Common Mistakes:
Using ROW_NUMBER() which never skips ranks
Using DENSE_RANK() which does not skip ranks
Missing parentheses in OVER clause
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.