In MySQL, we can use aggregate functions like "COUNT", "SUM", and "AVG" to perform calculations on sets of rows.
COUNT, AVG, and SUM aggregator functions are combined with the GROUP BY clause to perform calculations on groups of rows.
The COUNT aggregate function counts the number of rows that meet a specified condition in a table.
SELECT COUNT(*) AS total_products FROM products WHERE category = 'GAMES';
Note: The above query counts the total number of products in the `products``category` column is equal to 'GAMES' in the Table.
The SUM function is used to calculate the sum of values in a numeric column.
SELECT SUM(product_price) AS Total_Products_Price FROM products;
Note: The above query calculates all product Prices exist in the `products` Table.
The AVG function is used to calculate the average of values in a numeric column.
SELECT AVG(price) AS average_price FROM products WHERE category = 'GAMES';
Note: The above query calculates the average price of all products available in the `products` where `category` is equal to 'GAMES'.
These aggregate functions can also be used without a WHERE clause to perform calculations on all rows in a table.
-- Total number of products SELECT COUNT(*) AS total_products FROM products; -- Total salary of all products SELECT SUM(price) AS total_price FROM products; -- Average salary of all products SELECT AVG(price) AS average_price FROM products;
The below query returns the total count of products available in each category.
SELECT COUNT(*) AS Total_Products_Category_Wise, category FROM products GROUP BY category;
The below query returns the average price of products available in each category.
SELECT AVG(product_price) AS average_price_category_wise, category FROM products GROUP BY category;
The below query returns the total amount of products for each category.
SELECT SUM(product_price) AS total_amount_category_wise, category FROM products GROUP BY category;