Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following relational schema. An employee can work in more than one

ID: 3668903 • Letter: C

Question

Consider the following relational schema. An employee can work in more than one department; the pct_time field of the Works relation shows the percentage of time that a given employee works in a given department. Emp(eid: integer, ename: string, age: integer, salary: real) Works(eid: integer, did: integer, pct_time: integer) Dept(did: integer, dname: string, budget: real, managerid: integer) Write the following queries in SQL: 1. Print the names and ages of each employee who works in both the Hardware department and the Software department. 2. For each department with more than 20 full-time-equivalent employees (i.e. where the part-time and full-time employees add up to at least that many full-time employees), print the did together with the number of employees that work in that department. 3. Print the name of each employee whose salary exceeds the budget of all of the departments that he or she works in. 4. Find the managerids of managers who manage only departments with budgets greater than $1 mill on. 5. Find the enames of managers who manage the departments with the largest budgets. 6. 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. 7. Find the managerids of managers who control the largest amounts. 8. 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.

Explanation / Answer

#.Print the names and ages of each employee who works in both the Hardware department and the Software department.

Solution : select name,age from emp where emp.id in (select emp_id from dept inner join works on dept.id = works.dept_id
           where name='software' or name='hardware' group by emp_id having count(dept.name)=2)


#.For each department with more than 20 full-time-equivalent employees (i.e.,where the part-time and full-time employees
add up to at least that many full time employees), print the did together with the number of employees that work in that department.  

Solution : select w.dept_id,count(w.emp_id) as 'more than 20 full time' from works w group by w.dept_id having 2000 <
           (select sum(w1.pct_time) from works w1 where w1.dept_id = w.dept_id)
         
#.Print the name of each employee whose salary exceeds the budget of all of the departments that he or she works in.

Solution : select distinct emp.name from dept inner join works on dept.id = works.dept_id
            inner join emp on emp.id = works.emp_id where salary > budget

Find the manager_ids of managers who manage only departments with budgets greater than $1 million.          

Solution : select manager_id from dept where budget > 100000;

#.Find the names of managers who manage the departments with the largest budgets.

Solution : select distinct e.name from emp e,dept d where e.id = d.manager_id and d.budget =
           (select max(budget)from dept);
         
         
#.If a manager manages more than one department, he or she controls the sum of all the budgets for those departments.
Find the manager_ids and names of managers who control more than $5 million.

Solution : select name from emp where id = ( select d.manager_id from dept d where 50000000 < (select sum(d1.budget)
            from dept d1 where d1.manager_id = d.manager_id))

#.Find the manager_ids and names of managers who control the largest amounts.

Solution : select d.manager_id,e.name from emp e,dept d where d.manager_id = e.id and d.budget = (select max(budget)
            from dept);

#.Find the names of managers who manage only departments with budgets larger than $1 million, but at least one department
   with budget less than $5 million.

Solution : select dept.manager_id,emp.name from dept,emp where emp.id = dept.manager_id and dept.budget > 1000000
            and dept.budget < 500000 group by dept.manager_id , emp.name;
          
          

1. Write SQL queries to compute the average rating, using AVG; the
   sum of the ratings, using SUM; and the number of ratings, using
   COUNT.

Solution : SELECT AVG (S.rating) AS AVERAGE
           FROM Sailors S
           SELECT SUM (S.rating)
           FROM Sailors S
           SELECT COUNT (S.rating)
           FROM Sailors S

2. If you divide the sum just computed by the count, would the result
   be the same as the average? How would your answer change if these
   steps were carried out with respect to the age field instead of
   rating?

Solution : The result using SUM and COUNT would be smaller than the
           result using AVERAGE if there are tuples with
           rating = NULL.
           This is because all the aggregate operators, except for
           COUNT, ignore NULL values. So the first approach would
           compute the average over all tuples while the second
           approach would compute the average over all tuples with
           non-NULL rating values. However,if the aggregation is done
           on the age field, the answers using both approaches would
           be the same since the age field does not take NULL values

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote