Pranav Bakare
Posted on October 12, 2024
The GROUP BY and DISTINCT clauses in SQL are both used to handle duplicate data, but they serve different purposes and work in different ways:
- DISTINCT
Purpose: Removes duplicate rows from the result set, returning only unique values.
Use Case: When you want to fetch unique values from one or more columns without any aggregation.
Example:
SELECT DISTINCT department FROM employees;
This query will return a list of unique department names from the employees table.
- GROUP BY
Purpose: Groups rows that have the same values in specified columns and allows performing aggregate functions (like SUM, COUNT, AVG, etc.) on these groups.
Use Case: When you need to aggregate data and perform calculations on groups of data.
Example:
SELECT department, COUNT(*) FROM employees GROUP BY department;
This query will group the rows by department and count the number of employees in each department.
Key Differences
In summary, use DISTINCT when you only need to eliminate duplicate rows, and use GROUP BY when you need to aggregate data based on specific criteria.
Posted on October 12, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.