Azy Sır
Azy Sır

Reputation: 165

SQL - How can I find the average of an SQL statement with a LIMIT?

Currently I'm working with a database on phpmyadmin. I'm trying to find the Average of an SQL statement that is implementing a LIMIT code.

SQL Statement -

SELECT avg(value) FROM que LIMIT 10

The problem with the code is its not averaging the first 10 numbers in value column, but all of them. So the LIMIT 10 isn't actually working. Is there anyway to avoid this or an alternative?

Upvotes: 4

Views: 2195

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270763

You need to use a subquery:

SELECT avg(value)
FROM (select value
      from que
      LIMIT 10
     ) q;

Do note, however, that use of limit without an order by produces arbitrary results -- there is no definition of the "first ten" records in a table.

Upvotes: 9

Related Questions