Random SQL Examples

Russell Bateman
last update:

Use SUM()

OrderDetails
OrderDetailID OrderID ProductID Quantity
1 10248 11 12
2 10248 42 10
3 10248 72 5
4 10249 14 9
5 10249 51 40

This gets only the total of some of the rows (squint to see which ones):

    SELECT SUM( Quantity )
    AS TotalItemsOrdered
    FROM OrderDetails
    WHERE OrderId = 10248;

...and this just gets all of them:

    SELECT SUM(Quantity) AS TotalItemsOrdered FROM OrderDetails;