Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

avg function and its output

$
0
0
Hello
I guess I'm not understanding something about the avg function and how it's relating to my query. The objective is to find the books in each category that are below the average of the books in the given category.

When I put this
SELECT title, category, retail FROM books WHERE (category, retail) IN (SELECT category, avg(retail) FROM books GROUP BY category) ORDER BY category;

Output:
HOW TO MANAGE THE MANAGER BUSINESS 31.95
BODYBUILD IN 10 MINUTES A DAY FITNESS 30.95
SHORTEST POEMS LITERATURE 39.95
HOW TO GET FASTER PIZZA SELF HELP 29.95
4 rows returned in 0.02 seconds

This is not all of the categories listed.

This is a plain query with no subqueries. It shows 8 categories (correct)
SELECT title, category, retail FROM books WHERE (category, retail) IN (SELECT category, avg(retail) FROM books GROUP BY category) ORDER BY category;

USINESS 31.95
CHILDREN 34.45
COMPUTER 52.85
COOKING 24.35
FAMILY LIFE 55.975
FITNESS 30.95
LITERATURE 39.95
SELF HELP 29.95
8 rows returned in 0.01 seconds ''

I don't understand why the first query is only showing 4 categories.

Thanks in advance!

Viewing all articles
Browse latest Browse all 13329

Trending Articles