Dslab 07 2
Dslab 07 2
Database Systems
Lab 07
Using SQL wild cards, operators
SQL Wildcards
SQL wildcards can be used when searching for data in a database.
SYNTAX
The syntax for the Oracle LIKE Condition is:
pattern is a character expression that contains pattern matching. The patterns that you can
choose from are:
Wildcard Explanation
% Allows you to match any string of any length (including zero length)
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.
Next, we want to select the persons with a last name that starts with "S", followed by any character,
followed by "end", followed by any character, followed by "on" from the "Persons" table.
SYNTAX
The syntax for the Oracle NOT Condition is:
NOT condition
Parameters or Arguments
NOTE
The Oracle NOT condition requires that the opposite of the condition be must be met for
the record to be included in the result set.
University of Chakwal 4
For example:
SELECT *
FROM customers
WHERE customer_name NOT IN ( 'IBM', 'Hewlett Packard', 'Microsoft' );
This Oracle NOT example would return all rows from the customers table where the
customer_name is not IBM, Hewlett Packard, or Microsoft.
For example,
SELECT *
FROM customers
WHERE last_name IS NOT NULL;
This Oracle NOT example would return all records from the customers table where the
last_name does not contain a NULL value.
For example:
SELECT customer_name
FROM customers
WHERE customer_name NOT LIKE 'S%';
By placing the Oracle NOT Operator in front of the LIKE condition, you are able to
retrieve all customers whose customer_name does not start with 'S'.
University of Chakwal 5
For example:
SELECT *
FROM customers
WHERE customer_id NOT BETWEEN 4000 AND 4100;
This Oracle NOT example would return all rows where the customer_id was NOT
between 4000 and 4100, inclusive. It would be equivalent to the following Oracle
SELECT statement:
SELECT *
FROM customers
WHERE customer_id < 4000
OR customer_id > 4100;
The Oracle NOT condition can also be combined with the Oracle EXISTS Condition.
For example,
SELECT *
FROM suppliers
WHERE NOT EXISTS (SELECT *
FROM orders
WHERE suppliers.supplier_id = orders.supplier_id);
This Oracle NOT example would return all records from the suppliers table where there
are no records in the orders table for the given supplier_id.
University of Chakwal 6
TASKS:
Based on the employees table populated with the following data, find all records whose
employee_name ends with the letter "h".
Query A: List product name, finish, and standard price for all desks and all tables that cost
more than $300 in the Product table.
Query B: List product name, finish, and unit price for all desks and tables in the PRODUCT
table that cost more than $300.
The results follow. Only products with unit price greater than $300 are included.
1) Write SQL SELECT statement that uses the SQL LIKE condition to return the records in
PRODUCT_T table whose ProductFinish ends with the letter "h".
SELECT productid,productdiscription, productfinish, productstandardprice
FROM product_table
WHERE productid LIKE '%h'
2) Write SQL SELECT statement that uses the SQL LIKE condition to return the records in
PRODUCT_T table whose ProductFinish contains the letter "s".
3) Write SQL SELECT statement that uses the SQL LIKE condition to return the records in
PRODUCT_T table whose ProductID istarts with "1".
SELECT productid,productdiscription, productfinish, productstandardprice
FROM product_table
WHERE productid LIKE '1%'