Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 629
Azy Sır Avatar asked Jan 12 '23 00:01

Azy Sır


1 Answers

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.

like image 166
Gordon Linoff Avatar answered Jan 17 '23 14:01

Gordon Linoff