MySQL Tutorial

MySQL Installation and Configuration

MySQL Database Operations

Database Design

MySQL Data Types

MySQL Storage Engines

MySQL Basic Operations of Tables

MySQL Constraints

MySQL Operators

MySQL Function

MySQL Manipulate Table Data

MySQL View

MySQL Indexes

MySQL Stored Procedure

MySQL Trigger

MySQL Transactions

MySQL Character Set

MySQL User Management

MySQL Database Backup and Recovery

MySQL Log

MySQL Performance Optimization

MySQL Comparison Operators

Comparison operators are used in the WHERE clause to filter the results of a SELECT, INSERT, UPDATE, or DELETE statement.

Here are the most common comparison operators used in MySQL:

  • =: Equal to
  • <> or !=: Not equal to
  • <: Less than
  • >: Greater than
  • <=: Less than or equal to
  • >=: Greater than or equal to
  • BETWEEN ... AND ...: Between an inclusive range
  • LIKE: Search for a pattern
  • IN: If the operand is equal to one of a list of expressions

Examples:

  • Equal to (=)
SELECT * FROM employees WHERE employee_id = 1;

This will select all the data from the employees table where employee_id is equal to 1.

  • Not equal to (<> or !=)
SELECT * FROM employees WHERE employee_id <> 1;

This will select all the data from the employees table where employee_id is not equal to 1.

  • Less than (<)
SELECT * FROM employees WHERE salary < 50000;

This will select all the data from the employees table where salary is less than 50000.

  • Greater than (>)
SELECT * FROM employees WHERE salary > 50000;

This will select all the data from the employees table where salary is greater than 50000.

  • Less than or equal to (<=)
SELECT * FROM employees WHERE salary <= 50000;

This will select all the data from the employees table where salary is less than or equal to 50000.

  • Greater than or equal to (>=)
SELECT * FROM employees WHERE salary >= 50000;

This will select all the data from the employees table where salary is greater than or equal to 50000.

  • BETWEEN (BETWEEN ... AND ...)
SELECT * FROM employees WHERE salary BETWEEN 50000 AND 60000;

This will select all the data from the employees table where salary is between 50000 and 60000, inclusive.

  • LIKE (LIKE)
SELECT * FROM employees WHERE first_name LIKE 'J%';

This will select all the data from the employees table where first_name starts with "J". (The % character is a wildcard that matches any number of characters.)

  • IN (IN)
SELECT * FROM employees WHERE employee_id IN (1, 2, 3);

This will select all the data from the employees table where employee_id is either 1, 2, or 3.

These are the basic comparison operators in MySQL that you can use in your queries. You can also combine these operators using AND and OR to create more complex queries.

  1. MySQL comparison operators examples:

    • MySQL comparison operators are used to compare values and return boolean results.
    SELECT column1 = column2 AS equal, column1 != column2 AS not_equal, column1 > column2 AS greater_than,
           column1 < column2 AS less_than, column1 >= column2 AS greater_than_or_equal,
           column1 <= column2 AS less_than_or_equal;
    
  2. How to use equal to operator in MySQL:

    • The equal to operator (=) checks if two values are equal.
    SELECT * FROM table_name WHERE column1 = 10;
    
  3. Not equal to operator in MySQL queries:

    • The not equal to operator (!= or <>) checks if two values are not equal.
    SELECT * FROM table_name WHERE column1 <> 5;
    
  4. Greater than operator usage in MySQL:

    • The greater than operator (>) checks if one value is greater than another.
    SELECT * FROM table_name WHERE column1 > 20;
    
  5. Less than operator in MySQL comparisons:

    • The less than operator (<) checks if one value is less than another.
    SELECT * FROM table_name WHERE column1 < 15;
    
  6. Greater than or equal to operator in MySQL:

    • The greater than or equal to operator (>=) checks if one value is greater than or equal to another.
    SELECT * FROM table_name WHERE column1 >= 10;
    
  7. Less than or equal to operator in MySQL:

    • The less than or equal to operator (<=) checks if one value is less than or equal to another.
    SELECT * FROM table_name WHERE column1 <= 25;
    
  8. Combining multiple conditions with logical operators in MySQL:

    • Use logical operators (AND, OR, NOT) to combine multiple conditions.
    SELECT * FROM table_name WHERE column1 > 10 AND column2 = 'value';
    
  9. Case-sensitive vs case-insensitive comparisons in MySQL:

    • By default, comparisons in MySQL are case-insensitive, but you can make them case-sensitive using the BINARY keyword or COLLATE clause.
    SELECT * FROM table_name WHERE column1 = 'Value' COLLATE utf8_bin;