0% found this document useful (0 votes)
55 views

Aggregation

This document provides instructions and objectives for a laboratory activity on functions and aggregate functions in SQL. The activity aims to teach students how to query and summarize data using SELECT statements with DISTINCT, WHERE, IN, BETWEEN, LIKE, LIMIT, and IS NULL clauses. It also covers the use of CONCAT() to combine strings, and aggregate functions like AVG, COUNT, MIN, MAX and SUM to perform calculations on groups of values. Students are asked to complete tasks demonstrating their understanding of these SQL concepts and functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Aggregation

This document provides instructions and objectives for a laboratory activity on functions and aggregate functions in SQL. The activity aims to teach students how to query and summarize data using SELECT statements with DISTINCT, WHERE, IN, BETWEEN, LIKE, LIMIT, and IS NULL clauses. It also covers the use of CONCAT() to combine strings, and aggregate functions like AVG, COUNT, MIN, MAX and SUM to perform calculations on groups of values. Students are asked to complete tasks demonstrating their understanding of these SQL concepts and functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

College of Computing Education

3rd Floor, DPT Building


Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

Name :_______________________________ Date :__________________________________


Time :_______________________________ Prof :_______________________________

[ Laboratory No. 6: Functions and Aggregate Functions]


Objectives:

1. To know how to query data using SELECT and SELECT DISTINCT


2. To know how to invoke SQL commands using aggregate functions
3. To learn how to use Order by, Group by and Having statements

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.

The syntax of using the DISTINCT clause is as follows:

SELECT DISTINCT columns FROM table_name WHERE where_conditions;

Example: SELECT DISTINCT(pet_gender) from tbl_pet where pet_age=2;

TASK:

1. Display all records of every table in your database


2. Display the ages of pet without duplication
3. Display all combined unique pet’s status and type without duplicating its values
4. Display how all addresses in your tbl_owner having no same values
5. Display how many gender types in your tbl_owner

IT105/L Martzel P. Baste|Page 1


College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

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:

a. Percentage (% ) wildcard matches a sequence of any character including space.


Suppose you want to find pets whose last name starts with the letter ‘a’, you can use the following
query.

Or starts with letter ‘c’

Or to find pets whose first name ends with ‘a’, you can execute the following query:

IT105/L Martzel P. Baste|Page 2


College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

b. Underscore ( _ ) wildcard matches any single character.

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

 IS NULL – test whether a value is NULL or not by using IS NULL operator.

TASK:

1. WHERE. Display all records of your pet where owner id is 18.


2. IN. Display all pets which are dogs, cats, and bird but status must be ‘Inactive’.
3. BETWEEN. Display all pets whose price ranges from 1000 to 5000. The pet must be
dog and is active.
4. LIKE. Display all pets whose name starts with letter b where color must be blue.
5. LIKE. Display all pets whose name ends with letter e which is owned by owner_id 18.
6. LIKE. Display all pets whose name contains z.
7. LIMIT. Display ONLY 10 information of pet, but for all types which are dogs.
8. IS NULL. Find NO gender pet which color is green.

IT105/L Martzel P. Baste|Page 3


College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

FUNCTIONS AND AGGREGATE FUNCTIONS

CONCAT(). It is a function that is used to add two or more strings or values.

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.

Example: Pedro, a male who lives in Davao city.

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:

1. AVG – calculates the average of a set of values.


2. COUNT – counts rows in a specified table or view.
3. MIN – gets the minimum value in a set of values.
4. MAX – gets the maximum value in a set of values.
5. SUM – calculates the sum of values.

Run these statements below as an example:


1. Returns the total number of all male pets.

2. Returns the total sum of all ‘Dog’ pets.

IT105/L Martzel P. Baste|Page 4


College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

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.

IT105/L Martzel P. Baste|Page 5


College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

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.

8. HAVING – filter the groups by a specific condition.

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).

IT105/L Martzel P. Baste|Page 6


College of Computing Education
3rd Floor, DPT Building
Matina Campus, Davao City
Telefax: (082)
Phone No.: (082)300-5456/305-0647 Local 116

Or if you would like to combine your query just like this

TASK:

1. Display all pet’s information order by age from lowest to highest


2. Display all pet’s information order by status in descending order
3. Display the information of top 10 most expensive pets.
4. Display the information of top 10 youngest pets but only those who are dogs
5. Find the information of the cheapest female dog.
6. Find the information of the youngest dog which/who are inactive.
7. Find the most expensive pet whose type is dog but breed must be either dachshund,
askal, and/or Siberian husky.
8. Show each how many owners of each civil status.
9. Show each how many owners of each civil status by city.
10. Show how many pets per status.

IT105/L Martzel P. Baste|Page 7

You might also like