Question

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

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, budget: real, managerid: integer)

In the above, please note that there is an attribute pcttime (of Integer type) in the Works table, as the schema shows. A tuple/row <1, 1, 50> in the Works Table indicates that the Employee 1 works in Department 1 with 50% of time, which is PART TIME. Another tuple/row <2, 2, 90> in the Works Table indicates that the Employee 2 works in Department 2 with 80% of time, which is also PART TIME. Finally, a tuple/row <3, 5, 100> in the Works Table indicates that the Employee 3 works in Department 5 with 100% of time, which is FULL TIME. The number of FULL-TIME EQUIVALENT employees within a department is calculated based on the TOTAL PCTTIME of ALL employees in the department.

Find the managers who also work in another department (or other departments) but not as a manager there.

I need SQL statements!!!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

As stated in the problem , we have three tables EMP, DEPT and WORKS. Lets first write the create statements and set in some values or data to the tables. Just to note , SQL Server statements have been used here.

1. Create EMP table :

create table emp(Id int, ename nvarchar(100), age int, salary real, PRIMARY KEY(Id));

Insert values : lets have 3 different employees
insert into emp values(1, 'A', 30, 10000);
insert into emp values(2, 'AB', 30, 13000);
insert into emp values(3, 'ABC', 32, 12000);

2.Create DEPT table :

create table dept(did int, budget real, managerid int, PRIMARY KEY(did));
ALTER TABLE dept
ADD CONSTRAINT FK_emp_dept
FOREIGN KEY(managerid)
REFERENCES emp(Id)

Insert values: lets have 3 departments and managers set respectively.
insert into dept values(1, 1000000, 2);
insert into dept values(2, 281980000, 1);
insert into dept values(3, 290000000, 3);

Foreign key constraint makes sure that the managers are employees. Values used for manager id should be from EMP table.

So, Emp A is the manager of dept 2 and Emp AB is the manager of dept 1. Emp 3 is manger of dept 3 itself.

We can take more examples to proof the SQL query.

3. Create Works table:
create table works(eid int, did int, pct_time int);
ALTER TABLE works
ADD CONSTRAINT FK_emp_works
FOREIGN KEY(eid)
REFERENCES emp(Id)

ALTER TABLE works
ADD CONSTRAINT FK_emp_dept1
FOREIGN KEY(did)
REFERENCES dept(did)

Here eid and did are foreign keys.

Insert Values:
insert into works values(1, 1, 50);
insert into works values(1, 2, 40);
insert into works values(2, 2, 90);
insert into works values(2, 1, 10);
insert into works values(1, 3, 10);
insert into works values(3, 3, 100);

So with the values inserted to tables we can see, empid 1 (A)works for 3 departments 1,2 and 3. while he is manager of dept id 2. Similarly emp id 2 (AB) works for two departments 2 and 1, while he is manager of dept id 1. Empid 3 (ABC) works full time only for dept id 3 where he himself is manger.

Now, lets try the SQL query where we need to find the managers who also work in another department (or other departments) but not as a manager there. below SQL Query would help

select distinct eid Manager_Id, ename Manager_Name, w.did Department from emp e inner join works w on e.id = w.eid inner join dept d on w.did= d.did where eid <> managerid

output :

Manager_Id Manager_Name Department
1 1 A 1
2 1 A 3
3 2 AB 2

This means emp id 1 (A) works for dept 1 and 3 where he isn't manager. Emp id 2 (AB) works for dept id 2 where he isn't manager. This solves our stated problem to find the managers who also work in another department (or other departments) but not as a manager there.

In case we only need the manager names, below SQL query would help :

select distinct ename Manager_Name from emp e inner join works w on e.id = w.eid inner join dept d on w.did= d.did where eid <> managerid

Output :

   Manager_Name
1 A
2 AB

Hope this helps.

Add a comment
Know the answer?
Add Answer to:
Consider the following relational schema. An employee can work in more than one department; the p...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the following relational schema. An employee can work in more than one department; the pct...

    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, phone: integer, salary: real) Works(eid: integer, did: integer, pct_time: integer) Dept(did: integer, dname: string, budget: real, managerid: integer) Write the following SQL queries. d) Find the enames of managers who manage the departments with the largest budgets. e) Find the...

  • Answer each of the following questions. The questions are based on the following relational schema: Emp(*eid:...

    Answer each of the following questions. The questions are based on the following relational schema: Emp(*eid: integer¬, ename: string, age: integer, salary: decimal, doj: date) Works(*eid: integer, *did: integer, no_of_hours: integer) Dept(*did: integer, dname: string, budget: real, managerid: integer) a) Give an example of a foreign key constraint that involves the Dept relation. b) What are the options for enforcing this constraint when a user attempts to delete a Dept tuple? c) Define the Dept relation in SQL so that...

  • 2. (20 points) Consider the following relational schema. The key attributes are bold and underlined. Employee...

    2. (20 points) Consider the following relational schema. The key attributes are bold and underlined. Employee (eid, ename, sal, hobby) * Work_On (eid, did, date) * Department (did, dname, budget) Consider the following query SELECT E.ename, D.budget FROM Employee E, Work_On W, Deptartment D WHERE E.eid W.eid AND W.did - D.did AND D.dname Computer Science' AND W.date 11-13-2018 a) (10 pts) Draw an initial query tree using Cartesian product, selection, and projection. b) (10 pts) Draw a query tree that...

  • Any help appreciated! Question 1 (5 points). Answer each of the following questions briefly. The questions...

    Any help appreciated! Question 1 (5 points). Answer each of the following questions briefly. The questions are based on the following relational schema: Emp( eid: integer, ename: string, age: integer, sala1l1: real) Works( eid: integer, did: integer, pet_time: integer) Dept(did: integer, dname: string, budget: real, managerid: integer) 1. Give an example of a foreign key constraint that involves the Dept relation. 2. Write the SQL statements required to create the preceding relations, including appropriate versions of all primary and foreign...

  • TM-315 DATABASE SYSTEMS Please help me to draw an ERD and the Relational Schema and please...

    TM-315 DATABASE SYSTEMS Please help me to draw an ERD and the Relational Schema and please mark the Primary and Foreign Key ..... Deliverable: Word document with grade sheet followed by Part 1 ERD and the Part 2 relational schema. Part 1: Draw the ERD for the following situation. 8 pts Be sure to: Convert all many-to-many relationships to associative entities. Make sure each regular entity has an appropriate identifier. Make sure attribute names are unique within the ERD Wally...

  • - Consider the following relational schema. - Write SQL statements for the following queries employee fname...

    - Consider the following relational schema. - Write SQL statements for the following queries employee fname CHARACTER VARYING (15) minit CHARACTER VARYING(1) InameCHARACTER VARYING(15) essn CHARACTERO) bdate DATE address CHARACTER VARYING(50) dependent works on CHARACTERO dependent name CHARACTER VARYING(15) essn CHARACTER(9) pno NUMERIC hours NUMERIC CHARACTER() DATE CHARACTER VARYING(B) bdate CHARACTER(1) dept locations elationship salary NUMERIC super ssn CHARACTERO) dno dno NUMERIC dlocation CHARACTER VARYING(15) NUMERIC department CHARACTER VARYING(25) NUMERIC CHARACTERO) project dno mgssn mgstartdate DATE pname CHARACTER VARYING(25) pno...

  • Consider the relational database schema for a company below. EMPLOYEE/NAME, SSN, BDATE, ADDRESS, SEX, SALARY, SUPERSSN,...

    Consider the relational database schema for a company below. EMPLOYEE/NAME, SSN, BDATE, ADDRESS, SEX, SALARY, SUPERSSN, DNA) TA DEPARTMENT(DNAME, DNUMBER. MESSINS, MGRSTARTDATE) DEPT_LOCATIONS(DNUMBER. DLOCATION PROJECT(PNAME, PNUMBER. PLOCATION, DNLIM) WORKS_ONCESSN.PNG, HOURS) DEPENDENTESSN, DEPENDENT-NAME, SEX, BDATE, RELATIONSHIP) Write SQL statements for the following queries: a) List the names of those employees who work in the "Production" department (6 marks). b) Find the maximum salary, minimum salary, and the average salary among employees who work for the "Production department (6 marks). Count the...

  • Consider the following relational schemas: Employee (eid: integer, ename: string, Job_title: string, Years_of_experience: integer) Project (pid:...

    Consider the following relational schemas: Employee (eid: integer, ename: string, Job_title: string, Years_of_experience: integer) Project (pid: integer, pname: string, parea: string, mid: integer, budget: integer) Works_on (eid: integer, pid: integer) Manger (mid: integer, mname: string, deptid: integer) The meaning of these relations is straightforward; for example, Works_on has one record per Employee-Project pair such that the Employee Works_on the Project. 1. Write the SQL statements required to create these relations, including appropriate versions of all primary and foreign key integrity...

  • QUESTION B1. (7 marks) This question assumes the following relational schema Employee (employeeNumber, lastName, firstName, DOB,...

    QUESTION B1. (7 marks) This question assumes the following relational schema Employee (employeeNumber, lastName, firstName, DOB, HireDate, Position Salary, Dept) Primary Key: employeeNumber Foreign key: Dept refers to DeptID in Department Department (DeptID, DeptName, DeptLocation) Primary Key: DeptID You have been given the following MySQL stored procedure: CREATE PROCEDURE Find_EmployeeName (IN employeeNo INT (11), OUT employeeName VARCHAR (60)) BEGIN SELECT concat(firstName, '', lastName) INTO employeeName FROM employees WHERE employeeNumber employeeNo; END (a) (2 marks) Name the two types of parameters...

  • I have already turned in my HW, I need some understanding of the below SQL queries....

    I have already turned in my HW, I need some understanding of the below SQL queries. 1. Find the number of employees in each department. 2. List the names of departments that have more than 5 employees working there. 3. Retrieve the lowest and highest salary in each department. Output the department name in alphabetical order. Consider the following relational schema. An employee can work in more than on department; also, the percentTime field of the Works relations shows the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT