sqlite - Is it possible to simplify this sql query? -


i've got 3 tables.

'order details' orderid | quantity | unitprice | productid 1002 | 19 | 17 | 824 1003 | 5 | 15 | 285 1004 | 7 | 6 | 543

'orders' orderid | customerid 1002 | 224 1003 | 224 1004 | 348

'customers' customerid| name | phone 224 | john | 235-453 225 | july | 532-232

i have info people ordered more 10 products average price less 17. did this, not sure if best approach.

select * customers customerid in    (select distinct customerid     orders     orderid in        (select orderid id         'order details'         17 >             (select avg(unitprice)              'order details'              orderid = id              group orderid)           , 10 <             (select sum(quantity)              'order details'              orderid = id              group orderid)         group orderid)) 

you can use "having" clause filter records aggregates (sum, count etc.) meet set of criteria.

it like:

select customer.name,         count(*),         avg(od.unitprice)   customers c inner join orders o on c.customerid = o.customerid inner join orderdetails od on o.orderid = od.orderid group customer.name having count(*) > 10 , avg(od.unitprice) < 17 

Comments