0% found this document useful (0 votes)
5 views11 pages

Databaselab 5

Uploaded by

210316
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)
5 views11 pages

Databaselab 5

Uploaded by

210316
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/ 11

AIR UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER

ENGINEERING

LAB TITLE: SQL wildcards and operators

Student Name: Muhammad Bilal, Agha Ammar Khan, Muneeb ur Rehman

Reg. No: 210316,210300,212106

LAB ASSESSMENT:

Excellent Good Average Satisfactory Unsatisfactory


Attributes
(5) (4) (3) (2) (1)

Ability to Conduct
Experiment

Ability to assimilate the


results

Effective use of lab


equipment and follows
the lab safety rules

Total Marks: Obtained Marks:

LAB REPORT ASSESSMENT:

Excellent Good Average Satisfactory Unsatisfactory


Attributes
(5) (4) (3) (2) (1)

Data presentation

Experimental results

Conclusion

Total Marks: Obtained Marks:

Date: 13/10/2024 Signature:


LAB#05
TITLE: SQL Wildcards and operators

Objectives:
To understand SQL wild cards and the operators used in SQL.

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
With SQL, the following wildcards can be used:

SQL Wildcards Examples:


We have the following "Persons" table:
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:

The result-set will look like this:

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:

The result-set will look like this:

Using the _ Wildcard:


Now we want to select the persons with a first name that starts with any character, followed
by "ove" from the "Persons" table.
We use the following SELECT statement:

The result-set will look like this:

Using the [charlist] Wildcard:


Now we want to select the persons with a last name that starts with "b" or "s" or "p" from
the "Persons" table.
We use the following SELECT statement:
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.
We use the following SELECT statement:

The result-set will look like this:

SQL IN Operator:
The IN operator allows you to specify multiple values in a WHERE clause.

SQL IN Syntax:

IN Operator Example:
The "Persons" table:

Now we want to select the persons with a last name equal to "Hansen" or "Pettersen" from
the table above.
We use the following SELECT statement:

The result-set will look like this:

SQL BETWEEN Operator:


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.

SQL BETWEEN Syntax:

BETWEEN Operator Example:


Now we want to select the persons with P_ID between 2 and 4" from the person table. We
use the following SELECT statement:

The result-set will look like this:

Note: The BETWEEN operator is treated differently in different databases.


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).
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.
Therefore: Check how your database treats the BETWEEN operator.

Example 2:
To display the persons outside the range in the previous example, use NOT BETWEEN:

The result-set will look like this:


Lab Tasks:

Code and Output:


Task2:
Code and Output:
Conclusion:
In conclusion, the lab on SQL Wildcards and Operators successfully demonstrated how these
tools can be used to perform more flexible and efficient queries in SQL databases. Through
practical examples, we explored the use of wildcards such as "%" and "_" to match patterns
within string data, enabling more dynamic search capabilities. Additionally, we gained hands-
on experience with key SQL operators, including comparison, logical, and arithmetic
operators, to filter, compare, and manipulate data. The lab enhanced our understanding of
how wildcards and operators can optimize query performance and data retrieval.

You might also like