Aggregation
Aggregation
Materials:
1. PC or Laptop
2. WAMP/XAMPP Installer
3. Web Browser or CLI
Background
Write the prescribe SQL statement for the following items. Make sure to provide a screenshot for each action
you performed in each task given.
NOTE: Please see attached .sql file for this activity. Create a new database as (db_surname6) and import the
said .sql file.
This activity helps you learn how to query data from the MySQL database server. You learned from your
previous activities a simple SELECT statement that allows you to query data from a single table.
Additionally,
SELECT – show you how to use simple SELECT statement to query the data from a single table.
SELECT DISTINCT – learn how to use the DISTINCT operator in the SELECT statement to eliminate
duplicate rows in a result set.
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.
TASK:
But of course, you don’t stop from querying your data in your database. It is also good if you can filter
your data in a way it is summarized to present and project data well. Here are some of the SQL filter you
must learn:
WHERE – learn how to use the WHERE clause to filter rows based on specified conditions.
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. LIKE provides two
wildcard characters that allows you to construct patterns. These two wildcards are:
Or to find pets whose first name ends with ‘a’, you can execute the following query:
You can combine two wildcard characters ‘%’ and ‘_’ to construct a pattern. For example, you
can find pets whose last name starts with any single character, followed by character a, and
ends with any characters as the following query.
LIMIT – use LIMIT to constrain the number of rows returned by SELECT statement
TASK:
Example:
TASK:
1. Display in the screen the name, gender, and breed of pets in column "Pet_Information".
2. Display in the screen the name, breed, and price of pets in column "Pet_Information".
3. Display an output of the owner table name, "a" gender "who lives in" address.
AGGREGATE FUNCTIONS
An aggregate function allows you to perform a calculation on a set of values to return a single scalar
value. We often use aggregate functions with the GROUP BY and HAVING clauses of the SELECT
statement. The following are the most commonly used SQL aggregate functions:
TASK:
1. AVG. Show how much is the average price value of all pets whose gender is male.
2. SUM. Show how much sum of all pets’ price whose type is bird. Show the pet_type
and then the total sum.
3. SUM. Show how much is the total sum of all dogs and cats. Show the total sum of two
types.
4. COUNT. Show how many female single in your owner table.
5. MIN. Find the youngest pet by showing the age only.
6. MAX. Find the most expensive pet.
NOTE that below (items 7 and 8) ad hoc queries may show you wrong output/display,
but don’t bother as of the moment since aggregate requires subqueries and group by
clause. We will get this back in the next activity. You can check the output by checking
its values.
7. MIN. Find the cheapest female dog. Show the gender, type and price.
8. MAX. Find the eldest male dog. Show the gender, type and age.
6. ORDER BY – show you how to sort the result set using ORDER BY clause. The custom sort order
with the FIELD function will be also covered.
The query above shows all the pets in alphabetical order via a pet_name field. By default it is
displayed in ascending order. If you would like to show in descending order, use DESC.
You may also show the order of your information using two criteria, but mostly it will only prioritize
the first field or criterion.
7. GROUP BY clause – show you how to group rows into groups based on columns or expressions.
The query above shows the total number of each gender in tbl_pet.
The query above shows the total number of each gender in each type in tbl_pet. As you can see
there are 2 NULL values in your table, and it goes to bird and crocodile. Then there is 1 female
bird, 3 female cats, male dogs, and so on. Among all, a male dog shows the greatest number of
pets.
The query above shows the total number of all male pets in tbl_pet. And is similar to the query as
shown below:
NOTE: The HAVING clause was added to SQL because the WHERE keyword could not be used with
aggregate functions. Look the example as shown below:
The query above shows the total number of each gender int tbl_pet but only those having more
than 10 as its total. You may also try to change your query by using the alias (total).
TASK: