You are on page 1of 23

Introduction to MySQL WHERE clause

The search condition is a combination of one or


more expressions using the logical operator AND, OR SYNTAX
and NOT.
SELECT
In MySQL, a predicate is a Boolean expression that
select_list
evaluates to TRUE, FALSE, or UNKNOWN.
FROM
table_name
The SELECT statement will include any row that
WHERE
satisfies the search condition in the result set.
search_condition;
Besides the SELECT statement, you can use the
WHERE clause in the UPDATE or DELETE statement to
specify which rows to update or delete.
The AND operator evaluates to TRUE only if both expressions evaluate to TRUE. Therefore, the query returns rows whose values in the jobTitle column is Sales Rep and officeCode is 1.

1) USING MYSQL WHERE CLAUSE WITH


EQUALITY OPERATOR EXAMPLE
Example : Write an SQL query that will display the items or products
with Price equals to Php 20.00.
2) USING MYSQL WHERE CLAUSE WITH THE
AND OPERATOR
Example : Write an SQL query that will display the items or products
with Price equals to Php 15.00. and Icode = 105

In this example, the expression in the WHERE clause uses the


AND operator to combine two conditions:
The AND operator evaluates to TRUE only if both expressions evaluate to TRUE.
Therefore, the query returns rows whose values in the Price column is 15 and ICode is
105.
3) USING MYSQL WHERE CLAUSE WITH OR
OPERATOR
Example : Write an SQL query that will display the items or products
with Price equals to Php 15.00. and Icode = 101

The OR operator evaluates to TRUE only if one of the


expressions evaluates to TRUE:
4) USING MYSQL WHERE CLAUSE WITH THE
BETWEEN OPERATOR EXAMPLE
Example : Write an SQL query that will display the items or products
with Price range from 1 peso to 20 pesos.

The BETWEEN operator selects values within a given range. The values
can be numbers, text, or dates.
The BETWEEN operator is inclusive: begin and end values are included.
The BETWEEN operator returns TRUE if a value is in a
range of values:
5) USING MYSQL WHERE CLAUSE WITH THE
LIKE OPERATOR EXAMPLE
Example : Write an SQL query that will display the items or products
with ‘er’ at last

The LIKE operator evaluates to TRUE if a value matches a specified pattern.

To form a pattern, you use the % and _ wildcards. The % wildcard matches any string of zero or
more characters while the _ wildcard matches any single character.
6) USING MYSQL WHERE CLAUSE WITH THE IN
OPERATOR EXAMPLE

The IN operator returns TRUE if a value matches any value in a list.


7) USING MYSQL WHERE CLAUSE WITH THE IS
NULL OPERATOR
To check if a value is NULL or not, you use the IS NULL
operator, not the equal operator (=). The IS NULL operator
returns TRUE if a value is NULL.

In the database world, NULL is a marker that indicates


that a value is missing or unknown. And NULL is not
equivalent to the number 0 or an empty string.
8) USING MYSQL WHERE CLAUSE WITH
COMPARISON OPERATORS

Syntax:
Select from table_name
where column_name
operator value
INTRODUCTION TO THE MYSQL
ORDER BY CLAUSE
When you use the SELECT statement to query data
from a table, the order of rows in the result set is
unspecified. To sort the rows in the result set, you add
the ORDER BY clause to the SELECT statement.
In this syntax, you specify the one or more columns
that you want to sort after the ORDER BY clause
A) Using MySQL ORDER BY clause to sort the
result set by one column example
B) Using MySQL ORDER BY clause to sort the
result set by multiple columns example
If you want to sort the customers by the last name in descending order and then
by the first name in ascending order, you specify both DESC and ASC in these
respective columns as follows:
C) Using MySQL ORDER BY clause to sort a
result set by an expression example
The following query selects the order line items from the item1 table. It calculates the
subtotal for each line item and sorts the result set based on the subtotal.

Before we do that one let us add first column Name Quantity and insert the following
data
An employer creates a record for his employees whose names and details are listed below
EmployeeID EmployeeName Address Salary
1001 Manny Atienza Bulacan 3000
1002 Ferdinand Duterte Manila 2000
1003 Eleonor Pangilinan Naga 4000
1004 Lenny Moreno Makati 5000

a. After several months, he wants to find out whose employee has a salary of 4,000 above.
b. He wants also to know whose employee that is in between Manny Atienza and Lenny Moreno
c. After finding such records, he wants also to know whose employee that’s is in between 1002 and
1004 and employee id that is not in 1001.
After a few months another batch of employee was hired:
EmployeeID EmployeeName Address Salary
1005 Sailor Mooned Cotabato 10000
1006 Dorathy Explorer Roxas 12000
1007 Dorae Moon Gensan 7000

d. Since there were so many records the employer wants to know those names start with the letter D
e. Also, those names ended with “ny”.
c. He wants also to know whose records with the “A” character on them.
d. Lastly find an address that has 6 characters of length

You might also like