Leetcode Problem 2308. Arrange Table by Gender

2308. Arrange Table by Gender

Leetcode Solutions

Ranking and Conditional Ordering

  • Use the ROW_NUMBER() window function to assign a rank to each user within their gender group, ordered by user_id. This will be referred to as rnk.
  • Use a CASE statement to assign a sorting order based on the gender: 'female' as 1, 'other' as 2, and 'male' as 3. This will be referred to as ord.
  • Select user_id and gender from a subquery that contains rnk and ord.
  • Order the final result by rnk and ord to get the desired alternating pattern.
erDiagram
    GENDERS {
        int user_id PK
        varchar gender
    }

Dense Ranking and Length-based Sorting

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...