Leetcode Problem 2159. Order Two Columns Independently

2159. Order Two Columns Independently

Leetcode Solutions

Using Window Functions and JOIN to Order Columns Independently

  1. Use the ROW_NUMBER() window function to assign a row number to each row based on the ascending order of first_col. This will be a Common Table Expression (CTE) named first_col_ordered.
  2. Use the ROW_NUMBER() window function to assign a row number to each row based on the descending order of second_col. This will be another CTE named second_col_ordered.
  3. Perform an inner join between first_col_ordered and second_col_ordered on their respective row numbers.
  4. Select first_col from first_col_ordered and second_col from second_col_ordered to get the final result set.
  5. Order the final result set by the row number of first_col_ordered to ensure the rows are returned in the correct order.

erDiagram
    Data {
        int first_col
        int second_col
    }

Using Subqueries and LIMIT to Order Columns Independently

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...