📜  SQL Where - SQL (1)

📅  最后修改于: 2023-12-03 15:20:15.886000             🧑  作者: Mango

SQL Where - SQL

Introduction

In SQL, the WHERE clause is used to filter records that meet a certain condition. It is used in conjunction with the SELECT, UPDATE, DELETE and other SQL statements to specify criteria for selection, retrieval or modification of data from a database table.

Syntax

The basic syntax for the WHERE clause is as follows:

SELECT column1, column2, ...
FROM table_name
WHERE condition;

Here, column1, column2, ... are the names of the columns to be retrieved, table_name is the name of the table where data is stored, and condition is the expression that specifies the criteria for retrieving records.

Examples
SELECT statement with WHERE clause

To select all records from a table where the value in the name column is 'John', we can use the following SQL statement:

SELECT *
FROM customers
WHERE name = 'John';

In this example, customers is the name of the table, name is the name of the column, and 'John' is the value that we want to match.

UPDATE statement with WHERE clause

To update the value of a column in a table where the value in another column meets a certain condition, we can use the following SQL statement:

UPDATE employees
SET salary = 50000
WHERE department = 'IT';

In this example, employees is the name of the table, salary is the name of the column that we want to update, and department is the name of the column that we use as a filter to identify the records to be updated.

DELETE statement with WHERE clause

To delete records from a table where the value in a certain column meets a certain condition, we can use the following SQL statement:

DELETE FROM orders
WHERE priority = 'high';

In this example, orders is the name of the table, and priority is the name of the column that we use as a filter to identify the records to be deleted.

Conclusion

The WHERE clause is a powerful tool in SQL that allows us to filter records based on a certain condition. By using it in conjunction with SELECT, UPDATE, DELETE and other SQL statements, we can retrieve, modify or delete data from a database table with ease.