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

O SQL SQL Aggregate Functions and Multiple Table Queries

The document discusses SQL aggregate functions and multiple table queries. It provides examples of using aggregate functions like AVG, SUM, COUNT, MAX and MIN in SQL statements. It also gives examples of joining two tables in a query by specifying the matching columns in the WHERE clause and selecting columns from both tables in the SELECT statement.

Uploaded by

vanvanganda12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

O SQL SQL Aggregate Functions and Multiple Table Queries

The document discusses SQL aggregate functions and multiple table queries. It provides examples of using aggregate functions like AVG, SUM, COUNT, MAX and MIN in SQL statements. It also gives examples of joining two tables in a query by specifying the matching columns in the WHERE clause and selecting columns from both tables in the SELECT statement.

Uploaded by

vanvanganda12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CC – 105 : Information Management – 1

Chapter 4

INTRODUCTION TO SQL (Part 2)

Objectives:

At the end of this chapter, the students should be able to:

 Learn and perform basic SQL Aggregate Functions.


 Learn and perform basic SQL Multiple Table Queries.

 SQL AGGREGATE FUNCTIONS

SQL aggregate functions perform calculation on a set of values and return a single value
and are often used with the SELECT statement. Also, this functions commonly ignores NULL
values.

The following table summarizes some useful and the most common SQL aggregate functions:

Function Description

AVG() Return the average of values

SUM() Return the sum of values

COUNT() Returns the number of rows in a result set

MAX() Returns the maximum value

MIN() Returns the minimum value

Table 1. Commonly used SQL Aggregate Functions

Example #1:

 Supposedly you want to inquire for the average RENTAL_FEE of the different boats
from the marina_slip table which is located inside the alexamara database as shown
in the figure below:

Prepared by: Christian A. Fajardo, MACE 24


CC – 105 : Information Management – 1

 To get the necessary SQL query, the AVG() should be used along with the SELECT
statement as shown below:

SQL statement

SQL Query Result Set

Example #2:

 Supposedly you want to inquire for the total RENTAL_FEE of the different boats from
the same table the following SQL statement should be encoded as shown below:

SQL statement

SQL Query Result Set

Example #3:

 Supposedly you want to inquire for the minimum and maximum RENTAL_FEE of the
different boats from the same table the following SQL statement should be encoded as
shown below:

SQL statement

SQL Query Result Set

Example #4:

Prepared by: Christian A. Fajardo, MACE 25


CC – 105 : Information Management – 1

 Supposedly you want to inquire for the number of records into RENTAL_FEE of the
different boats from the same table the following SQL statement should be encoded as
shown below:

SQL statement

 If you are going to look carefully at the SQL query result set of all records in the
marina_slip table, it is evident that there some duplicate values within the rental_fee
column (or field). Therefore, using the DISTINCT keyword within the COUNT() function
ignores the duplicate values from the table as shown below:

SQL statement

SQL Query Result Set

And similar as to how we use the SELECT statement, these SQL aggregate functions can
be added with conditions by having additional clauses or keywords such as WHERE for a more
filtered result set depending on the query requirement.

Prepared by: Christian A. Fajardo, MACE 26


CC – 105 : Information Management – 1

 SQL MULTIPLE TABLE QUERIES

In order to retrieve data from at least two tables, it must be joined together by finding rows in
the two tables that have identical values in matching columns. And when constructing the
corresponding SQL command, it should be arranged as follows:

1. Within the SELECT statement, list ALL columns (or fields) that needs to be
displayed.
2. In the FROM clause, list ALL tables involved in the query (or SQL command).
3. In the WHERE clause, list the condition that restricts the data to be retrieved to
only those rows from the two tables that match. That is, restrict it to the rows that
have common values in matching columns. And;
4. If necessary (for specific query results), AND clause/s may be added before the
necessary condition to filter out query results. Additionally, ORDER BY may be
added for specific query result sorting.

Example #1:

 Supposedly you want to inquire for the list of the entry number, department name,
first name, and last name from the faculty and dept table under the sked database
as shown in the image below:

Prepared by: Christian A. Fajardo, MACE 27


CC – 105 : Information Management – 1

 To perform the necessary SQL query, the data dictionary of the faculty and dept table
must be inspected to see if there are any matching columns exists by using the
DESCRIBE statement for each table as shown in the image below:

Matching
Columns

 Notice that by looking at the data dictionaries for both faculty and dept, the dept_id
column can be considered as matching columns since they both use the same data type
and both of the columns require data entries. However, when joining these tables, the
said query result shall only include those records the matches the actual data entry
values for the said matching column. Therefore, the necessary SQL command used and
the query result is shown below:

 From the displayed SQL query result, only the name column came from the dept table
which is combined along with the rest of the columns that actually came from the faculty
table. Which means that when joining tables, naming conventions for table columns (or
field) is critical to avoid incorrect query results. Alternatively, use this SQL command to

Prepared by: Christian A. Fajardo, MACE 28


CC – 105 : Information Management – 1

specify where the said column/s should be extracted in case there are any duplicity on
the column names:

SQL statement

Same
SQL
Query
Results Alternate SQL statement

Example #2:

 Supposedly you want to inquire for the list of the entry number, first name, and last
name of those faculties from the Computer Science department using the same set of
tables from Example #1, the following SQL command should be used to get the correct
SQL query result as shown in the image below:

SQL statement

SQL Query Result

***END OF CHAPTER 4***

Prepared by: Christian A. Fajardo, MACE 29

You might also like