HOWTO · MySQL
How to Calculate Percentage in MySQL
Learn how to calculate a row's share of a total, a ratio between two columns, and grouped shares in MySQL.
On this page
To calculate a percentage in MySQL, divide the part by the whole and multiply by 100: (part / whole) * 100. The denominator defines what the result means. For a row’s share, use the sum of all rows; for a ratio, use the comparison column; for grouped shares, use the total across groups. The examples below show each pattern and how to round or format its result.
Calculate a row’s percentage of a total
Suppose sales has ID, RepresentativeName, and Sale columns, with sales of 15, 15, and 30. The total is 60, so each row contributes 25%, 25%, and 50%. Calculate the total once in a subquery, then combine that one-row result with every sale using CROSS JOIN: SELECT s.RepresentativeName, s.Sale, ROUND(s.Sale * 100.0 / totals.SalesSum, 2) AS Percentage FROM sales AS s CROSS JOIN (SELECT SUM(Sale) AS SalesSum FROM sales) AS totals;. The 100.0 makes the percentage calculation decimal-valued instead of relying on integer division. ROUND(..., 2) keeps two decimal places in the numeric result. To see the same arithmetic for the first sale, run this small example:
sale = 15
total = 60
percentage = sale * 100 / total
print(f"{percentage:.2f}%")
The total must be nonzero for the ratio to be meaningful; if the table can have no sales or a zero total, handle that condition explicitly, for example with NULLIF(totals.SalesSum, 0) as the denominator.
Calculate a percentage from two columns
For a row-level ratio, divide the numerator by the denominator in that row. If tests contains GroupName, EmployeesCount, and SurveysCount, the survey percentage is SurveysCount / EmployeesCount * 100. For example, 10 surveys out of 200 employees gives 5%. A direct query is SELECT GroupName, EmployeesCount, SurveysCount, ROUND(SurveysCount * 100.0 / NULLIF(EmployeesCount, 0), 2) AS Percentage FROM tests;. NULLIF turns a zero employee count into NULL, so the result is NULL rather than a divide-by-zero calculation. This query needs neither COUNT() nor GROUP BY: each source row already has the two values being compared. If the values are aggregated from multiple records instead, define the intended numerator and denominator first, then aggregate each consistently.
Calculate each group’s percentage of the total with a window function
To find each supplier’s share of all products, first count products per supplier and then divide each count by the total count across those groups. A window aggregate applies that overall total to each grouped row: SELECT SupplierID, COUNT(*) AS ProductCount, ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER (), 2) AS SupplierPercentage FROM products GROUP BY SupplierID;. The inner COUNT(*) is each supplier’s product count; SUM(COUNT(*)) OVER () sums those group counts without collapsing the grouped rows. With six products split as two, one, and three, the shares are 33.33%, 16.67%, and 50.00%. This denominator is the total represented by the query’s grouped rows, so filters affect both the groups and their overall total. The query requires MySQL window-function support; check the server’s documentation if you need to confirm support for your deployment.
Round and format percentage results
ROUND(value, 2) is a numeric function that rounds to two decimal places. Keep the value numeric when you will sort it, compare it, or use it in further calculations. To display a percent sign in a MySQL result, use CONCAT(ROUND(value, 2), '%'); this produces text, not a numeric percentage, and its trailing zeros may not be preserved as numeric formatting. Choose the denominator according to the question: the full-table sum for each row’s share, a second column for a ratio, or the sum across grouped rows for each group’s share. In every case, use decimal arithmetic and decide how a zero denominator should be represented for the application.