
Where in MySql
WHERE
Clause in MySQL
The WHERE
clause in MySQL is used to filter records in a SELECT
, UPDATE
, DELETE
, or INSERT
statement based on a condition.
1. Syntax
SELECT column1, column2 FROM table_nameWHERE condition;
πΉ Conditions can include:
β
Comparisons (=
, >
, <
, >=
, <=
, !=
)
β
Logical Operators (AND
, OR
, NOT
)
β
Wildcards (LIKE
)
β
Lists (IN
)
β
Ranges (BETWEEN
)
2. Example Table: employees
emp_id | name | department | salary |
---|---|---|---|
1 | Alice | IT | 6000 |
2 | Bob | HR | 5000 |
3 | Charlie | IT | 5500 |
4 | David | Finance | 7000 |
3. Using WHERE
in SELECT
SELECT * FROM employees WHERE department = 'IT';
β Result:
emp_id | name | department | salary |
---|---|---|---|
1 | Alice | IT | 6000 |
3 | Charlie | IT | 5500 |
4. Using WHERE
with Comparison Operators
SELECT * FROM employees WHERE salary > 5500;
β Finds employees with salary greater than 5500.
5. Using WHERE
with AND
SELECT * FROM employees WHERE department = 'IT' AND salary > 5500;
β Finds employees in IT with salary greater than 5500.
6. Using WHERE
with OR
SELECT * FROM employees WHERE department = 'IT' OR department = 'HR';
β Finds employees in either IT or HR.
7. Using WHERE
with BETWEEN
SELECT * FROM employees WHERE salary BETWEEN 5000 AND 6000;
β Finds employees with salary in the range 5000-6000.
8. Using WHERE
with IN
SELECT * FROM employees WHERE department IN ('IT', 'Finance');
β Finds employees in IT or Finance departments.
9. Using WHERE
with LIKE
(Wildcard Search)
SELECT * FROM employees WHERE name LIKE 'A%';
β Finds employees whose names start with "A".
10. Using WHERE
in UPDATE
UPDATE employees SET salary = 6500 WHERE emp_id = 1;
β Updates Aliceβs salary to 6500.
11. Using WHERE
in DELETE
DELETE FROM employees WHERE department = 'HR';
β Removes all employees from the HR department.
Key Takeaways
β
The WHERE
clause filters records based on conditions.
β
Supports comparisons, logical operators, wildcards, lists, and ranges.
β
Used with SELECT
, UPDATE
, and DELETE
statements.
β
Always use WHERE
in UPDATE
and DELETE
to avoid modifying all rows!