0
0
MySQLquery~5 mins

WHERE clause filtering in MySQL

Choose your learning style9 modes available
Introduction
The WHERE clause helps you pick only the rows you want from a table by setting conditions.
When you want to find all customers from a specific city.
When you need to get products that cost less than a certain amount.
When you want to see orders placed after a certain date.
When you want to filter employees by their job title.
When you want to find students with grades above a certain score.
Syntax
MySQL
SELECT column1, column2 FROM table_name WHERE condition;
The condition can use operators like =, <, >, <=, >=, <> (not equal).
You can combine conditions using AND, OR, and NOT.
Examples
Gets all employees who work in the Sales department.
MySQL
SELECT * FROM employees WHERE department = 'Sales';
Finds products that cost less than 100.
MySQL
SELECT name, price FROM products WHERE price < 100;
Shows orders shipped on or after January 1, 2024.
MySQL
SELECT * FROM orders WHERE order_date >= '2024-01-01' AND status = 'shipped';
Finds students with grade 90 or higher, or attendance above 95%.
MySQL
SELECT * FROM students WHERE grade >= 90 OR attendance > 95;
Sample Program
This creates a table of employees, adds some data, and then selects the names and salaries of employees who work in Sales.
MySQL
CREATE TABLE employees (
  id INT,
  name VARCHAR(50),
  department VARCHAR(50),
  salary INT
);

INSERT INTO employees (id, name, department, salary) VALUES
(1, 'Alice', 'Sales', 50000),
(2, 'Bob', 'HR', 45000),
(3, 'Charlie', 'Sales', 55000),
(4, 'Diana', 'IT', 60000);

SELECT name, salary FROM employees WHERE department = 'Sales';
OutputSuccess
Important Notes
String values in conditions must be in single quotes.
The WHERE clause filters rows before any grouping or ordering happens.
Use parentheses to group conditions when combining AND and OR for clarity.
Summary
WHERE clause filters rows based on conditions.
Use comparison operators and logical operators to build conditions.
It helps get only the data you need from a table.