Database Systems Practical 4 SQL Select Command/ Aggregate Functions
Database Systems Practical 4 SQL Select Command/ Aggregate Functions
Database Systems Practical 4 SQL Select Command/ Aggregate Functions
Database Systems
Practical 4
SQL Select Command/ Aggregate Functions
Version 1.1
In this section, we will play with the most vital statement of SQL- SELECT statement. Remember, you can
do so many things with this single statement along with others that a book can be written. Even many software
developers do not know many tricks that can be played with SELECT. So, try to catch more tricks, just don’t
sit with this practical.
In this case, only SELECT and FROM are must for this expression. All others are optional. Order of the clauses
cannot be changed.
Demonstration
Run the script employee2.sql by “START drive:/employee2.sql;” command. Then try to follow the steps
provided below one by one and see the results. You can also take a note of them if you wish. Not all of the
clauses used it SELECT statement will be covered in this section. We will go through them next week.
SELECT fname, mi, lname, ssn, bdate, address, salary, superssn, dno
FROM employee;
Use of DISTINCT
DISTINCT is used to eliminate repeating elements.
1
Practical 4: SQL SELECT Statement and Aggregate Functions Database Systems
Calculated Fields
You can make numerical calculations on related columns of a table also. In this case, we will divide the salary
of employees who have department number 5.
2
Practical 4: SQL SELECT Statement and Aggregate Functions Database Systems
Look at the range operators <= and >= here. These are more flexible than BETWEEN and NOT BETWEEN
clauses.
Set Membership
This finds out the first and last name of those who has salary exactly 30000 or 40000. Similarly the following
statement negates it.
Pattern Matching
Try to execute following statements one by one.
Found any difference? The second one does not work as the table has data in all capitals. That is why the third
one works.
When you are using multiple columns in ORDER BY clause, please be careful. Because, sometimes it is
difficult to see the difference in actual results.
3
Practical 4: SQL SELECT Statement and Aggregate Functions Database Systems
FROM employee
ORDER BY salary, dno;
You can find out the difference in the first 3 rows where salary is all 25000. take a look at the change in
ordering regarding to the department number.
4
Practical 4: SQL SELECT Statement and Aggregate Functions Database Systems
In this session, we will take a look at the Aggregate Functions- one of the handy functionalities provided by
SQL.
Demonstration
Run the script named “lab4table.sql”, and “lab4data.sql”.
Now you will see the maximum salary from all of the employees.
With the first syntax, take a look that sale_price column is NULLABLE. It has NULL values as well. Take a
look at how COUNT, SUM and AVG works on NULLABLE columns.
▪ Describe cust_order;
▪ SELECT COUNT(*), COUNT(sale_price) FROM cust_order;
▪ SELECT COUNT(*), SUM(sale_price), AVG(sale_price)
FROM cust_order;
There may be situations where you want an average to be taken over all the rows in a table, not just the rows
with non-NULL values for the column in question. In those situations you have to use the NVL function
within the AVG function call to assign 0 (or some other useful value) to the column in place of any NULL
values.
Most aggregate functions allow the use of DISTINCT or ALL along with the expression argument.
DISTINCT allows you to disregard duplicate expression values, while ALL causes duplicate expression
values to be included in the result. Notice that the column cust_nbr has duplicate values. Observe the result
of the following SQL:
5
Practical 4: SQL SELECT Statement and Aggregate Functions Database Systems
An important thing to note here is that ALL doesn't cause an aggregate function to consider NULL values. For
example, COUNT(ALL SALE_PRICE) in the following example still returns 14, and not 20:
GROUP BY clause
The GROUP BY clause, along with the aggregate functions, groups a result set into multiple groups, and
then produces a single row of summary information for each group. For example, if you want to find the
total number of orders for each customer, execute the following query:
While producing summary results using the GROUP BY clause, you can filter records from the table based
on a WHERE clause, as in the following example, which produces a count of orders in which the sale price
exceeds $25 for each customer:
The next example shows a more appropriate use of
▪ SELECT cust_nbr, COUNT(order_nbr) the HAVING clause:
FROM cust_order
WHERE sale_price > 25 ▪ SELECT cust_nbr, COUNT(order_nbr)
GROUP BY cust_nbr; FROM cust_order
GROUP BY cust_nbr
HAVING clause HAVING COUNT(order_nbr) > 2;
The HAVING clause is closely associated with the
GROUP BY clause. The HAVING clause is used The syntax for the HAVING clause is similar to that
to put a filter on the groups created by the GROUP of the WHERE clause. However, there is one
BY clause. If a query has a HAVING clause along restriction on the conditions you can write in the
with a GROUP BY clause, the result set will HAVING clause. A HAVING condition can refer
include only the groups that satisfy the condition only to an expression in the SELECT list, or to an
specified in the HAVING clause. Let's look at some expression involving an aggregate function.
examples that illustrate this. The following query
returns the number of orders per customer: