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.