Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

HITEC UNIVERSITY

Department of Software Engineering

Database Systems

BS Software Engineering
4th Semester, Session-2020
Lab Report # 08
Section B

Submitted to:
Lec. Shahid Iqbal

Submitted by:
Hassaan Mansoor 20-SE-071
1. For the above table perform the following tasks:-

CREATE TABLE Studet_T


(
Name VARCHAR(22) NOT NULL,
Reg_No CHAR(3),
Courses VARCHAR(7),
Course_Code INT,
Offered_By VARCHAR(22),
CONSTRAINT Studet_PK PRIMARY KEY(Reg_No));

INSERT INTO Studet_T


VALUES('Ali', '01', 'DIP', 1001, 'Mr. A'),
('Basit', '02', 'DBMS', 1002, 'Mr. X'),
('Akram', '03', 'OS', 1003, 'Mr. Y'),
('Asad', '04', 'DBMS', 1002, 'Mr. X'),
('Zeeshan', '05', 'DIP', 1001, 'Mr. A'),
('Shafqat', '07', 'NM', 1004, 'Mr. H'),
('Ahsan', '08', 'OS', 1003, 'Mr. Y')
INSERT INTO Studet_T(Name, Reg_No, Courses,
Offered_By)VALUES('Muneer', '06', 'OS', 'Mr. Y')
INSERT INTO Studet_T(Name, Reg_No, Courses,
Offered_By)VALUES('Ikram', '09', 'DIP', 'Mr. A')
INSERT INTO Studet_T(Name, Reg_No, Courses)VALUES('Hassan',
'10', 'DSP')
SELECT * FROM Studet_T
 Calculate the number of records for the 3rd, 4th and 5th column.

SELECT COUNT(Courses) AS Courses FROM Studet_T


SELECT COUNT(Course_Code) AS Course_Code FROM Studet_T
SELECT COUNT(Offered_By) AS Offered_By FROM Studet_T

 Find distinct number of records for the Course Code=1002 as Total.

SELECT COUNT(DISTINCT Name) AS COURSE_CODE FROM Studet_T


WHERE Course_Code=1002

 Find number of students registered for the course DIP as Total Courses.

SELECT COUNT(DISTINCT Name) AS COURSES FROM Studet_T


WHERE Courses='DIP'
2. Convert the text valued fields in the above table to the lower case and
upper case alphabets.

UPPER CASE

SELECT UPPER(Name) AS NAME, Courses FROM Studet_T

LOWER CASE

SELECT LOWER(Courses) AS COURSES, Name FROM Studet_T


3. Using GROUP BY statement, group the courses for the above table.

SELECT Courses,COUNT(Course_Code) FROM Studet_T GROUP BY


Courses

4. Select maximum of the Reg no and smallest valued course code for the
above given table.

LARGEST REGISTRATION NUMBER

SELECT MAX(Reg_No) AS LARGESTREG_NO FROM Studet_T


SMALLEST VALUED COURSE_CODE

SELECT MIN(Course_Code) AS SmallestCOURSECODE FROM Studet_T

5. Find the length of each record for the first column in the above table as
MAXIMUM LENGTH.

SELECT LEN(Name) AS lengthofname FROM Studet_T


6. Find the average value for the 3rd column.

CREATE TABLE Oder_T


(
O_ID VARCHAR(22) NOT NULL,
ORDERDATE VARCHAR(22),
ORDERPRICE INT,
CUSTOMER VARCHAR(22),
CONSTRAINT Oder_PK PRIMARY KEY(O_ID));

INSERT INTO Oder_T


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 * FROM Oder_T

AVERAGE

SELECT AVG(ORDERPRICE) AS OrderAverage FROM Oder_T


7. Find if the customer “Hansen” or “Nilsen” have a total order of less
than 2100 for the following table. Also find if any customer have order
of more than 1800.

CREATE TABLE Odr_T


(
O_ID VARCHAR(22) NOT NULL,
ORDERPRICE INT,
CUSTOMER VARCHAR(22),
CONSTRAINT Odr_PK PRIMARY KEY(O_ID));

INSERT INTO Odr_T


VALUES('1', 1000, 'Hansen'),
('2', 1600, 'Nilsen'),
('3', 700, 'Hansen'),
('4', 300, 'Hansen'),
('5', 2000, 'Jensen'),
('6', 100, 'Nilsen')
SELECT * FROM Odr_T

 “HANSEN” OR “JENSEN” TOTAL ORDER LESS THAN 2100

SELECT CUSTOMER, SUM(ORDERPRICE) FROM Odr_T


WHERE CUSTOMER='Hansen' OR CUSTOMER='Jensen'
GROUP BY Customer
HAVING SUM(ORDERPRICE)<2100
 “HANSEN” OR “JENSEN” TOTAL ORDER GREATER THAN 1800

SELECT CUSTOMER, SUM(ORDERPRICE) FROM Odr_T


WHERE CUSTOMER='Hansen' OR CUSTOMER='Jensen'
GROUP BY Customer
HAVING SUM(ORDERPRICE)>1800

8. Find the total sum (total order) of each customer.

SELECT CUSTOMER,SUM(ORDERPRICE) FROM Odr_T GROUP BY CUSTOMER

You might also like