Lab Manual No. 4
Lab Manual No. 4
CLO’s:
CLO-1
Semester:
Fall 2022
AIR UNIVERSITY ISLAMABAD
FACULTY OF ELECTRICAL AND COMPUTER ENGINEERING
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
Customer
Hansen
Nilsen
Jensen
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:
AIR UNIVERSITY ISLAMABAD
FACULTY OF ELECTRICAL AND COMPUTER ENGINEERING
NumberOfOrders
6
which is the total number of rows in the table.
The MIN() function returns the smallest value of the selected column.
Now we want to find the total sum (total order) of each customer.
We will have to use the GROUP BY statement to group the customers.
We use the following SQL statement:
Customer SUM(OrderPrice)
Hansen 2000
Nilsen 1700
Jensen 2000
Hansen 5700
Nilsen 5700
Hansen 5700
AIR UNIVERSITY ISLAMABAD
FACULTY OF ELECTRICAL AND COMPUTER ENGINEERING
Hansen 5700
Jensen 5700
Nilsen 5700
Now we want to find if any of the customers have a total order of less than 2000.
We use the following SQL statement:
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
HAVING SUM(OrderPrice)<2000
Nilsen 1700
Now we want to find if the customers "Hansen" or "Jensen" have a total order of more than
1500.
AIR UNIVERSITY ISLAMABAD
FACULTY OF ELECTRICAL AND COMPUTER ENGINEERING
Hansen 2000
Jensen 2000
TheUCASE() Function
Now we want to select the content of the "LastName" and "FirstName" columns above, and
convert the "LastName" column to uppercase.
We use the following SELECT statement:
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.
We use the following SELECT statement:
hansen Ola
svendson Tove
pettersen Kari
The LEN() function returns the length of the value in a text field.
SQL LEN() Syntax
Now we want to select the length of the values in the "Address" column above.
We use the following SELECT statement:
The GETDATE() function returns the current system date and time.
SQL GETDATE() Syntax
SELECT GETDATE();
AIR UNIVERSITY ISLAMABAD
FACULTY OF ELECTRICAL AND COMPUTER ENGINEERING
Tasks:
Name Reg_No Courses Course_Code Offered_By
Muneer 06 OS Mr. Y
Hassan 10 DSP
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:
O_Id OrderDate OrderPrice Customer
Find if the customers "Hansen" or "Nilsen" have a total order of less than 2100 for the following
table:
AIR UNIVERSITY ISLAMABAD
FACULTY OF ELECTRICAL AND COMPUTER ENGINEERING
Task 8:
Find the total sum (total order) of each customer.