DBMS Lab # 4
DBMS Lab # 4
[Type here]
LAB # 04
SQL Wildcards & Operators
Lab Objective:
SQL Wildcards
SQL wildcards can substitute for one or more characters when searching for data in a database.
Wildcard Description
or
[!charlist]
Lab Instructor: Engr. Shahid Ali Bhutta SQL Wildcards & Opperators
Using the % Wildcard
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.
Lab Instructor: Engr. Shahid Ali Bhutta SQL Wildcards & Opperators
The result-set will look like this:
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.
Lab Instructor: Engr. Shahid Ali Bhutta SQL Wildcards & Opperators
The result-set will look like this:
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.
SQL IN Operator
SQL IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...)
Lab Instructor: Engr. Shahid Ali Bhutta SQL Wildcards & Opperators
IN Operator Example
Now we want to select the persons with a last name equal to "Hansen" or "Pettersen" from the
table above.
Lab Instructor: Engr. Shahid Ali Bhutta SQL Wildcards & Opperators
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 P_ID betweek 2 and 4" from the persons table.
In some databases, persons with the P_ID of 2 and 4 will not be listed, because the BETWEEN
operator only selects fields that are between and excluding the test values).
In other databases, persons with id 2 and 4 will be listed, because the BETWEEN operator
selects fields that are between and including the test values).
Lab Instructor: Engr. Shahid Ali Bhutta SQL Wildcards & Opperators
And in other databases, persons with id 2 will be listed, but id=4 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:
The UNION operator is used to combine the result-set of two or more SELECT statements.
Notice that each SELECT statement within the UNION must have the same number of columns.
The columns must also have similar data types. Also, the columns in each SELECT statement
must be in the same order.
Lab Instructor: Engr. Shahid Ali Bhutta SQL Wildcards & Opperators
SQL UNION Syntax
Note: The UNION operator selects only distinct values by default. To allow duplicate values, use
UNION ALL.
PS: The column names in the result-set of a UNION are always equal to the column names in the
first SELECT statement in the UNION.
"Employees_Norway":
E_ID E_Name
01 Hansen, Ola
02 Svendson, Tove
03 Svendson, Stephen
04 Pettersen, Kari
"Employees_USA":
E_ID E_Name
01 Turner, Sally
02 Kent, Clark
03 Svendson, Stephen
04 Scott, Stephen
Lab Instructor: Engr. Shahid Ali Bhutta SQL Wildcards & Opperators
Now we want to list all the different employees in Norway and USA.
E_Name
Hansen, Ola
Svendson, Tove
Svendson, Stephen
Pettersen, Kari
Turner, Sally
Kent, Clark
Scott, Stephen
Note: This command cannot be used to list all employees in Norway and USA. In the example
above we have two employees with equal names, and only one of them will be listed. The
UNION command selects only distinct values.
Result
E_Name
Hansen, Ola
Svendson, Tove
Svendson, Stephen
Pettersen, Kari
Turner, Sally
Kent, Clark
Svendson, Stephen
Scott, Stephen
Lab Instructor: Engr. Shahid Ali Bhutta SQL Wildcards & Opperators
Lab Tasks:
• Write an SQL statement that selects all Customers with a Country starting with the letter
“s”.
• Write an SQL statement that selects all Customers with a Contact Name ending with the
letter “s”.
• Write an SQL statement that selects all Customers with a City containing the pattern
“ndo”.
• Write an SQL statement that selects all Customers with a City not containing the pattern
“ndo”.
• 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.
Lab Instructor: Engr. Shahid Ali Bhutta SQL Wildcards & Opperators
2. Consider the following table “Products”
• 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 an SQL statement that selects all products with a price from 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'.
Lab Instructor: Engr. Shahid Ali Bhutta SQL Wildcards & Opperators