Loading...
Loading...
00:00:00

SQL Operators

SQL is a database programming language that allows for the manipulation of data within a database. There are several types of SQL operators that can be used to perform different functions in SQL. These operators can be categorized into different types, including arithmetic operators, comparison operators, logical operators, and wildcard operators.

Arithmetic Operators:

In SQL, Arithmetic operators are used to perform mathematical calculations on numerical values. These operators are used to perform basic arithmetic operations like addition, subtraction, multiplication, division, and modulo. Arithmetic operators perform mathematical calculations on values in SQL.

  • Addition: SELECT 5 + 10 would return the result of 15.

  • Subtraction: SELECT 10 - 5 would return the result of 5.

  • Multiplication: SELECT 5 * 10 would return the result of 50.

  • Division: SELECT 10 / 5 would return the result of 2.

  • Modulo: SELECT 10 % 3 would return the result of 1.

Here are the arithmetic operators in SQL with examples:

  1. Addition +: The addition operator is used to add two or more numeric values. For example,
    SELECT 5 + 10;
    

    This SQL statement will return the result 15.

  2. Subtraction -: The subtraction operator is used to subtract one numeric value from another. For example,
    SELECT 10 - 5;
    

    This SQL statement will return the result 5.

  3. Multiplication *: The multiplication operator is used to multiply two or more numeric values. For example,
    SELECT 5 * 10;
    

    This SQL statement will return the result 50.

  4. Division /: The division operator is used to divide one numeric value by another. For example,
    SELECT 10 / 5;
    

    This SQL statement will return the result 2.

  5. Modulo %: The modulo operator is used to return the remainder of a division operation. For example,
    SELECT 10 % 3;
    

    This SQL statement will return the result 1 because 10 divided by 3 has a remainder of 1.

Arithmetic operators can also be used with columns in a table to perform calculations on the data in a database. For example,

SELECT salary * 1.1 FROM employees;

This SQL statement will return the result of increasing the salary of all employees by 10%.

Comparision Operators in SQL

In SQL, Comparison operators are used to compare two values and return a Boolean value (True or False) based on the comparison. Comparison operators are used in the WHERE clause of a SQL statement to filter data from a table.

Comparison operators are used to compare two values in SQL and return a Boolean value (True or False) based on the comparison.

  • Equal to: SELECT * FROM employees WHERE age = 30 would return all employees whose age is equal to 30.

  • Not equal to: SELECT * FROM employees WHERE age != 30 would return all employees whose age is not equal to 30.

  • Greater than: SELECT * FROM employees WHERE salary > 50000 would return all employees whose salary is greater than 50,000.

  • Less than: SELECT * FROM employees WHERE salary < 50000 would return all employees whose salary is less than 50,000.

  • Greater than or equal to: SELECT * FROM employees WHERE age >= 30 would return all employees whose age is greater than or equal to 30.

  • Less than or equal to: SELECT * FROM employees WHERE age <= 30 would return all employees whose age is less than or equal to 30.

Here are the comparison operators in SQL with examples:

  1. Equal to =: The equal to operator is used to compare whether two values are equal. For example,
    SELECT * FROM students WHERE age = 20;
    

    This SQL statement will return all the students whose age is equal to 20.

  2. Not equal to != or <>: The not equal to operator is used to compare whether two values are not equal. Both != and <> are used for not equal to. For example,
    SELECT * FROM students WHERE age != 20;
    

    This SQL statement will return all the students whose age is not equal to 20.

  3. Greater than >: The greater than operator is used to compare whether one value is greater than another value. For example,
    SELECT * FROM students WHERE age > 20;
    

    This SQL statement will return all the students whose age is greater than 20.

  4. Less than <: The less than operator is used to compare whether one value is less than another value. For example,
    SELECT * FROM students WHERE age < 20;
    

    This SQL statement will return all the students whose age is less than 20.

  5. Greater than or equal to >=: The greater than or equal to operator is used to compare whether one value is greater than or equal to another value. For example,
    SELECT * FROM students WHERE age >= 20;
    

    This SQL statement will return all the students whose age is greater than or equal to 20.

  6. Less than or equal to <=: The less than or equal to operator is used to compare whether one value is less than or equal to another value. For example,
    SELECT * FROM students WHERE age <= 20;
    

    This SQL statement will return all the students whose age is less than or equal to 20.

Comparison operators can also be used with dates, strings, and other data types in SQL. For example,

SELECT * FROM orders WHERE order_date > '2022-01-01';

This SQL statement will return all the orders that were placed after January 1, 2022.

Logical Operators in SQL

In SQL, Logical operators are used to combine two or more conditions to form a more complex condition. Logical operators are used in the WHERE clause of a SQL statement to filter data from a table.

Logical operators are used to combine multiple conditions together in SQL.

  • AND: SELECT * FROM employees WHERE age >= 30 AND salary >= 50000 would return all employees whose age is greater than or equal to 30 and whose salary is greater than or equal to 50,000.

  • OR: SELECT * FROM employees WHERE age >= 30 OR salary >= 50000 would return all employees whose age is greater than or equal to 30 or whose salary is greater than or equal to 50,000.

  • NOT: SELECT * FROM employees WHERE NOT age >= 30 would return all employees whose age is not greater than or equal to 30.

Here are the logical operators in SQL with examples:

  1. AND: The AND operator is used to combine two or more conditions and return true if all the conditions are true. For example,
    SELECT * FROM students WHERE age >= 20 AND gender = 'Male';
    

    This SQL statement will return all the male students whose age is greater than or equal to 20

  2. OR: The OR operator is used to combine two or more conditions and return true if any one of the conditions is true. For example,
    SELECT * FROM students WHERE age >= 20 OR gender = 'Male';
    

    This SQL statement will return all the students whose age is greater than or equal to 20 or whose gender is male.

  3. NOT: The NOT operator is used to negate a condition and return the opposite Boolean value. For example,
    SELECT * FROM students WHERE NOT age >= 20;
    

    This SQL statement will return all the students whose age is less than 20.

Logical operators can also be used with parentheses to group conditions and control the order of evaluation. For example,

SELECT * FROM students WHERE (age >= 20 OR gender = 'Male') AND grade = 'A';

This SQL statement will return all the male students whose age is greater than or equal to 20 and whose grade is A.

It's important to use logical operators carefully and understand the order of evaluation, as they can affect the results of a query.

Wildcard Operators in SQL

In SQL, Wildcard operators are used to match patterns in string values. Wildcard operators are used in the WHERE clause of a SQL statement to filter data from a table.

Wildcard operators are used to search for values that match a particular pattern in SQL.

  • Percent (%): SELECT * FROM employees WHERE name LIKE 'A%' would return all employees whose name starts with the letter "A".

  • Underscore (_): SELECT * FROM employees WHERE name LIKE '__o%' would return all employees whose name has "o" as the third letter.

These are the most commonly used SQL operators with examples that demonstrate their usage.

Here are the wildcard operators in SQL with examples:

  1. %: The % operator matches zero or more characters in a string. For example,
    SELECT * FROM customers WHERE customer_name LIKE '%John%';
    

    This SQL statement will return all the customers whose name contains the string "John", regardless of the position of the string within the name.

  2. _: The _ operator matches a single character in a string. For example,
    SELECT * FROM customers WHERE customer_name LIKE 'J__n';
    

    This SQL statement will return all the customers whose name is four characters long and starts with "J" and ends with "n".

  3. []: The [] operator matches any single character in a set of characters. For example,
    SELECT * FROM customers WHERE customer_name LIKE 'J[aeiou]hn';
    

    This SQL statement will return all the customers whose name starts with "J" and ends with "hn", and the second character can be any vowel.

  4. [^]: The [^] operator matches any single character not in a set of characters. For example,
    SELECT * FROM customers WHERE customer_name LIKE 'J[^aeiou]hn';
    

    This SQL statement will return all the customers whose name starts with "J" and ends with "hn", and the second character can be any consonant.

Wildcard operators can also be combined to form more complex patterns. For example,

SELECT * FROM customers WHERE customer_name LIKE '%J%y%';

This SQL statement will return all the customers whose name contains the string "J" followed by any number of characters, followed by the string "y" followed by any number of characters.

It's important to use wildcard operators carefully and understand the pattern matching rules, as they can affect the results of a query.