Lab 04 - DML AND OR Operators
Lab 04 - DML AND OR Operators
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.
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?
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 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:
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