Grokking SQL for Tech Interviews
Ask Author
Back to course home

0% completed

Operators
Table of Contents

Arithmetic Operators

Logical Operators

Compound Operators

Comparison Operators

In MySQL, operators are symbols used to perform operations on one or more operands.

Following are the types of operators in SQL.

  1. Arithmetic Operators
  2. Logical Operators
  3. Compound Operators
  4. Comparison Operators

Arithmetic Operators

Arithmetic Operators perform mathematical calculations such as Addition, Subtraction, Multiplication, Module, and Division. The arithmetic operators can be directly used in the SQL statement or combination with column values.

Here are some common arithmetic operators in MySQL:

OperatorDescriptionExampleResult
+AdditionSELECT 5 + 3;8
-SubtractionSELECT 10 - 4;6
*MultiplicationSELECT 3 * 7;21
/DivisionSELECT 20 / 5;4
%Modulus (Remainder)SELECT 17 % 3;2

Logical Operators

In MySQL, logical operators combine or compare conditions in a WHERE clause of a SQL query. These operators allow you to create more complex conditions by combining multiple conditions.

Here are some logical operators in MySQL:

OperatorDescriptionExample
ANDLogical ANDSELECT * FROM employees WHERE age > 25 AND department = 'Sales';
ORLogical ORSELECT * FROM employees WHERE age < 25 OR department = 'Marketing';
NOTLogical NOTSELECT * FROM employees WHERE NOT age = 22;

Compound Operators

In MySQL, compound operators are shorthand notations that combine an arithmetic or bitwise operation with an assignment. These operators operate and assign the result to a variable in a single step.

Here are some common compound operators in MySQL:

OperatorDescriptionExample
+=Add and assignSET x = 5; SET x += 3;
-=Subtract and assignSET y = 10; SET y -= 2;
*=Multiply and assignSET z = 3; SET z *= 4;
/=Divide and assignSET w = 20; SET w /= 2;

Comparison Operators

Comparison operators in MySQL are used to compare values in various conditions within SQL queries. They allow you to create conditions determining which rows should be included in the result set.

Here are some common comparison operators in MySQL:

OperatorDescriptionExample
=Equal toSELECT * FROM products WHERE Price = 500;
<>Not equal toSELECT * FROM products WHERE Price <> 500;
<Less thanSELECT * FROM products WHERE Price < 1000;
>Greater thanSELECT * FROM products WHERE Price > 1000;
<=Less than or equal toSELECT * FROM products WHERE Price <= 500;
>=Greater than or equal toSELECT * FROM products WHERE Price >= 500;
Mark as Completed

Table of Contents

Arithmetic Operators

Logical Operators

Compound Operators

Comparison Operators