MySQL 8: How to compare values (greater than, less than, equal to, etc.)

Updated: January 25, 2024 By: Guest Contributor Post a comment

Introduction

When working with databases, comparing values is a fundamental aspect of retrieving, analyzing, and understanding data. MySQL, as one of the leading relational database management systems, provides several operators for comparing values. Whether you are filtering records in a SELECT statement, evaluating conditions in a WHERE clause, or determining the order of a dataset, understanding how to use these comparison operators is essential. In this tutorial, we will explore various MySQL comparison operators such as greater than (>), less than (<), equal to (=), and more, through concise explanations and code examples.

Basic Comparison Operators

The basic comparison operators in MySQL are used to compare two values. Here are the common ones:

  • Equal to (=)
  • Greater than (>)
  • Less than (<)
  • Greater than or equal to (>=)
  • Less than or equal to (<=)
  • Not equal to (<> or !=)

Equal to (=)

To check if two values are equal, we use the=operator. It returns true when the two sides of the operator match in value.

SELECT *
FROM students
WHERE grade = 'A';

This SQL statement retrieves all records from the ‘students’ table where the ‘grade’ column equals ‘A’.

Greater than (>)

Use the > operator to determine if a value is greater than another.

SELECT *
FROM products
WHERE price > 100;

This retrieves all products with a price greater than 100.

Less than (<)

The < operator is used to find values that are less than a certain value.

SELECT *
FROM orders
WHERE quantity < 10;

This query selects all orders with a quantity less than 10.

Greater than or equal to (>=)

When considering boundary-inclusive comparisons, you will use the >= operator.

SELECT *
FROM employees
WHERE experience_years >= 5;

This example selects employees that have 5 or more years of experience.

Less than or equal to (<=)

For less than or inclusive comparisons, the <= operator is useful.

SELECT *
FROM apartments
WHERE rooms <= 3;

It returns all apartments with 3 rooms or fewer.

Not equal to (<> or !=)

To find values that are not equal, you may use <> or !=:

SELECT *
FROM books
WHERE author <> 'John Doe';

This will find all books not written by ‘John Doe’.

Using Comparison Operators with NULL

Remember that in MySQL, NULL represents the absence of a value. When you compare a NULL value using any of the standard comparison operators, the result will be NULL. To handle these comparisons, use theIS NULLor IS NOT NULL.

IS NULL

SELECT *
FROM customers
WHERE last_purchase IS NULL;

Selects all customers that have not made a purchase.

IS NOT NULL

SELECT *
FROM customers
WHERE last_purchase IS NOT NULL;

Selects all customers that have made a purchase.

Advanced Comparison Techniques

As we progress to more advanced comparison scenarios, we can use other SQL constructs such as BETWEEN, IN, and comparison operators with subqueries.

BETWEEN

The BETWEEN operator allows for value comparison within a specified range inclusive of the boundary values.

SELECT *
FROM transactions
WHERE amount BETWEEN 50 AND 150;

Selects all transactions where the amount is between 50 and 150, inclusive.

IN

The IN operator compares a value against a list of values.

SELECT *
FROM products
WHERE category_id IN (2, 4, 7);

Selects products that belong to categories 2, 4, or 7.

Comparison in Subqueries

You can also compare values in main queries with values resulting from subqueries.

SELECT *
FROM employees as e
WHERE salary > (
    SELECT AVG(salary)
    FROM employees
    WHERE department = e.department
);

This statement selects employees who earn more than the average salary within their own department.

Comparisons In JOIN Clauses

Comparisons are not limited to WHERE clauses. They can also be used in JOIN statements when specifying the condition that links two tables.

SELECT employees.name, departments.name
FROM employees
JOIN departments ON employees.department_id = departments.id;

This JOIN operation links employees with their respective departments based on department ID.

Conclusion

In this tutorial, we have covered the essentials of comparing values in MySQL through a variety of operators and contexts. We’ve included from the simplest equality checks to more sophisticated usage with NULL values and subqueries. Understanding and effectively employing these comparisons will bolster your SQL querying proficiency for a wide range of database interactions.

As you digest the material presented here and begin to apply it to your own MySQL tasks, remember that careful comparison logic is the backbone of accurate data retrieval. These foundations will serve you well as you progress to more advanced SQL topics and real-world database challenges.