Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

255
Views
PostgreSQL: Calculate percentage of rows with a column above a certain value

Suppose I have table of values:

+----+-------+
| Id | Value |
+----+-------+
| 1  |  2    |
| 2  |  9    |
| 3  |  5    |
| 4  |  5    |
| 5  |  8    |
| 6  |  1    |
+----+-------+

How can I calculate the percentage of rows who's value is higher than a given threshold:

Eg, with a threshold of 'higher than 5', how could I query to get the following:

+-------+------------------+
| Total | Percent above 5  |
+-------+------------------+
|   6   |     33.33        |
+-------+------------------+
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Use conditional aggregation:

select count(*),
       avg( (value > 5)::int ) * 100 as percentage_above_5
from t;
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!