Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL – Where.

Similar presentations


Presentation on theme: "SQL – Where."— Presentation transcript:

1 SQL – Where

2 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;

3 SELECT * FROM students WHERE spell_performance > 8;
first_name spell_performance type_cat Michael 8 No Austin cat Adi 10 Dog Daniel 9 Wesley

4 SELECT * FROM students WHERE spell_performance > 8;
first_name spell_performance type_cat Michael 8 No Austin cat Adi 10 Dog Daniel 9 Wesley

5 SELECT * FROM students WHERE spell_performance > 8;
first_name spell_performance type_cat Adi 10 Dog Daniel 9 cat

6 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

7 SQL operators Arithmetic Operators: Comparison Operators:
+ - * / % Comparison Operators: = != <> > < >= <= Logical Operators ALL AND ANY BETWEEN EXISTS IN LIKE NOT OR IS

8 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)

9 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

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

11 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

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

13 EXISTS, ANY, ALL These operators use sub-queries, which we'll get to later in the course.

14 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).

15 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

16 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, ½ ) = ½.

17 Example with null first_name spell_performance Yi NULL
SELECT * FROM students WHERE spell_performance > 5 OR spell_performance <= 5; UNKNOWN UNKNOWN UNKNOWN

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


Download ppt "SQL – Where."

Similar presentations


Ads by Google