Search code examples
sqlitesumlimit

Using SUM() with LIMIT sums all rows in the table ignoring the limit


The following two SQLite queries return the same result, the LIMIT is ignored. Is there a workaround?

SQL

select count(*),sum(close) from history where symbol=494 limit 1;  
select count(*),sum(close) from history where symbol=494; 

OUTPUT

#   Count(*)    Sum(close)
1.  501         97836.04
2.  501         97836.04

Solution

  • If you want to apply a LIMIT and count afterwards, you should make an embedded query:

    select count(*),sum(close) 
    from 
    (
      select close from history where symbol=494 limit 1
    ) t
    

    But this query hardly make sense.