Bird
0
0

Which of the following is the correct way to assign a sequential row number to each record ordered by salary ascending in PostgreSQL?

easy📝 Syntax Q3 of 15
PostgreSQL - Window Functions in PostgreSQL
Which of the following is the correct way to assign a sequential row number to each record ordered by salary ascending in PostgreSQL?
ASELECT name, ROW_NUMBER() OVER (ORDER BY salary ASC) FROM employees;
BSELECT name, ROW_NUMBER(salary) OVER (ORDER BY salary ASC) FROM employees;
CSELECT name, ROW_NUMBER() ORDER BY salary ASC FROM employees;
DSELECT name, ROW_NUMBER() PARTITION BY salary FROM employees;
Step-by-Step Solution
Solution:
  1. Step 1: Understand ROW_NUMBER() syntax

    ROW_NUMBER() requires an OVER() clause with ORDER BY to assign sequential numbers.
  2. Step 2: Check each option

    SELECT name, ROW_NUMBER() OVER (ORDER BY salary ASC) FROM employees; correctly uses ROW_NUMBER() OVER (ORDER BY salary ASC). SELECT name, ROW_NUMBER(salary) OVER (ORDER BY salary ASC) FROM employees; incorrectly passes salary as a parameter. SELECT name, ROW_NUMBER() ORDER BY salary ASC FROM employees; misses OVER(). SELECT name, ROW_NUMBER() PARTITION BY salary FROM employees; incorrectly uses PARTITION BY without ORDER BY.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    ROW_NUMBER() needs OVER with ORDER BY [OK]
Quick Trick: ROW_NUMBER() requires OVER() with ORDER BY clause [OK]
Common Mistakes:
  • Passing column inside ROW_NUMBER() function
  • Omitting OVER() clause
  • Using PARTITION BY without ORDER BY

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes