Functional similarities and detailed usage of HAVING and WHERE in MySQL

Abstract: This article will detail the functional similarities between HAVING and WHERE in MySQL. We'll explore their concepts, usage, and differences, with rich examples and output. By reading this article, you will gain an in-depth understanding of how HAVING and WHERE work and their application in database queries.

1. Functional similarities between HAVING and WHERE

Both HAVING and WHERE are used to specify conditions in the query to limit the result set. They have some functional similarities, but also some important differences.

Similarities:

  • Both are used to set conditions in the query.
  • Both can combine multiple conditions through logical operators (such as AND, OR).
  • Fields and values ​​can be compared using comparison operators such as =, <, >.

2. Usage of WHERE clause

The WHERE clause is used to filter rows in a query, returning only those rows that meet certain criteria.

Following is the syntax of WHERE clause −

SELECT 列名 FROM 表名 WHERE 条件;

Example and output

Suppose we have a employeestable called , which contains the employee's name, salary, and department.

Create sample tables and data

First, we create our sample table and data:

CREATE TABLE employees ( id INT PRIMARY KEY, name VARCHAR(50), salary INT, department VARCHAR(50) ); INSERT INTO employees (id, name, salary, department) VALUES (1, 'Alice', 3000, 'HR'), (2, 'Bob', 4000, 'IT'), (3, 'Charlie', 3500, 'HR'), (4, 'David', 5000, 'Sales');

WHERE clause example query

Next, we use the WHERE clause to query employees whose salary is less than 4000:

SELECT name, salary FROM employees WHERE salary < 4000;

Output result:

name salary
Alice 3000
Charlie 3500

3. Usage of the HAVING clause

The HAVING clause is used to filter the grouped result set in the query, and only return the groups that meet certain conditions.

Following is the syntax of the HAVING clause −

SELECT 列名 FROM 表名 GROUP BY 列名 HAVING 条件;

Example and output

We employeesdemonstrate the use of the HAVING clause using the above table.

HAVING clause example query

Next, we use the HAVING clause to query the departments where the average salary of each department is greater than 3500:

SELECT department, AVG(salary) AS avg_salary FROM employees GROUP BY department HAVING avg_salary > 3500;

Output result:

department avg_salary
IT 4000
Sales 5000

Summarize

This article details the functional similarities of HAVING and WHERE in MySQL. The WHERE clause is used to filter rows in the query, and the HAVING clause is used to filter the grouped result set in the query. Both can limit the result set by setting conditions, and support logical and comparison operators.

Through practical examples and output results, we show the application of WHERE clause and HAVING clause in database query. By understanding and skillfully using the WHERE clause and HAVING clause, you will be able to better handle complex query requirements.

Hope this article helps you understand the usage of HAVING and WHERE in MySQL. If you have any questions or doubts, please feel free to ask.

Guess you like

Origin blog.csdn.net/weixin_65846839/article/details/131719882