- SQL Chips and Chunks Newsletter
- Posts
- Correction: Missing Query Now Included! Week 5 - SQL Chips and Chunks Newsletter
Correction: Missing Query Now Included! Week 5 - SQL Chips and Chunks Newsletter
1. SQL Chip: Using SUM
to Add Values
The SUM
function is used to calculate the total of a numeric column. This is particularly useful for summarizing sales, expenses, or any other aggregate data that requires addition. Here’s an example to find the total salary expense for each department:
SELECT
department,
SUM(salary) AS total_salary
FROM employees
GROUP BY department;
In this example, SUM(salary)
calculates the total salary for each department. This function is essential for financial and business analysis, helping you summarize large datasets quickly.
2. SQL Chunk: Using AVG
for Averages
The AVG
function is used to find the average value of a numeric column. Whether you're calculating the average salary or average sales, AVG
helps to understand central tendencies in your data. Here’s an example to find the average salary by department:
SELECT
department,
AVG(salary) AS avg_salary
FROM employees
GROUP BY department;
This query groups the employees by department and calculates the average salary for each department. The AVG
function is invaluable when analyzing trends and making data-driven decisions.
3. SQL Challenge: Finding the Maximum and Minimum Salaries
This week’s challenge: Write a query to find the highest and lowest salary in each department.
Hint: Use MAX(salary)
for the highest salary and MIN(salary)
for the lowest salary.
SELECT
department,
MAX(salary) AS highest_salary,
MIN(salary) AS lowest_salary
FROM employees
GROUP BY department;
Try modifying this query to also return the names of the employees who earn the highest and lowest salaries in each department!
4. Common SQL Mistake: Misunderstanding SUM
with NULL Values
A common mistake is assuming that SUM
will count NULL
values. In reality, SUM
ignores NULL
values in the calculation. This means that if a column contains NULL
values, they will not be included in the sum.
SELECT
department,
SUM(salary) AS total_salary
FROM employees
GROUP BY department;
If there are NULL
salary values, they are simply ignored. Ensure you understand this behavior when performing aggregation!
5. SQL in Practice: Sales Analysis with SUM
and AVG
In sales analysis, it’s useful to know the total and average sales for each region. Here’s a query that calculates the total and average sales for each region:
SELECT
region,
SUM(sales_amount) AS total_sales,
AVG(sales_amount) AS avg_sales
FROM sales
GROUP BY region;
This query summarizes both the total and average sales by region, providing valuable insights into regional performance. What additional aggregate functions could you use to enhance this analysis?
6. SQL Resource: Data Engineer Academy (Free & Paid Resources)
As part of your journey into SQL and data engineering, don’t forget about Data Engineer Academy. They offer free resources for beginners and intermediate learners, covering SQL, data pipelines, and data warehouses. For those looking to dive deeper, paid members get access to structured courses, hands-on projects, mentorship, one-on-one coaching, mock interviews, job preparation resources, and even guaranteed interviews to help transition into a data engineering career. If you’re serious about leveling up your skills and landing a job, this is a fantastic place to explore!