Documente Academic
Documente Profesional
Documente Cultură
COUNT Returns the number of rows with non-null values for the
expression
STDDEV For two sets of data with approximately the same mean, the
greater the spread, the greater the standard deviation.
Group Function Operate on sets of rows to give one result per group
VARIANCE Used with columns that store numeric data to calculate the
spread of data around the mean
SUM Calculates the sum ignoring null values
Try It / Solve It
1. Define and give an example of the seven group functions: AVG,
COUNT, MAX, MIN, STDDEV, SUM, and VARIANCE.
AVG, COUNT :
· Calculates average value excluding nulls- AVG group function
· Returns the number of rows with non-null values for the expression- COUNT
group function
MAX, MIN:
STDDEV, VARIANCE:
· Used with columns that store numeric data to calculate the spread of data
around the mean- VARIANCE group function
· For two sets of data with approximately the same mean, the greater the
spread, the greater the standard deviation.- STDDEV group function
SUM:
SELECT 'The sum of ' || COUNT(salary) || ' salaries in employees table is ' ||
SUM(salary) ||'.' "Example4"
FROM EMPLOYEES;
Output:
The sum of 20 salaries in employees table is 175500.
2. Create a query that will show the average cost of the DJs on Demand
events. Round to two decimal places.
3. Find the average salary for Global Fast Foods staff members whose
manager ID is 19.
4. Find the sum of the salaries for Global Fast Foods staff members whose
IDs are 12 and 9.
5. Using the Oracle database, select the lowest salary, the most recent hire
date, the last name of the person who is at the top of an alphabetical list
of employees, and the last name of the person who is at the bottom of an
alphabetical list of employees. Select only employees who are in
departments 50 or 60.
6. Your new Internet business has had a good year financially. You have
had 1,289 orders this year. Your customer order table has a column
named total_sales. If you submit the following query, how many rows
will be returned?
SELECT sum(total_sales)
FROM orders;
One
7. You were asked to create a report of the average salaries for all
employees in each division of the company. Some employees in your
company are paid hourly instead of by salary. When you ran the report, it
seemed as though the averages were not what you expected—they were
much higher than you thought! What could have been the cause?
8. Employees of Global Fast Foods have birth dates of July 1, 1980, March
19, 1979, and March 30, 1969. If you select MIN(birthdate), which date
will be returned?
March 30, 1969
9. Create a query that will return the average order total for all Global Fast
Foods orders from January 1, 2002, to December 21, 2002.
10.What was the hire date of the last Oracle employee hired?
11.In the following SELECT clause, which value returned by the SELECT
statement will be larger?
SELECT SUM(operating_cost), AVG(operating_cost)
Try It / Solve It
1. How many songs are listed in the DJs on Demand D_SONGS table?
2. In how many different location types has DJs on Demand had venues?
Possible venues count: (4)
8. Which statement(s) is/are True about the following SQL statement: SELECT
AVG(NVL(selling_bonus, 0.10)) FROM bonuses;
_____ a. The datatypes of the values in the NVL clause can be any datatype
except date data.
FALSE, the data type of null value column and the new value must be the same in NVL. In
the example above, its 0.10 which suggests selling_bonus is a number. And one more thing,
below mentioned statement is valid too:
SELECT NVL(order_date,TO_DATE('01/01/2000' , 'MM/DD/YYYY')) from f_orders;
But one point to be noted here, AVG works on only numeric values.
_____ b. If the selling_bonus column has a null value, 0.10 will be substituted.
TRUE. The field will be assumed to contain 0.10 instead of null and hence won’t be skipped
by AVG.
_____ c. There will be no null values in the selling_bonus column when the
average is calculated.
TRUE, it won’t permanently change the column data in table, just in the scope of current
query, AVG will get 0.10 in the row where null selling_bonus is encountered.
_____ d. This statement will cause an error. There cannot be two functions in
the SELECT statement.
FALSE, AVG is a group function and NVL is single row function. And such two functions
are allowed. Definitely in any case above error depicted is out of the world here in this case.
9. Which of the following statements is/are TRUE about the following query?
SELECT DISTINCT colors, sizes FROM items;
_____ a. Each color will appear only once in the result set.
FALSE
_____ b. Each size will appear only once in the result set.
FALSE
_____ c. Unique combinations of color and size will appear only once in the
result set.
TRUE
_____ d. Each color and size combination will appear more than once in the
result set.
FALSE
Database Programming with SQL
9-1 : Using GROUP BY and HAVING Clauses
Practice Activities
Vocabulary Identify the vocabulary word for each definition below.
HAVING Used to specify which groups are to be displayed; restricts
groups that do not meet group criteria
GROUP BY Divides the rows in a table into groups
1. In the SQL query shown below, which of the following is true about this
query?
_______ a. Kimberly Grant would not appear in the results set.
TRUE
_______ b. The GROUP BY clause has an error because the manager_id is not
listed in the SELECT clause.
FALSE
_______ c. Only salaries greater than 16001 will be in the result set.
FALSE
_______ d. Names beginning with Ki will appear after names beginning with
Ko.
FALSE
_______ e. Last names such as King and Kochhar will be returned even if they
don’t have salaries > 16000.
FALSE
6. Write a query that will return the average of the maximum salaries in each
department for the employees table.
Database Programming with SQL
9-2 : Using ROLLUP and CUBE Operations and GROUPING SETS
Practice Activities
Vocabulary Identify the vocabulary word for each definition below.
ROLLUP Used to create subtotals that roll up from the most detailed
level to a grand total, following a grouping list specified in
the clause.
CUBE An extension to the GROUP BY clause like ROLLUP that
produces cross-tabulation reports.
GROUPING Used to specify multiple groupings of data
SETS
Try It / Solve It
1. Within the Employees table, each manager_id is the manager of one or
more employees who each have a job_id and earn a salary. For each
manager, what is the total salary earned by all of the employees within each
job_id? Write a query to display the Manager_id, job_id, and total salary.
Include in the result the subtotal salary for each manager and a grand total
of all salaries.
Union All operator that returns all rows from both tables,
including duplicates
Set operators used to combine results into one single result
from multiple SELECT statements
Minus operator that returns rows that are unique to
each table
Intersect operator that returns rows common to both
tables
Try It / Solve It
1. Name the different Set operators?
a. Union
b. Union All
c. Minus
d. Intersect
3. Amend the previous statement to not suppress duplicates and examine the
output. How many extra rows did you get returned and which were they?
Sort the output by employee_id to make it easier to spot.
4. List all employees who have not changed jobs even once. (Such
employees are not found in the job_history table)
5. List the employees that HAVE changed their jobs at least once.
6. Using the UNION operator, write a query that displays the employee_id,
job_id, and salary of ALL present and past employees. If a salary is not
found, then just display a 0 (zero) in its place.