---Advertisement---

MySQL WHERE Clause: A Complete Guide with Examples Best 2025

By Manisha

Updated On:

---Advertisement---
MySQL WHERE Clause

MySQL WHERE Clause: The WHERE clause in MySQL is used to filter records based on specific conditions in SQL queries. It can be applied to SELECT, UPDATE, DELETE, and INSERT statements to retrieve only the required data.

When querying a database, you may not always want to retrieve all rows. The WHERE clause helps narrow down results based on given conditions.


MySQL WHERE Clause: The basic syntax of the WHERE clause in a SELECT query is:

sql

SELECT * FROM tableName WHERE condition;

  • SELECT * FROM tableName: Fetches all records from a table.
  • WHERE condition: Filters records based on the given condition.

Example: To retrieve details of a specific member with membership_number = 1:

sql

SELECT * FROM members WHERE membership_number = 1;


1. WHERE Clause with AND Operator

MySQL WHERE Clause: The AND operator is used to filter data when multiple conditions must be met.

Example: Retrieve movies in category 2 that were released in 2008.

sql

SELECT * FROM movies WHERE category_id = 2 AND year_released = 2008;

movie_idtitledirectoryear_releasedcategory_id
2Forgetting Sarah MarshalNicholas Stoller20082

2. WHERE Clause with OR Operator

MySQL WHERE Clause: The OR operator returns results if at least one condition is true.

Example: Fetch all movies from category 1 or 2.

sql

SELECT * FROM movies WHERE category_id = 1 OR category_id = 2;

movie_idtitledirectoryear_releasedcategory_id
1Pirates of the Caribbean 4Rob Marshall20111
2Forgetting Sarah MarshalNicholas Stoller20082

3. WHERE Clause with IN Operator

MySQL WHERE Clause: The IN operator simplifies multiple OR conditions by checking if a value exists in a given list.

Example: Fetch members with membership_number 1, 2, or 3.

sql

SELECT * FROM members WHERE membership_number IN (1, 2, 3);

membership_numberfull_namesgenderdate_of_birthphysical_addressemail
1Janet JonesFemale21-07-1980First Street Plot No 4janetjones@yagoo.cm
2Janet Smith JonesFemale23-06-1980Melrose 123jj@fstreet.com
3Robert PhilMale12-07-19893rd Street 34rm@tstreet.com

4. WHERE Clause with NOT IN Operator

MySQL WHERE Clause: The NOT IN operator returns rows that do not match the values listed.

Example: Fetch members excluding those with membership_number 1, 2, or 3.

sql

SELECT * FROM members WHERE membership_number NOT IN (1, 2, 3);

membership_numberfull_namesgenderdate_of_birthphysical_addressemail
4Gloria WilliamsFemale14-02-19842nd Street 23NULL

1. WHERE with Equal to (=) Operator

MySQL WHERE Clause:: Retrieve all female members.

sql

SELECT * FROM members WHERE gender = ‘Female’;

membership_numberfull_namesgenderdate_of_birthphysical_addressemail
1Janet JonesFemale21-07-1980First Street Plot No 4janetjones@yagoo.cm
2Janet Smith JonesFemale23-06-1980Melrose 123jj@fstreet.com

2. WHERE with Greater than (>) Operator

Fetch all payments greater than 2,000.

sql

SELECT * FROM payments WHERE amount_paid > 2000;

payment_idmembership_numberpayment_datedescriptionamount_paid
1123-07-2012Movie rental payment2500
3330-07-2012Movie rental payment6000

3. WHERE with Not Equal to (<>) Operator

Retrieve all movies that do not belong to category 1.

sql

SELECT * FROM movies WHERE category_id <> 1;

movie_idtitledirectoryear_releasedcategory_id
2Forgetting Sarah MarshalNicholas Stoller20082
5Daddy’s Little GirlsNULL20078

Retrieve a list of movies that were not returned by 25th June 2012.

sql

SELECT * FROM movierentals WHERE return_date < ‘2012-06-25’ AND movie_returned = 0;

reference_numbertransaction_datereturn_datemembership_numbermovie_idmovie_returned
1421-06-201224-06-2012220

  • The WHERE clause filters records based on conditions in SELECT, UPDATE, and DELETE queries.
  • Logical operators like AND and OR help refine filtering.
  • The IN and NOT IN operators simplify multiple OR conditions.
  • Comparison operators like =, >, <, and <> help set precise conditions.

By mastering the WHERE clause, you can efficiently manage and retrieve data in MySQL.

MYSQL Select Statement

Download My SQL

Leave a Comment

Index