---Advertisement---

MySQL WHERE Clause: AND, OR, IN, NOT IN Query Examples Great 2025

By Manisha

Updated On:

---Advertisement---
MySQL WHERE Clause

πŸ‘‰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:

  1. MySQL WHERE Clause: AND, OR, IN, NOT IN Query ExamplesWHERE Clause Syntax
  2. Using WHERE with AND Operator
  3. Using WHERE with OR Operator
  4. Using WHERE with IN Keyword
  5. Using WHERE with NOT IN Keyword
  6. WHERE with Comparison Operators
  7. 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 table
  • WHERE 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.

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

ER Modeling DBMS

Download My SQL

Leave a Comment

Index