Lab 6 by Saad

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

Assingnment 6

By Mohammed Saadullah
(201851069)
Lab-B

create table employee (


Sno int primary key,
Name char(20) not null,
designation char(20),
branch char(20));

insert into employee values(1,'Ram','Manager','Chennai'),


(2,'Santhosh','Supervisor','Madurai'),
(3,'Hari','Assistant','Trichy');

Q1. Alter the table by adding a column Salary

Ans. alter table employee add column salary int;

Q2. Alter the table by modifying the column Name Describe the table employee as
employeeName

Ans. ALTER TABLE employee RENAME COLUMN Name to employeeName;

Q3. Copy the table employee as emp

Ans. CREATE TABLE emp


SELECT * FROM employee;
Q4. Truncate the table

Ans. Truncate emp;

Q5. Delete the Second row from the table

Ans.

Q6. Drop the table


Ans.
Schema for the Office of the Controller of Examinations Application.

create table department


(Dno int primary key,
Dname varchar(50));

create table branch


(Bcode int primary key,
BName varchar(50),
Dno int references department(Dno));

create table course(Ccode varchar(8) primary key,


Cname varchar(50),
credits int,
Dno int references department(Dno));

create table Branch_course


(Bcode int references branch(Bcode),
Ccode varchar(8) references course(Ccode),
semester int,
primary key(Bcode,Ccode));

create table student


(rollno int primary key auto_increment,
name varchar(30) not null,
dob date,
gender char(1),
doa date,
Bcode int references branch(Bcode),
check (gender in ('M','F','T')));

create table Enrolls (rollno int references student(rollno),


Ccode varchar(8) references course(Ccode),
Sess varchar(10),
grade char(1),
primary key(rollno,Ccode,Sess),
check(grade in('A','B','C','D','E','U','S'))
);
insert INTO department values
(1,'Physics'),
(2,'Mathematics'),
(3,'Humanities'),
(4,'Computer Science'),
(5,'Electronics'),
(6,'Mechanical Engineering'),
(7,'Civil Engineering');

insert into branch values


(1,'Modern Physics',1),
(2,'Algebra',2),
(3,'Applied Mathematics',2),
(4,'Mathematical Analysis',2),
(5,'Social Science',3),
(6,'Linguistics',3),
(7,'Data structures & Algorithms',4),
(8,'Computer Science',4),
(9,'Digital Electronics',5),
(10,'Power Electronics',5),
(11,'Circuit Design',5),
(12,'Integrated Circuits',5),
(13,'Mechanical Engineering',6),
(14,'Infromation Technology',4),
(15,'Civil Engineering',7);

insert into course values


('PHY101','Waves and Electromagnetics',4,1),
('PHY201','Quantum Physics',3,1),
('MA101','Linear Algebra',4,2),
('MA201','Discrete Mathematics',4,2),
('MA301','Probability and Statistics',4,2),
('MA401','Numerical Techniques',4,2),
('HS101','Spoken and written',2,3),
('HS201','Science,Technology and society',2,3),
('HS301','Technical Writing',2,3),
('CS101','Introduction to Programming',4,4),
('CS201','Data structures',4,4),
('CS301','Design and analysis of Algorithms',4,4),
('CS302','Introduction to Object Oriented Programming'
,4,4),
('CS401','Database Management Systems',4,4),
('CS402','Design and analysis of Algorithms',4,4),
('CS403','Operating Systems',4,4),
('EC401','Computer Organisation and Architecture',4,4)
,
('EC101','Basic Electronics Circuits',4,5),
('EC201','Basic Electrical Engineering',4,5),
('EC301','Digital Logic Design',4,5),
('EC601','Digital IC Design',4,5);

insert INTO Branch_course VALUES


(1,'PHY101',1),
(1,'PHY201',2),
(2, 'MA101',1),
(3, 'MA201',2),
(3, 'MA301',3),
(4, 'MA401',4),
(6, 'HS101',1),
(5, 'HS201',2),
(6, 'HS301',3),
(8,'CS101',1),
(7,'CS201',2),
(7,'CS301',3),
(8,'CS302',3),
(8,'CS401',4),
(8,'CS402',4),
(8,'CS403',4),
(11,'EC401',4),
(9,'EC101',1),
(10,'EC201',2),
(11,'EC301',3),
(12,'EC601',6),
(13,'CS101',1),
(14,'CS101',1),
(15,'CS101',1);

INSERT INTO STUDENT VALUES


( 1, 'Darshan Patel' , '2001-02-27', 'M' , '2018-06-24', 1),
( 2, 'Yash Patel' , '2000-11-13', 'M' , '2018-06-25', 2),
( 3, 'Neel Patel' , '2000-06-19', 'M' , '2018-06-24', 3),
( 4, 'Ramesh Kaushik' , '2001-10-13', 'M' , '2018-06-25', 4),
( 5, 'Keavy Tomlinson', '2000-05-19', 'F' , '2018-06-24', 5),
( 6, 'Catrin Dotson' , '2000-06-17', 'F' , '2018-06-25', 6),
( 7, 'Ravina Churill' , '2001-07-14', 'F' , '2018-06-24', 7),
( 8, 'Jackson Nairn' , '2000-08-02', 'M' , '2018-06-25', 8),
( 9,'Branden Mohammed', '1999-09-19', 'M' , '2018-06-24', 9),
( 10, 'Zhane Bailey' , '1998-10-14', 'F' , '2018-06-25', 10),
( 11, 'Myron Tanner' , '1999-11-24', 'M' , '2018-06-24', 11),
( 12, 'Aine Moreno' , '1997-12-24', 'F' , '2018-06-25', 12);

insert into Enrolls values


(1,'PHY101','AU2018','A'),
(2,'MA101','AU2018','A'),
(3,'MA201','WIN2018','A'),
(3,'MA301','AU2019','A'),
(7,'CS301','AU2019','U'),
(8,'CS302','AU2019','S'),
(8,'CS401','WIN2019','S'),
(8,'CS402','WIN2019','S'),
(8,'CS403','WIN2019','S'),
(10,'EC201','WIN2018','B'),
(10,'EC201','WIN2018','B');

Solving Queries

Q1. Develop a SQL query to list details of Departments that offer more than 3 branches.

Ans.
SELECT Dno,Dname,count(Bcode) as Branches FROM Branch natural join Department
GROUP BY Dno
having Branches>3;

Q2. Develop a SQL query to list the details of Departments that offer more than 6 courses.

Ans.
SELECT Dno,Dname,count(Ccode) as Courses FROM course natural join Departm
ent
GROUP BY Dno
having Courses>6;

Q3. Develop a SQL query to list the details of courses that are common for more than 3
branches.

Ans. SELECT Ccode,Cname,count(Bcode) as OfferedByBranches


from Branch_course natural join course
group by Ccode
having OfferedByBranches>3;

Q4. Develop a SQL query to list students who got ‘S’ in more than 2 courses during single
enrollment.

Ans.
select rollno,name,count(sess) as CoursesFailed,sess FROM student natural
join Enrolls
where grade='S'
GROUP BY sess
having CoursesFailed>2;
Q5. Create a view that will keep track of the roll number, name and number of courses, a
student has completed successfully.

Ans. create VIEW Sinfo as (


select rollno,name, count(IF( Ccode IS null and rollno IS not null,NULL,1
)) as Passed_Courses from student natural LEFT OUTER JOIN enrols
where grade in('A','B','C','D','E') OR grade IS NULL
group by rollno);

Schema for Order Processing Database Application.

Q1. Develop DDL to implement above schema enforcing primary key, check constraints and foreign
key constraints.

create table Customer(Customerno varchar(5) primary key,


Cname varchar(50),
check (Customerno LIKE 'C%'));

create table Cust_Order(Orderno varchar(5) primary key check (Orderno


LIKE'O%'),
Odate date,
Customerno varchar(5) references Customer(Customerno),
Odr_amt int DEFAULT(0)
);

create table Item (Itemno varchar(5) primary key,


Item_name varchar(30),
unit_price int,
check (Itemno LIKE 'I%'));

CREATE TABLE Order_Item(Orderno varchar(5) references Cust_Order(Orderno),


Itemno varchar(5) references Item(Itemno),
qty int,
primary key(Orderno,Itemno)
);

Inserting Values into tables

Insert INTO Customer VALUES('C0001','Patricia Anderson'),


('C0002','Lennon Thomson'),
('C0003','Saarah Boyle'),
('C0004','Doris Kaye'),
('C0005','Giorgio Avalos'),
('C0006','River Kirby');
Insert INTO Item VALUES('I0001','Laptop','60000'),
('I0002','pen','5'),
('I0003','bag','1500'),
('I0004','earphone','1000'),
('I0005','washing machine','30000'),
('I0006','TV','100000'),
('I0007','mouse','5000'),
('I0008','shirt','3000'),
('I0009','watch','500000'),
('I0010','smartphone','90000'),
('I0011','helmet','2500'),
('I0012','jeans','5000'),
('I0013','shoes','6000'),
('I0014','External HDD','4000'),
('I0015','PS4 Pro','40000');

INSERT INTO Cust_Order VALUES ('O0001','2019-03-18','C0001',DEFAULT),


('O0002','2019-04-08','C0002',DEFAULT),
('O0003','2019-04-14','C0003',DEFAULT),
('O0004','2019-05-21','C0004',DEFAULT),
('O0005','2019-06-23','C0001',DEFAULT),
('O0006','2019-08-08','C0006',DEFAULT),
('O0007','2019-11-08','C0001',DEFAULT),
('O0008','2019-12-22','C0005',DEFAULT),
('O0009','2020-01-26','C0001',DEFAULT),
('O0010','2020-02-17','C0002',DEFAULT);

INSERT INTO ORDER_ITEM VALUES('O0001','I0003','1'),


('O0001','I0001','1'),
('O0001','I0004','1'),
('O0001','I0005','1'),
('O0002','I0010','4'),
('O0003','I0005','2'),
('O0003','I0007','3'),
('O0003','I0010','1'),
('O0004','I0008','2'),
('O0005','I0002','1'),
('O0005','I0004','1'),
('O0005','I0005','1'),
('O0005','I0009','1'),
('O0006','I0006','2'),
('O0006','I0001','2'),
('O0006','I0015','2'),
('O0007','I0009','1'),
('O0007','I0010','1'),
('O0007','I0013','2'),
('O0008','I0011','1'),
('O0008','I0012','1'),
('O0008','I0015','1'),
('O0009','I0013','2'),
('O0009','I0015','2'),
('O0010','I0014','2'),
('O0010','I0015','1'),
('O0010','I0005','5');

Solving Queries

b.) Develop SQL query to list the details of customers who have placed more than 3 orders..

Ans. select Customerno,Cname from Customer natural JOIN Cust_Order


group by Customerno
having COUNT(Orderno)>3;

c.) Develop a SQL query to list details of items whose price is less than the average price of
all items in each order.

Ans. with Average as (select Orderno,AVG(unit_price) AVG_Order_Val


from Item natural JOIN ORDER_ITEM
group by Orderno)

SELECT Orderno,Itemno,Item_name
from Order_Item natural join Average natural join Item
where unit_price< AVG_Order_Val;

d.) Develop a SQL query to list the orderno and number of items in each order.

Ans. select Orderno,Count(Itemno) as Total_Items


FROM ORDER_ITEM
GROUP BY Orderno;

e.) Develop a SQL query to list the details of items that are present in 25% of the orders.

Ans. select Itemno,Item_name,Count(Orderno)*100/10 as Presence


FROM Order_item natural join Item
GROUP BY Itemno
having Presence>25;

f.) Develop an update statement to update the value of Ord_amt.

Ans. UPDATE Cust_Order


SET Odr_amt=(select sum(unit_price*qty)
from Order_Item natural join Item
where Cust_Order.Orderno=Orderno
group by Orderno);
g.) Create a view that keeps track of detail of each customer and number of Order placed.

Ans. CREATE VIEW Detail as (


SELECT Customerno,Cname,COUNT(Orderno) as Orders_placed
FROM Customer natural JOIN Cust_Order
GROUP by Customerno);

Schema for employee

CREATE TABLE employee(eid int primary key,


managerid int references employee);

INSERT INTO employee VALUES(1,null),


(2,1),
(3,2),
(4,1);

Solving Queries

Q1. Run a delete Query

DELETE FROM employee


WHERE eid=2;

Q2. Run a delete Query Using ON CASCADE

DELETE FROM employee


WHERE eid=2;

You might also like