Loading...
Loading...
Math for CS · Axiom Academy
WHERE clauses as logical propositions — AND, OR, NOT in real databases Every SQL WHERE clause is a propositional formula. The database evaluates it for each row, keeping only rows where the formula is true. The mapping is direct: Consider a students table with columns: name , gpa , major , year . Logically: M(x) G(x) where M(x) = "x's major is CS" and G(x) = "x's GPA > 3.5." SQL follows the same precedence as logic: NOT binds tightest, then AND , then OR . This causes a common bug: Because AND binds before OR, this is parsed as: ( > 3.5) De Morgan's laws apply directly to SQL conditions. These two queries are equivalent: Understanding this equivalence helps when rewriting queries for optimization or clarity. Standard logic has two values: true and false. SQL has three : TRUE, FALSE, and NULL (unknown). This breaks some expected equivalences. SQL's EXISTS maps directly to : For universal quantification ("for all"), use the double-negation trick: x\, P(x) x\, P(x) : Read as: "Find customers where there does NOT EXIST a product for which there does NOT EXIST an order." This is pure in action. SQL WHERE clauses are propositional formulas evaluated row by row. Precedence: NOT > AND > OR. Always use parentheses when mixing AND and OR. De Morgan's Laws work in SQL and are useful for query rewriting and optimization. NULL introduces three-valued logic that breaks some standard equivalences. Use IS NULL , never = NULL .
This is the written version of the interactive lesson above. See the full Math for CS course.