Filtering, Sorting, and Calculating Data with SQL Flashcards
Why filter?
- Reduce the number of records you retrieve
- Increase query performance
- Reduce the strain on the client application
SQL for Data Science Week 2
WHERE clause syntax
SELECT column_name, column_name FROM table_name WHERE column_name operator value
SQL for Data Science Week 2
WHERE clause operators
- =
- <>
- >
- <
- > =
- <=
- BETWEEN
- IS NULL
SQL for Data Science Week 2
Does the column used in the WHERE clause need to be in the SELECT clause?
N
No
SQL for Data Science Week 2
How do you find nulls?
WHERE ProductName IS NULL
SQL for Data Science Week 2
Syntax for IN
WHERE SupplierID IN (1, 6, 7, 9)
Not that BETWEEN wouldn’t have been helpful here becuase we want specific number, not a range of numbers.
SQL for Data Science Week 2
Benefit of using IN over OR
IN
- You don’t have to think about the order
- You can use a subquery to return the values you want included
OR
- You have to think about the order because OR stops executing once the first condition is met.
SQL for Data Science Week 2
Which executes faster, IN or OR?
IN
SQL for Data Science Week 2
What do you need to watch out for when using OR and AND?
Answer: Operator order of precedence
--Example SELECT ProductID, SupplierID FROM Products WHERE Supplier1D = 9 OR SupplierID = 11 AND UnitPrice > 15
Objective
Get records for supplier 9 and supplier 11 where the UnitPrice>15
Problem
AND is processed before OR
Result
First records for supplier 11 with a unit price of 15 are selected. Then records for supplier 9 with any unit price are selected
Solution
Put parenthesis around (Supplier1D = 9 OR SupplierID = 11). Expressions inside parenthesis are evaluated first.
SQL for Data Science Week 2
Operator order of precedence in SQLite
1. () Expressions inside parentheses are evaluated first 2. NOT 3. *, /, % 4. +, – 5. <, <=, >, >= 6. =, !=, IS, LIKE, GLOB, REGEXP 7. AND 8. OR
SQL for Data Science Week 2
How do you use the WHERE clause to exclude values you don’t want?
- It appears that <> may not be used in SQLite, need to research more
- Use WHERE NOT City=’Seattle’
- Instead of City<>’Seattle’
SQL for Data Science Week 2
What data type can wildcards be used on?
Strings only
They cannot be used on non-text datatypes
Just writing a bunch of text to get the card to left justify
SQL for Data Science Week 2
What special character do you use for wildcards when using LIKE?
Examples
- ‘%Pizza’
- ‘Pizza%’
- ‘%Pizza%’
- ‘S%E’ (would pick up the name Sadie)
- ‘t%@gmail.com (gmail addresses that start with t, instead of also looking in hotmail or outlook emails)
SQL for Data Science Week 2
How does a wildcard treat a null value when using LIKE?
It will not match with null values since NULL has no value
SQL for Data Science Week 2
What special character matches just a single character when using LIKE?
The underscore _
SQL for Data Science Week 2
What special characters specify a set of characters in a specific location when using LIKE?
Brackets []
This does not work on all database systems
SQL for Data Science Week 2
Downsides of using wildcards?
- Takes longer to run
- =, <, =<, etc. will be faster
- Wildcards at the end of the string will take longer to run