You are on page 1of 13

DBMS Worksheet 1

Name: Niveshika Agarwal

UID: 20BCS3324

Group: 20ITB-15

Date: 18th June, 2021

Ques 1: Consider the following schema

Ord_no purch_amt ord_date customer_id salesman_id

---------- ---------- ---------- ----------- -----------

70001 150.5 2012-10-05 3005 5002

70009 270.65 2012-09-10 3001 5005

70002 65.26 2012-10-05 3002 5001

70004 110.5 2012-08-17 3009 5003

70007 948.5 2012-09-10 3005 5002

70005 2400.6 2012-07-27 3007 5001

70008 5760 2012-09-10 3002 5001

70010 1983.43 2012-10-10 3004 5006

70003 2480.4 2012-10-10 3009 5003

70012 250.45 2012-06-27 3008 5002

70011 75.29 2012-08-17 3003 5007

70013 3045.6 2012-04-25 3002 5001


a) Create the table orders with columns order_no number type, purch_amtnumber(precision, scale),
ord_date date, customer_id number and salesman_id number.
b) Insert the values as given in the table.
c) Add customer name, email address and contact_number columns in the given table
d) Add column gender in the table with a single character value.
e) Update the values of newly added columns in the records

Answer 1:

Code:
create table Orders (ord_no int, purch_amt float, ord_date varchar(10), customer_id int, salesman_id
int);
insert into Orders values (70001, 150.5, '2012-10-05', 3005, 5002);
insert into Orders values (70009, 270.65, '2012-09-10', 3001, 5005);
insert into Orders values (70002, 65.26, '2012-10-05', 3002, 5001);
insert into Orders values (70004, 110.5, '2012-08-17', 3009, 5003);
insert into Orders values (70007, 948.5, '2012-09-10', 3005, 5002);
insert into Orders values (70005, 2400.6, '2012-07-27', 3007, 5001);
insert into Orders values (70008, 5760, '2012-09-10', 3002, 5001);
insert into Orders values (70010, 1983.43, '2012-10-10', 3004, 5006);
insert into Orders values (70003, 2480.4, '2012-10-10', 3009, 5003);
insert into Orders values (70012, 250.45, '2012-06-27', 3008, 5002);
insert into Orders values (70011, 75.29, '2012-08-17', 3003, 5007);
insert into Orders values (70013, 3045.6, '2012-04-25', 3002, 5001);
alter table Orders add customer_name varchar(30);
alter table Orders add email varchar(30);
alter table Orders add contact_no varchar(12);
alter table Orders add gender char;
insert into Orders values(70001, 150.5, '2012-10-05', 3005, 5002, 'Niveshika',
'nivagarwal64@gmail.com', 7269069310, 'F');
insert into Orders values(70009, 270.65, '2012-09-10', 3001, 5005, 'Mansi', 'mansi19005@gmail.com',
8752645689, 'F');
insert into Orders values(70002, 65.26, '2012-10-05', 3002, 5001, 'Palak', 'palakag@gmail.com',
563290918,'F');
insert into Orders values(70004, 110.5, '2012-08-17', 3009, 5003, 'Ayush', 'ayushk32@gmail.com',
7824578912, 'M');
insert into Orders values(70007, 948.5, '2012-09-10', 3005, 5002, 'Piyush',
'piyushmehra45@gmail.com', 7811904532, 'M');
insert into Orders values(70005, 2400.6, '2012-07-27', 3007, 5001, 'Anshika', 'anshimal@gmail.com',
9012785934, 'F');
insert into Orders values(70008, 5760, '2012-09-10', 3002, 5001, 'Vanshika', 'vanshi12@gmail.com',
8756328987, 'F');
insert into Orders values(70010, 1983.43, '2012-10-10', 3004, 5006, 'Vanshita', 'vanshi12@gmail.com',
8756328987, 'F');
insert into Orders values(70003, 2480.4, '2012-10-10', 3009, 5003, 'Aradhya', 'aradhi12@gmail.com',
8756328987, 'F');
insert into Orders values(70012, 250.45, '2012-06-27', 3008, 5002, 'Sakshi', 'sakshi31@gmail.com',
8756328987, 'F');
insert into Orders values(70011, 75.29, '2012-08-17', 3003, 5007, 'Harpreet', 'hkaur@gmail.com',
8756328987, 'F');
insert into Orders values(70013, 3045.6, '2012-04-25', 3002, 5001, 'Pritam', 'preet@gmail.com',
8756328987, 'M');
update Orders set customer_name = 'Ashutosh', email = 'ashu@gmail.com', contact_no =
'8890000421', gender = 'M' where customer_id = 3004;
update Orders set customer_name = 'Atul', email = 'atulag@gmail.com', contact_no = '7268000431',
gender = 'M' where customer_id = 3008;
select * from Orders;
output:

Ques 2:

Create table student for the given set of attributes and implement given operations using SQL commands:

1) Create table Student (Rno, Name, DOB, Gender, Class, College,City, Marks)
2) Insert 5 records in student table
3) Display the information of all the students
4) Display the detail structure of student table
5) Display Rno, Name and Class information of ‘Patiala’ students.
6) Display information on ascending order of marks
7) Change the marks of Rno 5 to 89.
8) Change the name and city of R.no 9.
9) Delete the information of ‘Amritsar’ city records
10) Delete the records of students where marks<30
Answer 2:

Code:
Create table student(Rno int, stud_name varchar(30), DOB varchar(30), gender char, stud_class varchar(10),
college varchar(30), city varchar(30), marks int);
insert into student values(1, 'Niveshika','17-11-2001', 'F', '28B', 'CU', 'Kanpur', 98);
insert into student values(2, 'Mansi','19-05-2002', 'F', '28B', 'CU', 'Amritsar', 27);
insert into student values(5, 'Ayush','12-11-2001', 'M', '28B', 'CU', 'Bareilly', 42);
insert into student values(7, 'Shivam','05-07-2001', 'M', '28B', 'CU', 'Amritsar', 88);
insert into student values(9, 'Palak','10-12-2002', 'F', '28B', 'CU', 'Patiala', 21);
select * from student;
desc student;
select Rno, stud_name, stud_class from student where city = 'Patiala';
select * from student order by marks;
update student set marks=89 where Rno=5;
select * from student where Rno=5;
update student set stud_name = 'Amrita', city = 'Lucknow' where Rno=9;
select * from student where Rno=9;
delete from student where city = 'Amritsar';
select * from student;
delete from student where marks<30;
select * from student;

output:

1) Create table Student (Rno, Name, DOB, Gender, Class, College,City, Marks)
2) Insert 5 records in student table
3) Display the information of all the students
4) Display the detail structure of student table

5) Display Rno, Name and Class information of ‘Patiala’ students

6) Display information on ascending order of marks

7) Change the marks of Rno 5 to 89.


8) Change the name and city of R.no 9

9) Delete the information of ‘Amritsar’ city records

10) Delete the records of students where marks<30

Ques 3:
Consider the following schema:

ID FNAME LNAME GENDER SALARY HIREDATE

1 Rajveer Singh M 30000 2017/11/05

2 Manveer Singh M 50000 2017/11/05

3 Ashutos Kumar M 40000 2017/12/12

4 Ankita Sharma F 45000 2017/12/15


5 Vijay Kumar M 50000 2018/01/12

6 Dilip Yadav M 25000 2018/02/26

7 Jayvijay Singh M 30000 2018/02/18

8 Reenu Kumari F 40000 2017/09/19

9 Ankit Verma M 25000 2018/04/04

10 Harpreet Singh M 50000 2017/10/10

a) Create table Employee.


b) Insert the values as mentioned.
c) Drop column LNAME from the employee table.
d) Add an annual increment to the salary of employee whose joining date is 19th September 2017.
e) Modify the department value of DILIP and VIJAY to ME
f) Delete the employees belonging to ME department

Answer 3:

Code:
create table employee (id int primary key, fname varchar(15), lname varchar(15), gender char, salary int,
hirdate varchar(12));
insert into employee values (1, 'Rajveer', 'Singh', 'M', 30000, '2017/11/05');
insert into employee values (2, 'Manveer', 'Singh', 'M', 50000, '2017/11/05');
insert into employee values (3, 'Ashutosh', 'Kumar', 'M', 40000, '2017/12/12');
insert into employee values (4, 'Ankita', 'Sharma', 'F', 45000, '2017/12/15');
insert into employee values (5, 'Vijay', 'Kumar', 'M', 50000, '2018/01/12');
insert into employee values (6, 'Dilip', 'Yadav', 'M', 25000, '2018/02/26');
insert into employee values (7, 'Jayvijay', 'Singh', 'M', 30000, '2018/02/18');
insert into employee values (8, 'Reenu','Kumari', 'F', 40000, '2017/09/19');
insert into employee values (9, 'Ankit', 'Verma', 'M', 25000, '2018/04/04');
insert into employee values (10, 'Harpreet', 'Singh', 'M', 50000, '2017/10/10');
select * from employee;
alter table employee drop column lname;
select * from employee;
update employee set salary = 100000 where hirdate = '2017/09/19';
select * from employee where hirdate = '2017/09/19';
update employee set fname = 'ME' where fname = 'Dilip';
update employee set fname = 'ME' where fname = 'Vijay';
select * from employee where fname = 'ME';
delete from employee where fname = 'ME';
select * from employee;

output:

a) Create table Employee.


b) Insert the values as mentioned.
c) Drop column LNAME from the employee table.

d) Add an annual increment to the salary of employee whose joining date is 19th September 2017.

e) Modify the department value of DILIP and VIJAY to ME


f) Delete the employees belonging to ME department

Ques 4:
Create table employee(ename,ecode,dep_name,salary) and implement following operations using aggregate
functions:
1. Display the number of employees working in each department.
2. Display the average salary of employees working in each department.
3. Find the employee with highest salary in ‘Physics’ Department

Answer 4:

Code:
create table employee (ename varchar(30), ecode int, dep_name varchar(20), salary int);
insert into employee values ('Niveshika Agarwal', 2017, 'CSE', 50000);
insert into employee values ('Mansi Sharma', 3821,'MEC', 45000);
insert into employee values ('Sumit Kumar', 4381, 'MEC', 60000);
insert into employee values ('Ayush Katiyar', 2901, 'MEC', 24000);
insert into employee values ('Krati Gupta', 8392, 'Physics', 32000);
insert into employee values ('Priya Yadav', 3282, 'CSE', 46000);
insert into employee values ('Rohan Singh',3721, 'Physics', 54000);
select * from employee;
select count(ename) as CSE from employee where dep_name = 'CSE';
select count(ename) as MEC from employee where dep_name = 'MEC';
select count(ename) as Physics from employee where dep_name = 'Physics';
select avg(salary) as CSE from employee where dep_name = 'CSE';
select avg(salary) as MEC from employee where dep_name = 'MEC';
select avg(salary) as Physics from employee where dep_name = 'Physics';
select max(salary) from employee where dep_name = 'Physics';

output:

1. Display the number of employees working in each department


2. Display the average salary of employees working in each department.

3. Find the employee with highest salary in ‘Physics’ Department

You might also like