0% found this document useful (0 votes)
13 views8 pages

Dslab 07 2

Uploaded by

itxirtza1122
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views8 pages

Dslab 07 2

Uploaded by

itxirtza1122
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

University of Chakwal

Database Systems

Lab 07
Using SQL wild cards, operators

Engr. Samina Bilquees


University of Chakwal 1

SQL Wildcards
SQL wildcards can be used when searching for data in a database.

ORACLE: LIKE CONDITION


DESCRIPTION
The Oracle LIKE condition allows wildcards to be used in the WHERE clause of a
SELECT, INSERT, UPDATE, or DELETE statement. This allows you to perform pattern
matching.

SYNTAX
The syntax for the Oracle LIKE Condition is:

expression LIKE pattern


Parameters or Arguments

expression is a character expression such as a column or field.

pattern is a character expression that contains pattern matching. The patterns that you can
choose from are:

The LIKE operator is used to search for a specified pattern in a column.

Wildcard Explanation

% Allows you to match any string of any length (including zero length)

_ Allows you to match on a single character

We have the following "Persons" table:

P_Id LastName FirstName Address City

1 Hansen Ola Timoteivn 10 Sandnes

2 Svendson Tove Borgvn 23 Sandnes

3 Pettersen Kari Storgt 20 Stavanger


University of Chakwal 2

Using the % Wildcard

Now we want to select the persons living in a city that starts with "sa" from the "Persons" table.

We use the following SELECT statement:

SELECT * FROM Persons


WHERE City LIKE 'sa%'

The result-set will look like this:

P_Id LastName FirstName Address City

1 Hansen Ola Timoteivn 10 Sandnes

2 Svendson Tove Borgvn 23 Sandnes

Next, we want to select the persons living in a city that contains the pattern "nes" from the "Persons"
table.

We use the following SELECT statement:

SELECT * FROM Persons


WHERE City LIKE '%nes%'

The result-set will look like this:

P_Id LastName FirstName Address City

1 Hansen Ola Timoteivn 10 Sandnes

2 Svendson Tove Borgvn 23 Sandnes

Now we want to select the persons with a first name that starts with any character, followed by "la"
from the "Persons" table.

We use the following SELECT statement:

SELECT * FROM Persons


WHERE FirstName LIKE '_la'

The result-set will look like this:


University of Chakwal 3
P_Id LastName FirstName Address City

1 Hansen Ola Timoteivn 10 Sandnes

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.

We use the following SELECT statement:

SELECT * FROM Persons


WHERE LastName LIKE 'S_end_on'

The result-set will look like this:

P_Id LastName FirstName Address City

2 Svendson Tove Borgvn 23 Sandnes

ORACLE: NOT CONDITION


DESCRIPTION
The Oracle NOT Condition (also called the NOT Operator) is used to negate a condition
in a SELECT, INSERT, UPDATE, or DELETE statement.

SYNTAX
The syntax for the Oracle NOT Condition is:

NOT condition
Parameters or Arguments

condition is the condition to negate.

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

EXAMPLE - COMBINE WITH IN CONDITION


The Oracle NOT condition can be combined with the IN Condition.

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.

EXAMPLE - COMBINE WITH IS NULL CONDITION


The Oracle NOT condition can also be combined with the IS NULL Condition.

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.

EXAMPLE - COMBINE WITH LIKE CONDITION


The Oracle NOT condition can also be combined with the LIKE Condition.

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

EXAMPLE - COMBINE WITH BETWEEN CONDITION


The Oracle NOT condition can also be combined with the BETWEEN Condition. Here
is an example of how you would combine the NOT Operator with the BETWEEN
Condition.

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;

Example - Combine With EXISTS condition

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".

CREATE TABLE employees


( employee_number number(10) not null,
employee_name varchar2(50) not null,
salary number(6),
CONSTRAINT employees_pk PRIMARY KEY (employee_number)
);

INSERT INTO employees (employee_number, employee_name, salary)


VALUES (1001, 'John Smith', 62000);

INSERT INTO employees (employee_number, employee_name, salary)


VALUES (1002, 'Jane Anderson', 57500);

INSERT INTO employees (employee_number, employee_name, salary)


VALUES (1003, 'Brad Everest', 71000);

INSERT INTO employees (employee_number, employee_name, salary)


VALUES (1004, 'Jack Horvath', 42000);

Tell the difference between the Query A and Query B.

Query A: List product name, finish, and standard price for all desks and all tables that cost
more than $300 in the Product table.

SELECT productdiscription, productfinish, productstandardprice


FROM product_table
WHERE productdiscription LIKE '%Desk'
OR productdiscription LIKE '%Table'
AND productstandardprice > 300;
University of Chakwal 7
If we had wanted to return only desks and tables costing more than $300, we should have
put parentheses after the WHERE and before the AND, as shown in Query B below. The
result is all desks and tables with a standard price of more than $300. The walnut computer
desk has a standard price of $250 and is not included.

Query B: List product name, finish, and unit price for all desks and tables in the PRODUCT
table that cost more than $300.

SELECT productdiscription, productfinish, productstandardprice


FROM product_table
WHERE (productdiscription LIKE '%Desk'
OR productdiscription LIKE '%Table')
AND productstandardprice > 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".

SELECT productid,productdiscription, productfinish, productstandardprice


FROM product_table
WHERE productid LIKE '%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%'

You might also like