Lab # 05 Implementation of SQL Wildcards & Operators
Lab # 05 Implementation of SQL Wildcards & Operators
LAB # 05
Implementation of SQL
Wildcards & Operators
Lab Objective:
SQL Wildcards
SQL wildcards can be used when searching for data in a database.
SQL wildcards can substitute for one or more characters when searching for data in a
database. SQL wildcards must be used with the SQL LIKE operator.
Wildcard Description
or
[!charlist]
Now we want to select the persons living in a city that starts with "sa" from the "Persons" table.
Next, we want to select the persons living in a city that contains the pattern "nes" from the
"Persons" table.
Now we want to select the persons with a first name that starts with any character, followed
by "la" from the "Persons" table.
Now we want to select the persons with a last name that starts with "b" or "s" or "p" from
the "Persons" table.
SELECT * FROM
Persons WHERE
LastName REGEXP
'^[bsp]'
Next, we want to select the persons with a last name that do not start with "b" or "s" or "p" from
the "Persons" table.
SELECT * FROM
Persons WHERE
LastName REGEXP
'^[^bsp]%'
SQL IN Operator
SQL IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...)
IN Operator Example
Now we want to select the persons with a last name equal to "Hansen" or "Pettersen" from the
table above.
SQL BETWEENOperator
The BETWEEN operator is used in a WHERE clause to select a range of data between two
values.
The BETWEEN operator selects a range of data between two values. The values can be
numbers, text, or dates.
SELECT column_name(s)
FROM table_name
WHERE column_name
BETWEEN value1 AND value2
Now we want to select the persons with a last name alphabetically between "Hansen"
and "Pettersen" from the table above.
In some databases, persons with the LastName of "Hansen" or "Pettersen" will not be
listed, because the BETWEEN operator only selects fields that are between and excluding
the test values).
In other databases, persons with the LastName of "Hansen" or "Pettersen" will be listed,
because the BETWEEN operator selects fields that are between and including the test values).
And in other databases, persons with the LastName of "Hansen" will be listed, but "Pettersen"
will not be listed (like the example above), because the BETWEEN operator selects fields
between the test values, including the first test value and excluding the last test value.
Example 2
To display the persons outside the range in the previous example, use NOT BETWEEN:
Lab Tasks:
Write an SQL statement that selects all Customers with a City starting with the letter “s”.
Write an SQL statement that selects all Customers with a City ending with the letter “s”.
Write an SQL statement that selects all Customers with a City containing the pattern
“land”.
Write an SQL statement that selects all Customers with a City not containing the pattern
“land”.
Write an SQL statement that selects the two first Customers from table who belong to
“Germany” or “Sweden”.
Write an SQL statement that selects all Customers with a City of "Paris" or "London"
without using ‘OR’ operator.
Write queries to extract the following outputs and show your result.
a):
b):
c):
d):
Write an SQL statement that selects all products with a price from 10 to 20.
Write an SQL statement that selects all products with a price from 20 to 30.
Write anSQL statement that selects all products with a pricefrom 10 to 22 but
products with a CategoryIDof 1,2, or 3 should not be displayed.
Write an SQL statement that selects all products with a ProductName beginning with
any of the letter not between 'C' and 'M'.