question

jowee avatar image
jowee asked

Need help with queries

Kindly help me with this...as am a newbies to SQL ******************* TABLES WITH DATA ******************* create table division (did integer, dname varchar (25), managerID integer, constraint division_did_pk primary key (did) ); create table employee (empID integer, name varchar(30), salary float, did integer, constraint employee_empid_pk primary key (empid), constraint employee_did_fk foreign key (did) references division(did) ); create table project (pid integer, pname varchar(25), budget float, did integer, constraint project_pid_pk primary key (pid), constraint project_did_fk foreign key (did) references division(did) ); create table workon (pid integer references project(pid), empID integer references employee(empID), hours integer, constraint workon_pk primary key (pid, empID), constraint workon_pid_fk foreign key(pid) references project(pid), constraint workon_empid_fk foreign key(empid) references employee(empid) ); /* loading the data into the database */ insert into division values (1,'engineering', 2); insert into division values (2,'marketing', 1); insert into division values (3,'human resource', 3); insert into division values (4,'Research and development', 5); insert into division values (5,'accounting', 4); insert into project values (1, 'DB development', 8000, 2); insert into project values (2, 'network development', 6000, 2); insert into project values (3, 'Web development', 5000, 3); insert into project values (4, 'Wireless development', 5000, 1); insert into project values (5, 'security system', 6000, 4); insert into project values (6, 'system development', 7000, 1); insert into employee values (1,'kevin', 32000,2); insert into employee values (2,'joan', 42000,1); insert into employee values (3,'brian', 37000,3); insert into employee values (4,'larry', 82000,5); insert into employee values (5,'harry', 92000,4); insert into employee values (6,'peter', 45000,2); insert into employee values (7,'peter', 68000,3); insert into employee values (8,'smith', 39000,4); insert into employee values (9,'chen', 71000,1); insert into employee values (10,'kim', 46000,5); insert into employee values (11,'smith', 46000,1); insert into workon values (3,1,30); insert into workon values (2,3,40); insert into workon values (5,4,30); insert into workon values (6,6,60); insert into workon values (4,3,70); insert into workon values (2,4,45); insert into workon values (5,3,90); insert into workon values (3,3,100); insert into workon values (6,8,30); insert into workon values (4,4,30); insert into workon values (5,8,30); insert into workon values (6,7,30); insert into workon values (6,9,40); insert into workon values (5,9,50); insert into workon values (4,6,45); insert into workon values (2,7,30); insert into workon values (2,8,30); insert into workon values (2,9,30); insert into workon values (1,9,30); insert into workon values (1,8,30); insert into workon values (1,7,30); insert into workon values (1,5,30); insert into workon values (1,6,30); insert into workon values (2,6,30); ********************** QUERIES TO WORKON ********************** 1 List the name of employee and the number of project he/she works on (use join) 2 For each division, list the name of project whose budget is highest in that division, also list the division name (corelated subquery). 3. List the total number of projects whose budget is above the average project budget. 4. List the name of the division that has more than 2 employees who participate in project. 5. List the name of employee who work on more than 2 projects 6. Increase the budget of the project by if 10% if it has two or more employees working on it 7. List the name of the manager (note, if an employee's ID is in the Division table then s/he is a manager) and her/his total working hours on projects (the hours employee working on the project). 8. List the name of the managers who do not work on any projects. (note, the manager is the employee whose ID is in division table) 9. List the name(s) of employees who is/are working on some project(s) but not on the project "Web development" 10 List the name of the division that has at least one employee who works on a project that is not sponsored by his division (MUST use EXISTS) 11. List the name of employee who works on a project that is from his own division (i.e., the project's DID = employee's DID, use corelated subquery) . 12 (bonus question) List the name of the employee whose salary is below the company average but the total number of projects he/her work on is above the employee’s average (i.e., the average of number of projects an employee work on) .
sql-server-2008homework
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image Kev Riley ♦♦ commented ·
@jowee: as this is blatantly homework or coursework, can you show us how far you have got, then we can point you in the right direction, rather than just giving an outright set of answers.
4 Likes 4 ·
Kev Riley avatar image Kev Riley ♦♦ commented ·
confidently tagged this as homework - not many projects offer 'bonus questions' :)
3 Likes 3 ·
sqlaj 1 avatar image
sqlaj 1 answered
This might be of some help. http://www.w3schools.com/sql/default.asp Good luck
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

TimothyAWiseman avatar image
TimothyAWiseman answered
As Kev said, you will get better results with a homework question if you provide more information on where exactly you are getting stuck. I would point out however that the tutorial at [ http://beginner-sql-tutorial.com/sql-select-statement.htm][1] probably covers everything you need to answer these questions. Also, if you are new to SQL, The Manga Guide to Databases is far better than its name would suggest. [1]: http://beginner-sql-tutorial.com/sql-select-statement.htm
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

avvipersonal avatar image
avvipersonal answered
1.List the name of employee and total hours he works on the project (use outer join so that employees who don't work on project will be also listed with zero project count ) select name, sum (hours) from employee e, workon w where e.empid = w.empid (+) group by e.empid , name 2.List the name of project that ‘chen’ does not work on (request: must use NOT EXISTS) select pname from project p where not exists (select * from employee e, workon w where e.empid = w.empid and lower(name) = 'chen' and p.pid = w.pid) 3.List the total number of projects whose budget is above the average project budget. select count(pid) from project p where p.budget > (select avg(budget) from project) 4.List the name of project that has more than two employee working on it select pname from project p, workon w where p.pid = w.pid group by pname having count(empid) > 2 5.List the name of the division that has more than 2 employees who work on project. select name from employee e, workon w where e.empid = w.empid and w.pid in (select pid from project p where p.did = e.did) 6. List the name of the managers who do not work on any projects. (note, the manager is the employee whose ID is in division table) select name from employee e where e.empid in (select managerID from division ) and e.empid NOT in (select w.empid from workon w) 7.List the name of project and the name of its sponsoring division that has more budget average budget of projects. select dname , pname, budget, round ((select avg(budget) from project) )as "average budget" from project p , division d where p.did= d.did and budget > (select avg(budget) from project) 8.List the name(s) of employees who is/are working on some project(s) but not on the project "Web development" select name from employee where empid in (select empid from workon) and empid not in (select empid from workon w, project p where w.pid=p.pid and lower(pname) ='web development') 9.List the name of division whose average salary is higher than company's average salary select dname from division d, employee e where d.did = e.did group by dname having avg(salary) > (select avg(salary) from employee) 10.List the name of the division that has at least one employee who works on a project that has budget over 4000 (use subquery) select dname from division d where did in (select e.did from employee e, workon w, project p where e.empid =w.empid and w.pid = p.pid and p.budget>4000) 11.List the name of project whose budget is higher than project “security system”. select pname from project where budget > (select budget from project where lower(pname)= 'security system') 12.List the name of the employee whose salary is below the company average but the total working hours of project is over 100 (subquery) select name from employee where salary < (select avg(salary) from employee) and empid in (select w.empid from workon w group by w.empid having sum(hours)>100) Q13. List the name of employee who works on a project that is sponsored by his own division (i.e., the project's DID = employee's DID, use corelated subquery) .
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

ThomasRushton avatar image ThomasRushton ♦♦ commented ·
As stated above, this is *not* a homework site.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.