Order Two Columns Independently - Problem

You have a database table called Data with two integer columns: first_col and second_col. Your task is to create a query that independently sorts these columns in different orders.

Goal: Create a result set where:

  • first_col values are arranged in ascending order
  • second_col values are arranged in descending order

The key insight is that these columns should be sorted independently - meaning the first value in the sorted first_col doesn't necessarily come from the same row as the first value in the sorted second_col.

Table Schema:

+-------------+------+
| Column Name | Type |
+-------------+------+
| first_col   | int  |
| second_col  | int  |
+-------------+------+

This problem tests your understanding of window functions and row numbering in SQL, which are essential for data analysis and reporting tasks.

Input & Output

example_1.sql โ€” Basic Case
$ Input: Data table: +------------+-------------+ | first_col | second_col | +------------+-------------+ | 2 | 1 | | 1 | 4 | | 3 | 3 | +------------+-------------+
โ€บ Output: +------------+-------------+ | first_col | second_col | +------------+-------------+ | 1 | 4 | | 2 | 3 | | 3 | 1 | +------------+-------------+
๐Ÿ’ก Note: first_col is sorted ascending: [1,2,3]. second_col is sorted descending: [4,3,1]. The result combines them by position.
example_2.sql โ€” With Duplicates
$ Input: Data table: +------------+-------------+ | first_col | second_col | +------------+-------------+ | 1 | 2 | | 1 | 2 | | 2 | 1 | +------------+-------------+
โ€บ Output: +------------+-------------+ | first_col | second_col | +------------+-------------+ | 1 | 2 | | 1 | 2 | | 2 | 1 | +------------+-------------+
๐Ÿ’ก Note: first_col ascending: [1,1,2]. second_col descending: [2,2,1]. Duplicates maintain their relative positions.
example_3.sql โ€” Single Row
$ Input: Data table: +------------+-------------+ | first_col | second_col | +------------+-------------+ | 5 | 10 | +------------+-------------+
โ€บ Output: +------------+-------------+ | first_col | second_col | +------------+-------------+ | 5 | 10 | +------------+-------------+
๐Ÿ’ก Note: With only one row, the result remains unchanged regardless of sorting order.

Visualization

Tap to expand
Original Tablefirst_col | second_col3 | 81 | 52 | 9Sort ASCSort DESCfirst_col ASCvalue | position1 | 12 | 23 | 3second_col DESCvalue | position9 | 18 | 25 | 3JOIN ONpositionFinal Resultfirst_col | second_col1 | 92 | 83 | 5๐ŸŽฏ Key: Use ROW_NUMBER() to create positions, then JOIN
Understanding the Visualization
1
Identify the columns
We have first_col (to be sorted ascending) and second_col (to be sorted descending)
2
Sort independently
Use ROW_NUMBER() window function to assign positions to each column when sorted
3
Join by position
Combine the sorted columns using their row number positions
4
Return result
The final result has first_col in ascending order and second_col in descending order
Key Takeaway
๐ŸŽฏ Key Insight: Window functions with ROW_NUMBER() allow us to sort columns independently while maintaining the ability to recombine them by position

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Dominated by sorting operations for each column

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space for storing intermediate results and row numbers

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค number of rows โ‰ค 1000
  • -106 โ‰ค first_col, second_col โ‰ค 106
  • The table may contain duplicate values
  • Both columns must be sorted independently
Asked in
Amazon 35 Microsoft 28 Google 22 Meta 18
38.2K Views
Medium Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen