SQL Operators
Sure! SQL operators are used to perform operations on data stored in a database. Here are some common SQL operators along with examples and their output:
1. Arithmetic Operators
Arithmetic operators perform mathematical operations.
-
Addition (+)
SELECT 5 + 3 AS Sum;
Output:
Sum
---
8
-
Subtraction (-)
SELECT 10 - 4 AS Difference;
Output:
Difference
----------
6
-
Multiplication (*)
SELECT 7 * 2 AS Product;
Output:
Product
-------
14
-
Division (/)
SELECT 20 / 4 AS Quotient;
Output:
Quotient
--------
5
-
Modulus (%)
SELECT 10 % 3 AS Remainder;
Output:
Remainder
---------
1
2. Comparison Operators
Comparison operators are used to compare two values.
-
Equal to (=)
SELECT 5 = 5 AS IsEqual;
Output:
IsEqual
-------
1
-
Not equal to (!= or <>)
SELECT 5 != 3 AS IsNotEqual;
Output:
IsNotEqual
----------
1
-
Greater than (>)
SELECT 7 > 5 AS IsGreaterThan;
Output:
IsGreaterThan
-------------
1
-
Less than (<)
SELECT 3 < 8 AS IsLessThan;
Output:
IsLessThan
----------
1
-
Greater than or equal to (>=)
SELECT 5 >= 5 AS IsGreaterThanOrEqual;
Output:
IsGreaterThanOrEqual
---------------------
1
-
Less than or equal to (<=)
SELECT 3 <= 4 AS IsLessThanOrEqual;
Output:
IsLessThanOrEqual
-----------------
1
3. Logical Operators
Logical operators are used to combine multiple conditions.
4. Bitwise Operators
Bitwise operators perform bit-level operations.
-
Bitwise AND (&)
SELECT 5 & 3 AS BitwiseAnd;
Output:
BitwiseAnd
----------
1
-
Bitwise OR (|)
SELECT 5 | 3 AS BitwiseOr;
Output:
BitwiseOr
---------
7
-
Bitwise XOR (^)
SELECT 5 ^ 3 AS BitwiseXor;
Output:
BitwiseXor
----------
6
-
Bitwise NOT (~)
SELECT ~5 AS BitwiseNot;
Output:
BitwiseNot
----------
-6
5. Other Operators
-
IN
SELECT 'apple' IN ('apple', 'banana', 'cherry') AS IsInList;
Output:
IsInList
--------
1
-
BETWEEN
SELECT 5 BETWEEN 1 AND 10 AS IsBetween;
Output:
IsBetween
---------
1
-
LIKE
SELECT 'hello' LIKE 'h%' AS MatchesPattern;
Output:
MatchesPattern
--------------
1
These examples cover a range of SQL operators used for arithmetic, comparison, logical, bitwise, and other operations, illustrating their usage and output in SQL queries.