SQL Tutorial: A Comprehensive Guide to SQL Operators.

Introduction to Operators in SQL

Operators in SQL are used to perform various operations on data stored in a relational database. They allow us to manipulate and compare data, perform calculations, and combine multiple conditions to retrieve specific information. In this tutorial, we will explore the different types of operators in SQL and provide examples of their usage.

Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations on numeric data types in SQL. The common arithmetic operators in SQL are:

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

Let’s see some examples:

SELECT 10 + 5; -- Output: 15
SELECT 10 - 5; -- Output: 5
SELECT 10 * 5; -- Output: 50
SELECT 10 / 5; -- Output: 2
SELECT 10 % 5; -- Output: 0

Comparison Operators

Comparison operators in SQL are used to compare values and return a result based on the comparison. The common comparison operators in SQL are:

  • Equal to (=): Checks if two values are equal.
  • Not equal to (!= or <>): Checks if two values are not equal.
  • Greater than (>): Checks if one value is greater than another.
  • Less than (<): Checks if one value is less than another.
  • Greater than or equal to (>=): Checks if one value is greater than or equal to another.
  • Less than or equal to (<=): Checks if one value is less than or equal to another.

Here are some examples:

SELECT * FROM employees WHERE age = 30; -- Retrieves employees with an age of 30
SELECT * FROM employees WHERE salary > 50000; -- Retrieves employees with a salary greater than 50000
SELECT * FROM employees WHERE name <> 'John'; -- Retrieves employees with a name not equal to John

Logical Operators

Logical operators in SQL are used to combine multiple conditions and evaluate the result as true or false. The common logical operators in SQL are:

  • AND: Returns true if all conditions are true.
  • OR: Returns true if at least one condition is true.
  • NOT: Negates the result of a condition.

Let’s see some examples:

SELECT * FROM employees WHERE age > 30 AND salary > 50000; -- Retrieves employees with an age greater than 30 and a salary greater than 50000
SELECT * FROM employees WHERE age > 30 OR salary > 50000; -- Retrieves employees with an age greater than 30 or a salary greater than 50000
SELECT * FROM employees WHERE NOT age = 30; -- Retrieves employees with an age not equal to 30

String Operators

String operators in SQL are used to manipulate and compare string values. The common string operators in SQL are:

  • Concatenation (||): Joins two or more strings together.
  • LIKE: Searches for a specified pattern in a string.

Here are some examples:

SELECT first_name || ' ' || last_name AS full_name FROM employees; -- Concatenates the first name, a space, and the last name of each employee
SELECT * FROM employees WHERE first_name LIKE 'J%'; -- Retrieves employees with a first name starting with 'J'
SELECT * FROM employees WHERE last_name LIKE '%son'; -- Retrieves employees with a last name ending with 'son'

Conclusion

Operators in SQL play a crucial role in manipulating and comparing data in a relational database. By understanding and utilizing these operators effectively, you can perform complex calculations, filter data based on specific conditions, and retrieve the information you need. This tutorial provided an overview of the different types of operators in SQL and demonstrated their usage with examples. With this knowledge, you can now confidently use operators to enhance your SQL queries and gain valuable insights from your database.

Leave a Comment