0% found this document useful (0 votes)
62 views18 pages

Customer-Sale Database Schema and Queries

The document describes experiments involving database schemas and queries on tables created based on those schemas. The first experiment involves a customer-sales schema with tables for customers, items, and sales. Various queries are written to retrieve customer names and item details for current sales, total bill details, customers who bought high priced items, number of products bought by each customer, and more. The second experiment involves a student library schema with tables for students, memberships, books, and book issues. Queries retrieve student-membership details, current book issues, students who borrowed a particular author's books, books borrowed by a specific student, and more. The third experiment has an employee-pay schema with tables for employees, departments, pay details,

Uploaded by

cihalab698
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views18 pages

Customer-Sale Database Schema and Queries

The document describes experiments involving database schemas and queries on tables created based on those schemas. The first experiment involves a customer-sales schema with tables for customers, items, and sales. Various queries are written to retrieve customer names and item details for current sales, total bill details, customers who bought high priced items, number of products bought by each customer, and more. The second experiment involves a student library schema with tables for students, memberships, books, and book issues. Queries retrieve student-membership details, current book issues, students who borrowed a particular author's books, books borrowed by a specific student, and more. The third experiment has an employee-pay schema with tables for employees, departments, pay details,

Uploaded by

cihalab698
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Experiment:-1

1. Database Schema for a customer-sale scenario Customer (Cust id: integer, Cust name: string)
Item (item_id: integer, item name: string, price: integer) Sale (bill no: integer, bill data: date,
cystoid: integer, item_id: integer, qty_sold: integer) For the above schema, perform the following
— Create the tables with the appropriate integrity constraints Insert around 10 records in each of
the tables.
Query:-
create table customer(cust_id int primary key, cust_name varchar(255) not null);
Output:-

create table item(item_id int primary key,item_name varchar(255) not null,item_price int);
Output:-

create table sale(bill_no int primary key,bii_date date,cust_id int not null,
item_id int not null,qty_sold int not null,foreign key (cust_id) references
customer(cust_id),foreign key (item_id ) references item(item_id));
Output:-

Query:-
insert into customer (cust_id,cust_name) values (1,'vashu'),(2,'abhay'),(3,'yash'),
(4,'jonh'),(5,'surbhi'),(6,'sam'),(7,'ashutosh'),(8,'mohit'),(9,'priyanka'),
(10,'muktanand');
Output:-
insert into item (item_id,item_name,item_price ) values(1,'laptop',53000),
(2,'mouse',799),(3,'keyboard',999),(4,'ssd',2999),(5,'key+mouse',1599),
(6,'monitor',17999),(7,'motherboard',6999),(8,'RAM',3999),(9,'printer',15999),
(10,'graphicscard',8999);
Output:-

insert into sale(bill_no,bii_date,cust_id,item_id,qty_sold) values


(001,'1 may 2023',1,1,2),(002,'2 may 2023',2,2,7),(003,'3 may 2023',3,3,5),
(004,'5 may 2023',4,4,3),(005,'6 may 2023',5,5,9),(006,'9 may 2023',6,6,1),
(007,'10 may 2023',7,7,2),(008,'13 may 2023',8,8,4),(009,'14 may 2023',9,9,6),
(010,'15 may 2023',10,10,5);
Output:-

(a)List all the bills for the current date with the customer names and item
numbers.
Query:-
select s.bill_no,c.cust_name,s.item_id from sale s join customer c
on s.cust_id=c.cust_id where bii_date='5 may 2023'
Output:-

(b)List the total Bill details with the quantity sold, price of the item and the
final amount.
Query:-
select s.bill_no,c.cust_name,i.item_name ,s.qty_sold,i.item_price,s.qty_sold*i.item_price as
final_amount from sale s join customer c on s.cust_id =c.cust_id join item i on
s.item_id=i.item_id;

Output:-

(c) List the details of the customer who have bought a product which has a
price>5000.
Query:-
select c.cust_id,c.cust_name from customer c join sale s on s.cust_id =c.cust_id join item i
on i.item_id = s.item_id where item_price > 5000;
Output:-
(d)Give a count of how many products have been bought by each customer.
Query:-
select cust_id,count(*) as num_product_bought from sale Group by cust_id;
Output:-

(e)Give a list of products bought by a customer having cust_id as 5.


Query:-
select i.item_id, i.item_name from item i join sale s on i.item_id=s.item_id where
s.cust_id=5;
Output:-

(f)List the item details which are sold as of today.


Query:-
select i.item_id, i.item_name from item i join sale s on i.item_id=s.item_id where bii_date='14
may 2023';
Output:-

(g)Create a view which lists out the bill no, bill date, cust_id, item_id, price,
qty_sold, amount.
Query:-
create view bill_detail as selects.bill_no,s.bii_date,s.cust_id,s.item_id,i.item_price, s.qty_sold
,i.item_price * s.qty_sold as amount from sale s join item i on s.item_id=i.item_id ;
Output:-

(h)Create a view which lists the daily sales date wise for the last one week.
Query:-
create view daliy_sales1 as select i.item_name , c.cust_name from item i join sale s on
s.item_id=i.item_id join customer c on s.cust_id=c.cust_id WHERE s.bii_date >=
DATEADD(day,-7, GETDATE());
select * from daliy_sales1;
output:-
Experiment:-2
2. Database Schema for a Student Library scenario Student (Stud no: integer, Stud name: string)
Membership (Mem_no: integer, Stud no: integer) Book(book no: integer, book_name:string,
author: string) Iss_rec (iss_no: integer, iss_date: date, Mem_no: integer, book_no: integer) .
(a) Create the tables with the appropriate integrity constraints.
Query:-
create table student(stu_no int primary key,stu_name varchar(255) not null);
create table membership(Mem_no int primary key,stu_no int not null,
foreign key (stu_no) references student(stu_no));
create table book(book_no int primary key,book_name varchar(255) not null,
author varchar(255) not null);

create table iss_rec(iss_no int primary key,iss_date date not null,Mem_no int not null,book_no
int not null,foreign key (mem_no) references membership(mem_no),foreign key (book_no)
references book(book_no));
Output:-

(b) Insert around 10 records in each of the tables.


Query:-
insert into student (stu_no,stu_name) values (1,'sam'),(2,'sant'),(3,'ashutosh'),
(4,'abhay'),(5,'aman'),(6,'yashika'),(7,'aliya'),(8,'shivam'),(9,'satyam'),(10,'vashu');

insert into membership ( mem_no,stu_no) values (001,1),(002,2),(003,3),(004,4),


(005,5),(006,6),(007,7),(008,8),(009,9),(010,10);

insert into book(book_no,book_name,author) values(01,'atomic habits','james clear'),


(02,'psychology of money', 'morgan'),(03,'harry potter', 'j.k'),(04,'the lords of the ring', 'j.r.r'),
(05,'the shining','stephen'),(06,'the little prince','antoine'),
(07,'database system', 'c.j date'),(08,'daa','jp patra'),(09,'discreatemaths','sapna sarkar'),(10,'oops
with java','vijaya bhaskar');

insert into iss_rec (iss_no,iss_date,mem_no,book_no) values(2,'2 may 2023',002,02),


(3,'3 may 2023',003,03),(4,'4 may 2023',004,04),(5,'21 may 2023',005,05),
(6,'23 may 2023',006,06),(7,'25 may 2023',007,07),(8,'26 may 2023',008,08),
(9,'26 may 2023',009,09),(10,'27 may 2023',010,10);
Output:-
(c) List all the student names with their membership numbers.
Query:-
select s.stu_name,m.mem_no from student s join membership m on s.stu_no=m.stu_no;
output:-

(d) List all the issues for the current date with student and Book names.
Query:-
select s.stu_name,b. book_name,i.iss_no from iss_rec i join book b on i.book_no=b.book_no join
membership m on m.Mem_no=i.Mem_no join student s on s.stu_no=m.stu_no where
iss_date='25 may 2023';
Output:-

(e)List the details of students who borrowed book whose author is CJ DATE.
Query:-
select s.stu_no,s.stu_name ,b.book_name from iss_rec i join Membership m on
m.Mem_no=i.Mem_no join student s on s.stu_no= m.stu_no join book b on b.book_no=
i.book_no where b.author='c.j date';
Output:-

(F)Give a count of how many books have been bought by each student.
Query:-
select s.stu_no, count(i.book_no) as total_book from student s,membership m, book b, iss_rec
i where s.stu_no=m.stu_no and b.book_no=i.book_no group by s.stu_no;
Output:-

(G)Give a list of books taken by student with stud_no as 5.


Query:-
select b.book_no,b.book_name,b.author from iss_rec i join membership m on
m.mem_no=i.mem_no join student s on s.stu_no= m.stu_no join book b on i.book_no=
b.book_no where s.stu_no=5;
Output:-

(h) List the book details which are issued as of today.


Query:-
select b.book_no, b.book_name ,b.author from iss_rec i join book b on
i.book_no=b.book_no where i.iss_date<=SYSDATETIME();
Output:-
(i)Create a view which lists out the iss_no, iss _date, stud_name, book name.
Query:-
create view issued_book as select i.iss_no ,i.iss_date ,s.stu_name ,b.book_name from iss_rec i
,student s, book b ,membership m where s.stu_no=m.stu_no and b.book_no=i.book_no and
m.mem_no=i.mem_no;
select *from issued_book;
Output:-

(j) Create a view which lists the daily issues-date wise for the last one week.
Query:-
create view daliy_issued as select i.iss_no ,i.iss_date from iss_rec i
WHERE i.book_no>= DATEADD(day,-7, GETDATE());
select * from daliy_issued;
Output:-
Experiment:-3
3. Database Schema for a Employee-pay scenario employee (emp_id: integer, emp_name: string)
department (dept_id: integer, emp_name: string) pay details (emp_id: integer, dept_id: integer,
basic: integer, deductions: integer, additions: integer, DOJ: date) payroll (emp_id: integer,
pay_date: date) For the above schema, perform the following:

(a) Create the tables with the appropriate integrity constraints.


Query:-
create table employee (emp_id int primary key, emp_name varchar(255) not null);
create table department (dept_id int primary key, emp_name varchar(255));
create table pay_details(emp_id int, dept_id int , basics int,deductions int, additions int, DOJ
date,foreign key (emp_id) references employee(emp_id),foreign key (dept_id) references
department(dept_id));
create table payroll (emp_id int, pay_date date , foreign key (emp_id) references
employee(emp_id));
Output:-

(b) Insert around 10 records in each of the table.


Query:-
insert into employee (emp_id,emp_name) values
(1,'a'),(2,'b'),(3,'c'),(4,'d'),(5,'e');

insert into department (dept_id,emp_name) values


(1,'a'),(2,'b'),(3,'c'),(4,'d'),(5,'e');

insert into pay_details(emp_id,dept_id,basics,deductions,additions,DOJ) values


(1,1,16000,1500,1000,'7 june 2023'), (2,2,18000,2000,1300,'8 june 2023'),
(3,3,20000,2500,2000,'9 june 2023'), (4,4,22000,2400,1800,'10 june 2023'),
(4,4,30000,3000,2500,'11 june 2023');

insert into payroll (emp_id,pay_date) values (1,'7 july 2023'),(2,' 8 july 2023'),
(3,'9 july 2023'),(4,'10 july 2023'), (5,'11 july 2023');

Output:-
(c) List the employee details department wise.
Query:-
select e.emp_id, e.emp_name, d.dept_id from employee e join department d
on e.emp_name= d.emp_name;
Output:-

(d) List all the employee names who joined after particular date.
Query:-
select e.emp_name from employee e join pay_details p on e.emp_id=p.emp_id
where DOJ>='9 june 2023';
Output:-

(e)List the details of employees whose basic salary is between 10,000 and
20,000.
Query:-
select e.emp_id, e.emp_name , p.basics from employee e join pay_details p on
e.emp_id=p.emp_id where p.basics between 10000 and 20000;
Output:-

(f)Give a count of how many employees are working in each department.


Query:-
select e.emp_id, count(e.emp_id) as total from employee e , department p
where e.emp_id=p.dept_id group by e.emp_id;
Output:-

(g)Give a name of the employees whose net salary>10,000.


Query:-
select e.emp_id ,e.emp_name from employee e join pay_details p on
p.emp_id=e.emp_id where basics>10000;
Output:-

(h) List the details for an employee_id=5.


Query:-
select e.emp_id,e.emp_name,p.basics,p.DOJ,p.dept_id from pay_details p join
employee e on e.emp_id=p.emp_id where e.emp_id=5;

Output:-

(i)Create a view which lists out the emp_name, department, basic, deductions,
net salary.
Query:-
create view lists as select e.emp_name ,p.basics,p.deductions,p.additions from pay_details p
join employee e on e.emp_id=p.emp_id;
select *from lists;
Output:-

(j) Create a view which lists the emp_name and his netsalary.
Query:-
create view lists1 as select e.emp_name,p.basics from pay_details p join
employee e on e.emp_id=p.emp_id;
select *from lists1;
output:-
Experiment:-4
4.Database Schema for a student-Lab scenario Student (stud_no: integer,
stud_name: string, class: string) Class (class: string, descript: string) Lab (mach_no:
integer, Lab_no: integer, description: String) Allotment (Stud_no: Integer,
mach_no: integer, dayof week: string) For the above schema, perform the
following—
(a) Create the tables with the appropriate integrity constraints
Query:-
create table student1(stud_no int primary key, stud_name varchar(255), class varchar(255));
create table Class (class varchar(255), descript varchar(255));
create table Lab (mach_no int primary key, Lab_no int, descriptions varchar(255));
create table Allotment (stud_no Int, mach_no integer, dayof_week varchar(20) ,
foreign key (stud_no) references student1(stud_no),
foreign key (mach_no) references Lab(mach_no));
Output:-

(b) Insert around 10 records in each of the tables


Query:-
insert into student1 (stud_no,stud_name, class) values
(1,'anshika','11'),(2,'vashu','12'),(3,'vaishnavi','12'),(4,'yash','12'),
(5,'soumya','12'),(6,'abhijeet','11'),(7,'shobit','11'),(8,'muktanand','12'),
(9,'priyanka','12'),(10,'preetpal','12');
insert into Class(class,descript) values
('11','phy lab'),('12','phy lab'),('12','che lab'),('12','phy'),
('12','che lab'),('11','che lab'),('11','phy lab'),('12','che lab'),
('12','che'),('12','phy');
insert into Lab (mach_no,Lab_no,descriptions) values
(1,01, 'machine no 1 lab no.01'),(2,01, 'machine no 2 lab no.01'),(3,02, 'machine no 3 lab no.02'),
(4,01, 'machine no 4 lab no.01'),(5,02, 'machine no 5 lab no.02'),(6,02, 'machine no 6 lab no.02'),
(7,01, 'machine no 7 lab no.01'),(8,01, 'machine no 1 lab no.01'),(9,02, 'machine no 9 lab no.10'),
(10,01, 'machine no 10 lab no.01');

insert into Allotment(stud_no,mach_no,dayof_week) values


(1,1,'mon'),(2,2,'tues'),(3,3,'wed'),(4,4,'mon'),(5,5,'thur'),(6,6,'wed'),
(7,7,'mon'),(8,8,'thur'),(9,9,'wed'),(10,10,'tues');
Output:-

(c) List all the machine allotments with the student names, lab and machine
number.
Query:-
select s.stud_name,l.Lab_no,l.mach_no from student1 s ,Lab l,
Allotment a where a.stud_no=s.stud_no and l.mach_no =a.mach_no;
Output:-

(d) List the total number of lab allotments day wise.


Query:-
select l.Lab_no,l.descriptions,a.dayof_week from Allotment a, Lab l where
a.mach_no=l.mach_no;
Output:-
(e) Give a count of how many machines have been allocated to the physics
class.
Query:-
select count(mach_no) as total from Allotment a join student1 s on a.stud_no=s.stud_no
where s.class='12';
Output:-

(f) Give a machine allotment detail of the stud_no 5 with his personal and
class details.
Query:-
select s.stud_no,s.stud_name,s.class,a.mach_no from student1 s,Allotment a
where a.stud_no =s.stud_no and s.stud_no=5;
Output:-

(g) Count for how many machines have been allocated in Lab_no 1 for the day
of the week as ―Monday‖.
Query:-
select count(mach_no) as count from Allotment a where a.dayof_week='mon';
Output:-
(h) How many students class wise have allocated machines in the labs?
Query:-
select count(a.stud_no) as 'allocated students in the labs' ,s.class from
Allotment a , student1 s where a.stud_no=s.stud_no group by s.class;
Output:-

(i) Create a view which lists out the stud_no, stud_name, mach_no, lab_no,
dayofweek.
Query:-
create view details as select s.stud_no,s.stud_name,a.mach_no,a.dayof_week,l.Lab_no
from student1 s,Allotment a,Lab l where s.stud_no=a.stud_no and a.mach_no=l.mach_no;
Output:-

(j) Create a view which lists the machine allotment details for ―Thursday.
Query:-
create view details1 as select a.mach_no,a.stud_no,l.Lab_no from Allotment a , Lab l where
l.mach_no=a.mach_no and dayof_week='thur' ;
output:-

You might also like