SQL iterative loop to see if employee works on all projects - mysql

The query I am supposed to form has to accomplish the following task:
Retrieve the names of all employees who work on every project.
I currently have three tables. The Employee, works_on, and project tables. The goal to accomplish this query is to get each project id from the project table, compare it to the project id in the works_on table. When there is a match it will get the SSN and get the names from the employee table. The query I have formed is this:
SELECT e.Fname, e.Minit, e.Lname, p.Pname
FROM EMPLOYEE e, PROJECT p, WORKS_ON w
WHERE p.Pnumber=w.Pno AND w.Essn=e.Ssn
But this outputs All the employees that work on each project not all the employees that work on EVERY project. Is there some way to iterate through a list of results from the query SELECT Pnumber FROM PROJECT?
I really hope I worded this question clearly for your understanding.

Also you don't need PROJECT, WORKS_ON is sufficient.
HAVING filters the results after a GROUP BY.
The GROUP BY e.Ssn means that the COUNT(*) in HAVING is per employee. The JOIN ON WORKS_ON is mapping the user to PROJECT giving the count.
Use JOIN table tbl ON .. = tbl.id JOIN syntax - easier to read.
SELECT e.Fname, e.Minit, e.Lname
FROM EMPLOYEE e
JOIN WORKS_ON w
ON w.Essn=e.Ssn
GROUP BY e.Ssn
HAVING COUNT(*) = (SELECT COUNT(*) FROM PROJECTS)

SELECT e.Fname, e.Minit, e.Lname
FROM EMPLOYEE e
WHERE NOT EXISTS(SELECT PNum
FROM PROJECT
WHERE NOT EXISTS(SELECT *
FROM WORKS_ON
WHERE PNum=PNo AND Essn=e.ssn));
You can select the employee on the condition that:
There doesn't exist a project where the employee doesn't work on it.
You can use the innermost nested query to select tuples where there doesn't exist a WORKS_ON tuple where employee with Ssn works on project with Pnum.
Then use the outermost nested query to select the tuples where the above condition doesn't hold ^^ (so there is an employee with Ssn that works on project with Pnum) for ALL projects.
I hope that makes sense and good luck!

Related

Trying to get a row count in a subquery

I have two tables, one is departments and the other is employees. The department id is a foreign key in the employees table. The employee table has a name and a flag saying if the person is part-time. I can have zero or more employees in a department. I'm trying to figure out out to get a list of all departments where a department has at least one employee and if it does have at least one employee, that all the employees are part time. I think this has to be some kind of subquery to get this. Here's what I have so far:
SELECT dept.name
,dept.id
,employee.deptid
,count(employee.is_parttime)
FROM employee
,dept
WHERE dept.id = employee.deptid
AND employee.is_parttime = 1
GROUP BY employee.is_parttime
I would really appreciate any help at this point.
You must join (properly) the tables and group by department with a condition in the HAVING clause:
select d.name, d.id, count(e.id) total
from dept d inner join employee e
on d.id = e.deptid
group by d.name, d.id
having total = sum(e.is_parttime)
The inner join returns only departments with at least 1 employee.
The column is_parttime (I guess) is a flag with values 0 or 1 so by summing it the result is the number of employees that are part time in the department and this number is compared to the total number of employees of the department.
As a preliminary aside, I recommend expressing joins with the JOIN keyword, and segregating join conditions from filter conditions. Doing so would make the original query look like so:
select dept.name, dept.id, employee.deptid, count(employee.is_parttime)
from employee
join dept on dept.id = employee.deptid
where employee.is_parttime = 1
group by employee.is_parttime
It doesn't make much practical difference for inner joins, but it does make the structure of the data and the logic of the query a bit clearer. On the other hand, it does make a difference for outer joins, and there is value in consistency.
As for the actual question, yes, one can rewrite the original query using a subquery or an inline view to produce the requested result. (An "inline view" is technically what one should call an embedded query used as a table in the FROM clause, but some people lump these in with subqueries.)
Example using a subquery
select dept.name, dept.id
from dept
where dept.id in (
select deptid
from employee
group by deptid
having count(*) == sum(is_parttime)
)
Example using an inline view
select dept.name, dept.id
from dept
join (
select deptid
from employee
group by deptid
having count(*) == sum(is_parttime)
) pt_dept
on dept.id = pt_dept.deptid
In each case, the subquery / inline view does most of the work. It aggregates employees by department, then filters the groups (HAVING clause) to select only those in which the part-time employee count is the same as the total count. Naturally, departments without any employees will not be represented. If a list of department IDs would suffice for a list of departments, then that's actually all you need. To get the department names too, however, you need to combine that with data from the dept table, as demonstrated in the two example queries.

Can you explain these 2 SQL queries to me?

I'm studying for my DB exam which covers a lot of SQL statements I need to write by hand. Below is the schema diagram and solutions for 2 scenarios that were outlined in my book that don't seem to make sense to me.
Q13: Retrieve the names of all employees in department 5 who work more than 10 hours per week on the ProductX project.
SELECT FNAME, LNAME
FROM EMPLOYEE,PROJECT, WORKS_ON
WHERE DNO = 5 AND PNAME = ‘PRODUCT X’ AND HOURS>10 AND ESSN=SSN;
Shouldn't the WHERE clause include PNO = NUMBER ? How would the WORKS_ON table know to reference the PROJECT table without including this? Is it because we reference the ESSN = SSN?
Q1: Retrieve the name of each employee who has a dependent with the same first name and is the same sex as the employee.
SELECT E.FNAME, E.LNAME
FROM EMPLOYEE AS E
WHERE E.SSN IN (SELECT D.ESSN FROM DEPENDENT AS D WHERE E.FNAME = D.DEPENDENT_NAME AND D.SEX = E.SEX);
I understand this query all the way up until the WHERE clause. I don't understand what E.SSN IN is trying to do with the sub query ahead of it. If someone can explain this, that would be great.
For the first question, yes you guessed it right. There should be another clause as PNO = NUMBER.
For second question, think of it this way: Select an employee where employee number[Ssn] is in the list of employeeIDs [Essn] returned by sub-query for each given employee number [Ssn]. This should work fine. But, because Essn and Dependant Name are both keys for the Dependent table, you can also use simple join statements and get it done. Read about it here: http://www.w3schools.com/sql/sql_join.asp
For Q13: You need to include one more condition in WHERE clause that tells the relation between Works_on and Project, which is
SELECT FNAME, LNAME
FROM EMPLOYEE,PROJECT, WORKS_ON
WHERE Pno = Pnumber AND DNO = 5 AND PNAME = ‘PRODUCT X’ AND HOURS>10 AND ESSN=SSN;
Q1: uses correlated sub-query.
SELECT E.FNAME, E.LNAME
FROM EMPLOYEE E
INNER JOIN WORKS_ON WO
ON WO.Essn = E.Ssn
INNER JOIN PROJECT P
ON P.Pnumber = WO.Pno
where E.DNO = 5
and P.name = 'ProductX'
and WO.Hours > 10

Get the first and last names of all employees who entered the project at the same time as at least one other employee

Question:
Get the first and last names of all employees who entered the project at the same time as at least one other employee.
I have four tables Employee, Department, Project, Works_on as shown in below.
I tried the query:
SELECT
emp_fname,
emp_lname
FROM employee a,
works_on b
WHERE a.empno=b.empno;
Please let me know what is wrong with what I am doing?
for this you need to use both Employee (emp_no,emp_fname,emp_lname) and Works_on (emp_no, Enter_date) relationship linked through emp_no on both tables.
in your query you just used Employee table which does not have connection with checking project submitted times.
You can use either of the below query to get the desired results. I have used partition by clause to group related data.
WITH QUERY1 AS (
SELECT EMP_NO,ENTER_DATE, COUNT(*) OVER (PARTITION BY ENTER_DATE ORDER BY EMP_NO ) "t" FROM works_on)
select t2.emp_fname, t2.emp_lname from query1 t1 inner join employee t2 on t1.emp_no=t2.emp_no where "t">1;
select emp_fname, emp_lname from employee where emp_no in (
SELECT EMP_NO FROM (
SELECT EMP_NO,ENTER_DATE, COUNT(*) OVER (PARTITION BY ENTER_DATE ORDER BY EMP_NO ) "t" FROM works_on
) query1 where query1."t">1);
First of all your query will return employee names who ever worked on any project, and better you would use INNER JOIN.
If I understand right; you want employee who work on the same project (let's say ProjectX) and again the employee who entered ProjectX on same time and also there must be at least 2 employee having the same time on ProjectX.
If I am correct with the above:
You need to group your data. For instance first group projects that have at least 2 same enter_date like
SELECT Project_no, Enter_date FROM Works_on having Count(emp_no)>=2
This will give you all projects and dates that have employee more or equal than 2. Now we can find employee that worked on these projects at these dates.
SELECT emp_fname, emp_lname FROM Employee INNER JOIN Works_on AS WO ON WO.emp_no=Employee.emp_no
INNER JOIN
(
SELECT Project_no, Enter_date FROM Works_on having Count(emp_no)>=2
) AS PWith2MoreEmployee
ON PWith2MoreEmployee.Project_no=WO.Project_no
AND
PWith2MoreEmployee.Enter_date=WO.Enter_date
I hope this will give you what you are looking for.

SQL Complex Query w/ Inner Join

I have 2 tables, 1 called Employee and 1 called Salary. Employee table consists of Emp_Name, Emp_Address, Emp_ID & Salary table consists of Salary_Details and Emp_ID. > Can you write down a query for retrieving the Salary_Details of 1 of the employee based on last name using Inner Join?
I am not sure what you are looking for, but this might help you:
SELECT * FROM Employee e
INNER JOIN Salary s ON e.Emp_ID = s.Emp_ID
WHERE e.Emp_Name = 'EMPLOYEENAME'
That will give you back all fields from Employee and Salary for an Employee with the name = 'EMPLOYEENAME' (which you can exchange then).
You can adjust the columns returned as needed depending on your app...
SELECT e.Emp_Name, e.Emp_ID, s.Salary_Details
FROM Employee e
INNER JOIN Salary s USING (Emp_ID)
WHERE e.Emp_Name = 'Smith';
The USING keyword is kind of obscure and works only if the join column is named identically in both tables. The previous answer with ON instead of USING will work in all cases. I like USING as a personal preference.

Very Basic Beginner SQL script

I need help writing some very basic SQL code. I do not need an eleborate code, the very basics of SQL. I will write my database first, then have the question under.
Okay so I have the four following tables:
Division(did, dname, managerID)
Employee(empid, name, salary, did)
Project(pid, pname, budget, did)
Workon(pid, empid, hours)
The text is bold is the primary key and the text in italic is the foreign key. The Workon table connects both the employee and project tables.
Here are my questions:
List the name of divisions that have/sponsor project(s) employee 'chen' works on.
This is what I have...
select pname, d.dname
from project p, division d
where pid in
(select pid
from employee e, workon w
where e.empid=w.empid and lower (name) 'Chen')
Now I know this is not completely right because I think I have to take it from the Workon table, but I am unsure how to.
List the name of the employee that has the lowest salary in his division and list the total number of projects this employee is work on (use correlated subquery).
This is what I have
select name, did, min(salary) as "lowest salary"
from employee
group by name, did
order by did
and I also have this code..
select did, min(salary)
from employee
group by did
order by did
I am confused because the first code is not only giving me the lowest salary of the division. (If you look at the second code I wrote it shows the average.)
List the name of employee who do more projects than his/her divisional colleagues (correlated subquery).
I have...
select pname
from project p, workon w
where p.pid= w.pid
group by pname
having count (empid) >2
I have a feeling this shouldn't be more than 2. It should be more than the colleague, but I can not figure out how to write that.
List the name of project that some employee(s) who is/are working on it make less than divisional 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)
I do not think this is right either, but I know I am almost there
Search for TDQD on SO to see how I go about Test-Driven Query Design. You can test each step of the query as it is built up, and if something goes wrong with a step, take appropriate corrective action to make the query correct.
I'll tackle the first of your queries:
List the names of divisions that sponsor projects that employee 'chen' works on.
Given your attempted query, I am going to assume that your DBMS supports a function LOWER(x) that case-converts all the letters in its argument to lower-case, leaving any non-letters alone.
Taking this one step at a time, we might come up with:
1. Employee ID for employee 'chen'
SELECT EmpID
FROM Employee
WHERE LOWER(Name) = 'chen'
2. Project IDs of projects that employee 'chen' works on
SELECT W.PID
FROM WorkOn AS W
JOIN Employee AS E
ON E.EmpID = W.EmpID
WHERE LOWER(E.Name) = 'chen'
3. Division IDs of divisions sponsoring projects that employee 'chen' works on
SELECT P.DID
FROM Project AS P
JOIN WorkOn AS W ON W.PID = P.PID
JOIN Employee AS E ON E.EmpID = W.EmpID
WHERE LOWER(E.Name) = 'chen'
4. Names of divisions sponsoring projects that employee 'chen' works on
SELECT DISTINCT D.Dname
FROM Division AS D
JOIN Project AS P ON P.DID = D.DID
JOIN WorkOn AS W ON W.PID = P.PID
JOIN Employee AS E ON E.EmpID = W.EmpID
WHERE LOWER(E.Name) = 'chen'
The DISTINCT removes any repeats if 'chen' works on several projects sponsored by the same division.
You need to apply a similar technique to the other queries in your question. I'm not sure that you need any correlated subqueries. You will probably need 'subqueries in the FROM clause', but you shouldn't need correlated subqueries — IMO, of course. If I spent long enough on it, I could probably come up with a correlated subquery, but such queries are unlikely to perform as well as straight-forward queries and will probably be a lot harder to read (and write). You'll need to assess whether your tutor will accept correct answers that do not use correlated subqueries (but that do produce the correct answer).