Filtering, Sorting, and Calculating Data with SQL Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Why filter?

A
  1. Reduce the number of records you retrieve
  2. Increase query performance
  3. Reduce the strain on the client application

SQL for Data Science Week 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

WHERE clause syntax

A
SELECT column_name, column_name
FROM table_name
WHERE column_name operator value

SQL for Data Science Week 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

WHERE clause operators

A
  1. =
  2. <>
  3. >
  4. <
  5. > =
  6. <=
  7. BETWEEN
  8. IS NULL

SQL for Data Science Week 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Does the column used in the WHERE clause need to be in the SELECT clause?

N

A

No

SQL for Data Science Week 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you find nulls?

A

WHERE ProductName IS NULL

SQL for Data Science Week 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Syntax for IN

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Benefit of using IN over OR

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which executes faster, IN or OR?

A

IN

SQL for Data Science Week 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What do you need to watch out for when using OR and AND?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Operator order of precedence in SQLite

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you use the WHERE clause to exclude values you don’t want?

A
  • 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What data type can wildcards be used on?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What special character do you use for wildcards when using LIKE?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How does a wildcard treat a null value when using LIKE?

A

It will not match with null values since NULL has no value

SQL for Data Science Week 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What special character matches just a single character when using LIKE?

A

The underscore _

SQL for Data Science Week 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What special characters specify a set of characters in a specific location when using LIKE?

A

Brackets []

This does not work on all database systems

SQL for Data Science Week 2

16
Q

Downsides of using wildcards?

A
  1. Takes longer to run
  2. =, <, =<, etc. will be faster
  3. Wildcards at the end of the string will take longer to run