The basic syntax of the WHERE
clause is:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
condition
: A logical expression that specifies the condition the data must satisfy.Let’s say we have a table employees
and want to fetch records for employees with a specific name:
SELECT * FROM employees
WHERE name = 'John Doe';
This query will retrieve all columns (*
) for employees where the name
is 'John Doe'
.
You can also use the WHERE
clause with UPDATE
or DELETE
statements to modify or remove specific records.
UPDATE Example:
UPDATE employees
SET email = 'john.new@example.com'
WHERE name = 'John Doe';
DELETE FROM employees
WHERE name = 'John Doe';
The WHERE
clause is a powerful tool for filtering data in MySQL queries. By defining conditions, you can retrieve, update, or delete only the data that meets your criteria, making it a vital part of SQL operations. Understanding how to use the WHERE
clause effectively will help you work more efficiently with MySQL databases.