0% found this document useful (0 votes)
2 views

DBMS Practical File

The document contains practical exercises for a Database Management System (DBMS) course, detailing SQL commands for creating and manipulating tables such as STUDENT, CUSTOMER, PARTS, ORDERS, EMPLOYEE, DEPARTMENT, and CLUB. It includes tasks like inserting records, querying data, and performing calculations such as total order amounts and average salaries. The exercises aim to provide hands-on experience with SQL syntax and database operations.

Uploaded by

laboxe7965
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DBMS Practical File

The document contains practical exercises for a Database Management System (DBMS) course, detailing SQL commands for creating and manipulating tables such as STUDENT, CUSTOMER, PARTS, ORDERS, EMPLOYEE, DEPARTMENT, and CLUB. It includes tasks like inserting records, querying data, and performing calculations such as total order amounts and average salaries. The exercises aim to provide hands-on experience with SQL syntax and database operations.

Uploaded by

laboxe7965
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

[Date]

DBMS PRACTICALS
In MS SQL Server

Saurabh Verma
M.SC OPERATIONAL RESEARCH
3ED SEMESTER SECTION A
DEPARTMENT OF OPERATIONAL RESEARCH
UNIVERSITY OF DELHI
DBMS PRACTICALS

Q1.Create a table STUDENT with the following fields:


IDno,Name,Birth Date,Address,Sex, Course(repeat at least two courses twice or thrice),
Marks in 5 subjects(marks1,marks2,marks3,marks4,marks5).

create table student


(
IDno int not null,
Name varchar(50) not null,
DOB date,
Address varchar(100),
Sex varchar(7),
Course varchar(50),
Marks1 int,
Marks2 int,
Marks3 int,
Marks4 int,
Marks5 int,
primary key(IDno)
);

Perform the following queries in SQL:


i.Insert at least 10 record in STUDENT table.

insert into student values(11,'ankit','15-jan-1998','delhi','male','m.sc',98,88,75,79,81);


insert into student values(4,'shukhapal','18-feb-1999','utter
pradesh','male','MBA',98,68,65,70,88);
insert into student values(10,'abhishek','18-jan-1998','noida','male','MBA',98,88,75,68,85);
insert into student values(1,'saurabh','15-dec-1999','delhi','male','m.sc',68,75,76,88,95);
insert into student values(3,'bhavana','12-mar-1999','pune','female','b.sc',99,68,92,88,76);
insert into student values(8,'shnkey','10-dec-1998','delhi','female','m.sc',98,68,88,85,75);
insert into student values(2,'manisha','19-jan-1997','narayna','female','m.sc',98,66,87,56,78);
insert into student values(9,'amit','19-feb-1998','delhi','male','b.sc',98,68,69,99,88);
insert into student values(7,'vyom','18-jun-1997','delhi','male','m.sc',95,88,67,81,85);
insert into student values(5,'kartikey','15-jan-1998','delhi','male','m.sc',58,55,53,86,52);

ii.Display the content of table in ascending order of IDno.

Select*
From student
ORDER BY IDno DESC;

SAURABH VERMA 1
DBMS PRACTICALS

iii.Display Student IDno,Name, marks1,marks2,marks3,marks4,marks5 and percentage in ascending order of


percentage.

Select IDno,name,Marks1,Marks2,Marks3,Marks4,Marks5,((Marks1+Marks2+Marks3+Marks4+Marks5)/5) as
percentage
From student
group by IDno,name,Marks1,Marks2,Marks3,Marks4,Marks5
ORDER BY percentage;

iv.Display the details of all student who securd first division.

Select IDno,name,Marks1,Marks2,Marks3,Marks4,Marks5,((Marks1+Marks2+Marks3+Marks4+Marks5)/5) as
percentage
From student
Where ((Marks1+Marks2+Marks3+Marks4+Marks5)/5)>=80.00
group by IDno,name,Marks1,Marks2,Marks3,Marks4,Marks5
Order by IDno;

v.Display the details of student who securd highest marks.

Select *
From student
Where ((Marks1+Marks2+Marks3+Marks4+Marks5)/5)>=80.00
Order by IDno;

SAURABH VERMA 2
DBMS PRACTICALS

Q2.Consider the following database having three tables:


Table1 CUSTOMER

create table customer


(
Name varchar(20),
Address varchar(50),
State char(2),
primary key(Name)
);

insert into customer values('True wheel','550 Husker','NY');


insert into customer values('Bike spec','CPT Shrive','LA');
insert into customer values('Le shoppe','Hometown','KS');
insert into customer values('AAA bike','10 Oldtown','SL');
insert into customer values('Jackls bike','24 Eglin','FL');

select*from customer;

Table2 PARTS

create table parts


(
Partnum int,
Description varchar(50),
Price decimal(10,2),
primary key(Partnum)
);

insert into parts values(54,'Pedals',54.25);


insert into parts values(42,'Seats',24.50);
insert into parts values(46,'Tires',15.25);
insert into parts values(23,'Mountain bike',350.45);
insert into parts values(76,'Road bike',530.00);
insert into parts values(10,'Tandem',1200.00);

select*from parts;

SAURABH VERMA 3
DBMS PRACTICALS

Table3 ORDERS

create table orders


(
Orderedon date,
Name varchar(20),
Partnum int,
Quantity int,
foreign key (Name) references customer(Name),
foreign key (Partnum) references parts(Partnum)
);

insert into orders values('15-may-1996','True wheel',23,6);


insert into orders values('19-may-1996','True wheel',76,3);
insert into orders values('2-sep-1996','True wheel',10,1);
insert into orders values('30-jun-1996','True wheel',42,8);
insert into orders values('30-jun-1996','Bike spec',54,10);
insert into orders values('30-may-1996','Bike spec',10,2);
insert into orders values('30-may-1996','Bike spec',23,8);
insert into orders values('17-jan-1996','Le shoppe',76,5);
insert into orders values('1-jun-1996','Le shoppe',10,3);
insert into orders values('1-jun-1996','AAA bike',10,1);
insert into orders values('11-jun-1996','Jackls bike',76,14);

select*from orders;

Create the above three table and populate the given data and perform the following queries in SQL:
i.Find out order details of parts having description like ‘Road____’.

SAURABH VERMA 4
DBMS PRACTICALS

Select *
From orders
Where (partnum in (Select partnum From parts Where description like 'Road%'));

ii.Determine the total order amount for part ‘Road bike’.

Select sum(o.Quantity*p.Price) as total_order_amount


From orders o,parts p
Where p.description like 'Road bike' AND p.Partnum=o.Partnum ;

iii.Find out the details(customername,data orderedon,totalvalue) of those customers whose order value is
more average order value .[totalvalue=quantity*price]

Select o.Name, o.Orderedon, sum(o.Quantity*p.Price) as total_value


From orders o,parts p
Where p.Partnum=o.Partnum
group by o.Name, o.Orderedon
HAVING SUM(o.Quantity*p.Price) >
(SELECT AVG(o1.Quantity*p1.Price)
FROM orders o1,parts p1
WHERE p1.Partnum=o1.Partnum);

iv.Alter table ORDERS to add one more column Remarks and update the value as ‘paid’ for all the records.

Alter table orders


add Remarks varchar(10);

Update orders
set Remarks = 'Paid';
select*from orders;

SAURABH VERMA 5
DBMS PRACTICALS

Q3.Create the following two tables

Table4 Employee

create table Employee


(
FNAME varchar(50),
ENG INT PRIMARY KEY,
BDATE DATE,
SEX CHAR(1),
SALARY INT,
SUPERENO INT,
DNO INT,
foreign key (SUPERENO) references Employee(ENG),
foreign key (DNO) references Department(DNUMBER)
);

insert into Employee values('Gaurav',888665555,'10-nov-1937','M',55000,NULL,1);


insert into Employee values('Karthik',333445555,'08-dec-1955','M',40000,888665555,5);
insert into Employee values('Priya',987654321,'06-jun-1941','f',43000,888665555,4);
insert into Employee values('Mahesh',123456789,'09-jan-1965','M',30000,333445555,5);
insert into Employee values('Swetha',999887777,'19-jan-1968','F',25000,987654321,4);
insert into Employee values('Ramesh',666884444,'15-sep-1962','M',28000,333445555,5);
insert into Employee values('Kiran',453453453,'31-jul-1972','F',25000,333445555,5);
insert into Employee values('Kikndra',987987987,'29-mar-1937','M',25000,987654321,4);

select*from Employee;

SAURABH VERMA 6
DBMS PRACTICALS

Table5 Department

create table Department


(
DNAME varchar(50),
DNUMBER INT PRIMARY KEY,
MGRENO INT,
MGRSTARTDATE DATE
);

insert into Department values('Research',5,333445555,'22-may-1988');


insert into Department values('Administration',4,987654321,'01-jan-1995');
insert into Department values('Headquarters',1,888665555,'19-jun-1981');

select*from Department;

i.Retrieve the data of birth and salary of the employee(s) whose name is ‘Priya’.

select BDATE,SALARY
FROM Employee
where FNAME = 'Priya';

ii.Retrieve the name of all employee who work for the ‘Research’ department.

select FNAME AS NAME


FROM Employee,Department
where DNO=DNUMBER AND DNAME='Research';

SAURABH VERMA 7
DBMS PRACTICALS

iii.Select all combination of employee number and department name in the database.

select ENG,DNAME
FROM Employee,Department;

iv.Find the sum of the salaries of all the employees of the ‘Research’ department as well as the maximum
salary, the minimum salary and the average salary in the department.

select SUM(SALARY) AS TOTAL,MAX(SALARY),MIN(SALARY),AVG(SALARY)


FROM Employee,Department
where DNO=DNUMBER AND DNAME='Research';

SAURABH VERMA 8
DBMS PRACTICALS

v.For each department , retrieve the department number,the number of employees in the department and
their average salary.

select DNAME,COUNT(SALARY),AVG(SALARY)
FROM Employee,Department
where DNO=DNUMBER
GROUP BY DNAME;

Q4.Create a table CLUB with following fields:


(Coach_id,Coach_name,Age,Sports,data_of_joining,Pay,Sex)
CREATE TABLE CLUB
(
Coach_id int,
Coach_name varchar(50),
Age int,
Sports varchar(50),
Date_of_joining date,
Pay int,
Sex char(1)
);

Perform the following queries in SQL:


i.Insert at least 10 record in the table CLUB.

INSERT INTO CLUB VALUES(5,'SAURABH',20,'Cricket','15-JAN-2015',50000,'M');


INSERT INTO CLUB VALUES(4,'TUSHAR',21,'Football','30-JAN-2014',45000,'M');
INSERT INTO CLUB VALUES(3,'BAWANA',20,'Badminton','12-MAR-2016',49000,'F');
INSERT INTO CLUB VALUES(6,'NAVEEN',22,'FOOTBALL','15-DEC-2015',35000,'M');
INSERT INTO CLUB VALUES(2,'MANISHA',26,'Badminton','29-JUN-2015',40000,'F');
INSERT INTO CLUB VALUES(1,'ABHISEK',23,'Cricket','15-JAN-2015',30000,'M');
INSERT INTO CLUB VALUES(8,'PANKAJ',24,'FOOTBALL','5-NOV-2014',48000,'M');
INSERT INTO CLUB VALUES(9,'KIRAN',20,'Badminton','10-APR-2016',25000,'F');
INSERT INTO CLUB VALUES(7,'ROHIT',20,'Cricket','25-JUL-2018',25000,'M');
INSERT INTO CLUB VALUES(10,'KAJAL',23,'Tennis','5-MAY-2016',40000,'F');

select*from CLUB;

SAURABH VERMA 9
DBMS PRACTICALS

ii.Display the description of the table CLUB.

sp_help CLUB;

iii.List the names of all the coaches with their date of joining in descending order.

SELECT *
FROM CLUB
ORDER BY DATE_OF_JOINING desc;

SAURABH VERMA 10
DBMS PRACTICALS

iv.Display a report, showing coach name ,pay, age and bouns (15%of pay) for all the coaches.

SELECT COACH_NAME,PAY,AGE,(PAY*0.15) AS BONUS, (PAY*0.15+PAY) AS TOTAL


FROM CLUB;

v.Display the name ,ID and age of all coaches with salary>5000.

SELECT COACH_NAME,COACH_ID,AGE
FROM CLUB
WHERE PAY>5000;

SAURABH VERMA 11
DBMS PRACTICALS

Q5.A departmental store deals in a variety of products perishable and non-perishable.Create a database
using two tables to store the details for product.

Table 1:Product information


• Unique Product Identification
• Description
• Category to which it brlongs(household,food,electrical etc.)
• Whether or not it is perishable

CREATE TABLE PRO_INFO


(
U_P_id int PRIMARY KEY,
DESCRIPTION VARCHAR(40),
CATEGORY VARCHAR(40),
PERISHABLE CHAR(3)
);

INSERT INTO PRO_INFO VALUES(2,'SAMOSA','FOOD','YES');


INSERT INTO PRO_INFO VALUES(3,'SHOES','FASHION','NO');
INSERT INTO PRO_INFO VALUES(1,'biscuit','FOOD','YES');
INSERT INTO PRO_INFO VALUES(4,'T-SHIRT','FASHION','NO');
SELECT*FROM PRO_INFO;

Table 2:For Stock of product


• Unique Product Identification
• Date of purchase
• Date of expiry
• Quantity
• Rate

SAURABH VERMA 12
DBMS PRACTICALS

CREATE TABLE PRO_STOCK


(
U_P_id int PRIMARY KEY,
DATE_OF_PURCH DATE,
DATA_OF_EXP DATE,
QUANTITY INT,
RATE INT,
foreign key (U_P_ID) references PRO_INFO(U_P_ID)
);

INSERT INTO PRO_STOCK VALUES(1,'15-JAN-2015','15-MAR-2015',15,20);


INSERT INTO PRO_STOCK VALUES(2,'15-MAR-2015','16-MAR-2015',10,12);
INSERT INTO PRO_STOCK VALUES(3,'15-JAN-2016','',100,1000);
INSERT INTO PRO_STOCK VALUES(4,'15-JAN-2015','',50,500);
SELECT*FROM PRO_STOCK;

i.Display a list of item that are perishable and whose expiry date has passed

SELECT *
FROM PRO_INFO I,PRO_STOCK S
WHERE I.PERISHABLE='YES' AND S.DATA_OF_EXP<='04-SEP-2019' AND I.U_P_ID=S.U_P_ID;

ii.Display the total stock of each product category

SELECT I.CATEGORY,SUM(S.QUANTITY) AS TOTAL_ITEM


FROM PRO_INFO I,PRO_STOCK S
WHERE I.U_P_ID=S.U_P_ID
GROUP BY I.CATEGORY;

iii.Display the details of all products with Rate b/n any 2 limits

SELECT *
FROM PRO_INFO I,PRO_STOCK S
WHERE I.U_P_ID=S.U_P_ID AND RATE>15 AND RATE<600;

SAURABH VERMA 13
DBMS PRACTICALS

iv.Find the number of perishable and non-perishable item

SELECT PERISHABLE,SUM(QUANTITY) AS TOTAL_ITEM


FROM PRO_INFO I,PRO_STOCK S
WHERE I.U_P_ID=S.U_P_ID
GROUP BY PERISHABLE;

v.Display those product categories with their avg rate which are having avg rate greater then 500

SELECT I.CATEGORY,AVG(S.RATE) AS AVG_RATE


FROM PRO_INFO I
left join PRO_STOCK S ON I.U_P_ID=S.U_P_ID
GROUP BY CATEGORY
having avg(RATE)>500;

SAURABH VERMA 14

You might also like