WHERE Syntax Flashcards

1
Q

What is the WHERE statement syntax?

A

Column, Operator, and Value

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

When do you need to add quotes around a value in a WHERE statement?

A

For date values like ‘12/08/1991’ and Text Values like ‘Smith’

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

When do you not need to add quote values for a WHERE statement?

A

For number values like the number 72

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

How would you write a WHERE statement where you compare columns against each other?

A

WHERE ColumnName1 operator ColumnName2

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

List the WHERE operators?

A

= (equal), <> and != (not equal) > (greater than) < (less than) >= (greater than or equal to) <= (less than or equal)

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

What are two ways you can use parenthesis in a WHERE clause?

A

To prioritize order of search

EX: WHERE Country = ‘Brazil’
OR (Country = ‘USA’ AND State = ‘CA’)

And to group together search expressions

AB.Title=T.Name AND G.Name IN (‘Rock’,’Latin’) AND AT.NAME NOT LIKE ‘[G-Z]%’)
OR
(AT.Name. = ‘Iron Maiden’ AND G.Name = ‘Blues’)

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

What does NULL represent?

A

NULL represents a missing or unknown value in the WHERE statement

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

Why can’t use use operators to for the NULL value?

A

Operators do not work with NULL values because NULL signifies an unknown value, and any comparison with an unknown value is also unknown.

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

Is this statement valid, and why?

SELECT * FROM Employees WHERE Department = NULL

A

That statement was not valid because it used an operator.

Here’s an example of a valid statement:
SELECT * FROM Employees WHERE Department IS NULL;

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

What keyword would you use to search for values within a range?

A

The BETWEEN keyword

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

What datatypes can you use with the BETWEEN value AND value keyword?

A

Date/Time, Alphanumeric, and Numeric

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

What WHERE keyword would you use if you needed to filter several values?

A

Use the IN keyword.

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

What’s the usage for the LIKE keyword?

A

The LIKE keyword in the WHERE clause is used to search for a specified pattern in a column. It is typically used with string data types to find rows where the column values match a particular pattern.

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

How would you write the WHERE statement to find all Gmail emails in a list?

A

WHERE Email LIKE ‘%gmail%’

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

Why do wildcard expressions use apostrophes?

A

Wildcard patterns are enclosed in single quotes to indicate that they are string literals to be used with the LIKE keyword. For example ‘A’

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

Write the syntax for an IN statement if you were filtering your Country column to only results from the United States, Brazil, and Canada

A

WHERE Country IN (‘Brazil’,’United States’,’Sweden’)

17
Q

Why do we surround this information with apostrophes? When do we not surround information with apostrophes?

A

In SQL, when you’re specifying a list of values for the IN operator, each individual value needs to be enclosed in single quotes if it’s a string or a character data type. This is because SQL treats anything within single quotes as a string literal.

18
Q

What are some examples of non string data types?

A

Integer (INT)
Decimal (DECIMAL or NUMERIC)
Floating-point (FLOAT)
Date (DATE)
Time (TIME)
Datetime (DATETIME)
Timestamp (TIMESTAMP)
Binary (BINARY)
Varbinary (VARBINARY)

19
Q

Can you use a subquery as a value in an IN statement?

A

Yes! As long as it’s only evaluating only one column.

SELECT Customer ID, InvoiceID
FROM Invoice (table)
WHERE CustomerID IN (
SELECT CustomerID
FROM Customer
WHERE Country = ‘India’)

20
Q

What does the EXIST keyword do?

A

The EXIST keyword checks a subquery whether a record exists. It tells if you if a match is found or not. If a match is found it says TRUE. If it’s not found, it says FALSE.

21
Q

What’s considered best practice in a SELECT clause of a subquery?

A

It’s best practice to use SELECT *. because it simplifies the syntax and conveys the intention clearly: you’re interested only in whether rows exist, not in the values of specific columns.

22
Q

What is a correlated subquery

A

A correlated subquery is where the inner query references columns from the outer query. In other words, the inner query depends on the outer query for its execution.

23
Q

What does the NOT keyword do?

A

Returns everything else besides what you listed out. What you list is negated.