Filtering Data Flashcards
What is returned if you only specify a date you want to filter for from a column but the column is DATETIME type?
When you only specify a date when filtering a DATETIME column, time of midnight is assumed. So, if asked for ‘20070212’, only records on that date with a time stamp of midnight would be returned.
SQL Server 70-461 03-01
How does the NOT operator treat nulls?
Nulls will be excluded unless IS NULL, IS NOT NULL or some similar expression is used.
For example, NOT region=’WA’ will only return rows where the region is filled in and doesn’t equal WA. Since a null has an unknown value, the query cannot decide if it doesn’t equal WA and excludes it.
SQL Server 70-461 03-01
What is the proper form of a predicate using LIKE?
column LIKE pattern
SQL Server 70-461 03-01
How can you combine predicates in the WHERE clause?
- AND
- OR
SQL Server 70-461 03-01
Explain Three Valued Logic
Predicate can evaluate to true, false or unknown since NULLS are involved.
SQL Server 70-461 03-01
Explain Two-Valued Logic
Predicate expression evaluates to true or false, if NULLS are not possible.
SQL Server 70-461 03-01
How does the % wildcard work?
- The percent sign stands for a string of any size, including an empty string.
- Example: ‘D%’ This will find all strings that start with a D and follow with pretty much anything else
SQL Server 70-461 03-01
How does the _ wildcard work?
- The _ stands for any one character.
- Example: So, if you wanted to find a name that started with any character but wanted to ensure the second character was D you would use ‘_D%’. % standing for a string of any number of characters, even an empty string.
SQL Server 70-461 03-01
How does the character list wildcard work?
- Between square brackets, [ ], list the acceptable characters.
- Example: ‘[ABC]%’ This will return all names that start with A, B or C and have anything after that.
SQL Server 70-461 03-01
What form does a predicate need to be in for the query to filter efficiently?
A search argument
SQL Server 70-461 03-01
What does not work when trying to include NULLS in a WHERE clause?
- Region=NULL
- Two nulls are not considered equal to each other since null is an unknown value.
- Region <>’WA’ will not work either because it will not include regions that are NULL.
SQL Server 70-461 03-01
What does it mean for a WHERE clause to evaluate to false?
The predicate evaluates to false and nulls are not included.
SQL Server 70-461 03-01
How does the character range wildcard work?
- Between square brackets, [ ], indicate a range of letters or numbers. Examples, [A-E] or [0-9].
- Example: ‘[A-E]%’ would indicate that the first letter of the name start with A, B, C, D or E and have anything after that.
SQL Server 70-461 03-01
How does the NOT IN character list or range wild card work?
It works just like the character list and character range wildcards except you put a ^ after the left bracket to indicate not in the list or range.
SQL Server 70-461 03-01
What is a predicate?
An expression that is either true or false.
SQL Server 70-461 03-01
How can null values be included or excluded in a WHERE clause?
- Use the operator IS NULL or IS NOT NULL
- Example: Region IS NULL
SQL Server 70-461 03-01
When combining predicates in a WHERE clause, what order of precedence holds?
- NOT
- AND
- OR
Parenthesis actually trump all of these so if you use parenthesis you can make sure your statement is evaluated the way you want it to be.
SQL Server 70-461 03-01
What does it mean for a WHERE clause to evaluate to true?
The predicate expression is true and nulls are not included.
SQL Server 70-461 03-01
What does it mean for a WHERE clause to evaluate to true?
The predicate expression is true and nulls are not included.
SQL Server 70-461 03-01
How can you look for a character that is used as a wildcard?
- Decide on an escape character to use.
- One could use !. Put ! before the character that is considered a wildcard but that you want to search for. Then put ESCAPE ‘!’ after your wildcard expression so it knows you are using ! for your escape character.
- Example: col1 LIKE ‘!_%’ ESCAPE ‘!’ This looks for strings that start with an underscore.
SQL Server 70-461 03-01
When a string starts with a known prefix, why should you use LIKE to search for it?
Because LIKE ‘ABC%’ is considered a search argument. Using LEFT(col, 3)=’ABC’ is not considered a search argument and therefore wouldn’t be as efficient.
SQL Server 70-461 03-01
What three query clauses allow you to filter data based on predicates?
- ON
- HAVING
- WHERE
SQL Server 70-461 03-01
What are the types of wildcards to be used with LIKE?
- %
- _
- Character List
- Character Range
- NOT IN character list or range
SQL Server 70-461 03-01
What would be an example of filtering for a particular date using a search argument? What would not be a search argument?
Search Argument
- orderdate>=’20070201’ AND orderdate<’20070301’
Not a Search Argument
- YEAR(orderdate)=2007 AND MONTH(orderdate)=2
- This is not a search argument because functions are used on the column being searched.
SQL Server 70-461 03-01