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

221
Views
SELECT rows matching an aggregate function without using a subquery

The following query: SELECT * FROM products WHERE price > (SELECT AVG(price) FROM products) selects all products with a above-average prices.

Is it possible to do this using aggregate functions somehow instead of a subquery (nested SELECT) or a Common Table Expression (WITH).

I am asking primarily about Postgres but MySQL or SQL Server also helps.

Something like SELECT * FROM products HAVING price > AVG(price), which doesn't work.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

I think the answer is no.

And there is one additional line because the answer must be at least 30 characters.

over 4 years ago · Santiago Trujillo Report

0

We can achieve these using inner join. Check below query.

SELECT p1.* FROM products P1
inner join (SELECT AVG(price)as price FROM products) P2 on P1.price > P2.price
over 4 years ago · Santiago Trujillo Report

0

The derived table for this solution is only syntactic sugar to be able to apply a where condition on the column alias:

select *
from (
  select p.*, 
         avg(price) over() as avg_price
  from products
) t
where price > avg_price;
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!