Experiment No 3
Experiment No 3
Aim: To study the use of WHERE, SELECT DISTINCT, AND, OR, IN, BETWEEN, LIKE, LIMIT, IS NULL clause
Theory:
WHERE – learn how to use the WHERE clause to filter rows based on specified conditions.
SELECT DISTINCT – show you how to use the DISTINCT operator in the SELECT statement to eliminate
duplicate rows in a result set.
AND – introduce you to the AND operator to combine Boolean expressions to form a complex condition
for filtering data.
OR– introduce you to the OR operator and show you how to combine the OR operator with
the AND operator to filter data.
IN – show you how to use the IN operator in the WHERE clause to determine if a value matches any
value in a list or a subquery.
BETWEEN – show you how to query data based on a range using BETWEEN operator.
LIKE – provide you with technique to query data based on a specific pattern.
LIMIT – use LIMIT to constrain the number of rows returned by SELECT statement
The WHERE clause allows you to specify a search condition for the rows returned by a query. The
following shows the syntax of the WHERE clause:
11
In the SELECT statement, the WHERE clause is evaluated after the FROM clause and before
the SELECT clause.
The SELECT statement examines all rows of the employees table and selects only row whose value in
the jobTitle column is Sales Rep.
12
In this example, the expression in the WHERE clause uses the AND operator to combine two conditions
The OR operator evaluates to TRUE only if one of the expression evaluates to TRUE:
13
Therefore, the query returns any employee who has the job title Sales Rep or office code 1.
The following query finds employees who locate in offices whose office code is from 1 to 3:
14
6) Using MySQL WHERE clause with the IN operator example
The IN operator returns TRUE if a value matches any value in a list.
The following example uses the WHERE clause with the IN operator to find employees who locate in the
office with office code 1.
In the database world, NULL is a marker that indicates a piece of information is missing or unknown. It is
not equivalent to the number 0 or an empty string.
This statement uses the WHERE clause with the IS NULL operator to get the row whose value in
the reportsTo column is NULL:
15
8) Using MySQL WHERE clause with comparison operators
The following table shows the comparison operators that you can use to form the expression in
the WHERE clause
The following query uses the not equal to (<>) operator to find all employees who are not the Sales Rep
16
The following query finds employees whose office code is greater than 5
The following query returns employees with office code less than or equal 4 (<=4):
When querying data from a table, you may get duplicate rows. In order to remove these duplicate rows,
you use the DISTINCT clause in the SELECT statement.
Here is the syntax of the DISTINCT clause:
17
MySQL DISTINCT examples
First, query the last names of employees from the employees table using the
following SELECT statement
As clearly shown in the output, some employees have the same last name e.g, Bondur,Firrelli .
This statement uses the DISTINCT clause to select unique last names of from the employees table:
18
MySQL DISTINCT and NULL values
If a column has NULL values and you use the DISTINCT clause for that column, MySQL keeps only
one NULL value because DISTINCT treats all NULL values as the same value.
For example, in the customers table, we have many rows whose state column has NULL values.
When you use the DISTINCT clause to query the customers’ states, you will see unique states
and NULL as the following query
19
MySQL DISTINCT with multiple columns
You can use the DISTINCT clause with more than one column. In this case, MySQL uses the combination
of values in these columns to determine the uniqueness of the row in the result set.
For example, to get a unique combination of city and state from the customers table, you use the
following query:
20
Without the DISTINCT clause, you will get the duplicate combination of state and city as follows
21
You can achieve a similar result by using the DISTINCT clause:
The DISTINCT clause is a special case of the GROUP BY clause. The difference between DISTINCT clause
and GROUP BY clause is that the GROUP BY clause sorts the result set whereas the DISTINCT clause does
not
22
MySQL AND operator
The AND operator is a logical operator that combines two or more Boolean expressions and returns true
only if both expressions evaluate to true. The AND operator returns false if one of the two expressions
evaluate to false.
Here is the syntax of the AND operator:
The following table illustrates the results of the AND operator when combining true, false, and null
The AND operator is often used in the WHERE clause of the SELECT, UPDATE, DELETE statement to form
a condition. The AND operator is also used in join conditions of the INNER JOIN and LEFT JOIN clauses.
When evaluating an expression that has the AND operator, MySQL stops evaluating the remaining parts
of the expression whenever it can determine the result. This function is called short-circuit evaluation
23
MySQL AND operator examples
The following statement uses the AND operator to find customers who locate in California (CA), USA:
24
MySQL OR operator
The MySQL OR operator combines two Boolean expressions and returns true when either condition is
true.
The following illustrates the syntax of the OR operator.
Both boolean_expression_1 and boolean_expression_2 are Boolean expressions that return true, false,
or NULL.
The following table shows the result of the OR operator.
Operator precedence
When you use more than one logical operator in an expression, MySQL always evaluates
the OR operators after the AND operators. This is called operator precedence which determines the
order of evaluation of the operators. MySQL evaluates the operator with higher precedence first
25
For example, to get the customers who locate in the USA or France, you use the OR operator in
the WHERE clause as follows:
MySQL IN Operator
The IN operator allows you to determine if a specified value matches any value in a set of values or
returned by a subquery.
The following illustrates the syntax of the IN operator:
26
Use a column or an expression ( expr ) with the IN operator in the WHERE clause.
Separate the values in the list by commas (,).
The IN operator returns 1 if the value of the column_1 or the result of the expr expression is equal to
any value in the list, otherwise, it returns 0.
When the values in the list are all constants, MySQL performs the following steps:
First, evaluate the values based on the type of the column_1 or result of the expr expression.
Second, sort the values.
Third, search for the value using the binary search algorithm. Therefore, a query that uses
the IN operator with a list of constants performs very fast.
Note that if the expr or any value in the list is NULL, the IN operator returns NULL.
27
MySQL IN
1 SELECT
2 column1,column2,...
3 FROM
4 table_name
5 WHERE
6 (expr|column_1) IN ('value1','value2',...);
Use a column or an expression ( expr ) with the IN operator in the WHERE clause.
Separate the values in the list by commas (,).
The IN operator returns 1 if the value of the column_1 or the result of the expr expression is equal to
any value in the list, otherwise, it returns 0.
When the values in the list are all constants, MySQL performs the following steps:
First, evaluate the values based on the type of the column_1 or result of the expr expression.
Second, sort the values.
Third, search for the value using the binary search algorithm. Therefore, a query that uses
the IN operator with a list of constants performs very fast.
Note that if the expr or any value in the list is NULL, the IN operator returns NULL.
You can combine the IN operator with the NOT operator to determine if a value does not match any
value in a list or a subquery. And you can also use the IN operator in the WHERE clause of
other statements such as UPDATE, and DELETE.
28
MySQL IN operator examples
If you want to find the offices that locate in the U.S. and France, you can use the IN operator as the
following query:
29
To get offices that do not locate in USA and France, you use NOT IN in the WHERE clause as follows:
The expr is the expression to test in the range defined by begin_expr and end_expr. All three
expressions: expr, begin_expr, and end_expr must have the same data type.
The BETWEEN operator returns true if the value of the expr is greater than or equal to (>=) the value
of begin_expr and less than or equal to (<= ) the value of the end_expr, otherwise, it returns zero.
The NOT BETWEEN returns true if the value of expr is less than (<) the value of the begin_expr or
greater than the value of the value of end_expr, otherwise, it returns 0.
If any expression is NULL, the BETWEEN operator returns NULL .
In case you want to specify an exclusive range, you can use the greater than (>) and less than (<)
operators.
30
MySQL BETWEEN operator examples
The following example uses the BETWEEN operator to find products whose buy prices
between 90 and 100:
This query uses the greater than or equal (>=) and less than or equal ( <= ) operators instead of
the BETWEEN operator to get the same result:
31
MySQL LIKE operator
The LIKE operator is a logical operator that tests whether a string contains a specified pattern or not.
Here is the syntax of the LIKE operator:
he LIKE operator is used in the WHERE clause of the SELECT , DELETE, and UPDATE statements to filter
data based on patterns.
MySQL provides two wildcard characters for constructing patterns: percentage % and underscore _ .
32
In this example, MySQL scans the whole employees table to find employees whose first names start with
the character a and are followed by any number of characters.
This example uses the LIKE operator to find employees whose last names end
with on e.g., Patterson, Thompson:
If you know the searched string is embedded inside in the middle of a string, you can use the percentage
( % ) wildcard at the beginning and the end of the pattern.
For example, to find all employees whose last names contain on , you use the following query with the
pattern %on%
33
B) Using MySQL LIKE with underscore( _ ) wildcard examples
To find employees whose first names start with T , end with m, and contain any single character
between e.g., Tom , Tim, you use the underscore (_) wildcard to construct the pattern as follows:
34
In this syntax:
The offset specifies the offset of the first row to return. The offset of the first row is 0, not 1.
The row_count specifies the maximum number of rows to return.
The following picture illustrates the LIMIT clause:
When you use the LIMIT clause with one argument, MySQL will use this argument to determine the
maximum number of rows to return from the first row of the result set.
Therefore, these two clauses are equivalent
And
35
For example, you may want to get fifth through tenth rows, but fifth through tenth in what order? The
order of rows is unknown unless you specify the ORDER BY clause.
Therefore, it is a good practice to always use the LIMIT clause with the ORDER BY clause to constraint
the result rows in unique order.
The following picture illustrates the evaluation order of the LIMIT clause in the SELECT statement:
This statement uses the LIMIT clause to get the top five customers who have the highest credit
36
In this example:
First, the ORDER BY clause sorts the customers by credits in high to low.
Similarly, this example uses the LIMIT clause to find 5 customers who have the lowest credits
the following finds the customer who has the second-highest credit
37
MySQL IS NULL examples
The following query uses the IS NULL operator to find customers who do not have a sales
representative:
Conclusion: Studied the use of MySQL WHERE, SELECT DISTINCT, AND, OR, IN, BETWEEN, LIKE, LIMIT, IS
NULL clause
38