0% found this document useful (0 votes)
27 views38 pages

8-In-Built Functions, Join and Group by Queries-19-03-2024

Uploaded by

anshpawan24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views38 pages

8-In-Built Functions, Join and Group by Queries-19-03-2024

Uploaded by

anshpawan24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

 The ORDER BY clause SELECT <columns>

sorts the results of a FROM <tables>


query WHERE <condition>
◦ You can sort in ascending
(default) or descending
ORDER BY <cols>
order [ASCENDING |
◦ Multiple columns can be DESCENDING|
given
ASC | DESC ]
◦ You cannot order by a
column which isn’t in the
result
SELECT * FROM Grades
Grades
ORDER BY Mark
Name Code Mark Name Code Mark
John DBS 56 Mark PR2 35
John IAI 72 Mark PR1 43
Mary DBS 60 Jane IAI 54
Mark PR1 43 John DBS 56
Mark PR2 35 Mary DBS 60
Jane IAI 54 John IAI 72
SELECT * FROM Grades
ORDER BY Code ASC,
Grades Mark DESC
Name Code Mark Name Code Mark
John DBS 56 Mary DBS 60
John IAI 72 John DBS 56
Mary DBS 60 John IAI 72
Mark PR1 43 Jane IAI 54
Mark PR2 35 Mark PR1 43
Jane IAI 54 Mark PR2 35
• Aggregate Row functions give the user
the ability to answer business questions
such as:

 What is the average salary of an employee in


the company?
 What were the total salaries for a particular
year?
 What are the maximum and minimum
salaries in the Computer’s Department?
• Aggregate functions perform a variety of
actions such as counting all the rows in a
table, averaging a column's data, and
summing numeric data.
• Aggregates can also search a table to find
the highest "MAX" or lowest "MIN" values in
a column.
• There are two rules that you must
understand and follow when using
aggregates:
• Aggregate functions can be used in both the
SELECT and HAVING clauses (the HAVING
clause is covered later in this chapter).
• Aggregate functions cannot be used in a
WHERE clause.
 The following query is wrong and will
produce the Oracle ORA-00934 group
function is not allowed here error
message.

SELECT *
FROM employee
WHERE emp_salary > AVG(emp_salary);

ERROR at line 3: ORA-00934: group function is


not allowed here.
 If a manager needs know how many
employees work in the organization,
COUNT(*) can be used to produce this
information.
 The COUNT(*) function counts all rows in a
table.
 The wild card asterisk (*) would be used as
the parameter in the function.
SELECT COUNT(*)
FROM employee;
COUNT(*)
--------
8
 The result table for the COUNT(*) function is
a single scalar value.
 Notice that the result table has a column
heading that corresponds to the name of the
aggregate function specified in the SELECT
clause.
 The output column can be assigned a more
meaningful column name as is shown in the
revised query .
 This is accomplished by simply listing the
desired column name inside double-quotes
after the aggregate function specification.

SELECT COUNT(*) as Number of


Employees
FROM employee;

Number of Employees
---------------------------
8
 COUNT(*) is used to count all the rows in a
table.
 COUNT(column name) does almost the
same thing. The difference is that you may
define a specific column to be counted.
 When column name is specified in the
COUNT function, rows containing a NULL
value in the specified column are omitted.
 A NULL value stands for “unknown” or
“unknowable” and must not be confused
with a blank or zero.
SELECT COUNT(emp_superssn)FROM employee;

COUNT(emp_superssn)
---------------------------
7

• In contrast the count(*) will count each row


regardless of NULL values.

SELECT COUNT(*) FROM employee;


COUNT(*)
-------------------
8
• AVG function is used to compute the
average value for the emp_salary column in
the employee table.
• For example, the following query returns
the average of the employee salaries.

SELECT AVG(emp_salary) as Average Employee


Salary FROM employee;

Average Employee Salary


-----------------------
$35,500
• What is the average salary offered to employees?
• This question asks you to incorporate the concept
of computing the average of the distinct salaries
paid by the organization.
• The same query with the DISTINCT keyword in the
aggregate function returns a different average.
SELECT AVG(DISTINCT emp_salary) FROM employee;

AVG(DISTINCT emp_salary)
-----------------------
$38,200
• The SUM function can compute the total of
a specified table column.
• The SELECT statement shown here will
return the total of the emp_salary column
from the employee table.

SELECT SUM(emp_salary) "Total Salary"


FROM employee;

Total Salary
------------
$284,000
• If management is preparing a budget for
various departments, you may be asked to
write a query to compute the total salary for
different departments.
• The query shown here will compute the total
emp_salary for employees assigned to
department #7.
SELECT SUM(emp_salary) "Total Salary Dept 7"
FROM employee WHERE dno = 7;

Total Salary Dept 7


-------------------
$136,000
• The MIN function returns the lowest value
stored in a data column.
• The MAX function returns the largest value
stored in a data column.
• Unlike SUM and AVG, the MIN and MAX
functions work with both numeric and
character data columns.
• A query that uses the MIN function to find the
lowest value stored in the emp_last_name column
of the employee table.
• This is analogous to determine which employee's
last name comes first in the alphabet.
• Conversely, MAX() will return the employee row
where last name comes last (highest) in the
alphabet.

SELECT MIN(emp_last_name), MAX(emp_last_name)


FROM employee;

MIN(EMP_LAST_NAME) MAX(EMP_LAST_NAME)
------------------------- -----------------------
Amin Zhu
• The power of aggregate functions is
greater when combined with the GROUP BY
clause.
• In fact, the GROUP BY clause is rarely used
without an aggregate function.
• It is possible to use the GROUP BY clause
without aggregates, but such a
construction has very limited functionality,
and could lead to a result table that is
confusing or misleading.
• The following query displays how many
employees are working in each
department?
SELECT dno,COUNT(*)
FROM employee
GROUP BY dno;

Dno Count(*)
---------- ----------------
1 1
3 3
7 4
• Some RDBMs provides considerable
flexibility in specifying the GROUP BY
clause.
• The column name used in a GROUP
BY does not have to be listed in the
SELECT clause; however, it must be a
column name from one of the tables
listed in the FROM clause.
• We could rewrite the last query without
specifying the dno column as part of the
result table, but as you can see below, the
results are rather cryptic without the dno
column to identify the meaning of the
aggregate count.
SELECT COUNT(*)
FROM employee GROUP BY dno;
Count(*)
----------------
1
3
4
• To keep it simple, just remember the
following:

1. If you have column name(s) AND


Aggr. Function(s) in the SELECT
clause, then you MUST also have a
GROUP BY clause.

2. The column name(s) in the SELECT


clause MUST match column name(s)
listed in the GROUP BY clause.
SELECT emp_dno, emp_gender,
COUNT(*)
FROM employee
GROUP BY emp_dno;

ERROR at line 2:
ORA-00979: not a GROUP BY
expression
SELECT emp_dno, emp_gender,COUNT(*)
FROM employee
GROUP BY emp_dno, emp_gender;

Emp_dno emp_gender Count(*)


---------- - --------------
1 M 1
3 F 2
3 M 1
7 F 1
7 M 3
Sales  Find the total value of
Month Department Value the sales for each
March Fiction 20
department in each
March Travel 30 month
March Technical 40 ◦ Can group by Month then
April Fiction 10 Department or
April Fiction 30 Department then Month
April Travel 25 ◦ Same results, but in a
April Fiction 20 different order
May Fiction 20
May Technical 50
SELECT Month, Department,
SELECT Month, Department, SUM(Value) AS Total
SUM(Value) AS Total FROM Sales
FROM Sales GROUP BY Department, Month
GROUP BY Month, Department
Month Department Total
Month Department Total
April Fiction 60
April Fiction 60
March Fiction 20
April Travel 25
May Fiction 20
March Fiction 20
March Technical 40
March Technical 40
May Technical 50
March Travel 30
April Travel 25
May Fiction 20
March Travel 30
May Technical 50
• The WHERE clause works to eliminates data table
rows from consideration before any grouping
takes place.
• The query shown here produces an average
hours worked result table for employees with a
employee id that is larger than 150.
SELECT empid,
AVG(work_hours) "Average Hours Worked"
FROM works_on
WHERE empid > 150
GROUP BY empid;

SSN Average Hours Worked


--------- --------------------
157 25.5
161 20.5
170 21.5
• The ORDER BY clause allows you to
specify how rows in a result table are
sorted.
• The default ordering is from smallest
to largest value.
• A GROUP BY clause in a SELECT
statement will determine the sort
order of rows in a result table.
• The sort order can be changed by
specifying an ORDER BY clause after
the GROUP BY clause.
SELECT emp_dno "Department",
AVG(emp_salary) "Average Salary"
FROM employee
GROUP BY emp_dno
ORDER BY AVG(emp_salary);

Department Average Salary


---------- --------------
3 $31,000
7 $34,000
1 $55,000
• The HAVING clause is used for
aggregate functions in the same way
that a WHERE clause is used for
column names and expressions.
• The HAVING and WHERE clauses do the
same thing, that is filter rows from
inclusion in a result table based on a
condition.
• a WHERE clause is used to filter rows
BEFORE the GROUPING action.
• a HAVING clause filters rows AFTER the
GROUPING action.
SELECT emp_dno as Department,
AVG(emp_salary) as Average Salary
FROM employee
GROUP BY emp_dno
HAVING AVG(emp_salary) > 33000;

Department Average Salary


---------- --------------
1 $55,000
7 $34,000
SELECT emp_dno "Department",
AVG(emp_salary) "Average Salary"
FROM employee
WHERE emp_dno <> 1
GROUP BY emp_dno
HAVING AVG(emp_salary) > 33000;

Department Average Salary


---------- --------------
7 $34,000
Conceptually, SQL performs the following steps
in the query given above.

1. The WHERE clause filters rows that do not


meet the condition
emp_dpt_number <> 1.
2. The GROUP BY clause collects the surviving
rows into one or more groups for each unique
emp_dpt_number.
3. The aggregate function calculates the
average salary for each emp_dpt_number
grouping.
4. The HAVING clause filters out the rows from
the result table that do not meet the condition
average salary greater than $33,000.
SELECT emp_dno, COUNT(*), MAX(salary), MIN(salary)
FROM employee
GROUP BY emp_dno
HAVING COUNT(*) >= 3;

Emp_dno Count(*) Max(Salary) Min(Salary)


---------- ---------------- ---------- ----------
3 3 $43,000 $25,000
7 4 $43,000 $25,000
• The HAVING clause is a conditional
option that is directly related to the
GROUP BY clause option because a
HAVING clause eliminates rows from a
result table based on the result of a
GROUP BY clause.

• In Oracle, A HAVING clause will not


work without a GROUP BY clause.
SELECT emp_dno, AVG(emp_salary)
FROM employee
HAVING AVG(emp_salary) > 33000;

ERROR at line 1:
ORA-00937: not a single-group
group function

You might also like