0
0
MySQLquery~5 mins

Comparison operators in MySQL

Choose your learning style9 modes available
Introduction

Comparison operators help you check if values are equal, bigger, smaller, or different. This lets you find specific data in your database.

Finding all customers older than 30 years.
Checking if a product price is less than 100 dollars.
Selecting orders that were made on a specific date.
Filtering employees who are not in a certain department.
Comparing two columns to find matching records.
Syntax
MySQL
value1 = value2
value1 <> value2
value1 != value2
value1 > value2
value1 < value2
value1 >= value2
value1 <= value2

= means equal to.

<> and != both mean not equal to.

Examples
Selects employees who are exactly 25 years old.
MySQL
SELECT * FROM employees WHERE age = 25;
Finds products with a price greater than 50.
MySQL
SELECT * FROM products WHERE price > 50;
Gets orders that are not shipped yet.
MySQL
SELECT * FROM orders WHERE status <> 'shipped';
Finds users who signed up on or before January 1, 2023.
MySQL
SELECT * FROM users WHERE signup_date <= '2023-01-01';
Sample Program

This creates a table of employees, adds three people, and then selects those older than 28.

MySQL
CREATE TABLE employees (
  id INT,
  name VARCHAR(50),
  age INT
);

INSERT INTO employees (id, name, age) VALUES
(1, 'Alice', 30),
(2, 'Bob', 25),
(3, 'Charlie', 35);

SELECT name FROM employees WHERE age > 28;
OutputSuccess
Important Notes

Use single quotes for text values in comparisons.

Comparison operators work with numbers, text, and dates.

Remember that = checks for equality, while <> and != check for inequality.

Summary

Comparison operators let you compare values to filter data.

Common operators include =, <>, !=, >, <, >=, and <=.

They are essential for searching and sorting data in databases.