Horje
List the Last 25% Rows in a Result Set

Fetching the last 25% of rows from a result set is a common query that provides insights into the most recent trends in data. This technique is particularly useful for quick analytics or reporting, especially when dealing with large datasets. In this article, we will explore how to list the last 25% of rows in a result set using MySQL, focusing on two primary methods: using the ‘LIMIT' clause and using a window function‘.

How to List the Last 25% Rows in a Result Set in MySQL

MySQL provides us with various methods through which we can achieve our task. We need to perform some basic math to get our result set. We will calculate the total number of rows in the table first and then perform necessary actions accordingly. We will see the two main methods to achieve our desired task:

  • Using LIMIT clause in MySQL
  • Using Window Function in MySQL

Setting Up the Environment

To understand how to list the last 25% of rows in a result set in MySQL, we need a table on which we will perform various operations and queries. Here we will consider a table called ‘horje‘ as shown below:

last25gfg

horje Table

Create Table:

CREATE TABLE horje(
id int PRIMARY KEY,
name varchar(100),
course varchar(100)
);

Insert Values:

INSERT INTO horje(id,name,course)
VALUES(1,'Vishu','Python');

INSERT INTO horje(id,name,course)
VALUES(2,'Sumit','Java');

INSERT INTO horje(id,name,course)
VALUES(3,'Vivek','C++');

INSERT INTO horje(id,name,course)
VALUES(4,'Neeraj','Java');

INSERT INTO horje(id,name,course)
VALUES(5,'Aayush','Python');

INSERT INTO horje(id,name,course)
VALUES(6,'Harsh','C++');

INSERT INTO horje(id,name,course)
VALUES(7,'Rahul','Python');

Using LIMIT Clause

For this example, we’ll use the LIMIT clause with OFFSET. Firstly, we will calculate the total number of rows in our table. Then, we’ll determine 75% of the total number of rows as our starting value. Finally, we’ll find the difference between the total number of rows and 75% of the total number of rows to get the total number of rows in the result set. Here’s a simpler explanation:

Query:

SELECT * 
FROM horje
LIMIT (
SELECT FLOOR(COUNT(*) * 0.75) FROM horje) ,
(SELECT COUNT(*) - FLOOR(COUNT(*) * 0.75) FROM horje
);

Output:

result25

Output

Explanation: In the above image, we can see that last 25% rows of the result set are displayed here. Our total number of rows is 7. Therefore, last 2 rows are displayed here. In the query, we can see that, we have used subqueries to calculate the total number of rows of the table and for calculating 75% of the total number of rows. LIMIT (5, 2) will display 2 rows skipping the first 5 rows.

Specifying Condition

We can also fetch the last 25% rows of the result set with some condition. In this case, we will consider a result set where all rows have course ‘Python‘.

Query:

SELECT * 
FROM horje Where course = 'Python'
LIMIT (
SELECT FLOOR(COUNT(*) * 0.75) FROM horje Where course = 'Python'),
(SELECT COUNT(*) - FLOOR(COUNT(*) * 0.75) FROM horje Where course = 'Python'
);

Output:

result2502

Output

Explanation: In the above image, we can see that only last row of the resultant set is displayed. This is because there are only 3 rows in the resultant set ( rows which contain ‘Python‘ in their course column). This means only one row will be displayed (25% 3 = 1 (approx).

Using Window Function

Another approach is to use the ROW_NUMBER() function, which assigns a unique integer to each row within the result set. In MySQL 8.0 and later, you can use window functions.

Query:

SELECT id, name, course
FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY id) AS rn,
COUNT(*) OVER () FROM horje )
WHERE rn > (SELECT FLOOR(COUNT(*) * 0.75) FROM horje);

Output:

Using-Row

Output

Explanation: In the above image, we can see that the last 25% rows of the result set are displayed. As ROW_NUMBER() function assigns a default starting value 1 to starting row, 2 for second row, and so on. Thus, we have specified a condition to display the rows only if the row number is greater than our calculated value, in this case, it is 5. This will eventually give us our last 25% rows of the result set.

Conclusion

Listing the last 25% of rows in a result set is a useful technique for analyzing recent trends in large datasets. This article covered two prominent methods: using the LIMIT clause and using the ROW_NUMBER() function. By understanding and applying these methods, you can efficiently query and analyze the most recent portions of your data in MySQL.

FAQs on How to List the Last 25% Rows in a Result Set in MySQL

What is the purpose of fetching the last 25% of rows in a result set?

Fetching the last 25% of rows helps in analyzing recent trends, providing quick insights into the most recent data. This is especially useful for quick analytics or reporting.

How do you calculate the starting point for the last 25% of rows using the LIMIT clause?

To calculate the starting point, you need to determine 75% of the total number of rows and use that as the offset value in the LIMIT clause.

How does the Window Function approach differ from the LIMIT clause method?

The Window Function approach, using functions like ROW_NUMBER() in MySQL 8.0 and later versions, allows for more complex and flexible row selection criteria compared to the straightforward LIMIT clause.

Can I apply conditions to filter the result set when using these methods?

Yes, both the LIMIT clause and Window Function methods can be combined with WHERE clauses to filter rows based on specific conditions, such as filtering by a particular column value.

Are these methods efficient for large datasets?

Both methods can be efficient for large datasets, though the performance may vary depending on the size of the dataset and the efficiency of your MySQL server configuration.




Reffered: https://www.geeksforgeeks.org


Databases

Related
Firebase Realtime Database: Reading and Writing Data Firebase Realtime Database: Reading and Writing Data
Understanding Relationships in Database Design Understanding Relationships in Database Design
What is Rollback in Firebase Hosting? What is Rollback in Firebase Hosting?
Firebase Cloud Function Firebase Cloud Function
Data Organization in Firebase Realtime Database Data Organization in Firebase Realtime Database

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14