Aggregate Functions in MySQL

Página 12

Aggregation functions in MySQL are functions that operate on a set of values, but return a single summarized value. These functions are capable of performing calculations that would normally require a for loop, a temporary variable, and many lines of code with just a single line of SQL code. This text will explore the main aggregation functions available in MySQL.

The AVG() function is used to calculate the average of a set of values. For example, if we wanted to calculate the average salary of all employees in a company, we could use the following SQL query: "SELECT AVG(salary) FROM employees". This query would return the average of all salaries in the employees table.

The SUM() function is used to sum all values ​​in a set. For example, to calculate total sales for a particular product, we could use the query: "SELECT SUM(quantity) FROM sales WHERE product_id = 1". This query would return the sum of all sold quantities of the product with id 1.

The MIN() and MAX() functions are used to get the smallest and largest value from a set, respectively. For example, to find the cheapest and most expensive product in a store, we could use the queries: "SELECT MIN(price) FROM products" and "SELECT MAX(price) FROM products". These queries would return the cheapest and most expensive product price, respectively.

The COUNT() function is used to count the number of rows that match a specified criteria. For example, to count the number of customers who have made at least one purchase, we could use the query: "SELECT COUNT(*) FROM customers WHERE purchases > 0". This query would return the number of customers who made at least one purchase.

The GROUP_CONCAT() function is used to concatenate values ​​from a column into a single string. For example, to get a list of all product names in a single string, we could use the query: "SELECT GROUP_CONCAT(name) FROM products". This query would return a string of all product names, separated by commas.

The DISTINCT function is used in conjunction with other aggregation functions to return unique results. For example, to get the number of unique customers who made purchases, we could use the query: "SELECT COUNT(DISTINCT customer_id) FROM purchases". This query would return the number of unique customers who made purchases.

Aggregation functions can be used in conjunction with the GROUP BY clause to group results by one or more columns. For example, to get total sales by product, we could use the query: "SELECT product_id, SUM(quantity) FROM sales GROUP BY product_id". This query would return the total sales for each product.

In summary, the aggregate functions in MySQL are powerful tools that allow you to perform complex calculations with a single line of SQL code. They are essential for any database developer and are often used in reporting and data analysis.

It is important to note that while aggregate functions can greatly simplify SQL code, they can also be slower than other approaches, especially on large datasets. So it's always a good idea to test different approaches and choose the one that offers the best balance between simplicity and performance.

Now answer the exercise about the content:

Which of the following aggregation functions in MySQL is used to calculate the mean of a set of values?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

13WHERE clause in MySQL

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text