# SQL Operators

Structured Query Language (SQL) is a powerful tool for managing and manipulating relational databases. SQL operators play a crucial role in formulating queries that retrieve, filter, and manipulate data within these databases. In this comprehensive exploration, we delve into the various SQL operators, understanding their types, applications, and nuances.

## 1. Introduction to SQL Operators

SQL operators are special keywords or symbols used to perform operations on one or more values in a SQL statement. These operations can range from simple arithmetic calculations to complex logical comparisons and string manipulations.

### 1.1 Arithmetic Operators

SELECT salary + bonus AS total_income FROM employees;


Subtraction (-): Subtraction operator subtracts the second operand from the first. Example:

SELECT total_sales - expenses AS net_profit FROM financial_data;


Multiplication (*): Multiplication operator multiplies two numeric values. Example:

SELECT quantity * unit_price AS total_cost FROM orders;


Division (/): Division operator divides the first operand by the second. Example:

SELECT revenue / number_of_customers AS average_revenue FROM sales_data;


### 1.2 Comparison Operators

Comparison operators are used to compare values in a SQL statement.

Equal to (=): Checks if two values are equal. Example:

SELECT * FROM products WHERE price = 50;


Not equal to (!= or <>): Checks if two values are not equal. Example:

SELECT * FROM customers WHERE country <> 'USA';


Greater than (>): Checks if the value on the left is greater than the value on the right. Example:

SELECT * FROM orders WHERE order_total > 1000;


Less than (<): Checks if the value on the left is less than the value on the right. Example:

SELECT * FROM employees WHERE age < 30;


Greater than or equal to (>=) and Less than or equal to (<=): Check if the values are greater than or equal to, and less than or equal to, respectively.

### 1.3 Logical Operators

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

AND: Returns true if both conditions are true. Example:

SELECT * FROM products WHERE category = 'Electronics' AND stock_quantity > 0;


OR: Returns true if at least one condition is true. Example:

SELECT * FROM customers WHERE country = 'USA' OR country = 'Canada';


NOT: Returns true if the following condition is false, and vice versa. Example:

SELECT * FROM orders WHERE NOT order_status = 'Cancelled';


### 1.4 Unary Operators

Unary operators operate on a single operand.

IS NULL: Checks if a value is null. Example:

SELECT * FROM employees WHERE supervisor_id IS NULL;


IS NOT NULL: Checks if a value is not null.

### 2.1 String Concatenation Operator

The concatenation operator (|| in some databases) is used to combine two or more strings into a single string.

SELECT first_name || ' ' || last_name AS full_name FROM employees;


### 2.2 LIKE Operator

The LIKE operator is used for pattern matching in strings. It supports wildcard characters such as % (matches any sequence of characters) and _ (matches any single character).

SELECT * FROM products WHERE product_name LIKE 'Laptop%';


### 2.3 IN Operator

The IN operator is used to specify multiple values in a WHERE clause.

SELECT * FROM orders WHERE customer_id IN (1, 3, 5);


### 2.4 BETWEEN Operator

The BETWEEN operator is used to filter results within a range.

SELECT * FROM products WHERE price BETWEEN 50 AND 100;


## 3. Conclusion

SQL operators are indispensable tools for crafting precise and powerful database queries. A solid understanding of these operators is essential for anyone working with relational databases, as they form the foundation of data manipulation and retrieval. By mastering SQL operators, you empower yourself to extract valuable insights and make informed decisions from vast datasets, contributing to the efficiency and effectiveness of database management and analysis.

Categories sql