You are on page 1of 47

ITC c201 - Database Fundamentals

Group Functions and Joins

Prepared by:
Mengvi P. Gatpandan, DIT
Learning Path
• Database Programming SQL
Topic

Mastery • List (describe) and use (apply)


Level

• Define and give an example of the seven group functions: SUM, AVG,
COUNT, MIN, MAX, STDDEV, VARIANCE
• Construct and execute a SQL query using group functions
Learning • Construct and execute group functions that operate only with numeric data
Objective
types
• Retrieve records in multiple tables using JOINS
• List and use basic SQL statement to create, manipulate, and query database; and
Learning
construct one working system using relational database and a front-end language based
Outcome on the requirements provided by the faculty
Today’s Menu
Everyday life is like
programming, I guess.
Tweet your self reaction in the topic If you love something,
140 – 280 characters tweet you can put beauty into
Using this format: [Your own word of it.
wisdom]
#functionsandjoin
[Question] [Answer] Donald Knuth
Learning Objectives
• We are learning to.....
– Define and give an example of the seven group functions:
SUM, AVG, COUNT, MIN, MAX, STDDEV, VARIANCE
– Construct and execute a SQL query using group functions
– Construct and execute group functions that operate only
with numeric data types
– Construct and execute a natural join using ANSI-99 SQL join
syntax
– Create a cross join using ANSI-99 SQL join syntax
– Construct and execute a join with the ANSI-99 USING
Clause
– Construct and execute a join with the ANSI-99 ON Clause
GROUP Functions
• In SQL, the following group functions can
operate on a whole table or on a specific
grouping of rows.
• Each function returns one result.
• Group Functions: AVG
– COUNT
– MIN
– MAX
– SUM
– VARIANCE
– STDDEV
GROUP Functions List
• MIN: Used with
columns that store
any data type to
return the
minimum value.
• MAX: Used with
columns that store
any data type to
return the
maximum value.
GROUP Functions List
• SUM: Used with
columns that store
numeric data to
find the total or
sum of values.
• AVG: Used with
columns that store
numeric data to
compute the
average.
GROUP Functions List
• COUNT: Returns the number of rows.
• VARIANCE: Used with columns that store numeric
data to calculate the spread of data around the
mean. For example, if the average grade for the
class on the last test was 82% and the student's
scores ranged from 40% to 100%, the variance of
scores would be greater than if the student's scores
ranged from 78% to 88%.
• STDDEV: Similar to variance, standard deviation
measures the spread of data. For two sets of data
with approximately the same mean, the greater the
spread, the greater the standard deviation
GROUP Functions SELECT Clause
• Group functions are
written in the SELECT
clause:

• What are Group Functions?


• Group Functions operate
on sets of rows to give one
result per group.
• SELECT column,
group_function
GROUP Function Cautions
• Important things you should know about
group functions: Group functions cannot
be used in the WHERE clause:
GROUP Function examples
• MIN: Used with columns that store any
data type to return the minimum value.
GROUP Function examples
• MAX: Used with columns that store any
data type to return the maximum value.
GROUP Function examples
• SUM: Used with columns that store
numeric data to find the total or sum of
values.
GROUP Function examples
• AVG: Used with columns that store
numeric data to compute the average.
GROUP Function examples
• VARIANCE: Used with columns that store
numeric data to calculate the spread of
data around the mean.
• STDDEV: Similar to variance, standard
deviation measures the spread of data.
GROUP Function and NULL
• Group functions ignore
NULL values.
• In the example below,
the null values were
not used to find the
average
commission_pct.
More Than One Group Function
• You can have more than one group
function in the SELECT clause, on the
same or different columns.
Rules for Group Functions
• Group functions ignore null values.
• Group functions cannot be used in the
WHERE clause.
• MIN, MAX and COUNT can be used with
any data type; SUM, AVG, STDDEV, and
VARIANCE can be used only with numeric
data types.
Note:

It is easy to confuse the functions COUNT(*)


and COUNT.

The function COUNT(*) is used to count all


rows selected by a query regardless of
whether any of the rows contain null values.

COUNT tallies only those rows that contain a


value. It ignores all null values.

19
COUNT(*)

• Using the COUNT aggregate function to


find totals

SELECT COUNT(*) FROM ORDER_LINE_T


WHERE ORDER_ID = 1004;

Note: with aggregate functions you can’t have


single-valued columns included in the SELECT
clause

20
SELECT COUNT(*) FROM ORDER_LINE_T
WHERE ORDER_ID = 1004;

Output:

COUNT(*)
2

SELECT COUNT(*) FROM ORDER_LINE_T

Output:
Returns the number of rows in the Order_Line_T

21
COUNT()
SELECT ORDER_ID, COUNT(ORDER_ID)
FROM ORDER_LINE_T
GROUP BY ORDER_ID

Output:
ORDER_ID COUNT(ORDER_ID)
1000 3
1001 2
1002 1
1003 2
1004 3
Join Commands
• There are two sets of commands or syntax
which can be used to make connections
between tables in a database: Oracle
proprietary joins
• ANSI/ISO SQL 99 complaint standard joins
• In this course, you will learn to use both
sets of join commands.
• Oracle proprietary joins will be covered
later in the course.
ANSI
• ANSI stands for American National Standards
Institute.
• Founded in 1918, ANSI is a private, non-profit
organization that administers and coordinates
the U.S. voluntary standardization and
conformity assessment system.
• The Institute's mission is to enhance both the
global competitiveness of U.S. business and
the U.S. quality of life by promoting and
facilitating voluntary consensus standards and
conformity assessment systems, and
safeguarding their integrity.
SQL
• Structured Query Language (SQL) is the
information-processing industry-standard
language of relational database management
systems (RDBMS).
• The language was originally designed by IBM in
the mid 1970s, came into widespread use in the
early 1980s, and became an industry standard in
1986 when it was adopted by ANSI.
• So far there have been three ANSI standardizations
of SQL, each one building on the previous one.
• They are named after the year in which they were
first proposed, and are widely known by their
short names: ANSI-86, ANSI-92 and ANSI-99.
Natural Join
• A SQL join clause combines fields from
2 (or more) tables in a relational
database.
• A natural join is based on all columns in
two tables that have the same name
and selects rows from the two tables
that have equal values in all matched
columns.
Natural Join
• The employees table has a job_id
column.
• This is a reference to the column of the
same name in the jobs table.
Natural Join
• As shown in the sample code, when using a
natural join, it is possible to join the tables
without having to explicitly specify the columns
in the corresponding table.
• However, the names and data types of both
columns must be the same.

• This join will return columns from the


employees table and their related job_title from
the jobs table based on the common column
job_id.
Natural Join
Natural Join
• Here is another example:
• The departments and
locations table both have
a column, location_id,
which is used to join the
two tables.

• Notice that the natural


join column does not
have to appear in the
SELECT clause.
Cross Join
• The ANSI/ISO SQL: 1999 SQL CROSS JOIN
joins each row in one table to every row
in the other table.
• The result set represents all possible row
combinations from the two tables.
• This could potentially be very large!
• If you CROSS JOIN a table with 20 rows
with a table with 100 rows, the query
will return 2000 rows.
Cross Join
• The employees table contains 20 rows
and the departments table has 8 rows.
Join Clauses – USING Clause
• USING Clause
– In a natural join, if the tables have columns
with the same names but different data
types, the join causes an error.
– To avoid this situation, the join clause can be
modified with a USING clause.
– The USING clause specifies the columns that
should be used for the join.
USING Clause
• The query shown is an example of the
USING clause.
• The columns referenced in the USING
clause should not have a qualifier (table
name or alias) anywhere in the SQL
statement.
USING Clause
• The USING clause allows us to use
WHERE to restrict rows from one or both
tables:
Join Clauses – ON Clause
• What if the columns to be joined have
different names, or if the join uses non-
equality comparison operators such as <, >,
or BETWEEN ?
• We can't use USING, so instead we use an
ON clause.
• This allows a greater variety of join
conditions to be specified.
• The ON clause also allows us to use WHERE
to restrict rows from one or both tables.
ON Clause Example
• In this example, the ON
clause is used to join
the employees table
with the jobs table.

• A join ON clause is
required when the
common columns have
different names in the
two tables.
ON Clause with WHERE Clause
• Here is the same query with a WHERE
clause to restrict the rows selected.
ON Clause with
non-equality operator
• Sometimes you may need to
retrieve data from a table that
has no corresponding column
in another table.
• Suppose we want to know the
grade_level for each
employees salary.
• The job_grades table does not
have a common column with
the employees table.
• Using an ON clause allows us
to join the two tables
ON Clause with
non-equality operator
Joining Three Tables
• Both USING and ON can be used to
join three or more tables.
• Suppose we need a report of our
employees, their department, and the
city where the department is located?
• We need to join three tables:
employees, departments and
locations.
Joining Three Tables - Example
• Capture your tweet and save it as
LastNameFirstname_code&section_Assessment11
and submit to canvas
• Capture your tweet and save it as
LastNameFirstname_code&section_Assessment12
and submit to canvas

Tweet your self reaction in the topic


140 – 280 characters tweet
Using this format: [Your own word of wisdom]
#functionsandjoin
[Question] [Answer]
• Laboratory Activity to Activity
• Use the naming conventions
• LastNameFirstname_code&section_Act12,
and submit it to canvas
Thank you

Have a nice day everyone. Stay safe and


Best of health to all of us.

You might also like