SQL – Where
Where SELECT ... FROM ... WHERE ...; The where clause stipulates a test (predicate) that each row must pass to be returned in the select statement. SELECT * FROM students WHERE spell_performance > 8;
SELECT * FROM students WHERE spell_performance > 8; first_name spell_performance type_cat Michael 8 No Austin cat Adi 10 Dog Daniel 9 Wesley
SELECT * FROM students WHERE spell_performance > 8; first_name spell_performance type_cat Michael 8 No Austin cat Adi 10 Dog Daniel 9 Wesley
SELECT * FROM students WHERE spell_performance > 8; first_name spell_performance type_cat Adi 10 Dog Daniel 9 cat
What should happen for the query: SELECT * FROM students WHERE spell_performance > 10; first_name spell_ performance type_cat Michael 8 No Austin cat Adi 10 Dog Daniel 9 Josh NULL It should return 0 rows It should raise a syntax error It should return 1 row It should raise a "no rows" error
SQL operators Arithmetic Operators: Comparison Operators: + - * / % Comparison Operators: = != <> > < >= <= Logical Operators ALL AND ANY BETWEEN EXISTS IN LIKE NOT OR IS
AND AND OR first_name spell_performance type_cat Michael 8 No Austin Adi 10 Dog Daniel 9 Wesley SELECT * FROM students WHERE spell_performance > 8 AND type_cat = 'cat'; 1 row SELECT * FROM students WHERE spell_performance > 8 OR type_cat = 'cat'; 4 rows (poor Michael)
Between first_name spell_performance type_cat Michael 8 No Austin cat Adi 10 Dog Daniel 9 Wesley SELECT * FROM students WHERE spell_performance BETWEEN 9 AND 10; 2 rows Between is inclusive of boundaries SELECT * FROM students WHERE spell_performance NOT BETWEEN 9 AND 10; 3 rows
IS first_name spell_performance fav_color Michael 8 NULL Austin 'red' Adi 10 Daniel 9 'Green' Wesley 'dark green' SELECT * FROM students WHERE fav_color IS NULL; 2 rows SELECT * FROM students WHERE fav_color IS NOT NULL; 3 rows The "IS" operator is only to be used to test for NULL values.
Like first_name spell_performance fav_color Michael 8 NULL Austin 'red' Adi 10 Daniel 9 'Green' Wesley 'dark green' SELECT * FROM students WHERE fav_color LIKE '%reen'; 2 rows % is a wildcard that can match 0 or more characters. Should have been *, but it was already taken _ (underscore) is a wildcard representing a single character Should have been . grrrrr
IN first_name spell_performance fav_color Michael 8 NULL Austin 'red' Adi 10 Daniel 9 'Green' Wesley 'dark green' SELECT * FROM students WHERE spell_performance IN (5, 8, 10); 4 rows IN tests for inclusion in a static, parenthesized list.
EXISTS, ANY, ALL These operators use sub-queries, which we'll get to later in the course.
Comparing NULLs to values The logic of conditions in SQL is really 3-valued logic: TRUE, FALSE, UNKNOWN. Comparing any value (including NULL itself) with NULL yields UNKNOWN. A tuple is in a query answer if and only if the WHERE clause is TRUE (not FALSE or UNKNOWN).
What should happen for the query: first_name spell_ performance type_cat Michael 8 No Austin cat Adi 10 Dog Daniel 9 Wesley Josh NULL SELECT * FROM students WHERE NOT spell_performance > 8; It should return 0 rows It should return 1 row It should return 2 rows It should return 3 rows
Three-Valued Logic To understand how AND, OR, and NOT work in 3-valued logic, think of TRUE = 1, FALSE = 0, and UNKNOWN = ½. AND = MIN; OR = MAX, NOT(x) = 1-x. Example: TRUE AND (FALSE OR NOT (UNKNOWN)) = MIN(1, MAX(0, (1 - ½ ))) = MIN(1, MAX(0, ½ )) = MIN(1, ½ ) = ½.
Example with null first_name spell_performance Yi NULL SELECT * FROM students WHERE spell_performance > 5 OR spell_performance <= 5; UNKNOWN UNKNOWN UNKNOWN
Reason: 2-Valued Laws != 3-Valued Laws Some common laws, like commutativity of AND, hold in 3-valued logic. But not others, e.g., the law of the excluded middle : p OR NOT p = TRUE. When p = UNKNOWN, the left side is MAX( ½, (1 – ½ )) = ½ != 1.