SQL (Structured Query Language) operators are essential components used to perform various operations on data stored in a database. Here's a basic overview of SQL operators for new users:
1. Arithmetic Operators
These operators are used to perform arithmetic operations on numeric data.
- Addition ('+'): Adds two numbers.
'''sql
SELECT 5 + 3; -- Result: 8
'''
- Subtraction ('-'): Subtracts one number from another.
'''sql
SELECT 5 - 3; -- Result: 2
'''
- Multiplication (''): Multiplies two numbers.
'''sql
SELECT 5 3; -- Result: 15
'''
- Division ('/'): Divides one number by another.
'''sql
SELECT 15 / 3; -- Result: 5
'''
- Modulus ('%'): Returns the remainder of a division.
'''sql
SELECT 5 % 3; -- Result: 2
'''
2. Comparison Operators
These operators are used to compare two values and return a boolean result (TRUE or FALSE).
- Equal to ('='): Checks if two values are equal.
'''sql
SELECT FROM users WHERE age = 25;
'''
- Not equal to ('!=' or '<>'): Checks if two values are not equal.
'''sql
SELECT FROM users WHERE age != 25;
'''
- Greater than ('>'): Checks if the value on the left is greater than the value on the right.
'''sql
SELECT FROM users WHERE age > 25;
'''
- Less than ('<'): Checks if the value on the left is less than the value on the right.
'''sql
SELECT FROM users WHERE age < 25;
'''
- Greater than or equal to ('>='): Checks if the value on the left is greater than or equal to the value on the right.
'''sql
SELECT FROM users WHERE age >= 25;
'''
- Less than or equal to ('<='): Checks if the value on the left is less than or equal to the value on the right.
'''sql
SELECT FROM users WHERE age <= 25;
'''
3. Logical Operators
These operators are used to combine multiple conditions.
- AND: Returns TRUE if all the conditions separated by AND are TRUE.
'''sql
SELECT FROM users WHERE age > 25 AND city = 'New York';
'''
- OR: Returns TRUE if any of the conditions separated by OR is TRUE.
'''sql
SELECT FROM users WHERE age > 25 OR city = 'New York';
'''
- NOT: Reverses the result of a condition.
'''sql
SELECT FROM users WHERE NOT age > 25;
'''
4. Bitwise Operators
These operators are used to perform bit-level operations.
- Bitwise AND ('&'): Compares each bit of two integers and returns a result with bits set to 1 where both integers have bits set to 1.
'''sql
SELECT 5 & 3; -- Result: 1 (0101 & 0011 = 0001)
'''
- Bitwise OR ('|'): Compares each bit of two integers and returns a result with bits set to 1 where either integer has a bit set to 1.
'''sql
SELECT 5 | 3; -- Result: 7 (0101 | 0011 = 0111)
'''
- Bitwise XOR ('^'): Compares each bit of two integers and returns a result with bits set to 1 where only one of the integers has a bit set to 1.
'''sql
SELECT 5 ^ 3; -- Result: 6 (0101 ^ 0011 = 0110)
'''
- Bitwise NOT ('~'): Inverts all the bits of an integer.
'''sql
SELECT ~5; -- Result: -6 (bitwise NOT of 0101 is 1010 in 2's complement)
'''
YOU ARE READING
SQL Operators Basics for New Users
HumorSQL (Structured Query Language) operators are essential components used to perform various operations on data stored in a database. Here's a basic overview of SQL operators for new users: 1. Arithmetic Operators These operators are used to perform...
