MySQL Activity 3
MySQL Activity 3
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
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.
We will now list all rows of customers who have address in the state of New
York. Figure 32 has the following output:
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.
AND and OR can be combined to qualify search criteria. The following statements
produce Figure 34 and Figure 35.
Page 3 of 9 http://ThrivingAndLiving.blogspot.com
SELECT description, unit, date_created
FROM products
WHERE unit='ea' AND date_created='2009-11-23';
Alternately, we may use IN to limit our row selection in the statement below. See
Figure 36.
Page 4 of 9 http://ThrivingAndLiving.blogspot.com
Figure 36. Combining NOT in statement
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:
Using AS may increase meaningful meaning column headings for the COUNT()
function:
Page 5 of 9 http://ThrivingAndLiving.blogspot.com
Figure 38 has this output.
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.
Page 6 of 9 http://ThrivingAndLiving.blogspot.com
Figure 39. The HAVING clause
1. List all customers with address living in CA. Figure 40 must be the output:
2. List all customers with address NOT living in NY state. Figure 41 must be the
output.
Page 7 of 9 http://ThrivingAndLiving.blogspot.com
3. Display all products on description which starts with Mo. Figure 42 has the output.
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.
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.
Page 9 of 9 http://ThrivingAndLiving.blogspot.com