Lab # 04 Implementation of SQL Functions
Lab # 04 Implementation of SQL Functions
LAB # 04
Implementation of
SQL Functions
(Aggregate/Scalar functions)
Lab Objective:
The aim of this lab is to understand the different functions used in the SQL.
Tip: The aggregate functions and the scalar functions will be explained in details in the next
chapters.
Example
OrderAverage
950
Now we want to find the customers that have an OrderPrice value higher than the
average OrderPrice value.
Customer
Hansen
Nilsen
Jensen
The COUNT(column_name) function returns the number of values (NULL values will
not be counted) 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:
The result of the SQL statement above will be 2, because the customer Nilsen has made
2 orders in total:
CustomerNilsen
NumberOfOrders
NumberOfCustomers
which is the number of unique customers (Hansen, Nilsen, and Jensen) in the
"Orders" table
LargestOrderPrice
2000
SmallestOrderPrice
100
OrderTotal
5700
The GROUP BY statement is used in conjunction with the aggregate functions to group
the result-set by one or more columns.
Now we want to find the total sum (total order) of each customer.
Customer SUM(OrderPrice)
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.
GROUP BY Customer,OrderDate
Now we want to find if any of the customers have a total order of less than 2000.
Customer SUM(OrderPrice)
Nilsen 1700
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
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
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.
Now we want to select the length of the values in the "Address" column
LengthOfAddress
12
The GETDATE() function returns the current system date and time.
SELECT GETDATE();
TASKS:
TASK 1:
rd th th
Calculate the number of records for the 3 , 4 and 5 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:
nd
Find the average value for the 2 column.
TASK 7:
Find if the customers "Hansen" or "Nilsen" have a total order of less than 2100 for the
following table:
TASK 8: