DBMS Lab # 5 Agregate Fns
DBMS Lab # 5 Agregate Fns
Tip: The aggregate functions and the scalar functions will be explained in details in the next
chapters.
Example
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 1
O_Id OrderDate OrderPrice Customer
OrderAverage
950
Now we want to find the customers that have an OrderPrice value higher than the average
OrderPrice value.
Customer
Hansen
Nilsen
Jensen
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 2
2- SQL COUNT() Function
The COUNT() function returns the number of rows that matches a specified criteria.
The COUNT(column_name) function returns the number of values (NULL values will not
be counted) of the specified column:
The COUNT (DISTINCT column_name) function returns the number of distinct values of
the specified column:
Note: COUNT(DISTINCT) works with ORACLE and Microsoft SQL Server, but not
with Microsoft Access.
Example
We have the following "Orders" table:
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 1
3 2008/09/02 700 Hansen
The result of the SQL statement above will be 2, because the customer Nilsen has made 2
orders in total:
CustomerNilsen
2
SQL COUNT(*) Example If we omit
the WHERE clause, like this:
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 4
NumberOfCustomers
3
which is the number of unique customers (Hansen, Nilsen, and Jensen) in the "Orders"
table
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 1
LargestOrderPrice
2000
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 6
The result-set will look like this:
SmallestOrderPrice
100
OrderTotal
5700
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 1
SQL GROUP BY Statement
Aggregate functions often need an added GROUP BY statement.
The GROUP BY statement is used in conjunction with the aggregate functions to group the
result-set by one or more columns.
SQL GROUP BY Syntax
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value GROUP BY column_name
SQL GROUP BY Example
Now we want to find the total sum (total order) of each customer.
Customer SUM(OrderPrice)
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 8
Hansen 2000
Nilsen 1700
Jensen 2000
Customer SUM(OrderPrice)
Hansen 5700
Nilsen 5700
Hansen 5700
Hansen 5700
Jensen 5700
Nilsen 5700
Explanation of why the above SELECT statement cannot be used: The SELECT statement
above has two columns specified (Customer and SUM(OrderPrice). The "SUM(OrderPrice)"
returns a single value (that is the total sum of the "OrderPrice" column), while "Customer"
returns 6 values (one value for each row in the "Orders" table). This will therefore not give us the
correct result. However, you have seen that the GROUP BY statement solves this problem.
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 1
SELECT Customer,OrderDate,SUM(OrderPrice) FROM Orders
GROUP BY Customer,OrderDate
Now we want to find if any of the customers have a total order of less than 200.
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 10
Customer SUM(OrderPrice)
Nilsen 100
Now we want to find if the customers "Hansen" or "Jensen" have a total order of more than
1500.
Customer SUM(OrderPrice)
Hansen 2000
Jensen 2000
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 1
P_Id LastName FirstName Address City
Now we want to select the content of the "LastName" and "FirstName" columns above, and
convert the "LastName" column to uppercase.
LastName FirstName
HANSEN Ola
SVENDSON Tove
PETTERSEN Kari
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 12
SQL LCASE() Example
Now we want to select the content of the "LastName" and "FirstName" columns above, and
convert the "LastName" column to lowercase.
LastName FirstName
hansen Ola
svendson Tove
pettersen Kari
The LEN() function returns the length of the value in a text field.
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 1
P_Id LastName FirstName Address City
Now we want to select the length of the values in the "Address" column above.
LengthOfAddress
12
9
9
TASKS:
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 14
Asad 04 DBMS 1002 Mr. X
TASK 1:
Calculate the number of records for the 3rd, 4th and 5th column.
Find distinct number of records for the Course Code=1002 as Total.
Find number of students registered for the course DIP as Total Courses.
TASK 2:
Convert the text valued fields in the above table to the lower case and upper case alphabets.
TASK 3:
Using GROUP BY statement, group the courses for the above table.
TASK 4:
Select maximum of the Reg no and smallest valued course code for the above given table.
TASK 5:
Find the length of each record for the first column in the above table as MAXIMUM LENGTH.
TASK 6:
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 1
2 2008/10/23 1600 Nilsen
TASK 7:
Find if the customers "Hansen" or "Nilsen" have a total order of less than 2100 for the following
table:
TASK 8:
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 16