Can you explain this to me?

They have: 75 posts

Joined: Mar 2000

Hey,

I'm currently studying for my database management final, which is tomorrow mornign (wednesday).

I doubt this question will be on the test, cuz only the CIS majors have a chance at getting it, and even then I doubt it as we've never done anything like this specific query in the class. (The class is mostly accounting majors, they'd definately get this question wrong).

But nevertheless, I'm curious cuz I'm having a hard time following the logic....
Anyone care to explain? (It's SQL)

• Retrieve the names of all employees who work on every project.

Select E_name
From EMPLOYEE E
Where not exists
(Select ProjID
From PROJECT P
Where not exists
(Select *
From PROJ-EMP PE
Where P.ProjID = PE.ProjID AND
PE.EmpID = E.EmpID);

Thanks...

-tallon

He has: 113 posts

Joined: Jul 2005

Its a bit complex to explain, but you ave 2 sub queries running in that one select query which will determine the overall output. Its like a true or false situation.

Froma quick glance, I would have to say that script lists employees who are not currently assigned to a project. It uses the SQL NOT EXISTS method to execute a few sub queries which determines if the selected employee is related to a project or not and it will only display employees who have no Project Links.

thats just a guess though. I would need to see the rest of your database structure and relationships before I can give you a final verdict, but im pretty sure thats what it does.

They have: 75 posts

Joined: Mar 2000

Right, I realize it's a query with 2 subqueries...
I just couldn't figure out how doing it the way the teacher said actually got the answer needed.

As far as I understand it... the first one to execute will list all project ID's and employee ID's (which are the only things kept in the proj-emp table). The next query finds all project ID's which are not in the table generated by the first.

Considering that All projects *must* have atleast one employee working on them... Every project ID will be listed in the proj-emp table. Thus, trying to find all project ID's that don't exist in the proj-emp table is, ... dumb? And it wouldn't work.

Either that, or the teacher never taught us this (and, he didn't) and I'm having a hard time following how this actually solves the problem.

I attached a picture of the Simplified diagram(he didn't want all attributes to be listed on it), and the info for the database from the question is pasted below. (this was a question for the Final review. Sadly, he *never* taught us this, but it also wasnt on the final).

****
Schema:
DEPARTMENT (DeptID, D_name, mgrID)
LOCATION (DeptID, address)
EMPLOYEE (EmpID, E_name, DOB, address, gender, salary, supervisorID)
DEPT-EMP (DeptID, EmpID)
PROJECT (ProjID, P_name, address, DeptID)
EMP-PROJ (EmpID, ProjID, hours_worked, weekID)

The DB Challenged Company (DBC) has several departments. Each department has a department ID that is unique and a department name which is also unique. All departments have at least one location, but may have multiple locations. Any location is for only one department. Locations have an address.
Each department must be managed by one employee. An employee may manage at most one department. Every employee is assigned to at least one department and may be assigned to many departments. Every department may have many employees working for it. An employee has a unique employee ID, name, date of birth, address, gender, and salary. An employee must have one supervisor, and a supervisor may supervise many employees, or may supervise no one.
Each department may control several projects, or no projects. Every project must be controlled by one and only one department. Projects have an address, a project ID which is unique, and a project name which is also unique.
An employee may work on many projects but must work on at least one project. Every project may have many employees working on it, but must have at least one employee working on it. DBC wants to know how many hours an employee works on any project.
*****

And again, the question is:
"Retrieve the names of all employees who work on every project."
The SQL is in my first post.

-tallon

They have: 14 posts

Joined: Nov 2005

Hopefully this might help as well.....sql EXISTS checks for the existence of something. In this case we are trying to see if the employee does NOT exist in the combination of the two sub queries. The not part is the key because the sub queries technically list will list all projects that a certain employee (AND PE.EmpID = E.EmpID) is not working on.

In the end, we are listing any results from the employee table that have no results in:
show me a list of projects from the projects table
that a certain employee is not working on, by looking
at the employee project table.

Tried to keep the explanation to words instead of SQL statements...hopefully it will help.

They have: 75 posts

Joined: Mar 2000

It does help, I see what they are trying to do. If an employee is not in the list that shows employees and projects they are not working on, then he's worked (or is working) on every project.

Still am confused a bit, but I guess that's expected. The teacher expects us to do things we've never done *EVER* and he even admitted that we are never taught this stuff in previous classes. Like doing queries in forms in Access, referential integrity and such in sql server using an access adp file (3 hour project that should have taken 20). Nothing in the notes, nothing in the help, nothing from his lecture, nothing in the book, meh. Waste of time class, sadly Sad.

Thx for the help though. (still a bit confused, heh)

-tallon

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.