SQL: The Essential Operators in SQL

Home /

Table of Contents

In Structured Query Language (SQL), Operators are used to performing operations on one or more values to produce a result. They are used in conjunction with SELECT, WHERE, and other SQL statements to filter, manipulate, and aggregate data. There are several types of operators in SQL, including arithmetic operators (+, -, *, /), comparison operators (=, <>, <, >, <=, >=), logical operators (AND, OR, NOT), and string operators (concatenation, substring, length). By using these operators, you can write complex SQL statements to retrieve the data you need from a database, perform calculations on the data, and manipulate it in various ways. Understanding operators is essential for working with SQL effectively and efficiently.

Operators in SQL

An operator is a symbol that represents a specific operation or comparison. Operators are used to manipulate data, test conditions, and perform calculations.

There are several types of operators in SQL, including

  1. Arithmetic Operators: These operators perform arithmetic calculations, such as addition, subtraction, multiplication, and division. Examples: +, -, *, /.
  2. Comparison Operators: These operators compare two values and return a Boolean value (true or false). Examples: =, <>, >, <, >=, <=.
  3. Logical Operators: These operators perform logical operations, such as AND, OR, and NOT. They are used to test multiple conditions—for example: AND, OR, NOT.
  4. Set Operators: These operators perform set operations, such as UNION and INTERSECT, on two or more sets of data. Examples: UNION, INTERSECT, MINUS.
  5. Other Operators: There are a few other operators, such as the BETWEEN operator, which tests if a value is within a range of values, and the IN operator, which tests if a value is in a list of values.

SQL Arithmetic Operators

Arithmetic operators perform arithmetic calculations on numeric values.

Here is a list of standard arithmetic operators in SQL, along with examples of their usage:

  • ” + ” Addition Operator:
    Example: SELECT 5 + 2 AS result; (returns 7).
  • ” – ” Subtraction Operator:
    Example: SELECT 5 - 2 AS result; (returns 3).
  • ” * ” Multiplication Operator:
    Example: SELECT 5 * 2 AS result; (returns 10).
  • ” / ” Division Operator:
    Example: SELECT 5 / 2 AS result; (returns 2.5).
  • ” % ” Modulo Operator: Returns the remainder of a division.
    Example: SELECT 5 % 2 AS result; (returns 1).

Uses of SQL Arithmetic Operators

To use arithmetic operators in SQL, you can include them in a SELECT statement to perform calculations on numeric values.

Here is an example of how you can use arithmetic operators in SQL:

SELECT 5 + 2 AS result;

This statement uses the + operator to add the values 5 and 2, and it returns the result as the value of the result column.

You can also use arithmetic operators to perform calculations on column values. For example:

SQL
SELECT column1 + column2 AS result<br>FROM table_name;

This statement adds the values in column1 and column2 for each row in the table_name table, and it returns the results as the value of the result column.

SQL Comparison Operators

Comparison operators are used to comparing two values and return a Boolean value (true or false).

Here is a list of common comparison operators in SQL, along with examples of their usage:

  • ” = ” Equal to an Operator:
    Example: SELECT * FROM table_name WHERE column = 5; (returns rows where the value of the column is 5).
  • ” <> ” Not Equal to an Operator:
    Example: SELECT * FROM table_name WHERE column <> 5; (returns rows where the value of the column is not 5).
  • ” > ” Greater than an Operator:
    Example: SELECT * FROM table_name WHERE column > 5; (returns rows where the value of the column is greater than 5).
  • ” < ” Less than an Operator:
    Example: SELECT * FROM table_name WHERE column < 5; (returns rows where the value of the column is less than 5).
  • ” >= ” Greater than or Equal to an Operator:
    Example: SELECT * FROM table_name WHERE column >= 5; (returns rows where the value of the column is greater than or equal to 5).
  • ” <= ” Less than or Equal to an Operator:
    Example: SELECT * FROM table_name WHERE column <= 5; (returns rows where the value of the column is less than or equal to 5).

Uses of SQL Comparison Operators

To use comparison operators in SQL, you can include them in a WHERE clause to test the values of a column.

Here is an example of how you can use comparison operators in SQL:

SQL
SELECT * FROM table_name WHERE column1 = 5;

This statement returns all rows from the table_name table where the value of column1 is equal to 5.

You can also use comparison operators to compare the values of two different columns. For example:

SQL
SELECT * FROM table_name WHERE column1 < column2;

This statement returns all rows from the table_name table where the value of column1 is less than the value of column2.

SQL Logical Operators

Logical operators are used to performing logical operations, such as AND, OR, and NOT, on Boolean values (true or false). They are often used in conjunction with comparison operators to test multiple conditions.

Here is a list of common logical operators in SQL, along with examples of their usage:

SQL Logical Operator: AND

The AND operator is a logical operator that is used to test multiple conditions. It returns a Boolean value (true or false).

Here is an example of how you can use the AND operator in SQL:

SQL
SELECT * FROM table_name WHERE column1 = 'value1' AND column2 = 'value2';

This statement returns all rows from the table_name table where the value of column1 is ‘value1’ and the value of column2 is ‘value2’.

You can also use the AND operator with other logical operators, such as IN, NOT IN, BETWEEN, etc. For example:-

SQL
SELECT * FROM table_name WHERE column1 IN (5, 10, 15) AND column2 BETWEEN 20 AND 30;

This statement returns all rows from the table_name table where the value of column1 is 5, 10, or 15 and the value of column2 is between 20 and 30.

SQL Logical Operator: OR

The OR operator is a logical operator that is used to test multiple conditions. It returns a Boolean value (true or false).

Here is an example of how you can use the OR operator in SQL:

SQL
SELECT * FROM table_name WHERE column1 = 'value1' OR column2 = 'value2';

This statement returns all rows from the table_name table where the value of column1 is ‘value1’ or the value of column2 is ‘value2’.

You can also use the OR operator with other logical operators, such as IN, NOT IN, BETWEEN, etc. For example:-

SQL
SELECT * FROM table_name WHERE column1 IN (5, 10, 15) OR column2 BETWEEN 20 AND 30;

This statement returns all rows from the table_name table where the value of column1 is 5, 10, or 15 or the value of column2 is between 20 and 30.

SQL Logical Operator: NOT

In SQL, the NOT operator is a logical operator that is used to negate a condition. It is often used in conjunction with other logical operators, such as IN, NOT IN, BETWEEN, etc.

Here is an example of how you can use the NOT operator in SQL:

SELECT * FROM table_name WHERE NOT column1 = 'value';

This statement returns all rows from the table_name table where the value of column1 is not ‘value’.

You can also use the NOT operator with other logical operators, such as IN, NOT IN, BETWEEN, etc. For example:-

SQL
SELECT * FROM table_name WHERE column1 NOT IN (5, 10, 15);

This statement returns all rows from the table_name table where the value of column1 is not 5, 10, or 15.

SQL Logical Operator: ALL

The ALL operator is a logical operator that is used with the IN and NOT IN operators. It allows you to compare a value to every value in a list or subquery.

Here is an example of how you can use the ALL operator in SQL:

SQL
SELECT * FROM table_name WHERE column1 > ALL (SELECT column2 FROM table_name WHERE column3 = 'value');

This statement returns all rows from the table_name table where the value of column1 is greater than all the values in the column2 column, which are selected from the table_name table where the value of column3 is ‘value’.

SQL Logical Operator: ANY

In SQL, the ANY operator is a logical operator that is used with the IN and NOT IN operators. It allows you to compare a value to any value in a list or subquery.

Here is an example of how you can use the ANY operator in SQL:

SQL
SELECT * FROM table_name WHERE column1 > ANY (SELECT column2 FROM table_name WHERE column3 = 'value');

This statement returns all rows from the table_name table where the value of column1 is greater than any of the values in the column2 column, which are selected from the table_name table where the value of column3 is ‘value’.

SQL Logical Operator: BETWEEN

The BETWEEN operator is a logical operator that is used to test if a value is within a range of values. It is often used in conjunction with the AND operator.

Here is an example of how you can use the BETWEEN operator in SQL:

SQL
SELECT * FROM table_name WHERE column1 BETWEEN 5 AND 10;

This statement returns all rows from the table_name table where the value of column1 is greater than or equal to 5 and less than or equal to 10.

You can also use the NOT operator with the BETWEEN operator to find values that are not within a certain range. For example:-

SQL
SELECT * FROM table_name WHERE column1 NOT BETWEEN 5 AND 10;

This statement returns all rows from the table_name table where the value of column1 is less than 5 or greater than 10.

SQL Logical Operator: EXISTS

In SQL, the EXISTS operator is a logical operator that is used to test whether a subquery returns any rows. It returns a Boolean value (true or false).

Here is an example of how you can use the EXISTS operator in SQL:

SQL
SELECT * FROM table_name WHERE EXISTS (SELECT * FROM table_name WHERE column1 = 'value');

This statement returns all rows from the table_name table where there is at least one row in the table_name table where the value of column1 is ‘value’.

You can also use the NOT operator with the EXISTS operator to find rows where a subquery does not return any rows. For example:-

SQL
SELECT * FROM table_name WHERE NOT EXISTS (SELECT * FROM table_name WHERE column1 = 'value');

This statement returns all rows from the table_name table where there are no rows in the table_name table where the value of column1 is ‘value’.

SQL Logical Operator: IN

Here, the IN operator is a logical operator that allows you to test whether a value is in a list of values. It is often used in conjunction with the WHERE clause.

Here is an example of how you can use the IN operator in SQL:

SQL
SELECT * FROM table_name WHERE column1 IN (5, 10, 15);

This statement returns all rows from the table_name table where the value of column1 is 5, 10, or 15.

You can also use the IN operator with a subquery to test whether a value is in a list of values returned by the subquery. For example:-

SQL
SELECT * FROM table_name WHERE column1 IN (SELECT column2 FROM table_name WHERE column3 = 'value');

This statement returns all rows from the table_name table where the value of column1 is in the list of values returned by the subquery, which selects the values in the column2 column from the table_name table where the value of column3 is ‘value’.

SQL Logical Operator: LIKE

The LIKE operator is a logical operator that is used to test if a value matches a specific pattern. It is often used in conjunction with the WHERE clause.

The LIKE operator uses two wildcard characters:

  1. %” Operator: This wildcard matches any number of characters (including zero characters).
  2. _” Operator: This wildcard matches exactly one character.

Here is an example of how you can use the LIKE operator in SQL:

SQL
SELECT * FROM table_name WHERE column1 LIKE 'A%';

This statement returns all rows from the table_name table where the value of column1 starts with the letter ‘A’.

You can also use the NOT operator with the LIKE operator to find values that do not match a specific pattern. For example:-

SQL
SELECT * FROM table_name WHERE column1 NOT LIKE 'A%';

This statement returns all rows from the table_name table where the value of column1 does not start with the letter ‘A’.

SQL Logical Operator: IS NULL

In SQL, the IS NULL operator is a logical operator that is used to test if a value is NULL. It is often used in conjunction with the WHERE clause.

Here is an example of how you can use the IS NULL operator in SQL:

SQL
SELECT * FROM table_name WHERE column1 IS NULL;

This statement returns all rows from the table_name table where the value of column1 is NULL.

You can also use the NOT operator with the IS NULL operator to find values that are not NULL. For example:-

SQL
SELECT * FROM table_name WHERE column1 IS NOT NULL;

This statement returns all rows from the table_name table where the value of column1 is not NULL.

SQL Logical Operator: UNIQUE

Here, the UNIQUE operator is a logical operator that is used to test if a column or a combination of columns has unique values. It is often used in conjunction with the SELECT statement.

Here is an example of how you can use the UNIQUE operator in SQL:

SQL
SELECT DISTINCT column1 FROM table_name;

This statement returns all unique values in the column1 column from the table_name table.

You can also use the UNIQUE operator with multiple columns to find unique combinations of values. For example:-

SQL
SELECT DISTINCT column1, column2 FROM table_name;

This statement returns all unique combinations of values in the column1 and column2 columns from the table_name table.

SQL SET Operators

The set operators are used to combine the results of two or more SELECT statements. The most commonly used set operators in SQL are UNION, INTERSECT, and MINUS.

Here is an overview of each set operator:

  • UNION: The UNION operator combines the results of two or more SELECT statements and returns a single result set that includes all rows that appear in any of the SELECT statements. The UNION operator removes duplicate rows by default.
  • INTERSECT: The INTERSECT operator returns a single result set that includes only rows that appear in both of the SELECT statements. The INTERSECT operator removes duplicate rows by default.
  • MINUS: The MINUS operator returns a single result set that includes only rows that appear in the first SELECT statement and not in the second SELECT statement. The MINUS operator removes duplicate rows by default.

Here is an example of how you can use set operators in SQL:

SQL
SELECT column1, column2 FROM table1
UNION
SELECT column1, column2 FROM table2;

This statement combines the results of two SELECT statements and returns a single result set that includes all rows from both SELECT statements.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.

Other Recommended Article