MySQL : Operators

In MySQL, operators are symbols used to perform operations on data in SQL queries. Here are some common types of operators:

1. Arithmetic Operators: Used for mathematical operations.

  •    Addition: `+`
  •    Subtraction: `-`
  •    Multiplication: `*`
  •    Division: `/`
  •    Modulus (remainder): `%`

   
   SELECT 10 + 5, 10 - 5, 10 * 5, 10 / 5, 10 % 3;
   

2. Comparison Operators: Used to compare values.

  •    Equal to: `=`
  •    Not equal to: `<>` or `!=`
  •    Greater than: `>`
  •    Less than: `<`
  •    Greater than or equal to: `>=`
  •    Less than or equal to: `<=`

   
   SELECT * FROM table_name WHERE column1 > 10 AND column2 <> 'value';
   

3. Logical Operators: Used to combine conditions.

  •    AND: Returns true if all conditions are true.
  •    OR: Returns true if at least one condition is true.
  •    NOT: Negates the result of a condition.

   
   SELECT * FROM table_name WHERE column1 > 10 AND column2 = 'value';
   

4. Concatenation Operator: Used to concatenate strings.

   CONCAT(): Concatenates two or more strings.


   SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM employees;
   

5. NULL-Safe Equal Operator: Used to compare values, handling NULL values properly.

    <=>: Returns true if both operands are equal, or both are NULL.

   
   SELECT * FROM table_name WHERE column1 <=> NULL;
   

6. Assignment Operator: Used to assign a value to a variable.

    :=

  
   SET @variable_name := 10;
   

These are some of the most commonly used operators in MySQL queries, but there are others as well, depending on the specific requirements of your query.