Row Number with CTE
Basically this is the simplest approach because in that we will give the index or id to the whole data set and we will get only the result set which is required. Which means using this we can also first 25% rows.
WITH total_count AS (
SELECT COUNT(*) AS cnt
FROM employees
),
last_25_percent AS (
SELECT *,
ROW_NUMBER() OVER (ORDER BY employee_id) AS row_num
FROM employees
)
SELECT *
FROM last_25_percent, total_count
WHERE row_num > (cnt* 3.0 / 4.0)
Output:
Explanation: In first CTE we just take the total count of rows. While in second CTE we have given row number to all the rows and then in main query applied where condition in which only the rows more than 3/4th will be shown.
List the Last 25% Rows in a Result Set in PostgreSQL
In PostgreSQL, extracting specific portions of a result set can be achieved using a variety of SQL techniques. One common requirement is to retrieve the last 25% of rows from a result set. This can be useful for various purposes, such as paginating results, performing analyses on a subset of data, or optimizing queries for performance.
Contact Us