MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
The WHERE
clause in MySQL is used to filter records. It's used to specify the criteria that the rows or records must meet to be selected or affected by a SELECT
, UPDATE
, DELETE
, or INSERT
statement.
Here's the basic syntax:
SELECT column1, column2, ... FROM table_name WHERE condition;
The WHERE
clause follows the FROM
clause and precedes the GROUP BY
, HAVING
, ORDER BY
, LIMIT
, or OFFSET
clauses if they are used.
Examples:
Let's say you have a table named employees
with the following data:
id | name | age | salary |
---|---|---|---|
1 | John | 25 | 50000 |
2 | Jane | 28 | 60000 |
3 | Alice | 30 | 70000 |
4 | Bob | 35 | 80000 |
5 | Carol | 40 | 90000 |
You can use the WHERE
clause to filter records. For example, if you want to select employees who earn more than 60000, you would use:
SELECT * FROM employees WHERE salary > 60000;
This will return:
id | name | age | salary |
---|---|---|---|
3 | Alice | 30 | 70000 |
4 | Bob | 35 | 80000 |
5 | Carol | 40 | 90000 |
You can also use the WHERE
clause to combine more than one condition using the AND
and OR
operators. For example, to select employees who earn more than 60000 and are less than 35 years old:
SELECT * FROM employees WHERE salary > 60000 AND age < 35;
This will return:
id | name | age | salary |
---|---|---|---|
3 | Alice | 30 | 70000 |
The WHERE
clause is also used in the UPDATE
and DELETE
statements to specify which records to update or delete. For example, to give a raise to employees who earn less than 60000:
UPDATE employees SET salary = salary * 1.1 WHERE salary < 60000;
Or to delete employees who are older than 60:
DELETE FROM employees WHERE age > 60;
Remember to use the WHERE
clause carefully, especially with UPDATE
and DELETE
statements, as it could affect more records than you intend.
MySQL WHERE Clause Example:
SELECT column1, column2, ... FROM table_name WHERE condition;
How to Use WHERE in MySQL:
SELECT product_name, price FROM products WHERE category_id = 1;
Conditional Filtering with WHERE in MySQL:
SELECT customer_name, order_date FROM orders WHERE total_amount > 1000;
Using AND and OR in WHERE Conditions in MySQL:
SELECT employee_name, salary FROM employees WHERE department = 'Sales' AND (position = 'Manager' OR position = 'Representative');
Comparisons and Operators in MySQL WHERE Clause:
SELECT product_name, price FROM products WHERE price BETWEEN 50 AND 100;
Negating Conditions with NOT in MySQL WHERE:
SELECT customer_name, total_purchases FROM customers WHERE NOT total_purchases < 500;
Combining Multiple Conditions with Parentheses in MySQL WHERE:
SELECT order_id, order_date FROM orders WHERE (total_amount > 1000 OR status = 'Pending') AND customer_id = 102;
Dynamic Filtering Using Variables in MySQL WHERE:
SET @min_price = 50; SET @max_price = 100; SELECT product_name, price FROM products WHERE price BETWEEN @min_price AND @max_price;
Examples of Using WHERE in MySQL Queries:
SELECT employee_name, hire_date FROM employees WHERE department_id = 3;
UPDATE products SET stock_quantity = stock_quantity - 1 WHERE product_id = 101;