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

Group By Query

$
0
0
Ok here's a very stupid question from an old man who is trying to learn SQL.
I have a table which maintains some records about some books in a library.
The columns are like: Book_No, Book_Name, Category, Cost
Now if I want to get the details of the books with the highest price in their category, I am facing an issue.
If I go with
Code:

select category, max(cost) from books group by category
I get the proper output even though it doesnt give me the exact details of the books.
But if I go for something like
Code:

select book_no, book_name, category, max(cost) from books
group by category, book_no, book_name;

the output goes all haywire.
Can someone please explain why this is happening?
I have found an alternate way of doing it with inline views but really not sure if I am doing the right thing here especially the where clauses.

Code:

select b.book_no, b.book_name, b.author_name, b.cost, inl.category from books b,
(select category, max(cost) mcst from books group by category) inl
where b.cost = inl.mcst
and b.category = inl.category
order by 4 desc;

Thanks in advance :)

Viewing all articles
Browse latest Browse all 13329

Trending Articles