3.1: Special operators and clauses Flashcards

1
Q

What is the purpose of the IN operator in SQL?

A

The IN operator is used in a WHERE clause to determine if a value matches one of several specified values.

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

This is an example of a query using which $ operator.

SELECT * 
FROM CountryLanguage 
WHERE Language IN ('Dutch', 'Kongo', 'Albanian');
A

This is an example of a query using the IN operator.

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

What does the BETWEEN operator do in SQL?

A

The BETWEEN operator checks if a value is within a specified range, inclusive of the minimum and maximum values.

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

How is the BETWEEN operator written in SQL?

A
  1. It is written as value BETWEEN minValue AND maxValue
  2. Equivalent to value >= minValue AND value <= maxValue.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the function of the LIKE operator in SQL?

A
  • % matches any number of characters.
    • Ex: LIKE 'L%t' matches “Lt”, “Lot”, “Lift”, and “Lol cat”.
  • _ matches exactly one character.
    • Ex: LIKE 'L_t' matches “Lot” and “Lit” but not “Lt” and “Loot”.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
SELECT * FROM CountryLanguage 
WHERE Language LIKE 'A%';

This example uses the $ operator.

A

This example is a query using the LIKE operator.

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

What does the DISTINCT clause do in SQL?

A

The DISTINCT clause returns only unique or distinct values from a column in a SELECT statement.

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

What would be the query result.

SELECT DISTINCT Language
 FROM CountryLanguage 
WHERE IsOfficial = 'F'
;
A
  • Spanish
  • Chinese
  • Nahuatl
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Explain the use of the ORDER BY clause in SQL.

A

The ORDER BY clause is used to sort the result set of a query by one or more columns in ascending or descending order.

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

How would you sort results (Language) in descending order by a specific column (CountryLanguage)?

Table CountryLanguage

A
  1. By using ORDER BY & DESC keyword, e.g.,
SELECT * 
FROM CountryLanguage 
ORDER BY Language DESC;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the effect of using NULL in the IN operator?

A
  1. Including NULL in the list with the IN operator will generally result in no matches
  2. Unless explicitly handled ; NULL is not considered equal to any value.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the syntax for the BETWEEN operator?

A

The syntax is written as: value BETWEEN minValue AND maxValue.

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

Can the LIKE operator handle case sensitivity?

A

By default, the LIKE operator performs case-insensitive matching unless followed by the BINARY keyword.

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

What is the result of a query using SELECT DISTINCT?

A

It returns only unique entries from the specified column, removing duplicates from the result set.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
SELECT * 
FROM CountryLanguage 
WHERE Language IN ('Dutch', 'Kongo', 'Albanian');

Explain the result

A
ABW   Dutch      T   5.3
AGO   Kongo      F   13.2
ALB   Albanian   T   97.9
  • The IN operator is used in a WHERE clause to determine if a value matches one of several values.
  • The SELECT statement in the figure below uses the IN operator to select only rows where the Language column has a Dutch, Kongo, or Albanian value.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Refer to the Country table

Explain the results

SELECT Name 
FROM Country 
WHERE Continent IN ('Asia', 'Europe', 'South America');
A

Afghanistan is in Asia, Albania, and Andorra are in Europe, and no countries are in South America.

17
Q

Refer to the Country table

Explain the results

SELECT Name 
FROM Country 
WHERE Code IN ('AGO', 'Aruba', 'Europe', NULL);
A

Angola’s Code is AGO, but other values listed are not used in the Code column.

18
Q

What is the alternatrive way BETWEEN

SELECT Name
FROM Employee
WHERE HireDate >= '2000-01-01' AND HireDate <= '2020-01-01';
A
SELECT Name
FROM Employee
WHERE HireDate BETWEEN '2000-01-01' AND '2020-01-01';
19
Q

Using the BETWEEN syntax

What are the alternative ways to determine if a value is between two other values

A
  1. value BETWEEN minValue AND maxValue
  2. value >= minValue AND value <= maxValue
20
Q

Using the LIKE syntax

Explain the result.

SELECT *
FROM CountryLanguage
WHERE CountryCode LIKE 'A_W';
A

Using A_W with the LIKE operator matches ‘A’, any character, then ‘W’.

21
Q

Using the LIKE syntax

Explain the results.

SELECT *
FROM CountryLanguage
WHERE Language LIKE 'A%n';
A

Using A%n with the LIKE operator matches ‘A’, any number of characters, then ‘n’.

22
Q
  1. If the answer is Maori
  2. What can the LIKE statement be depicted as?
A
SELECT Language
FROM CountryLanguage
WHERE Language LIKE 'M%i';
23
Q

What can the LIKE satement be depicted as?

  1. If the answer is Arawakan and Caribbean
SELECT Language 
FROM CountryLanguage 
WHERE Language '[$r$n]';
A

LIKE '%r%n'

Both languages have any number of characters before “r” and any number of characters before “n”.

24
Q

What can the LIKE satement be depicted as?

  1. If the answer is Chibcha
SELECT Language 
FROM CountryLanguage 
WHERE Language '[$cha]';
A

LIKE '%cha'

Only Chibcha ends with “cha”.

25
Q

What can the LIKE satement be depicted as?

  1. If the answer is Maori
SELECT Language 
FROM CountryLanguage 
WHERE Language LIKE '[$m_o$]';
A

LIKE '%m_o%'

Only Maori has an “m” followed by a single character and “o”.

26
Q

What can the LIKE satement be depicted as?

  1. If the answer is English
SELECT Language 
FROM CountryLanguage 
WHERE Language '[$E$]';
A

LIKE BINARY '%E%'

BINARY makes the pattern matching case-sensitive. Only English has a capital “E”.

27
Q

DISTINCT Clause

If the result set is listed; what can the query be depicted as?

ARG  Distrito Federal
BRA  São Paulo
KEN  Central
NPL  Central
VEN  Distrito Federal
A
SELECT DISTINCT CountryCode, District 
FROM City;

The ‘NPL’ and ‘Central’ row only appears once in the result set because of the DISTINCT clause.

28
Q

DISTINCT Clause

If the result set is listed; what can the query be depicted as?

ARG  Distrito Federal
BRA  São Paulo
KEN  Central
NPL  Central
NPL  Central
VEN  Distrito Federal
A
SELECT CountryCode, District 
FROM City;

Every country code and district appears in the result set because the query does not use a DISTINCT clause.

29
Q

DISTINCT Clause

If the result set is listed; what can the query be depicted as?

Distrito Federal
São Paulo
Central
A
SELECT DISTINCT District 
FROM City;

Duplicate ‘Central’ and ‘Distrito Federal’ rows do not appear in the result set because of the DISTINCT clause.

30
Q

If the result set is listed; what can the query be depicted as?

Distrito Federal
São Paulo
Central
Central
Central
Distrito Federal
A
SELECT District
FROM City;

Every district appears in the result set because the query does not use a DISTINCT clause.

31
Q

ORDER BY Clause

If the result set is listed; what can the query be depicted as?

A
SELECT Name, District, Population 
FROM City 
ORDER BY Name;

The rows are in ascending order by Name. Text is ordered in a case-insensitive fashion

32
Q

ORDER BY Clause

If the result set is listed; what can the query be depicted as?

A
SELECT Name, District, Population 
FROM City 
ORDER BY Population;

The Population column is in ascending order.

33
Q

ORDER BY Clause

If the result set is listed; what can the query be depicted as?

A
SELECT Name, District, Population 
FROM City 
ORDER BY Disctrict DESC;

The rows are ordered by District in descending order.

35
Q

ORDER BY Clause

If the result set is listed; what can the query be depicted as?

A
SELECT Name, District, Population 
FROM City 
ORDER BY District, Population;

Within each District grouping, the rows are then ordered by Population in ascending order.