0% found this document useful (0 votes)
3 views2 pages

Operators in SQL

Uploaded by

maheshkaikala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Operators in SQL

Uploaded by

maheshkaikala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Arithmetic Operators

These operators are used to perform mathematical calculations within SQL queries.
● Addition (+): Adds two numbers.
● Subtraction (-): Subtracts one number from another.
● Multiplication (*): Multiplies two numbers.
● Division (/): Divides one number1 by another.
● Modulo (% or MOD): Returns the remainder of a division. (Note: The specific symbol for
modulo might vary depending on the SQL database system you're using).

Example:

SELECT 10 + 5; -- Addition
SELECT 20 - 8; -- Subtraction
SELECT 6 * 3; -- Multiplication
SELECT 15 / 4; -- Division
SELECT 17 % 5; -- Modulo (remainder is 2)

2. Logical Operators

These operators are used to combine or negate conditions in the WHERE clause of a SQL
query. They return a boolean value (TRUE or FALSE).
● AND: Returns TRUE if both conditions are TRUE.
● OR: Returns TRUE if at least one condition is TRUE.
● NOT: Negates a condition (returns TRUE if the condition is FALSE, and vice versa).

Example:

SELECT *
FROM Employees
WHERE Department = 'Sales' AND Salary > 50000; -- Both conditions
must be true

SELECT *
FROM Customers
WHERE City = 'New York' OR City = 'Los Angeles'; -- At least one
condition must be true

SELECT *
FROM Products
WHERE NOT Category = 'Electronics'; -- Returns products that are NOT
in the Electronics category

3. Comparison Operators

These operators are used to compare two values. They also return a boolean value (TRUE or
FALSE).
● Equal to (=): Checks if two values are equal.
● Not equal to (<> or !=): Checks if two values are not equal. (Note: The specific symbol for
"not equal to" might vary depending on the SQL database system).
● Greater than (>): Checks if the left value is greater than the right value.
● Less than (<): Checks if the left value is less than the right value.
● Greater than or equal to (>=): Checks if the left value is greater than or equal to the right
value.
● Less than or equal to (<=): Checks if the left value is less than or equal to the right value.2
● BETWEEN: Checks if a value is within a specified range.
● LIKE: Checks if a value matches a specified pattern.
● IN: Checks if a value matches any value in a list.
● IS NULL: Checks if a value is NULL (empty).
● IS NOT NULL: Checks if a value is not NULL.

Example:

SELECT *
FROM Products
WHERE Price > 100; -- Greater than

SELECT *
FROM Orders
WHERE OrderDate BETWEEN '2023-01-01' AND '2023-03-31'; -- Within a
range

SELECT *
FROM Customers
WHERE LastName LIKE 'S%'; -- Starts with "S"

SELECT *
FROM Employees
WHERE Department IN ('Sales', 'Marketing'); -- In a list

SELECT *
FROM Addresses
WHERE City IS NULL; -- Is empty

Important Notes:
● Database System Variations: Some of the operators or their specific symbols might have
slight variations depending on the SQL database system you are using (e.g., MySQL,
PostgreSQL, SQL Server, Oracle, etc.).
● Operator Precedence: Just like in mathematics, SQL operators have precedence. This
determines the order in which operations are performed. You can use parentheses () to
explicitly control the order of operations.

You might also like