Bird
Raised Fist0
PostgreSQLquery~10 mins

ROW_NUMBER, RANK, DENSE_RANK in PostgreSQL - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to assign a unique row number to each employee ordered by salary.

PostgreSQL
SELECT employee_id, salary, ROW_NUMBER() OVER (ORDER BY [1]) AS row_num FROM employees;
Drag options to blanks, or click blank then click option'
Asalary
Bemployee_id
Cdepartment
Dhire_date
Attempts:
3 left
💡 Hint
Common Mistakes
Using a column not present in the table.
Leaving the ORDER BY clause empty.
Confusing ROW_NUMBER() with RANK().
2fill in blank
medium

Complete the code to rank employees by salary, allowing ties to have the same rank.

PostgreSQL
SELECT employee_id, salary, RANK() OVER (ORDER BY [1] DESC) AS salary_rank FROM employees;
Drag options to blanks, or click blank then click option'
Aemployee_id
Bhire_date
Csalary
Ddepartment
Attempts:
3 left
💡 Hint
Common Mistakes
Ordering by ascending salary when descending is needed.
Using ROW_NUMBER() instead of RANK() for ties.
Forgetting DESC for highest salary first.
3fill in blank
hard

Complete the code to assign dense ranks to employees by salary within each department.

PostgreSQL
SELECT employee_id, department, salary, DENSE_RANK() OVER (PARTITION BY [1] ORDER BY [2] DESC) AS dept_salary_rank FROM employees;
Drag options to blanks, or click blank then click option'
Aemployee_id
Bsalary
Cdepartment
Dhire_date
Attempts:
3 left
💡 Hint
Common Mistakes
Using salary in PARTITION BY instead of department.
Omitting PARTITION BY when grouping is needed.
Confusing DENSE_RANK() with RANK() behavior.
4fill in blank
hard

Fill both blanks to rank employees by salary within each department using RANK(), ordering salaries descending.

PostgreSQL
SELECT employee_id, department, salary, RANK() OVER (PARTITION BY [1] ORDER BY [2] DESC) AS dept_rank FROM employees;
Drag options to blanks, or click blank then click option'
Adepartment
Bsalary
Cemployee_id
Dhire_date
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping partition and order columns.
Ordering ascending instead of descending.
Using ROW_NUMBER() instead of RANK() for ties.
5fill in blank
hard

Fill all three blanks to assign dense ranks to employees by department and salary, and filter to show only top 3 ranks per department.

PostgreSQL
WITH ranked_employees AS (SELECT employee_id, department, salary, DENSE_RANK() OVER (PARTITION BY [1] ORDER BY [2] DESC) AS dense_rank FROM employees) SELECT * FROM ranked_employees WHERE dense_rank [3] 3;
Drag options to blanks, or click blank then click option'
Adepartment
Bsalary
C<=
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '<=' to include top 3 ranks.
Swapping partition and order columns.
Forgetting to filter ranks in the WHERE clause.

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

  1. Step 1: Understand ROW_NUMBER() purpose

    ROW_NUMBER() gives each row a unique sequential number, starting at 1.
  2. Step 2: Compare with RANK() and DENSE_RANK()

    Unlike RANK() or DENSE_RANK(), ROW_NUMBER() never assigns the same number to multiple rows.
  3. Final Answer:

    Assigns a unique sequential number to each row in the result set. -> Option D
  4. 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

  1. Step 1: Recall correct RANK() syntax

    RANK() is used with OVER() clause specifying ORDER BY column and direction.
  2. 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.
  3. Final Answer:

    SELECT RANK() OVER (ORDER BY score DESC) FROM table_name; -> Option C
  4. 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
medium
A. [{"name": "Alice", "score": 100, "rank": 1}, {"name": "Bob", "score": 90, "rank": 1}, {"name": "Carol", "score": 90, "rank": 1}, {"name": "Dave", "score": 80, "rank": 2}]
B. [{"name": "Alice", "score": 100, "rank": 1}, {"name": "Bob", "score": 90, "rank": 2}, {"name": "Carol", "score": 90, "rank": 2}, {"name": "Dave", "score": 80, "rank": 4}]
C. [{"name": "Alice", "score": 100, "rank": 1}, {"name": "Bob", "score": 90, "rank": 2}, {"name": "Carol", "score": 90, "rank": 2}, {"name": "Dave", "score": 80, "rank": 3}]
D. [{"name": "Alice", "score": 100, "rank": 1}, {"name": "Bob", "score": 90, "rank": 2}, {"name": "Carol", "score": 90, "rank": 3}, {"name": "Dave", "score": 80, "rank": 4}]

Solution

  1. Step 1: Understand RANK() behavior with ties

    RANK() assigns the same rank to tied scores and skips ranks after ties.
  2. Step 2: Apply ranks to scores

    Alice has highest score 100 -> rank 1. Bob and Carol tie at 90 -> both rank 2. Next rank after tie skips to 4 for Dave.
  3. Final Answer:

    [{"name": "Alice", "score": 100, "rank": 1}, {"name": "Bob", "score": 90, "rank": 2}, {"name": "Carol", "score": 90, "rank": 2}, {"name": "Dave", "score": 80, "rank": 4}] -> Option B
  4. Quick Check:

    RANK() skips ranks after ties [OK]
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

  1. Step 1: Check DENSE_RANK() syntax

    DENSE_RANK() does not take any arguments inside parentheses; it is called as DENSE_RANK() OVER (...).
  2. Step 2: Identify the error in the query

    The query incorrectly uses DENSE_RANK(score), which causes a syntax error.
  3. Final Answer:

    DENSE_RANK() should not have a column inside the parentheses. -> Option A
  4. 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

  1. Step 1: Understand ranking requirements

    The requirement is to assign ranks with no gaps even if employees tie in sales.
  2. 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.
  3. Step 3: Choose correct function

    DENSE_RANK() fits the requirement perfectly.
  4. Final Answer:

    DENSE_RANK() because it assigns ranks without gaps after ties. -> Option A
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard