Skip to content

Working with percentages in MySQL involves several operations, such as formatting data as a percent, calculating percentiles, and finding the top percentage of a dataset. This guide covers the main techniques and queries for each.

Formatting as Percent

To format a value as a percent, multiply it by 100 and concatenate a ‘%’ sign, which makes results easier to read.

SELECT CONCAT(ROUND(yourValue * 100, 2), '%') AS percent_formatted
FROM yourTable;

How to Get Ten Percent of a Value

To calculate 10% of a value in a MySQL query, multiply the value by 0.1.

SELECT yourColumn * 0.1 AS ten_percent
FROM yourTable;

Ranking and Percent Rank

To rank rows in MySQL, use the RANK() or DENSE_RANK() function. For percent rank, divide the rank by the total number of rows.

SET @total_rows = (SELECT COUNT(*) FROM yourTable);

SELECT
  yourColumn,
  RANK() OVER (ORDER BY yourColumn) AS rank,
  RANK() OVER (ORDER BY yourColumn) / @total_rows AS percent_rank
FROM
  yourTable;

Top Percent of Data

To select the top X percent of data, calculate the number of rows that represent the desired percentage and use it in a LIMIT clause.

SET @percent = 10;
SET @limit = (SELECT ROUND(COUNT(*) * (@percent / 100)) FROM yourTable);

SELECT *
FROM yourTable
ORDER BY yourColumn DESC
LIMIT @limit;

Calculating Percentiles

You can calculate percentiles with the PERCENT_RANK() window function.

SELECT
  yourColumn,
  PERCENT_RANK() OVER (ORDER BY yourColumn) AS percentile
FROM yourTable;

Dealing with Null Values in Percentage Calculations

When calculating percentages, unhandled null values can lead to incorrect results. Use IFNULL or COALESCE to handle them.

-- Example: Calculating 10% of a value, treating nulls as zero
SELECT
  yourColumn,
  IFNULL(yourColumn, 0) * 0.1 AS ten_percent_handling_null
FROM yourTable;

This query treats a null yourColumn as zero, which prevents miscalculations in your percentage results.

Percentile Contiguous (PERCENTILE_CONT) for Specific Percentile Calculation

The PERCENTILE_CONT function in MySQL computes the percentile for a given distribution of values. It’s useful for finding the median or any other specific percentile.

-- Example: Calculating the median (50th percentile)
SELECT PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY yourColumn) OVER () AS median
FROM yourTable;

This query calculates the median value of yourColumn by finding the 50th percentile.

Calculating Percent Increase or Decrease Between Two Values

To see how a value has changed over time, calculate the percentage increase or decrease. This comes up often in financial or performance data analysis.

-- Example: Calculating percent change between two values
SELECT
  oldValue,
  newValue,
  ((newValue - oldValue) / oldValue) * 100 AS percent_change
FROM yourTable;

This query shows the percentage change from oldValue to newValue.

Conditional Percentages Based on Criteria

To calculate percentages based on certain conditions, use conditional aggregation.

-- Example: Calculating the percentage of rows that meet a specific condition
SELECT
  COUNT(*) AS total_rows,
  SUM(CASE WHEN condition THEN 1 ELSE 0 END) AS rows_meeting_condition,
  SUM(CASE WHEN condition THEN 1 ELSE 0 END) / COUNT(*) * 100 AS conditional_percent
FROM yourTable;

This query calculates what percentage of rows in yourTable meet the specified condition.

Conclusion

If this query pattern is part of recurring reporting, Basedash helps you turn it into reusable, AI-native BI workflows: prompt-to-SQL, shared dashboards, and trusted answers that stay aligned with your data model.

Written by

Robert Cooper avatar

Robert Cooper

Senior Software Engineer

Robert Cooper is a senior engineer who builds full-stack product systems across SQL data infrastructure, APIs, and frontend architecture. His work focuses on application performance, developer velocity, and reliable self-hosted workflows that make data operations easier for teams at scale.

View full author profile →

Basedash lets you build charts, dashboards, and reports in seconds using all your data.