Sei sulla pagina 1di 4

Shri G. S.

Institute of Technology and Science, Indore


Department of Computer Engineering
Assignment 2-#2

Part A
Emp(eid: integer, ename: string, age: integer, salary: integer)
Works(eid: integer, did: integer, hrs: integer)
Dept(did: integer, dname: string, budget: integer, managerid: integer)

1. Print the name of each employee whose salary exceeds the budget of all of the departments
that he or she works in.
Ans.
Select Ename from Emp E where salary> ALL( select budget from depart D,works W where
E.eid=W.eid And D.did=W.did );
+-------+
| Ename |
+-------+
| SJ |
+-------+
2. Find the enames of managers who manage only departments with budgets larger than $1
million, but at least one department with budget less than $5 million.
SELECT E.ename
FROM Emp E, Dept D
WHERE E.eid = D.managerid GROUP BY E.Eid, E.ename
HAVING EVERY (D.budget > 1000000)
AND ANY (D.budget < 5000000);

net pe yahi ans 2 ka lekin runn nhi hi rha


3. If a manager manages more than one department, he or she controls the sum of all the
budgets for those departments. Find the managerids of managers who control more than $5 million.
Select managerid from depart group by managerid having sum(budget)>5000000;
Select managerid from depart group by managerid having sum(budget)>5000000;
+-----------+
| managerid |
+-----------+
| 5|
+-----------+
4. Write a query that displays the last name (with the first letter uppercase and all other letters
lowercase) and the length of the last name for all employees whose name starts with the letters J, A,
or M. Give each column an appropriate label. Sort the results by the employees’ last names.
5. Create a query that displays the first eight characters of the employees’ last names and indicates
the amounts of their salaries with asterisks. Each asterisk signifies a thousand dollars. Sort the data
in descending order of salary. Label the column.

Part B
Salespeople(snum,sname,city,comm%)
Customer(cnum,cname,city,rating,snum)
Orders(onum, amt, odate, cnum, snum)
customer:
+------+--------+---------+--------+------+
| cnum | cname | city | rating | snum |
+------+--------+---------+--------+------+
| 10 | ashok | indore | 8| 1|
| 20 | yuvraj | sagar | 9| 2|
| 30 | abc | gwalior | 9| 3|
| 40 | xyz | ratlam | 5| 5|
| 50 | pqrs | indore | 9| 6|
+------+--------+---------+--------+------+
salespeople:
| snum | sname | city | comm |
+------+----------+---------+------+
| 1 | Karishma | Indore | 10 |
| 2 | Somil | Sagar | 30 |
| 3 | Rini | gwalior | 30 |
| 4 | Fatema | ratlam | 25 |
| 5 | Shakshi | sagar | 35 |
| 6 | Deepali | Indore | 30 |
+------+----------+---------+------+
Orders:
+------+------------+------+------+
| onum | odate | cnum | snum |
+------+------------+------+------+
| 1 | 2014-08-22 | 10 | 1 |
| 2 | 2015-12-08 | 10 | 1 |
| 3 | 2016-05-10 | 20 | 2 |
| 4 | 2011-06-10 | 20 | 5 |
| 5 | 2011-06-20 | 20 | 2 |
| 6 | 2012-06-20 | 30 | 3 |
| 7 | 2009-08-08 | 40 | 4 |
| 8 | 2010-08-08 | 50 | 6 |
+------+------------+------+------+

1. Write a query that uses a subquery to obtain all orders for the customer named ‘Ashok’. Assume you
do not know his customer number
select o.onum,o.amt,o.odate from orders o,customer c where c.cnum=orders.cnum and c.snum=o.snum and
c.cname='Ashok';
or
select o.onum,o.odate from Orders o where cnum in (select cnum from customer c where
c.cname='Ashok');
+------+------------+
| onum | odate |
+------+------------+
| 1 | 2014-08-22 |
| 2 | 2015-12-08 |
+------+------------+
2. Write a query that produces the names and cities of all customers with the same rating as Yuvraj.
Write the query using Yuvraj’s cnum rather than his rating, so that it would still be usable if his rating is
changed.
Select name,city from customer where rating=(select rating from customer where cnum=(select cnum from
customer where cname='yuvraj'));
+--------+---------+
| cname | city |
+--------+---------+
| yuvraj | sagar |
| abc | gwalior |
| pqrs | indore |
+--------+---------+
3. Write a query that lists customers in a descending order of rating Output the rating field first,
followed by the customer’s names and numbers.
Select rating, cname,cnum from customer order by rating desc;
+--------+--------+------+
| rating | cname | cnum |
+--------+--------+------+
| 9 | yuvraj | 20 |
| 9 | abc | 30 |
| 9 | pqrs | 50 |
| 8 | ashok | 10 |
| 5 | xyz | 40 |
+--------+--------+------+
4. Which salesperson has earned the maximum commission?
Select snum,sname from salespeople where comm=(select max(comm) from salespeople);
5. +------+---------+
6. | snum | sname |
7. +------+---------+
8. | 5 | Shakshi |
9. +------+---------+
5. Does the customer who has placed the maximum number of orders have the maximum rating?

6.

Part C
Employee(Eid, Ename, Age, Salary, City, Hiredate, Managerid)
Dept(Did, Dname, City, Budget, Mid)
Workson(Eid,Did)
Project(Pid, Pname, Start date, Expected ending date)
Assign_pro(Pid, Eid, Assign date)

1. List of manager name of those employee who works on only department with budget greater then
10,000.
2. List details of employee along with department details.[Employee with department allotted and
employee with no department alloted.
3. Apply cross join, self join ,natural join ,inner join, left outer join, right outer join, full outer join
on employee and workson table.
4. Find ename of manager who manages department with largest budget.
5. List eid, ename and count of department having 2 or more department.
6. List employee name with manager name.
7. Name of employee who lives in city where department for which they work located .
8. For each project on which more than 2 employee works, retrieve project number, project name,
number of employee works on project.
9. For each department that has more than 5 employee , retrieve department number and no. of
employee whose salary is greater than 40000.
10. Display all employee who earn less than their manager.
11. Find out employee name who assigned project with expected duration greater than 2 years.
12. Name of employee who works on all projects.
13. Details of employee who works on any of the department(using in and exists)
14. List of employee who are senior to their manager.
15. Name and age of employee who works in both ‘computer science’ and ‘information technology’
department.
16. select nth max salary
17. List of employee whose salary is of 5 digit.
18. Details of employee who does not work in any department(using not in and not exists)
19. Display details of recently joined employee from employee table.
20. List of employee having same name.
21. Highest paid employee of department ‘computer science’.
22. Name of employee who manages more than 3 department.
23. List of employee name who works in 2 department.
24. Name of dept with most of the employee.
25. Find the fname and lname of the employees with more than 2 department and work
on more than 2 projects
26. Find out the detail of Top 3 earners of the company.
27. List the empno, ename, salary, dname of all the ‘Mgrs’ and ‘Analyst’ working in NEWYORK,
DALLAS with an exp more than 7 years .
28. List the details of the emps whose salaries more than the employee BLAKES(using self join and
subquery).

Potrebbero piacerti anche