Ray Fat
Ray Fat

Reputation: 41

Average price with categories SQL query

I currently have a table called Book which has the following fields

BOOK
BOOK_CODE
TITLE
TYPE

There are many options within TYPE, eg SFI,FIC,MYS

I need a query that shows the average price for each option

currently have this but its just showing the average price of all the books:

SELECT AVG(PRICE) FROM BOOK
WHERE TYPE IN ('SFI','MYS','ART','FIC');

Thanks in advance,

Upvotes: 0

Views: 3588

Answers (2)

Valerica
Valerica

Reputation: 1630

"You can use GROUP BY to group values from a column, and, if you wish, perform calculations on that column ... " More info: MySQL - GROUP BY Clause

SELECT
    Type
    ,AVG(Price)
FROM Book
GROUP BY Type

Upvotes: 3

Ullas
Ullas

Reputation: 11556

You should add a GROUP BY

Query

select `type`, avg(`price`) as `avg_price`
from `book`
where `type` in ('SFI','MYS','ART','FIC')
group by `type`;

Upvotes: 0

Related Questions