Skip to content
Back to 50 Concepts
Topic 11 of 50query-executionBeginner

# 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.

Relational Invariant:Deterministic relational algebra contract evaluated by the Cost-Based Optimizer.

2. Explain Like I'm 5

Intuitive mental model for beginners

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 Trace
Interactive Engine Simulation

SQL Operators Visual Simulator

Step 1 of 3
SQL>SELECT name, price, price * 1.18 AS price_with_tax FROM items WHERE price > 50;Active Input

Step 1: Input & Parsing Stage

Engine Action: Validate SQL syntax and resolve schema metadata for SQL Operators
SQL Input

Initiating SQL Operators

Schema Catalog

Checking Table Invariants

How the Database Executes This:

The database engine inspects active tables, verifies permissions, and prepares the physical operation.

💡 Math Class PEMDAS: Multiplication and division always execute before addition and subtraction unless parentheses force an override.

4. Real-World Example

An invoice calculation with volume discount percentages and shipping charges.

Line Items
unit_pricequantitydiscount_rate
50.0040.10
Relational walkthrough: Total cost calculation: (50.00 * 4) * (1 - 0.10) + 5.00 shipping.

5. SQL Syntax

ANSI SQL / PostgreSQL Dialect
Arithmetic and comparison operators
SELECT
  product_name,
  price * quantity AS subtotal,
  price * quantity * (1 - discount) AS discounted_total
FROM line_items
WHERE (price * quantity) >= 100.00;
String and modulo operators
SELECT first_name || ' ' || last_name AS full_name, (id % 2 = 0) AS is_even_batch FROM users;

6. Important Rules & Behaviors

RuleBehavioral Explanation
Operator PrecedenceMultiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-).
Integer Division TrapsIn engines like SQL Server and older Postgres, 5 / 2 yields 2 (integer division) unless cast to float: 5.0 / 2.
NULL PropagationAny arithmetic operation involving NULL yields NULL (e.g., 100 + NULL = NULL).
Operator evaluation precedenceMultiplication/division takes precedence over addition/subtraction; comparison operators evaluate before logical operators.
Integer division truncationIn 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.
Caution: Use CONCAT or COALESCE to avoid dropping entire strings when nullable columns are concatenated.

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

Scenario Question:

What does "SELECT 5 + 5 * 2;" return in standard SQL?

Select your answer: