2๏ธโƒฃ ๐Ÿ”Ž MySQL SQL Fundamentals
Estimated reading: 4 minutes 31 views

๐Ÿ”Ž MySQL Comparison Operators (IN, BETWEEN, LIKE, IS NULL, NOT NULL)


๐Ÿงฒ Introduction โ€“ Why Learn MySQL Comparison Operators?

When writing queries in MySQL, it’s not just about fetching dataโ€”it’s about fetching the right data. MySQL comparison operators like IN, BETWEEN, LIKE, IS NULL, and NOT NULL give you powerful ways to filter rows based on patterns, value ranges, or null checks.

๐ŸŽฏ In this tutorial, youโ€™ll learn:

  • Syntax and purpose of each comparison operator
  • When and how to use IN, BETWEEN, LIKE, IS NULL, NOT NULL
  • Examples of each operator in real-world scenarios
  • Tips to write clear and efficient filter conditions

๐Ÿ“˜ What Are MySQL Comparison Operators?

MySQL comparison operators are used inside WHERE clauses to evaluate conditions and return only matching rows. These operators offer various methods for value checking, such as membership (IN), pattern search (LIKE), null detection (IS NULL), and range selection (BETWEEN).


๐Ÿงช Examples of MySQL Comparison Operators

๐Ÿ”น 1. IN โ€“ Match Any Value in a List

SELECT * FROM employees
WHERE department IN ('HR', 'Finance', 'IT');

๐Ÿ“Œ Returns rows where department is one of the listed values.


๐Ÿ”น 2. NOT IN โ€“ Exclude Values in a List

SELECT * FROM products
WHERE category NOT IN ('Clothing', 'Shoes');

๐Ÿ“Œ Excludes products in the listed categories.


๐Ÿ”น 3. BETWEEN โ€“ Filter by Value Range (Inclusive)

SELECT * FROM orders
WHERE amount BETWEEN 1000 AND 5000;

๐Ÿ“Œ Returns orders with amount from โ‚น1000 to โ‚น5000, inclusive.


๐Ÿ”น 4. LIKE โ€“ Pattern Matching

SELECT * FROM customers
WHERE email LIKE '%@gmail.com';

๐Ÿ“Œ Matches any email ending with โ€œ@gmail.comโ€.

SELECT * FROM customers
WHERE name LIKE 'A%';

๐Ÿ“Œ Matches names starting with the letter โ€œAโ€.


๐Ÿ”น 5. IS NULL โ€“ Test for Null Values

SELECT * FROM payments
WHERE discount IS NULL;

๐Ÿ“Œ Finds rows where the discount column has no value.


๐Ÿ”น 6. IS NOT NULL โ€“ Test for Non-Null Values

SELECT * FROM payments
WHERE discount IS NOT NULL;

๐Ÿ“Œ Filters rows that have a valid (non-null) discount value.


โš–๏ธ Operator Comparison Table

OperatorPurposeExample Usage
INMatch against a list of valuesWHERE city IN ('Delhi', 'Mumbai')
NOT INExclude values from a listWHERE role NOT IN ('guest', 'test')
BETWEENCheck if a value is within a rangeWHERE age BETWEEN 18 AND 30
LIKEMatch values using wildcard patternsWHERE name LIKE '%Singh'
IS NULLCheck if a column has a NULL valueWHERE phone IS NULL
IS NOT NULLCheck if a column does not have NULLWHERE email IS NOT NULL

๐Ÿ› ๏ธ Wildcard Characters for LIKE

WildcardMeaningExample
%Zero or more characters'S%' matches โ€˜Samโ€™, โ€˜Steveโ€™, โ€˜Smithโ€™
_Exactly one character'S_m' matches โ€˜Samโ€™, โ€˜Simโ€™

๐Ÿ” Use Cases of Comparison Operators

ScenarioSQL Example
โœ… Find students aged between 18โ€“25SELECT * FROM students WHERE age BETWEEN 18 AND 25;
๐Ÿ“ง List emails from GmailSELECT * FROM users WHERE email LIKE '%@gmail.com';
๐Ÿงพ Show orders without discountsSELECT * FROM orders WHERE discount IS NULL;
๐Ÿšซ Exclude test usersSELECT * FROM users WHERE user_type NOT IN ('demo', 'test');
๐Ÿ” Match names starting with โ€œRajโ€SELECT * FROM customers WHERE name LIKE 'Raj%';

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

MySQL comparison operators make your queries more targeted, efficient, and flexible. Whether you’re checking ranges, excluding nulls, or finding patterns, these operators are fundamental to advanced SQL querying.

๐Ÿ” Key Takeaways

  • Use IN to match multiple values
  • Use BETWEEN for range filtering
  • Use LIKE for flexible text matching
  • Use IS NULL / IS NOT NULL to check for presence/absence of values
  • Combine with AND, OR, NOT for complex conditions

โš™๏ธ Real-World Relevance

From login systems to CRM filters, these operators power condition logic in nearly every MySQL-powered application.


โ“ FAQ โ€“ MySQL Comparison Operators

โ“ Is BETWEEN inclusive in MySQL?

โœ… Yes. BETWEEN 10 AND 20 includes 10 and 20.

โ“ Can I use LIKE with numbers?

โœ… Technically yes, but it’s meant for string comparisons. Convert numbers to strings with CAST() if needed.

โ“ What is the difference between = and IN?

โœ… = checks for a single value, while IN checks for a list:

WHERE department = 'Sales'          -- Single value  
WHERE department IN ('Sales','HR')  -- Multiple values

โ“ What does % mean in LIKE?

โœ… It matches any number of characters:

WHERE name LIKE 'A%' -- Names starting with A

โ“ How do I filter rows with no value?

โœ… Use IS NULL or IS NOT NULL.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ”Ž MySQL Comparison Operators (IN, BETWEEN, LIKE, IS NULL, NOT NULL)

Or Copy Link

CONTENTS
Scroll to Top