Sie sind auf Seite 1von 9

Laboratory Activity 3

Comparison Operators in MySQL


Objectives
At the end of the activity, student is expected to perform/identify the following:

 Identify the different relational/comparison operators;


 Use GROUP BY clause;
 Combine Aggregate functions such as COUNT();
 Impose selection criteria using HAVING clause.

This is a continuation of Laboratory Activity 2. In order to gain full understanding


in-depth, we suggest that you do the said lab activity first before proceeding on this activity.
You may visit it at http://thrivingandliving.blogspot.com/2009/12/mysql-query-browser-
tutorial.html.

Relational/Comparison Operators

WHERE clause can use other operators to limit records. The following are comparison
operators that support WHERE:

Operator Description
= Equal to
<> or != Not equal to
< Less than
<= Less than or equal to
> greater than
>= greater than or equal to
LIKE Used to compare strings
Checks for values between
BETWEEN a range
IN Checks for values in a list
Ensures the value is not in
NOT IN the list

Table 3. Relational / Comparison Operators

When working with strings, % and _ (underscore) can be used as wildcards to retrieve
keywords from rows from the search table.

Page 1 of 9 http://ThrivingAndLiving.blogspot.com
Let us use again MySQL Query Browser application. Click Start | Programs | MySQL
| MySQL Query Browser.

Supply localhost at server host; root at username. We will use the existing
database that we did on Activity 2: my_store. Enter the password you have designated on
your server. See Figure 31.

Figure 31. Connecting to server

We will now list all rows of customers who have address in the state of New
York. Figure 32 has the following output:

SELECT name, address


FROM customers
WHERE address LIKE '%ny%';

Figure 32. Using % as wild card

Page 2 of 9 http://ThrivingAndLiving.blogspot.com
The statement below retrieves all rows that begins with p with six characters
following it (six underscores were used). Figure 33 has the output.

SELECT description, unit


FROM products
WHERE description LIKE '%P______';

Figure 33. Using _ as wildcard

AND and OR can be combined to qualify search criteria. The following statements
produce Figure 34 and Figure 35.

SELECT description, unit


FROM products
WHERE unit='ea' OR unit='pc';

Figure 34. Using OR on selection criteria

Page 3 of 9 http://ThrivingAndLiving.blogspot.com
SELECT description, unit, date_created
FROM products
WHERE unit='ea' AND date_created='2009-11-23';

Figure 35. Using OR on selection criteria

Alternately, we may use IN to limit our row selection in the statement below. See
Figure 36.

SELECT description, unit


FROM products
WHERE unit IN ('pkg','mtr');

Figure 36. The use of IN clause

Adding NOT on the statement produces Figure 37.

SELECT description, unit


FROM products
WHERE unit NOT IN ('pkg','mtr');

Page 4 of 9 http://ThrivingAndLiving.blogspot.com
Figure 36. Combining NOT in statement

GROUP BY modifier and Aggregate Functions

The GROUP BY modifier may be used to perform aggregate functions, such as COUNT records.
If we want to count rows that fall under column businesstype and counts its instance, we
have:

SELECT businesstype, COUNT(businesstype)


FROM customers
GROUP by businesstype;

Figure 37 has the output:

Figure 37. COUNT() function

Using AS may increase meaningful meaning column headings for the COUNT()
function:

SELECT businesstype, COUNT(businesstype) AS 'Number of Accounts'


FROM customers
GROUP by businesstype;

Page 5 of 9 http://ThrivingAndLiving.blogspot.com
Figure 38 has this output.

Figure 38. Using AS as column name

List of Aggregate Functions available in MySQL

Table 4 shows all the aggregate functions used in MySQL.

Function Example Description


AVG() SELECT AVG(cost) Returns the average value in a group of
FROM Invoice GROUP records. The example returns the average
BY ClientID; order for each customer.
COUNT() SELECT COUNT(cost) Returns the number of records in a group of
FROM Invoice GROUP records. The example returns the number of
BY ClientID; orders for each customer.
MAX() SELECT MAX(cost) Returns the largest value in a group of
FROM Invoice GROUP records. The example returns the largest
BY ClientID; order by each customer.
MIN() SELECT MIN(cost) Returns the lowest value in a group of
FROM Invoice GROUP records. The example returns the smallest
BY ClientID; order by each customer.
SUM() SELECT SUM(cost) Returns the sum of a field. The example
FROM Invoice GROUP returns the total amount spent by each
BY ClientID; customer.

The HAVING Clause

The WHERE clause is used to restrict records in a query. If you wish to restrict records from
an aggregate function, you use the HAVING clause. The difference is that the HAVING
clause restricts the records after they have been grouped.

The following statement produces Figure 39 as output:

SELECT unit, COUNT(unit) AS 'Number more than 2'


FROM products
GROUP BY unit
HAVING COUNT(unit)>2;

Page 6 of 9 http://ThrivingAndLiving.blogspot.com
Figure 39. The HAVING clause

CHECK YOUR UNDERSTANDING


Do the following tasks:

1. List all customers with address living in CA. Figure 40 must be the output:

Supply statement/s here.

Figure 40. Answer item 1.

2. List all customers with address NOT living in NY state. Figure 41 must be the
output.

Supply statement/s here.

Figure 41. Answer item 2.

Page 7 of 9 http://ThrivingAndLiving.blogspot.com
3. Display all products on description which starts with Mo. Figure 42 has the output.

Supply statement/s here.

Figure 42. Answer item 3

4. Display all records from customers where date_created does not fall under
11/01/2009, 11/07/2009 and 11/30/2009. Figure 43 should be the output.

Supply statement/s here.

Figure 43. Answer to item 4

Page 8 of 9 http://ThrivingAndLiving.blogspot.com
5. List summary count of units under products EXCLUDING ea from the count.
Figure 44 must be the output.

Supply statement/s here.

Figure 44. Answer to item 5

Page 9 of 9 http://ThrivingAndLiving.blogspot.com

Das könnte Ihnen auch gefallen