Welcome to The Coding College, where we simplify coding and database management! In this guide, we’ll explore PostgreSQL Operators, which are essential tools for performing operations on data stored in your database.
What Are Operators in PostgreSQL?
Operators in PostgreSQL are special symbols or keywords that allow you to perform actions such as comparisons, mathematical calculations, and logical operations on data.
Types of Operators
PostgreSQL offers various operators categorized as follows:
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- String Operators
- Other Specialized Operators
1. Arithmetic Operators
These operators perform basic mathematical operations.
Operator | Description | Example | Result |
---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 5 - 3 | 2 |
* | Multiplication | 5 * 3 | 15 |
/ | Division | 10 / 2 | 5 |
% | Modulus (Remainder) | 10 % 3 | 1 |
Example Query
SELECT 5 + 3 AS addition, 10 / 2 AS division;
2. Comparison Operators
Used to compare two values.
Operator | Description | Example | Result |
---|---|---|---|
= | Equal to | 5 = 5 | TRUE |
!= or <> | Not equal to | 5 != 3 | TRUE |
< | Less than | 5 < 3 | FALSE |
> | Greater than | 5 > 3 | TRUE |
<= | Less than or equal to | 5 <= 5 | TRUE |
>= | Greater than or equal to | 5 >= 3 | TRUE |
Example Query
SELECT * FROM students WHERE age >= 18;
3. Logical Operators
Used for combining multiple conditions.
Operator | Description | Example | Result |
---|---|---|---|
AND | Logical AND | TRUE AND FALSE | FALSE |
OR | Logical OR | TRUE OR FALSE | TRUE |
NOT | Logical NOT | NOT TRUE | FALSE |
Example Query
SELECT * FROM students WHERE age >= 18 AND age <= 25;
4. Bitwise Operators
Perform operations at the binary level.
Operator | Description | Example | Result |
---|---|---|---|
& | Bitwise AND | 5 & 3 | 1 |
` | ` | Bitwise OR | `5 |
# | Bitwise XOR | 5 # 3 | 6 |
<< | Bitwise shift left | 5 << 1 | 10 |
>> | Bitwise shift right | 5 >> 1 | 2 |
5. String Operators
Used for manipulating string data.
Operator | Description | Example | Result |
---|---|---|---|
` | ` | Concatenation | |
LIKE | Pattern matching | 'Hello' LIKE 'H%' | TRUE |
ILIKE | Case-insensitive pattern matching | 'hello' ILIKE 'H%' | TRUE |
~ | Regex match | 'abc' ~ 'a.c' | TRUE |
6. Other Specialized Operators
PostgreSQL-specific operators include:
- Array Operators Operator Description Example Result
@>
ContainsARRAY[1, 2] @> ARRAY[1]
TRUE
<@
Is contained byARRAY[1] <@ ARRAY[1, 2]
TRUE
- JSON Operators Operator Description Example Result
->
Get JSON object fieldjson_col->'name'
"John"
->>
Get JSON text valuejson_col->>'name'
John
Common Use Cases
Example 1: Filtering Records
SELECT * FROM students WHERE age > 18 AND name LIKE 'A%';
Example 2: Calculating Grades
SELECT name, (marks / total_marks) * 100 AS percentage FROM students;
Example 3: Joining Tables with Conditions
SELECT s.name, c.course_name
FROM students s
JOIN courses c
ON s.id = c.student_id
WHERE c.duration > 3;
Learn More at The Coding College
For more in-depth tutorials and practical examples, visit The Coding College. Our PostgreSQL guides are designed to enhance your database skills and ensure you follow Google’s E-E-A-T principles for reliable, expert content.
Conclusion
PostgreSQL operators are versatile tools for handling data efficiently. By mastering these operators, you can build powerful queries and manage your database with ease.
Have any questions? Drop a comment or connect with The Coding College for more personalized help!