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.
Give an example of a query using the IN
operator.
SELECT * FROM CountryLanguage WHERE Language IN ('Dutch', 'Kongo', 'Albanian');
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
, which is 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”. -
_
matches exactly one character. Ex: LIKE'L_t'
matches “Lot” and “Lit” but not “Lt” and “Loot”.
Provide an example of a query using the LIKE
operator.
SELECT * FROM CountryLanguage WHERE Language LIKE 'A%';
What does the DISTINCT
clause do in SQL?
The DISTINCT
clause returns only unique or distinct values from a column in a SELECT
statement.
Write a query that uses the DISTINCT
clause.
SELECT DISTINCT Language FROM CountryLanguage WHERE IsOfficial = 'F' ;
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 in descending order by a specific column?
- By using the
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 the IN
operator will generally result in no matches unless explicitly handled, as 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.