SQL question Consider the following relational schema Empeid

SQL question

Consider the following relational schema. Emp(eid integer, ename: string, age: integer, salary: real) Works (eid: integer, did: integer, pet-time: integer) Dept (did: integer, dname: string, budget: real, managerid: integer) Note that a manager is an employee as well and their manager id and employee id are the same. An employee can work in more than one department. The pet time field of the Works relation shows the percentage of time that a given employee works in a given department. Formulate the following queries in SQL: Print the name of each employee whose salary exceeds the budget of all of the departments that he or she works in Find the managerids of managers who manage only departments with budgets greater than $1 million Find the enames of managers who manage the departments with the largest budget Find the managerids of managers who control the largest amount of total budget. As an example, if a manager manages two departments, the amount of total budget for him will be the sum of budgets of two departments. We want to find managers that have max total budget.

Solution

a.

SELECT E.ename FROM Emp E WHERE E.salary > ALL (SELECT D.budget FROM Dept D, Works W WHERE E.eid = W.eid AND D.did = W.did)

b.

SELECT DISTINCT D.managerid FROM Dept D WHERE 1000000 < ALL (SELECT D2.budget FROM Dept D2 WHERE D2.managerid = D.managerid )

c.

SELECT E.ename FROM Emp E WHERE E.eid IN (SELECT D.managerid FROM Dept D WHERE D.budget = (SELECT MAX (D2.budget) FROM Dept D2))

d.

SELECT DISTINCT tempD.managerid FROM (SELECT DISTINCT D.managerid, SUM (D.budget) AS tempBudget FROM Dept D GROUP BY D.managerid ) AS tempD WHERE tempD.tempBudget = (SELECT MAX (tempD.tempBudget) FROM tempD)

SQL question Consider the following relational schema. Emp(eid integer, ename: string, age: integer, salary: real) Works (eid: integer, did: integer, pet-time:

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site