0% found this document useful (0 votes)
24 views22 pages

DBMS File Section B

Uploaded by

Raj Mehra
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)
24 views22 pages

DBMS File Section B

Uploaded by

Raj Mehra
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
You are on page 1/ 22

MAHARISHI DAYANAND UNIVERSITY

AKIDO College of Engineering

DATABASE MANAGEMENT SYSTEM


DATABASE MANAGEMENT SYSTEM LAB PRACTICAL

FILE
DATABASE MANAGEMENT SYSTEM LAB CODE :LC-CSE-209G

SUBMITTED BY: RUDRAKSHA SHARMA

Registration No. : 2213091704

Course : B. Tech

INDEX
S no. Assignment Date sign

1. Assignment1 -Bank sql 4 sept, 11 sept

2 Assignment 2- Queries 18 sept, 25 sept

2. Assignment 2 -Dreamhome 3 oct, 10 oct


sql
3. Assignment3 -Queries 17 oct, 20 oct

4 View 31 oct
5 Queries 7 nov,14 Nov
1. Bank sql

➢ Salesman table

➢ Customer table

➢ Orders table
2. Queries
1. Display name and commission of all the salesman. ANS: select

name,commission from salesman;

2. Retrieve salesman id of all salesman from orders table without any


repeats.

ANS: select distinct salesman_id from orders;

3. Display names and city of salesman who belong to the city of Paris.

ANS: select name,city from salesman where city=’Paris’;

4. Display all the information for those customers with a grade of 200.

ANS: select * from customer where grade=200;

5. Display the order number,order date and the purchase amount for
order(s) which will be delivered by the salesman with ID 5001.

ANS: select order_no,order_date,purch_amt from orders where


salesman_id=5001;
6. Display all the customers, who are either belongs to the city New York
or not had a grade above 100.

ANS: select * from customer where city=’New York’ OR NOT grade>100;

7. Find those salesman with all information who gets the commission
within the range of 0.12 and 0.14.

ANS: select salesman_id,name,city,commission from salesman where


commission between 0.10 and 0.12;

8. Find all those customers with all information whose names are ending
with the letter ‘n’.

ANS: select * from customer where customer_name like ‘%n’;

9. Find those salesman with all the information whose name containing
the
1st character is ’N’ and the 4th character is ‘l’ abd rests may be any
character.

ANS: select * from salesman where name like ‘N__l%’;


10. Find the customer with all the information who does not get any grade
except NULL.

ANS: select * from customer where grade IS NULL;

11. Fing the total purchase amount of all orders. ANS: select

sum(purch_amt) from orders;

12. Find the numbers of salesman currently listing for all their customers.

ANS: select count(distinct salesman_id) from orders;

13. Find the highest grade for each of all the cities of all the customers.

ANS: select city,max(grade) from customer group by cutomer;

14. Find the highest purchase amount of ordered by the each customer
with their ID and highest purchase amount.

ANS: select customer_id,max(purch_amt) from orders group by


customer_id;
15. Find the highest purchase amount of ordered by the each customer on
a particular date with their ID, order date and highest purchase amount.

ANS: select cutomer_id,order_date,max(purch_amt) from orders group by


customer_id,order_date;

16. Find the highest purchase amount on a date ‘2016-08-17’ for each
salesman with their ID.

ANS: select salesman_id,max(purch_amt) from orders where


order_date=’2016-08-17’ group by salesman_id;

17. Find the highest purchase amount of with their customer ID and order
date, for those customers who have the highest purchase amount in a
day is more than 2000.

ANS: select customer_id,order_date,max(purch_amt) from orders group by


customer_id,order_date having max(purch_amt)>2000.00;

18. Write a SQL statement that counts all orders for a date August 17th
2016.
ANS: select count(*) from orders where order_date=’2016-08-17’;

19. Find the name and city of those customers and salesman who lives in
the same city.

ANS: select c.customer_name,s.name,s.city from salesman as s,customer


as c where s.city=c.city;

20. Find the name of all the customers along with the salesman who works
for them.

ANS: select customer.customer_name,salesman.name from


customer,salesman where salesman.salesman_id=customer.salesman_id;

21. Display all those orders by the customers not located in the same cities
where their salesmen live.

ANS: select order_no,customer_name,orders.customer_id,orders.salesm


an_id from salesman,customer,orders where customer.city<>salesman.city
and orders,customer_id=customer.customer_id and
orders.salesman_id=salesman.salesman_id;

22. Display all the orders issued by the salesman ‘Paul Adams’ from the
orders table.
ANS: select * from orders where salesman_id=(select salesaman_id from
salesman where name=’Paul Adams’);

23. Display all the orders which values are greater than the average order
value for the 10th October 2016.

ANS: select * from orders where purch_amt>(select avg(purch_amt) from


orders where irder_date=’2016-10-10’);

24. Find all the orders attributed to salesman in Paris.

ANS: select * from orders where salesman_id IN(select salesman_id from


salesman where city=’Paris’);

25. Extract the data from the orders tables for the salesman who earned
the maximum commission.

ANS: select order_no,purch_amt,order_date,salesman_id from orders


where salesman_id IN(select salesman_id from salesman where
commission=(select max(commission) from salesman));

26. Find the name and IDs of all salesman who had more than one
customer.
ANS: select salesman_id,name from salesman as a where 1<(select
count(*) from customer as c where c.salesman_id=a.salesman_id);

27. Write a query to find all the salesman who worked for only one
customer.

ANS: select c.salesman_id,s.name,s.city,s.commission from salesman


s,customer c where s.salesman_id=c.salesman_id group by
c.salesman_id,s.name having count(c.salesman_id)=1;

28. Display all the orders that had amounts that were greater than at least
one of the orders from September 10th 2016.

ANS: select * from orders where purch_amt>ANY(select purch_amt from


orders where ord_date=’2016-09-10’);

29. Display only those customers whose grades are in fact, higher than
every customer in New York.

ANS: select * from customer where grade>ALL(select grade from customer


where city=’New York’);
3. Dreamhome Sql

➢branch Table

➢client Table
➢privateowner Table

➢propertyforrent Table

➢registration table
➢staff table

➢viewing table

4. Queries
1. Produce a list of salaries for all staff, showing only the staff number,
the first and last names, and the salary details.

ANS: SELECT staffNo, fName, IName, salary FROM Staff;


2. List the unique property numbers of all properties that have been
viewed.

ANS: SELECT DISTINCT propertyNo FROM Viewing;

3. Write a SQL query to list the staff who work in London. Please show
their first name, last name and branchno.

ANS: SELECT fname,lname,b.branchno FROM Staff s, Branch b WHERE


s.branchno=b.branchno and b.city='London';

4. Produce a list of monthly salaries for all staff, showing the staff
number, the first and last names, and the salary details.

ANS: SELECT staffNo, fName, lName, salary/12 FROM Staff;

5. List all staff with a salary greater than $10,000.


ANS: SELECT staffNo, fName, IName, position, salary FROM Staff
WHERE salary > 10000;

6. Write a SQL query to list the unique position for female staff with salary
less than 10000.

ANS: SELECT distinct position FROM Staff WHERE sex='F' and salary <
10000;

7. List the addresses of all branch offices in London or Glasgow.

ANS: SELECT street FROM Branch WHERE city = 'London' OR city =


'Glasgow';

8. List all staff with a salary between $20,000 and $30,000.

ANS: SELECT staffNo, fName, IName, position, salary FROM Staff


WHERE salary > = 20000 AND salary < = 30000;

9. List all managers and supervisors.

ANS: SELECT staffNo, fName, IName, position FROM Staff WHERE


position IN ('Manager', 'Supervisor');
10. What is the output of the following SQL query?

SELECT clientno, propertyno FROM Viewing WHERE comment!='';

11. Find all owners with the string ‘Glasgow’ in their address.

ANS: SELECT ownerNo, fName, IName, address, telNo FROM


PrivateOwner WHERE address LIKE '%Glasgow%';

12. List the details of all viewings on property PG4 where a comment has
not been supplied.

ANS: SELECT clientNo, viewDate FROM Viewing WHERE propertyNo =


'PG4' AND comment IS NULL;

13. Produce a list of salaries for all staff, arranged in descending order of
salary.

ANS: SELECT staffNo, fName, lName, salary FROM Staff ORDER BY


salary DESC;
14. Produce an abbreviated list of properties arranged in order of property
type.

ANS: SELECT propertyNo, type, rooms, rent FROM PropertyForRent


ORDER BY type;

15.. Write a SQL query to display the total salary of all staff.

ANS: SELECT SUM(salary) from Staff;

16. Write a SQL query to list the unique branchno that branch has at least
two female staff.

ANS: SELECT distinct branchno FROM Staff group by branchno,sex


having count(sex) >= 2 and sex='F';

17. Write the SQL query to show the branchno and the maximum salary for
each branch in the Staff table. The output should be sorted from high to
low based on the max salary of each branch;

ANS: SELECT branchno, max(salary) FROM Staff group by branchno order


by max(salary) desc;
18. Can we run the following SQL query without any problem?

SELECT fname FROM Staff WHERE salary > max(salary);

ANS: NO, the statement invalid use of group function. Aggregation cannot
be in WHERE.

19. Write the SQL query to find how many properties cost more than $350
per month to rent?

ANS: SELECT COUNT(*) AS myCount FROM PropertyForRent WHERE


rent > 350;

20. How many different properties were viewed in May 2013?

ANS: SELECT COUNT(DISTINCT propertyNo) AS myCount FROM


Viewing WHERE viewDate BETWEEN '2013-05-01' AND '2013-05-31';

21. Find the total number of Managers and the sum of their salaries.

ANS: SELECT COUNT(staffNo) AS myCount, SUM(salary) AS mySum


FROM Staff WHERE position = 'Manager';

22. Find the minimum, maximum, and average staff salary.

ANS: SELECT MIN(salary) AS myMin, MAX(salary) AS myMax,


AVG(salary) AS myAvg FROM Staff;
23. Find the number of staff working in each branch and the sum of their
salaries.

ANS: SELECT branchNo, COUNT(staffNo) AS myCount, SUM(salary) AS


mySum FROM Staff GROUP BY branchNo ORDER BY branchNo;

24. For each branch office with more than one member of staff, find the
number of staff working in each branch and the sum of their salaries.

ANS: SELECT branchNo, COUNT(staffNo) AS myCount, SUM(salary) AS


mySum FROM Staff GROUP BY branchNo HAVING COUNT(staffNo) > 1
ORDER BY branchNo;

25. What is the output of the following SQL statement? SELECT

count(comment) FROM Viewing;

26. Write a SQL query to find the staff name (first name, last name) who
has salary higher than the average salary of branchno =’B003’;

ANS: SELECT fname,lname FROM Staff where salary > (select


avg(salary) from Staff WHERE branchno ='B003');
27. Can we run the following query without any problem?

SELECT city FROM Branch WHERE branchNo = (SELECT branchNo


FROM Staff where sex='F');

ANS: Error, Subquery returns more than 1 row. Cannot use =, should use
IN

28. Write a SQL query to display the staffno and working branch city for the
staff who has the lowest salary.

ANS: SELECT s.staffno,b.city FROM Staff s, Branch b WHERE


s.branchno=b.branchno and s.salary in (SELECT min(salary) FROM Staff);

29. List the staff who work in the branch at ‘163 Main St’.

ANS: SELECT staffNo, fName, IName, position FROM Staff WHERE


branchNo = (SELECT branchNo FROM Branch WHERE street = '163 Main
St');

30. List all staff whose salary is greater than the average salary, and show
by how much their salary is greater than the average.

ANS: SELECT staffNo, fName, LName, position, (salary - (SELECT


AVG(salary) FROM Staff)) AS salDiff FROM Staff WHERE salary >
(SELECT AVG(salary) FROM Staff);
4. Views
➔ Displaying views with the help of Dreamhome sql
5. Queries

1. Create a view so that the manager at the branch B003 can see the
details only for staff who work in his or her office.

ANS: create view Manager3Staff as select * from staff where


branchNo=’B003’;

2. Create a view of the staff details at branch B003 that excludes salary
information, so that only manages can access the salary details for
staff who work at their branch.

ANS: create view Staff3 as select staffNO,fname,lname,position,sex


from staff where branchNo=’B003’;
3. Create a view vTest to show the first name and the working city for
staff who was born after January first 1960.

ANS: create view vTest as select fname,city from staff s,branch b


where s.branchNo=b.branchNo and DOB>’1960-01-01’;

4. Create a view of staff who manages properties for rent, which


includes the branch number they work at, their staff number and the
number of properties they manage.
ANS: create view StaffPropCnt (barnchNo,staffNo,cnt) as select
s.branchNo,s.staffNo,count(*) from staff s,Propertyforrent p where
s.staffNo=p.staffNo group by s.branchNo,s.staffNo;

You might also like