Using Group Functions in MySQL

Página 56

MySQL is a relational database management system (RDBMS) that uses SQL (Structured Query Language) to access, add to, or manage database content. Group functions in MySQL are an essential part of data manipulation as they allow you to perform an operation on a group of values ​​to return a single result that is applied to a group of rows.

Group Functions in MySQL

The group functions in MySQL are used to return a single result after performing an operation on a group of values. The most common group functions include AVG(), COUNT(), MAX(), MIN(), SUM(), GROUP_CONCAT() and many others.

AVG()

The AVG() function is used to return the average of a group of values. For example, if we want to find the average salary of all employees, we can use the AVG() function like this:

SELECT AVG(salary) AS 'Average Salary'
FROM employees;

This will return the average salary of all employees.

COUNT()

The COUNT() function is used to return the number of rows that match a specified condition. For example, if we want to count the number of employees in a certain department, we can use the COUNT() function like this:

SELECT COUNT(*) AS 'Number of Employees'
FROM employees
WHERE department = 'Sales';

This will return the number of employees in the sales department.

MAX() and MIN()

The MAX() and MIN() functions are used to return the largest and smallest value in a set of values, respectively. For example, if we want to find the oldest and youngest employee, we can use the MAX() and MIN() functions like this:

SELECT MAX(age) AS 'Oldest Employee', MIN(age) AS 'Youngest Employee'
FROM employees;

This will return the age of the oldest and youngest employee.

SUM()

The SUM() function is used to return the sum total of a numeric field over a set of values. For example, if we want to find the total salaries paid to employees, we can use the SUM() function like this:

SELECT SUM(salary) AS 'Total Salaries'
FROM employees;

This will return the total sum of salaries for all employees.

GROUP_CONCAT()

The GROUP_CONCAT() function is used to concatenate field values ​​from multiple lines into a single string. For example, if we want to list all department names in a single string, we can use the GROUP_CONCAT() function like this:

SELECT GROUP_CONCAT(department) AS 'Department List'
FROM departments;

This will return a single string of all department names.

Using Group Functions with the GROUP BY Clause

Group functions are often used with the GROUP BY clause in an SQL query. The GROUP BY clause is used to group rows that have the same values ​​in specified columns into groups. Group functions are then applied to each group.

For example, if we want to find the average salary for each department, we can use the AVG() function with the GROUP BY clause like this:

SELECT department, AVG(salary) AS 'Average Salary'
FROM employees
GROUP BY department;

This will return the average salary for each department.

In summary, the group functions in MySQL are powerful tools that allow you to perform operations on a set of values ​​and return a single result. They are essential for data manipulation and data analysis in a MySQL database.

Now answer the exercise about the content:

Which of the following MySQL group functions is used to return the sum total of a numeric field over a set of values?

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

You missed! Try again.

Next page of the Free Ebook:

57Using Aggregate Functions 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