ROW_NUMBER() assigns a unique sequential number to each row in the result set, starting at 1, without skipping any numbers, even if there are ties.
Click to reveal answer
intermediate
How does RANK() differ from ROW_NUMBER() when there are ties?
RANK() assigns the same rank to tied rows but skips the next ranks accordingly. For example, if two rows tie for rank 1, the next rank will be 3, not 2.
Click to reveal answer
intermediate
Explain DENSE_RANK() in simple terms.
DENSE_RANK() assigns the same rank to tied rows like RANK(), but it does not skip any ranks after ties. So if two rows tie for rank 1, the next rank will be 2.
Click to reveal answer
beginner
Write a SQL query using ROW_NUMBER() to number employees by salary descending.
SELECT employee_id, salary, ROW_NUMBER() OVER (ORDER BY salary DESC) AS row_num FROM employees;
Click to reveal answer
intermediate
When should you use DENSE_RANK() instead of RANK()?
Use DENSE_RANK() when you want to rank rows with ties but keep the ranks consecutive without gaps, which is useful for compact ranking lists.
Click to reveal answer
What will ROW_NUMBER() assign to tied rows?
ASame number for all tied rows
BRanks without gaps after ties
CRanks with gaps after ties
DUnique sequential numbers without gaps
✗ Incorrect
ROW_NUMBER() always assigns unique sequential numbers to each row, even if rows have the same values.
If two rows tie for rank 1, what rank does RANK() assign to the next row?
A2
B3
C1
D4
✗ Incorrect
RANK() skips ranks after ties, so after two rows tied at 1, the next rank is 3.
Which function does NOT skip ranks after ties?
AROW_NUMBER()
BRANK()
CDENSE_RANK()
DCOUNT()
✗ Incorrect
DENSE_RANK() assigns the same rank to ties but does not skip ranks after them.
Which function assigns unique numbers regardless of ties?
AROW_NUMBER()
BDENSE_RANK()
CRANK()
DSUM()
✗ Incorrect
ROW_NUMBER() always assigns unique sequential numbers to each row.
What is the main use of RANK() in SQL?
ATo assign ranks with gaps after ties
BTo count rows
CTo assign unique row numbers
DTo sum values
✗ Incorrect
RANK() assigns ranks to rows and skips ranks after ties, creating gaps.
Describe the difference between ROW_NUMBER(), RANK(), and DENSE_RANK() with examples.
Think about how each function handles ties and numbering.
You got /4 concepts.
Explain a real-life scenario where you would use DENSE_RANK() instead of RANK().
Consider situations where you want a clean list of ranks.
You got /3 concepts.
Practice
(1/5)
1. What does the ROW_NUMBER() function do in PostgreSQL?
easy
A. Counts the total number of rows in the table.
B. Assigns the same rank to rows with identical values, skipping ranks after ties.
C. Assigns ranks to rows without skipping numbers after ties.
D. Assigns a unique sequential number to each row in the result set.
Solution
Step 1: Understand ROW_NUMBER() purpose
ROW_NUMBER() gives each row a unique sequential number, starting at 1.
Step 2: Compare with RANK() and DENSE_RANK()
Unlike RANK() or DENSE_RANK(), ROW_NUMBER() never assigns the same number to multiple rows.
Final Answer:
Assigns a unique sequential number to each row in the result set. -> Option D
Quick Check:
ROW_NUMBER() = unique row numbers [OK]
Hint: ROW_NUMBER always gives unique numbers, no ties allowed [OK]
Common Mistakes:
Confusing ROW_NUMBER with RANK or DENSE_RANK
Thinking ROW_NUMBER skips numbers after ties
Assuming ROW_NUMBER counts total rows
2. Which of the following is the correct syntax to assign a rank using RANK() over a column named score ordered descending?
easy
A. SELECT RANK(score) OVER (ORDER BY DESC) FROM table_name;
B. SELECT RANK() ORDER BY score DESC FROM table_name;
C. SELECT RANK() OVER (ORDER BY score DESC) FROM table_name;
D. SELECT RANK() OVER (PARTITION BY score DESC) FROM table_name;
Solution
Step 1: Recall correct RANK() syntax
RANK() is used with OVER() clause specifying ORDER BY column and direction.
Step 2: Check each option
SELECT RANK() OVER (ORDER BY score DESC) FROM table_name; correctly uses RANK() OVER (ORDER BY score DESC). Others misuse syntax or clauses.
Final Answer:
SELECT RANK() OVER (ORDER BY score DESC) FROM table_name; -> Option C
Quick Check:
RANK() OVER (ORDER BY column DESC) syntax [OK]
Hint: RANK() needs OVER with ORDER BY column [OK]
Common Mistakes:
Putting column inside RANK() function
Using PARTITION BY incorrectly
Omitting OVER() clause
3. Given the table players with columns name and score, what will be the output of this query?
SELECT name, score, RANK() OVER (ORDER BY score DESC) AS rank FROM players;
Data: name | score
-----|------
Alice | 100
Bob | 90
Carol | 90
Dave | 80
Hint: RANK() skips numbers after ties, DENSE_RANK() does not [OK]
Common Mistakes:
Assuming ranks after ties are consecutive
Confusing RANK() with DENSE_RANK()
Misordering rows by score
4. Identify the error in this query:
SELECT name, DENSE_RANK(score) OVER (ORDER BY score DESC) FROM players;
medium
A. DENSE_RANK() should not have a column inside the parentheses.
B. DENSE_RANK() cannot be used without PARTITION BY clause.
C. ORDER BY clause is missing in the OVER() clause.
D. The query is correct and will run without errors.
Solution
Step 1: Check DENSE_RANK() syntax
DENSE_RANK() does not take any arguments inside parentheses; it is called as DENSE_RANK() OVER (...).
Step 2: Identify the error in the query
The query incorrectly uses DENSE_RANK(score), which causes a syntax error.
Final Answer:
DENSE_RANK() should not have a column inside the parentheses. -> Option A
Quick Check:
DENSE_RANK() has empty parentheses [OK]
Hint: Window functions have empty parentheses, column goes in ORDER BY [OK]
Common Mistakes:
Putting column inside DENSE_RANK()
Thinking PARTITION BY is mandatory
Omitting OVER() clause
5. You want to assign ranks to employees based on their sales, but you want no gaps in ranking numbers even if some employees have the same sales. Which function should you use and why?
Options: A) ROW_NUMBER() because it gives unique numbers. B) RANK() because it skips ranks after ties. C) DENSE_RANK() because it assigns ranks without gaps after ties. D) COUNT() because it counts rows.
hard
A. DENSE_RANK() because it assigns ranks without gaps after ties.
B. RANK() because it skips ranks after ties.
C. ROW_NUMBER() because it gives unique numbers.
D. COUNT() because it counts rows.
Solution
Step 1: Understand ranking requirements
The requirement is to assign ranks with no gaps even if employees tie in sales.
Step 2: Match functions to behavior
ROW_NUMBER() gives unique numbers, ignoring ties. RANK() skips ranks after ties causing gaps. DENSE_RANK() assigns ranks without gaps after ties.
Step 3: Choose correct function
DENSE_RANK() fits the requirement perfectly.
Final Answer:
DENSE_RANK() because it assigns ranks without gaps after ties. -> Option A
Quick Check:
DENSE_RANK() = no gaps in ranks [OK]
Hint: Use DENSE_RANK() to avoid gaps in ranking numbers [OK]
Common Mistakes:
Choosing RANK() and expecting no gaps
Using ROW_NUMBER() which ignores ties
Confusing COUNT() with ranking functions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.