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
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
โก Linearithmic
Space Complexity
O(n)
Space for storing intermediate results and row numbers
โก 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code