MySQL - accessing a table sum and compare to another table?

Posted by assignment_operator on Stack Overflow See other posts from Stack Overflow or by assignment_operator
Published on 2013-10-19T03:32:38Z Indexed on 2013/10/19 3:54 UTC
Read the original article Hit count: 152

Filed under:
|
|

This is for a homework assignment. I just plain don't understand how to do it.

The instructions for this particular question is:

List the branch name for all branches that have at least one
book that has at least 4 copies on hand.

Where the tables in question are:

Branch:

BranchName        | BranchId
Henry Downtown    |    1
16 Riverview      |    2
Henry On The Hill |    3

Inventory:

BookId   |  BranchId  |  OnHand
   1     |     1      |     2
   2     |     3      |     4
   3     |     1      |     8
   4     |     3      |     1
   5     |     1      |     2
   6     |     2      |     3

From what I understand, I can get the number of OnHand per branch name with:

SELECT BranchName, SUM(OnHand) 
FROM Branch B, Inventory I 
WHERE B.BranchId = I.BranchId 
GROUP BY BranchName;

but I don't get how I'd do the comparison between the sum of OnHand per branch and 4.

Any help would be appreciated, guys!

© Stack Overflow or respective owner

Related posts about mysql

Related posts about sql