Professional Documents
Culture Documents
Prepared by:
Mengvi P. Gatpandan, DIT
Learning Path
• Database Programming SQL
Topic
• 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:
19
COUNT(*)
20
SELECT COUNT(*) FROM ORDER_LINE_T
WHERE ORDER_ID = 1004;
Output:
COUNT(*)
2
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.
• 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§ion_Assessment11
and submit to canvas
• Capture your tweet and save it as
LastNameFirstname_code§ion_Assessment12
and submit to canvas