Dbms Lab # 3: SQL Aggregate & Scalar Functions
Dbms Lab # 3: SQL Aggregate & Scalar Functions
Tip: The aggregate functions and the scalar functions will be explained in details in the next
chapters.
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 1
Example
OrderAverage
950
Now we want to find the customers that have an OrderPrice value higher than the average
OrderPrice value.
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 3
The result-set will look like this:
Customer
Hansen
Nilsen
Jensen
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:
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 1
Note: COUNT(DISTINCT) works with ORACLE and Microsoft SQL Server, but not
with Microsoft Access.
Example
We have the following "Orders" table:
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 5
SELECT COUNT(*) AS NumberOfOrders FROM Orders
NumberOfOrders
6
which is the total number of rows in the table.
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
O_Id OrderDate OrderPrice Customer
LargestOrderPrice
2000
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 7
SQL MIN() Syntax
SmallestOrderPrice
100
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 1
SQL SUM() Function
OrderTotal
5700
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 9
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
Now we want to find the total sum (total order) of each customer.
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 1
SELECT Customer,SUM(OrderPrice) FROM Orders GROUP
BY Customer
Customer SUM(OrderPrice)
Hansen 2000
Nilsen 1700
Jensen 2000
Customer SUM(OrderPrice)
Hansen 5700
Nilsen 5700
Hansen 5700
Hansen 5700
Jensen 5700
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 11
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
O_Id OrderDate OrderPrice Customer
Now we want to find if any of the customers have a total order of less than 200.
Customer SUM(OrderPrice)
Nilsen 100
Now we want to find if the customers "Hansen" or "Jensen" have a total order of more than
1500.
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 13
The result-set will look like this:
Customer SUM(OrderPrice)
Hansen 2000
Jensen 2000
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 1
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 15
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 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
The GETDATE() function returns the current system date and time.
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 17
TASKS:
Muneer 06 OS Mr. Y
Hassan 10 DSP
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.
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 1
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:
TASK 7:
Find if the customers "Hansen" or "Nilsen" have a total order of less than 2100 for the following
table:
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 19
5 2000 Jensen
6 100 Nilsen
TASK 8:
Lab Instructor: Engr. Shahid Ali Bhutta SQL Aggregate & Scalar Functions Page 1