Objective: 🔍 Goal: Master the art of summarizing and grouping your data using aggregate functions in SQL, such as SUM, COUNT, AVG, MAX, MIN, and the GROUP BY clause.
Introduction: 💡 Ever wondered how e-commerce platforms quickly calculate the total sales for a particular product, or how forums determine the number of posts a user has made? Aggregate functions in SQL are the heroes behind the scenes, performing these operations in milliseconds.
Main Content:
1️⃣ SUM Function:
SUM
function allows you to add up all the values in a specific column, providing a total.SELECT SUM(total_amount) as total_amount FROM orders;
2️⃣ COUNT Function:
COUNT
function, you can determine how many rows match a certain condition or just count all rows in a tableSELECT COUNT(product_id) as number_of_products FROM products;
2️⃣ COUNT DISTINCT:
DISTINCT
keyword in SQL is used to return only distinct (unique) values in the result set of a SELECT
statement. It eliminates any duplicate records based on the columns specified. It's particularly useful when you want to know the unique values of a column or set of columns in a table.