0% found this document useful (0 votes)
15 views

Lab 04 - DML AND OR Operators

The document discusses MySQL operators like AND, OR and NOT that can be used with the WHERE clause to filter records based on multiple conditions. It also covers other clauses and operators like LIKE, ORDER BY, LIMIT, SUM and GROUP BY along with examples of using each.

Uploaded by

Mushaim Aftab
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lab 04 - DML AND OR Operators

The document discusses MySQL operators like AND, OR and NOT that can be used with the WHERE clause to filter records based on multiple conditions. It also covers other clauses and operators like LIKE, ORDER BY, LIMIT, SUM and GROUP BY along with examples of using each.

Uploaded by

Mushaim Aftab
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Database Systems Lab

Lab Manual (Lab 04)

Topic: DML - AND & OR Operators with Different Clauses


Instructor: Mr. Masab Akram (Lab Engineer)

Session: Spring 2024


School of Systems and Technology
UMT Lahore Pakistan

Page 1
MySQL AND, OR and NOT Operators
The WHERE clause can be combined with AND, OR, and NOT operators.

The AND and OR operators are used to filter records based on more than one
condition:

• The AND operator displays a record if all the conditions separated by AND are
TRUE.
• The OR operator displays a record if any of the conditions separated by OR is
TRUE.

The NOT operator displays a record if the condition(s) is NOT TRUE.

AND Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
Example:

OR Syntax
Page 2
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
Example:

NOT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
Example:

Page 3
More Examples:
Write a SQL query to show those customer id, name, city, and ammount
having amount greater than or equal to 500 and amount less than or equal
to 1000 and customerid is greater than 5?

Write a SQL query to show those customer id, name, city, and ammount
having amount greater than or equal to 500 and amount less than or equal
to 1000 and customerid is greater than 5 and less than 10?

Write a SQL query to show those customer id, name, city, and ammount
having amount greater than or equal to 500 and amount less than or equal
to 1000 and customerid is greater than 5 or either less than 10?

Page 4
Write a SQL query to show those customer records having country Pakistan and
ammount is not 1000 or either their customerid is 2?

Write a query to update country to 'Pak' having customerid is 1 or having customerid


greater than 7 and whose city is not Lahore?

Page 5
Write a query to Selects all fields from "Customer" where country is "Pakistan"
AND city must be "Lahore" OR "Kasur".

Page 6
Page 7
Products Table:

Page 8
The MySQL LIKE Operator
The LIKE operator is used in a WHERE clause to search for a specified pattern in a
column.

There are two wildcards often used in conjunction with the LIKE operator:

• The percent sign (%) represents zero, one, or multiple characters


• The underscore sign (_) represents one, single character

The percent sign and the underscore can also be used in combinations!

LIKE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;

Page 9
Query: The following SQL statement selects all customers with a CustomerName
starting with "a":

The following SQL statement selects all customers with a CustomerName ending
with "r":

The following SQL statement selects all customers with a CustomerName that
have "r" in any position:

Page 10
The following SQL statement selects all customers with a CustomerName that
have "a" in the second position:

Select Statement with Order by clause


• The order by clause is used to sort the rows. The process of arranging data or records in a sequence is
called sorting. A sort can be ascending or descending.
• In ascending sort, the smallest value is placed at first position and largest value is placed at the last
position.
• In descending sort, the largest value is placed at first position and smallest value is placed at the last
position.
• SQL uses ASC keyword to specify ascending sort and DESC keyword for descending sort. If nothing is
specified, ASC is used as default.
• Syntax:
Select * from TableName Order by ColumnName;

Page 11
The SQL SELECT LIMIT Clause
• The SELECT limit clause is used to specify the number of records to return.
• The SELECT limit clause is useful on large tables with thousands of records. Returning a
large number of records can impact performance.
• Syntax:
• SELECT column_name(s) FROM table_name WHERE condition LIMIT number;

Page 12
Page 13
Page 14
Page 15
The following SQL statement finds the sum of the "Price" fields in the "Products" table:

Page 16
Page 17
Page 18
Page 19
Page 20
The following SQL statement lists the number of customers in each country. Only include
countries with more than 1 customers:

The following SQL statement lists the number of customers in each country, sorted high to
low (Only include countries with more than 1 customers):

END

Page 21

You might also like