How to count the number of instances of each foreign-key ID in a table?

MysqlSql

Mysql Problem Overview


Here's my simple SQL question...

I have two tables:

Books

-------------------------------------------------------
| book_id | author | genre | price | publication_date |
-------------------------------------------------------

Orders

------------------------------------
| order_id | customer_id | book_id |
------------------------------------

I'd like to create a query that returns:

--------------------------------------------------------------------------
| book_id | author | genre | price | publication_date | number_of_orders |
--------------------------------------------------------------------------

In other words, return every column for ALL rows in the Books table, along with a calculated column named 'number_of_orders' that counts the number of times each book appears in the Orders table. (If a book does not occur in the orders table, the book should be listed in the result set, but "number_of_orders" should be zero.

So far, I've come up with this:

SELECT
    books.book_id,
    books.author,
    books.genre,
    books.price,
    books.publication_date,
    count(*) as number_of_orders
from books
left join orders
on (books.book_id = orders.book_id)
group by
    books.book_id,
    books.author,
    books.genre,
    books.price,
    books.publication_date

That's almost right, but not quite, because "number_of_orders" will be 1 even if a book is never listed in the Orders table. Moreover, given my lack of knowledge of SQL, I'm sure this query is very inefficient.

What's the right way to write this query? (For what it's worth, this needs to work on MySQL, so I can't use any other vendor-specific features).

Thanks in advance!

Mysql Solutions


Solution 1 - Mysql

Your query is almost right and it's the right way to do that (and the most efficient)

SELECT books.*, count(orders.book_id) as number_of_orders        
from books
left join orders
on (books.book_id = orders.book_id)
group by
    books.book_id

COUNT(*) could include NULL values in the count because it counts all the rows, while COUNT(orders.book_id) does not because it ignores NULL values in the given field.

Solution 2 - Mysql

SELECT b.book_id,
    b.author,
    b.genre,
    b.price,
    b.publication_date,
    coalesce(oc.Count, 0) as number_of_orders
from books b
left join (
    select book_id, count(*) as Count 
    from Order 
    group by book_id
) oc on (b.book_id = oc.book_id)

Solution 3 - Mysql

Change count(*) to count(orders.book_id)

Solution 4 - Mysql

You're counting the wrong thing. You want to count the non-null book_id's.

SELECT
    books.book_id,
    books.author,
    books.genre,
    books.price,
    books.publication_date,
    count(orders.book_id) as number_of_orders
from books
left join orders
on (books.book_id = orders.book_id)
group by
    books.book_id,
    books.author,
    books.genre,
    books.price,
    books.publication_date

Solution 5 - Mysql

select author.aname,count(book.author_id) as "number_of_books"
from author 
left join book 
on(author.author_id=book.author_id)
GROUP BY author.aname;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmattstuehlerView Question on Stackoverflow
Solution 1 - MysqlFabioView Answer on Stackoverflow
Solution 2 - MysqlD'Arcy RittichView Answer on Stackoverflow
Solution 3 - MysqlDerek KrommView Answer on Stackoverflow
Solution 4 - MysqlCarl F.View Answer on Stackoverflow
Solution 5 - MysqlMohit TejwaniView Answer on Stackoverflow