SQL operators

  • javier gomez
  • 03, March 2023

In this SQL tutorial/class I am going to explain to you operators in SQL.

Operations to set conditions or perform calculations on any SQL query.

In addition, since it could not be missing in this lesson, I am going to explain them to you through practical examples.

Content

  • Definition of Operator in SQL
  • Types of Operators Available in SQL
  • Boolean or logical SQL operators
    • Boolean operators
    • Logical operators
    • Example query with AND operator
    • Example SQL query OR operator
    • SQL example with NOT operator

Definition of Operator in SQL

An operator is defined, as is the dictionary and the RAE: mathematical symbol denoting a set of operations to be performed.

We can also say that it is the one that operates between two operands. Thus, this operator defines an operation that will get a result.

That? Have you stayed the same?

Don’t worry, continue.

For example, in SQL, we can use the classic operators: +, -, >, <, >=, <=, etc

Look at the following query with SQL operators:

SELECT 1 + 2;

This SQL query is fully valid and returns 3.

Types of Operators Available in SQL

Various types of operators can be used in SQL and, for this reason, it is better to categorize them.

The most common SQL operators are:

  • Logical operators: as AND or OR.
  • Boolean operators: like >,<,!=,=,etc.
  • arithmetic operators: as addition or subtraction.
  • flow control operators: like CASE, etc.

Boolean or logical SQL operators

The boolean operators They are the ones that allow us to configure conditions.

Don’t you know what a boolean is? Well, a boolean or boolean variable is one that can only have two possible values: true or false. Or in English: TRUE or FALSE.

If you have played trivia games where you can only answer YES or NO, you have played with Boolean questions.

Also mention that some programming languages ​​these values ​​are accepted as 1 (true) or 0 (false).

The most common in SQL are:”>”: or greater than.

Boolean operators

  • “<": or less than.
  • “>=”: greater than or equal to.
  • “<=": less than or equal to.
  • “=”: same as.
  • “!=”: is different from .

Logical operators

  • AND: which will evaluate to “TRUE” when both parts of the condition are true.
  • OR: which evaluates to “TRUE” when one of the two parts is true.
  • NOT: Evaluates to “TRUE” if the value or expression below is FALSE.

Example query with AND operator

SELECT nombre
FROM tabla_productos
WHERE precio > 5 AND precio < 10;

In the previous example we select, with the boolean operator AND, products with a price between 5 and 10 euros.

The operators have also been used “>” or “<" to evaluate the conditions separately from price, and finally check that both are met to select the interval: if “price > 5” is TRUE “and” “price < 10" is TRUE, then the product meets the condition.

Example SQL query OR operator

SELECT nombre
FROM tabla_productos
WHERE en_oferta = 1 OR descatalogado = 1;

SQL example with NOT operator

In the following example you can see how I use NOT for a boolean type column in MySQL “on_offer”.

SELECT nombre, precio
FROM tabla_productos
WHERE NOT en_oferta;

In this example, we select the products that are NOT on sale, that is, when the column value is FALSE.

Leave a Reply