Introduction
When you first start working with databases, it can feel like you’re looking at a giant, overwhelming spreadsheet. Aggregate functions are the tools that turn that mountain of data into meaningful stories.
This article highlights 5 fundamental SQL functions that every analyst should be familiar with, providing a brief overview of their purpose and usage.
1. COUNT()
The COUNT() function is used to count the number of rows that match a specified condition in a database table. It is particularly useful for aggregating data and understanding the size of datasets.
Example:
SELECT COUNT(*)
FROM employees
WHERE department = ‘Finance’;
The query returns the total number of employees in the Finance department
2. AVG()
The AVG() function calculates the average value of a numeric column. This function is essential for analyzing trends and performance metrics over time.
Example:
SELECT AVG(salary)
FROM employees
WHERE department = ‘Engineering’;
This query provides the average salary of employees in the Engineering department.
3. SUM()
The SUM() function calculates the total sum of a numeric column. It is commonly used for financial data, sales records, or any other scenario where the sum of numeric values needs to be determined.
Example:
SELECT
category,
SUM(price * quantity) AS total_revenue
FROM sales
GROUP BY category;
The GROUP BY clause tells the database how to group the rows before performing the calculation.
This query calculates total sales per category. This shows which products are actually making the most money.
4. MIN() and MAX()
The MIN() and MAX() functions retrieve the smallest and largest values from a column.
Example:
SELECT
MIN(salary) AS lowest_pay,
MAX(salary) AS highest_pay
FROM employees;
The query finds the highest and the lowest pay from the employees.
5. CONCAT()
This is a String Function used to join two or more strings together into one.
Example:
SELECT
CONCAT(first_name, ' ', last_name) AS full_name
FROM users;
This query combines the first_name and last_name columns to create a single full_name field for reports.
Conclusion
Mastering these five functions takes you from simply "reading" data to actually analyzing it.
This helps analysts transform raw, messy rows of information into clear insights; leading to more informed decision-making.
Top comments (0)