You are on page 1of 10

AIR UNIVERSITY

DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING

EXPERIMENT NO 4

Lab Title: SQL Functions (Aggregate & Scalar Functions)


Student Name: Umar Mustafa Reg. No: 200365
Objective: To learn SQL Functions. .
To understand the need of Aggregate & Scalar Functions. .
.
LAB ASSESSMENT:

Excellent Good Average Satisfactory Unsatisfactory


Attributes
(5) (4) (3) (2) (1)
Ability to Conduct
Experiment
Ability to assimilate the
results
Effective use of lab
equipment and follows
the lab safety rules

Total Marks: Obtained Marks:

LAB REPORT ASSESSMENT:


Excellent Good Average Satisfactory Unsatisfactory
Attributes
(5) (4) (3) (2) (1)

Data presentation

Experimental results

Conclusion

Total Marks: Obtained Marks:

Date: Signature:
AIR University Islamabad DBS LAB

LAB TASKS
Task1:
Calculate the number of records for the 3rd, 4th and 5 th 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.
Find number of students registered for the course DIP as Total Courses.
CREATE TABLE Students (
Name varchar(25),
Reg_No int,
Courses varchar(255),
Course_Code int,
Offered_By varchar(255)
);
INSERT INTO Students (Name, Reg_No, Courses, Course_Code, Offered_By)
VALUES
('Ali', '01', 'DIP', '1001', 'Mr.A'),
('Basit', '02', 'DLD', '1002', 'Mr.A'),
('Junaid', '03', 'DBS', '1003', 'Mr.B'),
('Hamza', '04', 'DIP', '1002', 'Mr.C'),
('Ahmed', '05', 'MLI', '1001', 'Mr.C'),
('Akram', '06', 'ENG', '1003', 'Mr.B'),
('Ahsen', '07', 'MLI', '1004', 'Mr.A'),
('Imtiaz', '08', 'ENG', '1003', 'Mr.C'),
('Hassan', '09', 'DIP', null, null),
('Mutahir', '10', null, null, null);

SELECT COUNT(Courses) AS Courses_Record FROM Students;


SELECT COUNT(Course_Code) AS Course_Code_Record FROM Students;
SELECT COUNT(Offered_By) AS Offered_By_Record FROM Students;

SELECT COUNT(DISTINCT Course_Code) AS Total FROM Students


WHERE Course_Code='1002';

SELECT COUNT(Courses) AS Total_Courses FROM Students


WHERE Courses='DIP';

2
AIR University Islamabad DBS LAB

Output:

Task2:
Convert the text valued fields in the above table to the lower case and upper
case alphabets.

CREATE TABLE Students (


Name varchar(25),
Reg_No int,
Courses varchar(255),
Course_Code int,
Offered_By varchar(255)
);
INSERT INTO Students (Name, Reg_No, Courses, Course_Code, Offered_By)
VALUES
('Ali', '01', 'DIP', '1001', 'Mr.A'),
('Basit', '02', 'DLD', '1002', 'Mr.A'),
('Junaid', '03', 'DBS', '1003', 'Mr.B'),
('Hamza', '04', 'DIP', '1002', 'Mr.C'),
('Ahmed', '05', 'MLI', '1001', 'Mr.C'),
('Akram', '06', 'ENG', '1003', 'Mr.B'),
('Ahsen', '07', 'MLI', '1004', 'Mr.A'),
('Imtiaz', '08', 'ENG', '1003', 'Mr.C'),
('Hassan', '09', 'DIP', null, null),
('Mutahir', '10', null, null, null);

SELECT LCASE(Name) AS Name, LCASE(Courses) as Courses, LCASE(Offered_By) as Offered_By


FROM Students;
SELECT SPACE(0) as '';
SELECT UCASE(Name) AS Name, UCASE(Courses) as Courses, UCASE(Offered_By) as Offered_By
FROM Students;

3
AIR University Islamabad DBS LAB

Output:

Task3:
Using GROUP BY statement, group the courses for the above table.
CREATE TABLE Students (
Name varchar(25),
Reg_No int,
Courses varchar(255),
Course_Code int,
Offered_By varchar(255)
);
INSERT INTO Students (Name, Reg_No, Courses, Course_Code, Offered_By)
VALUES
('Ali', '01', 'DIP', '1001', 'Mr.A'),
('Basit', '02', 'DLD', '1002', 'Mr.A'),
('Junaid', '03', 'DBS', '1003', 'Mr.B'),
('Hamza', '04', 'DIP', '1002', 'Mr.C'),
('Ahmed', '05', 'MLI', '1001', 'Mr.C'),
('Akram', '06', 'ENG', '1003', 'Mr.B'),
('Ahsen', '07', 'MLI', '1004', 'Mr.A'),
('Imtiaz', '08', 'ENG', '1003', 'Mr.C'),
('Hassan', '09', 'DIP', null, null),
('Mutahir', '10', null, null, null);

SELECT Courses FROM Students


GROUP BY Courses;

4
AIR University Islamabad DBS LAB

Output:

Task4:
Select maximum of the Reg no and smallest valued course code for the above
given table.
CREATE TABLE Students (
Name varchar(25),
Reg_No int,
Courses varchar(255),
Course_Code int,
Offered_By varchar(255)
);
INSERT INTO Students (Name, Reg_No, Courses, Course_Code, Offered_By)
VALUES
('Ali', '01', 'DIP', '1001', 'Mr.A'),
('Basit', '02', 'DLD', '1002', 'Mr.A'),
('Junaid', '03', 'DBS', '1003', 'Mr.B'),
('Hamza', '04', 'DIP', '1002', 'Mr.C'),
('Ahmed', '05', 'MLI', '1001', 'Mr.C'),
('Akram', '06', 'ENG', '1003', 'Mr.B'),
('Ahsen', '07', 'MLI', '1004', 'Mr.A'),
('Imtiaz', '08', 'ENG', '1003', 'Mr.C'),
('Hassan', '09', 'DIP', null, null),
('Mutahir', '10', null, null, null);

SELECT MAX(Reg_No) FROM Students;


SELECT MIN(Course_Code) FROM Students;

Output:

5
AIR University Islamabad DBS LAB

Task5:
Find the length of each record for the first column in the above table as
MAXIMUM LENGTH.
CREATE TABLE Students (
Name varchar(25),
Reg_No int,
Courses varchar(255),
Course_Code int,
Offered_By varchar(255)
);
INSERT INTO Students (Name, Reg_No, Courses, Course_Code, Offered_By)
VALUES
('Ali', '01', 'DIP', '1001', 'Mr.A'),
('Basit', '02', 'DLD', '1002', 'Mr.A'),
('Junaid', '03', 'DBS', '1003', 'Mr.B'),
('Hamza', '04', 'DIP', '1002', 'Mr.C'),
('Ahmed', '05', 'MLI', '1001', 'Mr.C'),
('Akram', '06', 'ENG', '1003', 'Mr.B'),
('Ahsen', '07', 'MLI', '1004', 'Mr.A'),
('Imtiaz', '08', 'ENG', '1003', 'Mr.C'),
('Hassan', '09', 'DIP', null, null),
('Mutahir', '10', null, null, null);

SELECT LENGTH(Name) AS Name_Length FROM Students;

Output:

6
AIR University Islamabad DBS LAB

Task6:
Find the average value for the 3rd column.
CREATE TABLE Customers(
O_Id int,
OrderDate varchar(200),
OrderPrice int,
Customer varchar(25)
);
INSERT INTO Customers (O_Id, OrderDate, OrderPrice, Customer)
VALUES
('1', '2008/11/12', '1000', 'Hansen'),
('2', '2008/10/23', '1600', 'Nilsen'),
('3', '2008/09/02', '700', 'Hansen'),
('4', '2008/09/03', '300', 'Hansen'),
('5', '2008/08/30', '2000', 'Jensen'),
('6', '2008/10/04', '100', 'Nilsen');

SELECT AVG(OrderPrice) AS OrderAverage FROM Customers;

Output:

7
AIR University Islamabad DBS LAB

Task7:
Find if the customers "Hansen" or "Nilsen" have a total order of less than 2100.
Also, find if any customer have order of more than 1800.
table:
CREATE TABLE Customers(
O_Id int,
OrderPrice int,
Customer varchar(25)
);
INSERT INTO Customers (O_Id, OrderPrice, Customer)
VALUES
('1', '1000', 'Hansen'),
('2', '1600', 'Nilsen'),
('3', '700', 'Hansen'),
('4', '500', 'Hansen'),
('5', '2000', 'Jensen'),
('6', '100', 'Nilsen');

SELECT Customer,SUM(OrderPrice) AS OrderBelow2100 FROM Customers


WHERE Customer='Hansen' OR Customer='Nilsen'
GROUP BY Customer
HAVING SUM(OrderPrice)<2100;

SELECT SPACE('0') AS '';

SELECT Customer,SUM(OrderPrice) AS OrderAbove1800 FROM Customers


GROUP BY Customer
HAVING SUM(OrderPrice)>1800;

Output:

8
AIR University Islamabad DBS LAB

Task8:
Find the total sum (total order) of each customer.
table:
CREATE TABLE Customers(
O_Id int,
OrderPrice int,
Customer varchar(25)
);
INSERT INTO Customers (O_Id, OrderPrice, Customer)
VALUES
('1', '1000', 'Hansen'),
('2', '1600', 'Nilsen'),
('3', '700', 'Hansen'),
('4', '500', 'Hansen'),
('5', '2000', 'Jensen'),
('6', '100', 'Nilsen');

SELECT Customer,SUM(OrderPrice) AS TotalOrder FROM Customers


GROUP BY Customer;

Output:

9
AIR University Islamabad DBS LAB

Learning Outcomes:
 Aggregate Functions are used to perform calculations on multiple rows of data,
returning a single output value. They are commonly used for statistical analysis or
to summarize data.
 Scalar Functions operate on a single value and return a single value.
 This contrasts with Aggregate Functions, which return one value per group of
rows. Perform bitwise operations on expressions. Manipulate conditional
expressions.

10

You might also like