# Topic 11: SQL Operators
Arithmetic, comparison, concatenation, bitwise operators, and expression evaluation precedence rules.
1. Definition
SQL operators are reserved symbols or keywords used in clauses to perform arithmetic computations, comparisons, and logical transformations on operands.
2. Explain Like I'm 5
Operators are math and comparison symbols (+, -, *, =, <, >). Just like PEMDAS in school math, SQL follows strict rules for what gets calculated first.
3. Animation & Visual Simulation
Interactive Engine TraceSQL Operators Visual Simulator
SELECT name, price, price * 1.18 AS price_with_tax FROM items WHERE price > 50;Active InputStep 1: Input & Parsing Stage
Initiating SQL Operators
Checking Table Invariants
The database engine inspects active tables, verifies permissions, and prepares the physical operation.
4. Real-World Example
An invoice calculation with volume discount percentages and shipping charges.
| unit_price | quantity | discount_rate |
|---|---|---|
| 50.00 | 4 | 0.10 |
5. SQL Syntax
ANSI SQL / PostgreSQL DialectSELECT product_name, price * quantity AS subtotal, price * quantity * (1 - discount) AS discounted_total FROM line_items WHERE (price * quantity) >= 100.00;
SELECT first_name || ' ' || last_name AS full_name, (id % 2 = 0) AS is_even_batch FROM users;
6. Important Rules & Behaviors
| Rule | Behavioral Explanation |
|---|---|
| Operator Precedence | Multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-). |
| Integer Division Traps | In engines like SQL Server and older Postgres, 5 / 2 yields 2 (integer division) unless cast to float: 5.0 / 2. |
| NULL Propagation | Any arithmetic operation involving NULL yields NULL (e.g., 100 + NULL = NULL). |
| Operator evaluation precedence | Multiplication/division takes precedence over addition/subtraction; comparison operators evaluate before logical operators. |
| Integer division truncation | In standard SQL and PostgreSQL, dividing an integer by an integer yields an integer (5 / 2 = 2), not a float. |
7. Deep Dive: String Concatenation Standards: || vs CONCAT
ANSI SQL uses the double pipe || for string concatenation. However, in standard SQL, "Hello" || NULL results in NULL. Functions like CONCAT("Hello", NULL) in MySQL/Postgres treat NULL as an empty string.
SELECT 'Lucky' || ' ' || NULL; -- Returns NULL in standard SQL!
SELECT CONCAT('Lucky', ' ', NULL); -- Returns 'Lucky ' safely.8. Advantages & Disadvantages
Advantages
- •Enables expressive calculations inside the query engine.
- •Minimizes application middleware round-trips.
Disadvantages & Trade-offs
- •Complex arithmetic in WHERE clauses disables B-Tree index traversal.
9. Common Interview Questions
Q1.What is the integer division trap in SQL and how do you prevent unexpected zero results?
In PostgreSQL and many SQL dialects, dividing two integers performs integer division, truncating the fractional part (e.g. 1 / 4 evaluates to 0, not 0.25). To perform floating-point or exact decimal division, at least one operand must be explicitly cast to float or numeric: 1.0 * val1 / val2 or CAST(val1 AS NUMERIC) / val2.
Q2.What are SQL operator precedence rules and how do they lead to bugs in compound expressions?
Precedence from highest to lowest: 1. Parentheses, 2. Unary operators (+, -), 3. Multiplication/Division (*, /, %), 4. Addition/Subtraction (+, -), 5. Comparison operators (=, !=, <, >), 6. NOT, 7. AND, 8. OR. Because AND evaluates before OR, a query like WHERE role = 'admin' OR department = 'IT' AND active = true is evaluated as role = 'admin' OR (department = 'IT' AND active = true), allowing inactive admins access.
Q3.What is the difference between string concatenation operators across PostgreSQL, MySQL, and SQL Server?
PostgreSQL, Oracle, and SQLite use the ANSI standard || operator for string concatenation ('hello' || ' ' || 'world'). SQL Server uses the + operator. MySQL historically treats + as numeric addition and requires the CONCAT() function. Furthermore, standard || returns NULL if any operand is NULL, whereas CONCAT_WS() skips NULLs.
Q4.What are bitwise operators in SQL and where are they used in high-performance architectures?
SQL supports bitwise AND (&), OR (|), XOR (#), and shift operators (<<, >>). High-performance systems use bitwise operations to store permission bitmasks inside a single INT or BIGINT column (e.g. 1=Read, 2=Write, 4=Delete, 8=Admin). Checking if a user has Write permission is an ultra-fast bitwise test: WHERE (permissions & 2) != 0.
Q5.How do comparison operators handle NULL values?
Any standard comparison operator (=, !=, <, >, <=, >=) evaluated against NULL produces UNKNOWN, never TRUE or FALSE. In SQL, UNKNOWN is rejected in WHERE and HAVING clauses. The only standard operators capable of safely testing for NULL are IS NULL, IS NOT NULL, and the null-safe equality operator (IS NOT DISTINCT FROM in PostgreSQL or <=> in MySQL).
10. Interview-Ready Answer
“SQL provides arithmetic, comparison, and string operators. Precedence determines evaluation order, and developers must account for integer division truncations and NULL propagation.”
11. Quick Test
What does "SELECT 5 + 5 * 2;" return in standard SQL?