SQL Operators

In SQL, operators are special symbols that perform operations on one or more expressions and return a result. Understanding these operators is essential for writing effective SQL queries.

SQL Arithmethic Operators

Arithmetic operators are used to perform mathematical calculations.

  • Addition (+): Adds two values.
  • Subtraction (-): Subtracts one value from another.
  • Multiplication (*): Multiplies two values.
  • Division (/): Divides one value by another.
  • Modulus (%): Returns the remainder of a division operation.

SELECT column1, column2, column1 + column2 AS Sum
FROM table_name;




SQL Bitwise Operators

Bitwise operators are used to perform operations on binary representations of integers. These operators are typically used for manipulating binary data.

  • AND (&): Compares each bit of two integers and returns a bit set to 1 if both bits are 1.
  • OR (|): Compares each bit of two integers and returns a bit set to 1 if at least one of the bits is 1.
  • XOR (^): Compares each bit of two integers and returns a bit set to 1 if only one of the bits is 1.
  • NOT (~): Inverts all bits of an integer.

SELECT column1
FROM table_name
WHERE (column1 & column2) = some_value;




SQL Comparison Operators

Comparison operators are used to compare two values and return a boolean result (true or false).

  • Equal (=): Checks if two values are equal.
  • Not Equal (!= or <>): Checks if two values are not equal.
  • Greater Than (>): Checks if the left value is greater than the right value.
  • Less Than (<): Checks if the left value is less than the right value.
  • Greater Than or Equal To (>=): Checks if the left value is greater than or equal to the right value.
  • Less Than or Equal To (<=): Checks if the left value is less than or equal to the right value.

SELECT column1
FROM table_name
WHERE column1 operator value;




SQL Compound Operators

Compound operators are shorthand for performing an operation and assigning a value in one step. They are used to simplify statements.

  • Addition Assignment (+=): Adds the right operand to the left operand and assigns the result to the left operand.
  • Subtraction Assignment (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.
  • Multiplication Assignment (*=): Multiplies the left operand by the right operand and assigns the result to the left operand.
  • Division Assignment (/=): Divides the left operand by the right operand and assigns the result to the left operand.

column1 operator= value;




SQL Logical Operators

Logical operators are used to combine multiple conditions in a SQL statement.

  • AND: Returns true if both conditions are true.
  • OR: Returns true if at least one condition is true.
  • NOT: Reverses the result of a condition (true becomes false and vice versa).

SELECT column1
FROM table_name
WHERE condition1 AND condition2;




SQL Operators Labs

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top