3.1: Special operators and clauses Flashcards
What is the purpose of the IN
operator in SQL?
The IN
operator is used in a WHERE
clause to determine if a value matches one of several specified values.
This is an example of a query using which $
operator.
SELECT * FROM CountryLanguage WHERE Language IN ('Dutch', 'Kongo', 'Albanian');
This is an example of a query using the IN
operator.
What does the BETWEEN
operator do in SQL?
The BETWEEN
operator checks if a value is within a specified range, inclusive of the minimum and maximum values.
How is the BETWEEN
operator written in SQL?
- It is written as value
BETWEEN minValue AND maxValue
- Equivalent to
value >= minValue AND value <= maxValue
.
What is the function of the LIKE
operator in SQL?
-
%
matches any number of characters.- Ex: LIKE
'L%t'
matches “Lt”, “Lot”, “Lift”, and “Lol cat”.
- Ex: LIKE
-
_
matches exactly one character.- Ex: LIKE
'L_t'
matches “Lot” and “Lit” but not “Lt” and “Loot”.
- Ex: LIKE
SELECT * FROM CountryLanguage WHERE Language LIKE 'A%';
This example uses the $
operator.
This example is a query using the LIKE
operator.
What does the DISTINCT
clause do in SQL?
The DISTINCT
clause returns only unique or distinct values from a column in a SELECT
statement.
What would be the query result.
SELECT DISTINCT Language FROM CountryLanguage WHERE IsOfficial = 'F' ;
- Spanish
- Chinese
- Nahuatl
Explain the use of the ORDER BY
clause in SQL.
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 would you sort results (Language) in descending order by a specific column (CountryLanguage)?
Table CountryLanguage
- By using
ORDER BY
&DESC
keyword, e.g.,
SELECT * FROM CountryLanguage ORDER BY Language DESC;
What is the effect of using NULL
in the IN
operator?
- Including
NULL
in the list with theIN
operator will generally result in no matches - Unless explicitly handled ;
NULL
is not considered equal to any value.
What is the syntax for the BETWEEN operator?
The syntax is written as: value BETWEEN minValue AND maxValue.
Can the LIKE
operator handle case sensitivity?
By default, the LIKE
operator performs case-insensitive matching unless followed by the BINARY
keyword.
What is the result of a query using SELECT DISTINCT
?
It returns only unique entries from the specified column, removing duplicates from the result set.
SELECT * FROM CountryLanguage WHERE Language IN ('Dutch', 'Kongo', 'Albanian');
Explain the result
ABW Dutch T 5.3 AGO Kongo F 13.2 ALB Albanian T 97.9
- The
IN
operator is used in aWHERE
clause to determine if a value matches one of several values. - The
SELECT
statement in the figure below uses theIN
operator to select only rows where theLanguage
column has a Dutch, Kongo, or Albanian value.
Refer to the Country
table
Explain the results
SELECT Name FROM Country WHERE Continent IN ('Asia', 'Europe', 'South America');
Afghanistan is in Asia, Albania, and Andorra are in Europe, and no countries are in South America.
Refer to the Country
table
Explain the results
SELECT Name FROM Country WHERE Code IN ('AGO', 'Aruba', 'Europe', NULL);
Angola’s Code is AGO
, but other values listed are not used in the Code
column.
What is the alternatrive way BETWEEN
SELECT Name FROM Employee WHERE HireDate >= '2000-01-01' AND HireDate <= '2020-01-01';
SELECT Name FROM Employee WHERE HireDate BETWEEN '2000-01-01' AND '2020-01-01';
Using the BETWEEN
syntax
What are the alternative ways to determine if a value is between two other values
value BETWEEN minValue AND maxValue
value >= minValue AND value <= maxValue
Using the LIKE
syntax
Explain the result.
SELECT * FROM CountryLanguage WHERE CountryCode LIKE 'A_W';
Using A_W
with the LIKE operator matches ‘A’, any character, then ‘W’.
Using the LIKE
syntax
Explain the results.
SELECT * FROM CountryLanguage WHERE Language LIKE 'A%n';
Using A%n
with the LIKE
operator matches ‘A’, any number of characters, then ‘n’.
- If the answer is
Maori
- What can the
LIKE
statement be depicted as?
SELECT Language FROM CountryLanguage WHERE Language LIKE 'M%i';
What can the LIKE
satement be depicted as?
- If the answer is
Arawakan
andCaribbean
SELECT Language FROM CountryLanguage WHERE Language '[$r$n]';
LIKE '%r%n'
Both languages have any number of characters before “r” and any number of characters before “n”.
What can the LIKE
satement be depicted as?
- If the answer is
Chibcha
SELECT Language FROM CountryLanguage WHERE Language '[$cha]';
LIKE '%cha'
Only Chibcha ends with “cha”.
What can the LIKE
satement be depicted as?
- If the answer is
Maori
SELECT Language FROM CountryLanguage WHERE Language LIKE '[$m_o$]';
LIKE '%m_o%'
Only Maori has an “m” followed by a single character and “o”.
What can the LIKE
satement be depicted as?
- If the answer is
English
SELECT Language FROM CountryLanguage WHERE Language '[$E$]';
LIKE BINARY '%E%'
BINARY makes the pattern matching case-sensitive. Only English has a capital “E”.
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
SELECT DISTINCT CountryCode, District FROM City;
The ‘NPL’ and ‘Central’ row only appears once in the result set because of the DISTINCT clause.
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
SELECT CountryCode, District FROM City;
Every country code and district appears in the result set because the query does not use a DISTINCT clause.
DISTINCT
Clause
If the result set is listed; what can the query be depicted as?
Distrito Federal São Paulo Central
SELECT DISTINCT District FROM City;
Duplicate ‘Central’ and ‘Distrito Federal’ rows do not appear in the result set because of the DISTINCT clause.
If the result set is listed; what can the query be depicted as?
Distrito Federal São Paulo Central Central Central Distrito Federal
SELECT District FROM City;
Every district appears in the result set because the query does not use a DISTINCT clause.
ORDER BY
Clause
If the result set is listed; what can the query be depicted as?
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
ORDER BY
Clause
If the result set is listed; what can the query be depicted as?
SELECT Name, District, Population FROM City ORDER BY Population;
The Population column is in ascending order.
ORDER BY
Clause
If the result set is listed; what can the query be depicted as?
SELECT Name, District, Population FROM City ORDER BY Disctrict DESC;
The rows are ordered by District in descending order.
ORDER BY
Clause
If the result set is listed; what can the query be depicted as?
SELECT Name, District, Population FROM City ORDER BY District, Population;
Within each District grouping, the rows are then ordered by Population in ascending order.