Logic and Comparison
Single conditions are rarely enough for complex data analysis. SQL provides logical operators to combine multiple conditions and specialized comparison operators to handle ranges and lists efficiently.
Definition: Logical Operators
Section titled “Definition: Logical Operators”Logical operators allow you to build complex “Boolean” expressions.
AND: ReturnsTRUEonly if both conditions are true.OR: ReturnsTRUEif at least one condition is true.NOT: Reverses the value of a condition (True becomes False, and vice-versa).
Context: Order of Precedence
Section titled “Context: Order of Precedence”Just like in math where multiplication happens before addition ($2 + 3 imes 5 = 17$), SQL has an order of precedence for logical operators:
NOT(Highest)ANDOR(Lowest)
Detailed Explanation: Range and List Operators
Section titled “Detailed Explanation: Range and List Operators”While you could use multiple AND or OR statements, SQL provides cleaner alternatives.
1. The BETWEEN Operator
Section titled “1. The BETWEEN Operator”Used to filter values within a range (inclusive of the start and end values).
- Standard:
Price >= 10 AND Price <= 20 - SQL Idiomatic:
Price BETWEEN 10 AND 20
2. The IN Operator
Section titled “2. The IN Operator”Used to check if a value matches any value in a provided list.
- Standard:
City = 'London' OR City = 'Paris' OR City = 'Tokyo' - SQL Idiomatic:
City IN ('London', 'Paris', 'Tokyo')
Example: Complex Logic in Action
Section titled “Example: Complex Logic in Action”We have a Staff table:
| ID | Name | Dept | Salary | Performance |
|---|---|---|---|---|
| 1 | John | IT | 60000 | 4 |
| 2 | Jane | HR | 55000 | 5 |
| 3 | Bob | IT | 90000 | 3 |
| 4 | Mary | Sales | 70000 | 5 |
Scenario: High-Potential Employees
Section titled “Scenario: High-Potential Employees”We want IT employees earning between 50k and 100k, OR anyone with a Performance score of 5.
SELECT Name, Dept, Salary, PerformanceFROM StaffWHERE (Dept = 'IT' AND Salary BETWEEN 50000 AND 100000) OR Performance = 5;Output
Section titled “Output”| Name | Dept | Salary | Performance |
|---|---|---|---|
| John | IT | 60000 | 4 |
| Jane | HR | 55000 | 5 |
| Bob | IT | 90000 | 3 |
| Mary | Sales | 70000 | 5 |
Wait, why was Bob included? Let’s check the logic:
- Bob is in IT? Yes.
- Bob’s salary is between 50k-100k? Yes (90k).
- Thus, the first part of the
ORis true.
Under the Hood: Short-Circuit Evaluation
Section titled “Under the Hood: Short-Circuit Evaluation”Most modern SQL engines use Short-Circuit Evaluation.
- For an
ANDoperation: If the first condition isFALSE, the engine stops immediately because the result can never beTRUE. - For an
ORoperation: If the first condition isTRUE, the engine stops immediately because the result is already guaranteed to beTRUE.
Optimization Tip
Section titled “Optimization Tip”If you have two conditions in an AND clause:
- One is a complex calculation (slow).
- One is a simple ID check (fast).
Place the simple check first. The engine might skip the slow calculation entirely if the simple check fails.