What is the main purpose of DISTINCT?
Anonymous Quiz
9%
A) Sort rows
60%
B) Remove duplicate rows from the result
19%
C) Delete duplicate rows from the table
11%
D) Group rows for calculations
🚀 𝗪𝗵𝗶𝗰𝗵 𝗧𝗲𝗰𝗵 𝗦𝗸𝗶𝗹𝗹 𝗪𝗶𝗹𝗹 𝗬𝗼𝘂 𝗠𝗮𝘀𝘁𝗲𝗿 𝗶𝗻 𝟮𝟬𝟮𝟲?
Want to build apps, uncover insights, or create AI solutions? Explore certification courses that match your career goals! 🎯
💻 𝗙𝘂𝗹𝗹 𝗦𝘁𝗮𝗰𝗸 𝗗𝗲𝘃𝗲𝗹𝗼𝗽𝗺𝗲𝗻𝘁
👉 https://pdlink.in/3SuUeuD
📊 𝗗𝗮𝘁𝗮 𝗔𝗻𝗮𝗹𝘆𝘁𝗶𝗰𝘀
👉 https://pdlink.in/45vk5ph
💫𝗔𝗜 𝗘𝗻𝗴𝗶𝗻𝗲𝗲𝗿𝗶𝗻𝗴
👉 https://pdlink.in/4fWJVID
🔥 Choose your path, sharpen your skills, and take the next step towards your tech career!
📌 Save this post & share it with a friend planning to upskill.
Want to build apps, uncover insights, or create AI solutions? Explore certification courses that match your career goals! 🎯
💻 𝗙𝘂𝗹𝗹 𝗦𝘁𝗮𝗰𝗸 𝗗𝗲𝘃𝗲𝗹𝗼𝗽𝗺𝗲𝗻𝘁
👉 https://pdlink.in/3SuUeuD
📊 𝗗𝗮𝘁𝗮 𝗔𝗻𝗮𝗹𝘆𝘁𝗶𝗰𝘀
👉 https://pdlink.in/45vk5ph
💫𝗔𝗜 𝗘𝗻𝗴𝗶𝗻𝗲𝗲𝗿𝗶𝗻𝗴
👉 https://pdlink.in/4fWJVID
🔥 Choose your path, sharpen your skills, and take the next step towards your tech career!
📌 Save this post & share it with a friend planning to upskill.
❤3
🚀 Data Science Roadmap 2026
📍 Phase 3: SQL for Data Science
📖 Topic 6 — GROUP BY
GROUP BY is used to group rows that have the same value in one or more columns.
It is mainly used when you want to perform calculations for each group.
For example:
• Total sales by region
• Average salary by department
• Number of customers by city
• Total orders by product
• Maximum transaction by category
🔹 Basic Syntax
Common aggregate functions:
•
•
•
•
•
🔹 Simple Example
Suppose we have:
Query:
Result:
Instead of calculating one overall average, SQL calculates an average for each department.
🔹 GROUP BY with COUNT()
To count employees in each department:
Result:
🔹 GROUP BY with SUM()
Suppose you have sales data:
Query:
Result:
This is extremely common in business analytics.
🔹 GROUP BY with MAX()
Find the highest salary in each department:
🔹 GROUP BY with MIN()
Find the lowest salary in each department:
🔹 GROUP BY with Multiple Columns
You can group by multiple columns.
Here SQL creates groups based on:
For example:
🔹 GROUP BY with WHERE
WHERE filters rows before grouping.
🔹 GROUP BY with ORDER BY
You can sort grouped results.
For example, find departments with the highest total salary first:
This is very useful for ranking groups.
🔹 GROUP BY vs DISTINCT
These two can sometimes look similar.
DISTINCT
Used to find unique departments.
GROUP BY
📍 Phase 3: SQL for Data Science
📖 Topic 6 — GROUP BY
GROUP BY is used to group rows that have the same value in one or more columns.
It is mainly used when you want to perform calculations for each group.
For example:
• Total sales by region
• Average salary by department
• Number of customers by city
• Total orders by product
• Maximum transaction by category
🔹 Basic Syntax
SELECT column_name, aggregate_function(column_name)
FROM table_name
GROUP BY column_name;
Common aggregate functions:
•
COUNT() → Count rows•
SUM() → Calculate total•
AVG() → Calculate average•
MIN() → Find minimum•
MAX() → Find maximum🔹 Simple Example
Suppose we have:
department salary
IT 60000
IT 80000
HR 50000
HR 70000
Finance 90000
Query:
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department;
Result:
department avg_salary
IT 70000
HR 60000
Finance 90000
Instead of calculating one overall average, SQL calculates an average for each department.
🔹 GROUP BY with COUNT()
To count employees in each department:
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department;
Result:
department employee_count
IT 2
HR 2
Finance 1
🔹 GROUP BY with SUM()
Suppose you have sales data:
region sales
North 10000
North 15000
South 20000
South 12000
Query:
SELECT region, SUM(sales) AS total_sales
FROM sales
GROUP BY region;
Result:
North 25000
South 32000
This is extremely common in business analytics.
🔹 GROUP BY with MAX()
Find the highest salary in each department:
SELECT department, MAX(salary) AS highest_salary
FROM employees
GROUP BY department;
🔹 GROUP BY with MIN()
Find the lowest salary in each department:
SELECT department, MIN(salary) AS lowest_salary
FROM employees
GROUP BY department;
🔹 GROUP BY with Multiple Columns
You can group by multiple columns.
SELECT department, job_title, COUNT(*) AS employee_count
FROM employees
GROUP BY department, job_title;
Here SQL creates groups based on:
department + job_titleFor example:
IT Analyst 5
IT Developer 8
HR Analyst 3
Finance Manager 4
🔹 GROUP BY with WHERE
WHERE filters rows before grouping.
SELECT department, AVG(salary) AS avg_salary
FROM employees
WHERE salary > 50000
GROUP BY department;
🔹 GROUP BY with ORDER BY
You can sort grouped results.
For example, find departments with the highest total salary first:
SELECT department, SUM(salary) AS total_salary
FROM employees
GROUP BY department
ORDER BY total_salary DESC;
This is very useful for ranking groups.
🔹 GROUP BY vs DISTINCT
These two can sometimes look similar.
DISTINCT
SELECT DISTINCT department
FROM employees;
Used to find unique departments.
GROUP BY
SELECT department, COUNT(*)
FROM employees
GROUP BY department;
❤3
Used when you want to perform calculations for each department.
Simple rule:
DISTINCT → Give me unique values
GROUP BY → Give me groups so I can calculate something
🔹 Real-World Data Science Example
Imagine an e-commerce dataset.
You want to know the total revenue generated by each product category.
This gives you a category-level summary that can be used for:
• Revenue analysis
• Product performance
• Business reporting
• Feature creation
• Exploratory data analysis
🔹 Common Mistake
Consider:
This can cause an error in many SQL databases because employee_name is neither:
• included in GROUP BY, nor
• used inside an aggregate function.
A valid query would be:
🔹 Important Rule
When using GROUP BY, selected columns generally need to be either:
1. Included in the GROUP BY, or
2. Wrapped inside an aggregate function.
Example:
🔹 Interview Questions
1. What is GROUP BY?
GROUP BY groups rows with the same values so aggregate calculations can be performed for each group.
2. Which aggregate functions are commonly used with GROUP BY?
3. What is the difference between WHERE and GROUP BY?
WHERE filters individual rows before grouping.
GROUP BY creates groups from the filtered rows.
4. Can GROUP BY use multiple columns?
Yes.
5. Can GROUP BY be used without aggregate functions?
Yes, although DISTINCT is often clearer when the goal is simply to return unique combinations.
🎯 Practice Questions
1. Find the number of employees in each department.
2. Find the average salary for each department.
3. Find the total sales for each region.
4. Find the highest salary in each department.
5. Find the total revenue for each product category and sort categories from highest to lowest revenue.
🎯 Key Takeaways
• GROUP BY creates groups of similar rows.
• It is commonly used with aggregate functions.
•
•
•
•
•
• WHERE filters rows before grouping.
• ORDER BY can sort grouped results.
• GROUP BY is one of the most important SQL concepts for Data Science and Analytics.
📍Double Tap ❤️ For More
Simple rule:
DISTINCT → Give me unique values
GROUP BY → Give me groups so I can calculate something
🔹 Real-World Data Science Example
Imagine an e-commerce dataset.
You want to know the total revenue generated by each product category.
SELECT category,
SUM(quantity * price) AS total_revenue
FROM sales
GROUP BY category
ORDER BY total_revenue DESC;
This gives you a category-level summary that can be used for:
• Revenue analysis
• Product performance
• Business reporting
• Feature creation
• Exploratory data analysis
🔹 Common Mistake
Consider:
SELECT department, employee_name, AVG(salary)
FROM employees
GROUP BY department;
This can cause an error in many SQL databases because employee_name is neither:
• included in GROUP BY, nor
• used inside an aggregate function.
A valid query would be:
SELECT department, AVG(salary)
FROM employees
GROUP BY department;
🔹 Important Rule
When using GROUP BY, selected columns generally need to be either:
1. Included in the GROUP BY, or
2. Wrapped inside an aggregate function.
Example:
SELECT department, COUNT(*)
FROM employees
GROUP BY department;
department → grouped columnCOUNT(*) → aggregate 🔹 Interview Questions
1. What is GROUP BY?
GROUP BY groups rows with the same values so aggregate calculations can be performed for each group.
2. Which aggregate functions are commonly used with GROUP BY?
COUNT(), SUM(), AVG(), MIN(), and MAX().3. What is the difference between WHERE and GROUP BY?
WHERE filters individual rows before grouping.
GROUP BY creates groups from the filtered rows.
4. Can GROUP BY use multiple columns?
Yes.
GROUP BY department, job_title;5. Can GROUP BY be used without aggregate functions?
Yes, although DISTINCT is often clearer when the goal is simply to return unique combinations.
🎯 Practice Questions
1. Find the number of employees in each department.
2. Find the average salary for each department.
3. Find the total sales for each region.
4. Find the highest salary in each department.
5. Find the total revenue for each product category and sort categories from highest to lowest revenue.
🎯 Key Takeaways
• GROUP BY creates groups of similar rows.
• It is commonly used with aggregate functions.
•
COUNT() counts.•
SUM() totals.•
AVG() calculates averages.•
MIN() finds the minimum.•
MAX() finds the maximum.• WHERE filters rows before grouping.
• ORDER BY can sort grouped results.
• GROUP BY is one of the most important SQL concepts for Data Science and Analytics.
📍Double Tap ❤️ For More
❤5
𝗧𝗢𝗣 𝗙𝗥𝗘𝗘 𝗖𝗢𝗨𝗥𝗦𝗘𝗦 𝗧𝗢 𝗕𝗢𝗢𝗦𝗧 𝗬𝗢𝗨𝗥 𝗦𝗞𝗜𝗟𝗟𝗦 𝗜𝗡 𝟮𝟬𝟮𝟲!
Explore FREE online learning resources to develop in-demand skills and improve your career opportunities!
🎓 Why Explore These Courses?
✅ Learn valuable professional skills
✅ Improve your resume
✅ Build practical knowledge
✅ Learn at your own pace
𝗘𝘅𝗽𝗹𝗼𝗿𝗲 𝗙𝗥𝗘𝗘 𝗖𝗼𝘂𝗿𝘀𝗲𝘀 𝗛𝗲𝗿𝗲👇
https://pdlink.in/3VBdt6V
💡 Learn New Skills → Build Confidence → Unlock Career Opportunities!
Explore FREE online learning resources to develop in-demand skills and improve your career opportunities!
🎓 Why Explore These Courses?
✅ Learn valuable professional skills
✅ Improve your resume
✅ Build practical knowledge
✅ Learn at your own pace
𝗘𝘅𝗽𝗹𝗼𝗿𝗲 𝗙𝗥𝗘𝗘 𝗖𝗼𝘂𝗿𝘀𝗲𝘀 𝗛𝗲𝗿𝗲👇
https://pdlink.in/3VBdt6V
💡 Learn New Skills → Build Confidence → Unlock Career Opportunities!
❤1
Which aggregate function calculates the average salary?
Anonymous Quiz
5%
A) SUM()
5%
B) COUNT()
89%
C) AVG()
2%
D) TOTAL()
❤2
Which clause filters rows before GROUP BY?
Anonymous Quiz
22%
A) HAVING
32%
B) ORDER BY
43%
C) WHERE
2%
D) LIMIT
❤1
What is the main difference between DISTINCT and GROUP BY?
Anonymous Quiz
11%
A) DISTINCT sorts data while GROUP BY filters data
85%
B) DISTINCT removes duplicate results, while GROUP BY is commonly used for grouping and aggregation
4%
C) GROUP BY deletes duplicate rows
0%
D) There is no difference
❤4👍1😢1
What does this query return?
SELECT department, SUM(salary) AS total_salary
FROM employees GROUP BY department;
SELECT department, SUM(salary) AS total_salary
FROM employees GROUP BY department;
Anonymous Quiz
13%
A) The highest salary in each department
12%
B) The average salary in each department
70%
C) The total salary for each department
5%
D) The number of employees in each department
❤2
𝗗𝗮𝘁𝗮 𝘀𝗰𝗶𝗲𝗻𝗰𝗲 𝗖𝗲𝗿𝘁𝗶𝗳𝗶𝗰𝗮𝘁𝗶𝗼𝗻 𝗪𝗶𝘁𝗵 𝗔𝗜😍
Future Ready Data Science and AI Skills You Build
->Build Python, Machine Learning, and AI Skills
->Get Mentored by Industry Experts
->Practice with Mock Interviews
->Ace Your Placements & Land Dream Tech Job
🤝 500+ Partner Companies |🎓 2000+ Students Placed
🔗 𝗥𝗲𝗴𝗶𝘀𝘁𝗲𝗿 𝗡𝗼𝘄 👇 :-
https://pdlink.in/47pezFz
📲 Save & Share with Friends Preparing for Data Science & AI Jobs!
Future Ready Data Science and AI Skills You Build
->Build Python, Machine Learning, and AI Skills
->Get Mentored by Industry Experts
->Practice with Mock Interviews
->Ace Your Placements & Land Dream Tech Job
🤝 500+ Partner Companies |🎓 2000+ Students Placed
🔗 𝗥𝗲𝗴𝗶𝘀𝘁𝗲𝗿 𝗡𝗼𝘄 👇 :-
https://pdlink.in/47pezFz
📲 Save & Share with Friends Preparing for Data Science & AI Jobs!
❤3
𝗜𝗕𝗠 𝗙𝗥𝗘𝗘 𝗧𝗘𝗖𝗛 𝗖𝗢𝗨𝗥𝗦𝗘𝗦 𝟮𝟬𝟮𝟲! 🎓
Explore IBM Cognitive Class and build in-demand tech skills without spending money on course fees!
📚 𝗧𝗼𝗽 𝗙𝗥𝗘𝗘 𝗖𝗼𝘂𝗿𝘀𝗲𝘀 𝘁𝗼 𝗘𝘅𝗽𝗹𝗼𝗿𝗲:-
💫 Artificial Intelligence & Machine Learning
🔥Python for Data Science
📊 Data Science Fundamentals
☁️ Cloud Computing & Big Data
🔗 𝗦𝘁𝗮𝗿𝘁 𝗟𝗲𝗮𝗿𝗻𝗶𝗻𝗴 𝗛𝗲𝗿𝗲 👇
https://pdlink.in/4zGswuX
📲 Save & Share with friends preparing for tech careers! 🔥
Explore IBM Cognitive Class and build in-demand tech skills without spending money on course fees!
📚 𝗧𝗼𝗽 𝗙𝗥𝗘𝗘 𝗖𝗼𝘂𝗿𝘀𝗲𝘀 𝘁𝗼 𝗘𝘅𝗽𝗹𝗼𝗿𝗲:-
💫 Artificial Intelligence & Machine Learning
🔥Python for Data Science
📊 Data Science Fundamentals
☁️ Cloud Computing & Big Data
🔗 𝗦𝘁𝗮𝗿𝘁 𝗟𝗲𝗮𝗿𝗻𝗶𝗻𝗴 𝗛𝗲𝗿𝗲 👇
https://pdlink.in/4zGswuX
📲 Save & Share with friends preparing for tech careers! 🔥
❤1