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';
Copy after login
- 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;
Copy after login
- 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;
Copy after login
- 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%';
Copy after login
- 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');
Copy after login
- 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;
Copy after login
- 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;
Copy after login
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!