List the First 50% Rows in a Result Set
Last Updated :
23 Jul, 2025
When working with substantial quantities of data in MySQL, it's often necessary to extract just a portion of the data. For example, during data analysis or when implementing pagination, you might need to list the top 50% of rows from a query's result set. This article will guide you through the steps to achieve this.
How to Listing First 50% Rows
Step 1. Find the number of rows
Determine the total number of rows in your result set. This can be done using a query with the COUNT(*)
function. For example, if we have a table named employees
:
SELECT COUNT(*) AS totalrows
FROM employees;
This query will return the total number of rows in the employees
table.
Step 2. Calculating Half of the total rows
Calculate half the number of rows by first determining the total number of rows and then dividing it by 2. This can be done using user-defined variables in MySQL:
SET @totalrows = (SELECT COUNT(*) FROM employees);
SET @halfrows = @totalrows / 2;
Here, @totalrows stores the total number of rows, @halfrows stores half of that number, and both are user-defined variables.
Step 3. Listing the first 50% rows
Using the LIMIT clause, we can get the first 50% of rows from our table as we have determined the number of rows to fetch. We can limit the number of rows returned by a query using the number of rows. The query for restricting the number of rows will be
set @sql = concat("SELECT * FROM employees LIMIT ", @halfrows);
PREPARE stmt FROM @sql;
EXECUTE stmt;
- Here we cannot use LIMIT statements directly because user-defined variables cannot be accessed directly in a LIMIT clause.
- To overcome this difficulty, we are using the statement method of MySQL. Here we will declare a statement under which we will use the user-defined variable i.e. 'halfrows'.
- We will now use the PREPARE and Execute method to execute the created statement.
This query will give us the first 50% of rows from employees tables according to the order they already present.
Step 4. Arranging the result by specific order (optional)
On many occasions, we have to get the result in specific order, for example first 50% of rows by id. We can do this by adding an ORDER BY statement within our query. For example, if we need the rows ordered by employee ID, we can write the next statement:
set @sql = concat("SELECT * FROM employees ORDER BY salary LIMIT ", @halfrows);
PREPARE stmt FROM @sql;
EXECUTE stmt
- If we want to get the first 50% of data for a specific condition, such as if we we want the first 50% by salary of employees, we have to use the order by clause.
- Here also we need to use user user-defined variable, So we will use the statement mentioned in the above step.
The first 50% of employee table rows will be returned by this query using employee ID column ordering.
Example
1. Create the employees
Table:
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
position VARCHAR(100),
salary DECIMAL(10, 2)
);
2. Insert Records into the employees
Table:
INSERT INTO employees (employee_id, first_name, last_name, position, salary) VALUES
(1, 'Arjun', 'Nair', 'Marketing Specialist', 50000.00),
(2, 'Rajesh', 'Gupta', 'Project Manager', 75000.00),
(3, 'Amit', 'Sharma', 'Software Engineer', 60000.00),
(4, 'Anita', 'Joshi', 'Accountant', 62000.00),
(5, 'Vijay', 'Kumar', 'DevOps Engineer', 70000.00),
(6, 'Ritu', 'Verma', 'Business Analyst', 60000.00),
(7, 'Sneha', 'Patil', 'Data Analyst', 55000.00),
(8, 'Pooja', 'Mehta', 'UX Designer', 65000.00),
(9, 'Sanjay', 'Singh', 'Software Tester', 45000.00),
(10, 'Priya', 'Reddy', 'HR Manager', 80000.00);
Output:
Table Employees3. Calculate the Total Number of Rows:
SELECT COUNT(*) AS totalrows
FROM products;
4. Calculate Half of the Total Rows:
SET @totalrows = (SELECT COUNT(*) FROM products);
SET @halfrows = @total_rows / 2;
5. Retrieve the First 50% of the Rows:
set @sql = concat("SELECT * FROM employees LIMIT ", @halfrows);
PREPARE stmt FROM @sql;
EXECUTE stmt;
The Result after executing this code will be :
First 50% rows6. Order the Results by Salary(Optional):
If we want to retrieve the first 50% of data by the salary of employees, we will execute it with the following lines of code
set @sql = concat("SELECT * FROM employees ORDER BY salary LIMIT ", @halfrows);
PREPARE stmt FROM @sql;
EXECUTE stmt;
Output:
First 50% rows by salaryConclusion
To obtain the first 50% of results in MySQL, we have to first calculate half of the total number of rows. The half count of rows has to be stored in the variable. Then to obtain that 50% rows only, the LIMIT clause will be used to limit the number of rows that we require. This is how we can obtain the first 50% of rows in a result set in MySQL.
Similar Reads
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%
5 min read
List the Last 5 Rows of a Result Set SQL (Structured Query Language) is a powerful tool for managing databases and performing complex operations on data. A common requirement when working with large datasets is retrieving the last few rows of a result set. This can be especially useful for tracking recent records, sorting data, or anal
4 min read
PL/SQL Query to List the First 50% Rows in a Result Set Listing the First 50% Rows in a Result Set can be considered as a normal day-to-day problem. When dealing with data analysis-related tasks, especially with large data sets, fetching all the data at once might create a problem. Getting the first 50% of rows can help in performing the initial analysis
5 min read
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, o
4 min read
List the Last 5 Rows of a Result Set in PostgreSQL PostgreSQL provides several methods to retrieve data from tables. One common approach involves using the SELECT statement with ORDER BY to sort data, with the option to use DESC for descending order. By understanding these basics, we can explore techniques to list the last few rows of a result set i
4 min read
PL/SQL Query to List the Last 25% Rows in a Result Set We sometimes need to fetch some specific rows from our result set. There are many reasons to do so. When we are working on real-time analytics, focusing on recent data is then our need. This helps us in quicker decision-making as it provides us the quicker insight into the latest data. PL/SQL is a p
5 min read