How to code aggregate functions

The syntax of the aggregate functions

Function syntax Resullt
AVG(expression) The average of the non-NULL values in the expression.
SUM(expression) The total of the non-NULL values in the expression.
MIN(expression) The lowest non-NULL value in the expression.
MAX(expression) The highest non-NULL value in the expression.
COUNT(expression) The number of non-NULL values in the expression.
COUNT(*) The number of rows selected by the query.

Count all products

       SELECT COUNT(*) AS productCount 
       FROM products;
      
count() image

Count all orders and orders that have been shipped

      SELECT COUNT(*) AS totalCount, COUNT(shipDate) AS shippedCount 
      FROM orders;
      
count(*) image

Find lowest, highest, and average prices

      SELECT MIN(listPrice) AS lowestPrice, MAX(listPrice) AS highestPrice, AVG(listPrice) AS averagePrice 
      FROM products;
      
aggregate functions image

Get the total of the calculated values for all orders

      SELECT SUM(itemPrice * quantity – discountAmount) AS ordersTotal 
      FROM orderItems;
      
sum() image

Description

Back