
πWhat is the WHERE Clause in MySQL?
MySQL WHERE Clause: AND, OR, IN, NOT IN Query Examples The WHERE clause in MySQL is used to filter records and specify conditions for SQL operations like SELECT, INSERT, UPDATE, and DELETE. It helps retrieve specific rows based on defined criteria, making queries more efficient and accurate.
πTable of Contents:
- MySQL WHERE Clause: AND, OR, IN, NOT IN Query ExamplesWHERE Clause Syntax
- Using WHERE with AND Operator
- Using WHERE with OR Operator
- Using WHERE with IN Keyword
- Using WHERE with NOT IN Keyword
- WHERE with Comparison Operators
- Summary & Brain Teaser
π WHERE Clause Syntax
MySQL WHERE Clause: AND, OR, IN, NOT IN Query ExamplesThe basic syntax for using WHERE in a MySQL SELECT query:
SELECT * FROM tableName WHERE condition;
Components:
SELECT * FROM tableName
β Retrieves all records from the tableWHERE condition
β Filters the results based on the specified condition
Example:
SELECT * FROM members WHERE membership_number = 1;
This query fetches details of the member with membership_number = 1
.
π WHERE Clause with AND Operator
MySQL WHERE Clause: AND, OR, IN, NOT IN Query ExamplesThe AND operator ensures that all specified conditions must be met.
Example: Fetching movies from category 2 released in 2008:
SELECT * FROM movies WHERE category_id = 2 AND year_released = 2008;
π WHERE Clause with OR Operator
MySQL WHERE Clause: AND, OR, IN, NOT IN Query ExamplesThe OR operator retrieves records if at least one condition is met.
Example: Fetching movies from category 1 or category 2:
SELECT * FROM movies WHERE category_id = 1 OR category_id = 2;
π WHERE Clause with IN Keyword
MySQL WHERE Clause: AND, OR, IN, NOT IN Query ExamplesThe IN keyword simplifies multiple OR conditions.
Example: Fetching members with membership numbers 1, 2, or 3:
SELECT * FROM members WHERE membership_number IN (1,2,3);
π WHERE Clause with NOT IN Keyword
The NOT IN keyword filters out specified values.
Example: Fetching members excluding those with membership numbers 1, 2, or 3:
SELECT * FROM members WHERE membership_number NOT IN (1,2,3);
π WHERE Clause with Comparison Operators
= Equal To Fetching female members:
SELECT * FROM members WHERE gender = 'Female';
> Greater Than Fetching payments greater than 2000:
SELECT * FROM payments WHERE amount_paid > 2000;
<> Not Equal To Fetching movies not in category 1:
SELECT * FROM movies WHERE category_id <> 1;
π Summary
- The SQL WHERE clause filters records based on specific conditions.
- It can be used with logical operators (AND, OR) and comparison operators (=, >, <, <>).
- The IN keyword selects multiple values, while NOT IN excludes them.
Brain Teaser:
Find movies rented but not returned before June 25, 2012:
SELECT * FROM movierentals WHERE return_date < '2012-06-25' AND movie_returned = 0;
Click To Open
πTutorial-2: MySQL SELECT Statement
πTutorial-3: How to Create a Database in MySQL
πTutorial-4: MySQL INSERT INTO Query
πTutorial-5: MYSQL DELETE Query
πTutorial-6: MYSQL Update Query