1. Home
  2. Articles
  3. Courses
    1. Articles
  4. Community
  5. Definitions
  6. Files
    1. Terms Of Condition
  • Login
  • Register
  • Search
Course Articles
  • Everywhere
  • Articles
  • Pages
  • Forum
  • Definitions
  • Course Articles
  • Filebase Entry
  • More Options
  1. Scientific Tools
  2. Courses
  3. MySQL Guides

Complete MySQL WHERE Clause guide with examples

  • ScirntificTools.org
  • November 22, 2023 at 5:42 PM
  • 787 times read
Contents [hideshow]
  1. Understanding the WHERE Clause
  2. Basic Syntax
  3. 1. Simple Conditions
    1. Example: Equality
    2. Example: Inequality
  4. 2. Combining Conditions
    1. Example: Using AND
    2. Example: Using OR
    3. Example: Using NOT
  5. 3. Using Comparison Operators
    1. Example: Less Than
  6. 4. LIKE Operator
    1. Example: Using LIKE
  7. 5. BETWEEN Operator
    1. Example: Using BETWEEN
  8. 6. IN Operator
    1. Example: Using IN
  9. 7. Working with NULL
    1. Example: Finding NULL

When working with databases, filtering data is one of the main tasks. In MySQL, the WHERE clause is your primary tool for doing this. This guide breaks down the concept of the WHERE clause with several examples to better explain this command.

Understanding the WHERE Clause

The WHERE clause in MySQL is used to filter records based on specified conditions. It’s used in conjunction with SQL statements like SELECT, UPDATE, or DELETE to manipulate data more precisely.

Basic Syntax

The basic structure of a WHERE clause is:

SQL
SELECT column1, column2, ...
FROM table_name
WHERE condition;

The condition specifies which rows should be retrieved, updated, or deleted.

1. Simple Conditions

Example: Equality

To select users with the name 'John Doe':

SQL
SELECT * FROM Users
WHERE Name = 'John Doe';

Example: Inequality

To select products with a price greater than 100:

SQL
SELECT * FROM Products
WHERE Price > 100;

2. Combining Conditions

You can use AND, OR, and NOT to combine multiple conditions.

Example: Using AND

To find users named 'John Doe' who are 30 years old:

SQL
SELECT * FROM Users
WHERE Name = 'John Doe' AND Age = 30;

Example: Using OR

To select users who are either 25 or 30 years old:

SQL
SELECT * FROM Users
WHERE Age = 25 OR Age = 30;

Example: Using NOT

To select all users except those named 'John Doe':

SQL
SELECT * FROM Users
WHERE NOT Name = 'John Doe';

3. Using Comparison Operators

MySQL supports various comparison operators such as =, !=, <, >, <=, >=.

Example: Less Than

To find products with a stock quantity less than 50:

SQL
SELECT * FROM Products
WHERE Stock < 50;

4. LIKE Operator

The LIKE operator is used for pattern matching.

Example: Using LIKE

To find users with names starting with 'J':

SQL
SELECT * FROM Users
WHERE Name LIKE 'J%';

% is a wildcard character representing any number of characters.

5. BETWEEN Operator

BETWEEN selects values within a given range.

Example: Using BETWEEN

To select products with a price range between 50 and 100:

SQL
SELECT * FROM Products
WHERE Price BETWEEN 50 AND 100;

6. IN Operator

IN allows you to specify multiple values in a WHERE clause.

Example: Using IN

To select users who are either 25, 30, or 35 years old:

SQL
SELECT * FROM Users
WHERE Age IN (25, 30, 35);

7. Working with NULL

IS NULL and IS NOT NULL check for NULL values.

Example: Finding NULL

To find products without a specified category:

SQL
SELECT * FROM Products
WHERE Category IS NULL;
  • Previous Article What is a MySQL subquery and how are subqueries executed in MySQL?
  • Next Article MySQL ORDER BY Clause guide with practical examples

Categories

  • MySQL

Archive

  1. 2023 (33)
    1. November (27)
      • Guide to MySQL IN and NOT IN Operators
      • Guide to MySQL OR Operator
      • Guide to MySQL AND Operator
      • Guide to MySQL DISTINCT Clause
      • MySQL SELECT FROM Statement Guide
      • What is EXPLAIN statement in MySQL and what it does ?
      • Guide to Query Caching in MySQL
      • How to Optimize MySQL Queries?
      • Guide to Entity-Relationship (ER) Diagrams in MySQL with Examples
      • What is Normal Forms and Normalization in MySQL
      • How to design a MySQL database with high performance
      • Principals of Secure Database Design in MySQL
      • How to secure and harden MySQL
      • User Management MySQL: Guide to Create, Manage and Permissions
      • Guide to Views in MySQL: Creating Updating Deleting
      • Guide to Stored Procedures and Functions in MySQL
      • Guide to Indexes in MySQL: Creating and Managing Indexes
      • Advanced Filtering in MySQL guide and examples
      • Practical guide for UNION and UNION ALL in MySQL with examples
      • Joining Tables in MySQL guide with examples
      • Guide to GROUP BY and HAVING clauses in MySQL with examples
      • MySQL ORDER BY Clause guide with practical examples
      • Complete MySQL WHERE Clause guide with examples
      • What is a MySQL subquery and how are subqueries executed in MySQL?
      • How to do queries in MySQL
      • How to create, modify, rename and delete tables using MySQL
      • MySQL CRUD Operations (Create, Read, Update, Delete)
    2. October (2)
    3. August (2)
    4. July (2)

Tags

  • mysql
  • WHERE Clause
  1. Privacy Policy
  2. Legal Notice
Copyright© ScientificTools.org 2026. All rights reserved.
All the content posted on this website are licenses by MySecure Space GmbH under Creative Commons CC BY-NC-ND 4.0
Creative Commons CC BY-NC-ND 4.0
Developed & Hosted by: MySecure.Space | Powered by: WoltLab Suite™