It's not completely clear how your "first 50 rows" are identified and in what order they shall be returned. There is no "natural order" in tables of a relational database. No guarantees without explicit ORDER BY
.
However, there is a current physical order of rows you can (ab-)use. Typically that's the order in which rows have been inserted - as long as nothing else has happened to that table. But the RDBMS is free to change the physical order any time, so the physical order is not reliable. Results can and will change with write operations to the table (including VACUUM
or other utility commands).
Let's call your sort column sort_col
.
( -- parentheses required
TABLE yellow_tripdata_staging LIMIT 50
)
UNION ALL
( -- parentheses required
SELECT *
FROM (TABLE yellow_tripdata_staging OFFSET 50) sub
ORDER BY sort_col
);
More explanation (incl. TABLE
and parentheses):
Or, assuming sort_col
is defined NOT NULL
:
SELECT *
FROM yellow_tripdata_staging
ORDER BY CASE WHEN row_number() OVER () > 50 THEN sort_col END NULLS FIRST;
The window function row_number()
is allowed to appear in the ORDER BY
clause.
row_number() OVER ()
(with empty OVER
clause) will attach serial numbers according to the current physical order of row - all the disclaimers above still apply.
The CASE
expression replaces the first 50 row numbers with NULL, which sort first due to attached NULLS FIRST
. In effect, the first 50 rows are unsorted the rest is sorted by sort_col
.
Or, if you actually mean to take the first 50 rows according to sort_col
and leave them unsorted, while the rest is to be sorted:
SELECT *
FROM yellow_tripdata_staging
ORDER BY GREATEST (row_number() OVER (ORDER BY sort_col), 50);
Or, if you just mean to fetch the "first" 50 rows according to current physical order or some other undisclosed (more reliable) criteria, you need a subquery or CTE to sort those 50 rows in the outer SELECT
:
SELECT *
FROM (TABLE yellow_tripdata_staging LIMIT 50) sub
ORDER BY sort_col;
You need to define your requirements clearly.