What are the usages of WHERE in SQL, specific code examples are required
When using SQL statements for data query, the WHERE clause is a very important part. It can be used to filter out data that meets specific conditions and provides flexible data filtering and retrieval functions. This article will introduce several common uses of the WHERE clause and provide corresponding code examples.
- Simple conditional filtering:
The most basic usage of the WHERE clause is to filter data by giving a condition. For example, to filter out employee information named "John", you can use the following code:
SELECT * FROM employees
WHERE name = 'John';
- Use comparison operators:
The WHERE clause can also use comparison operators (such as , =, =, , etc.) to perform more complex conditional filtering of data. For example, to filter out employee information with a salary greater than 5,000, you can use the following code:
SELECT * FROM employees
WHERE salary > 5000;
- Use logical operators:
The WHERE clause can also use logical operators (such as AND, OR, NOT) combined with multiple conditions to filter. For example, to filter out employee information with the name "John" and a salary greater than 5,000, you can use the following code:
SELECT * FROM employees
WHERE name = 'John' AND salary > 5000;
- Use wildcards:
The WHERE clause also supports the use of wildcards for fuzzing match. For example, to filter out employee information starting with "J", you can use the following code:
SELECT * FROM employees
WHERE name LIKE 'J%';
- Use the IN operator:
The WHERE clause also supports the use of the IN operator to filter Data that satisfies any one of a set of values. For example, to filter out employee information named "John" or "Mike", you can use the following code:
SELECT * FROM employees
WHERE name IN ('John', 'Mike');
- Use BETWEEN and AND operators:
The WHERE clause also supports Use the BETWEEN and AND operators to filter data between two values. For example, to filter out employee information with a salary between 5,000 and 10,000, you can use the following code:
SELECT * FROM employees
WHERE salary BETWEEN 5000 AND 10000;
- Use NULL and IS NULL operators:
The WHERE clause also supports the use of NULL and IS NULL operators filter NULL or non-null values. For example, to filter out employee information without assigned departments, you can use the following code:
SELECT * FROM employees
WHERE department IS NULL;
To sum up, the use of WHERE clauses in SQL statements is very flexible, and various conditions can be carried out according to specific needs. filter. This article introduces several common uses of the WHERE clause and provides corresponding code examples. I hope it will be helpful to readers when writing SQL query statements.
The above is the detailed content of Various uses of WHERE keyword in SQL. For more information, please follow other related articles on the PHP Chinese website!
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn