I have the lines of code in My SQL Workbench, but I am receiving the error: Error Code: 1242. Subquery returns more than 1 row.
SELECT Fname, Lname, Dnum, Pno
FROM EMPLOYEE e, PROJECT p, WORKS_ON w
WHERE p.Dnum = (SELECT Dno FROM EMPLOYEE WHERE e.Ssn = w.Essn AND w.Pno = p.Pnumber)
I am trying to display a table of employees that are working on a project that is not sponsored by their department.
try this
SELECT Fname, Lname, Dnum, Pno
FROM EMPLOYEE e, PROJECT p, WORKS_ON w
WHERE p.Dnum in (SELECT Dno FROM EMPLOYEE WHERE e.Ssn = w.Essn AND w.Pno =
p.Pnumber);
use in clause instad =
Optimized query:
SELECT Fname, Lname, Dnum, Pno
FROM EMPLOYEE e
INNER JOIN WORKS_ON w ON e.Ssn = w.Essn
INNER JOIN PROJECT p ON w.Pno = p.Pnumber AND p.Dnum = e.Dno
Related
Im going over a past paper and have found some select statements I cannot do. Can anyone help with these? I'm using MySQL. Thanks
Schema:
employee = (employee id, name, address, date of birth, salary)
project = (project id, name, budget, start date, end date)
manages = (employee id, project id)
works on = (employee id, project id)
Questions :
Names and addresses of all employees who work on projects which are
current (have not ended yet)
The names of managers of projects, ordered by the total number of employees they manage
The names of employees who work on the project with the largest budget
amount.
My attempts:
1.
select employee.name, address
from employee natural join works_on natural join project
where end_date is null
2.
select employee.name, count(works_on.employee_id) as manages_count
from (employee natural join manages) joins works_on using (employee_id)
I'm completely lost I cannot even attempt this one ^
3.
select employee.name, address, budget
from employee natural join works_on natural join project
order by budget
limit n
^ I know this is wrong as technically I should be able to show them without a limit
Here you go:
1.
select
e.name,
e.address
from employee e
join works_on w on w.employee_id = r.employee_id
join project p on p.project_id = e.project_id
where p.end_date is null
2.
select
n.name
from employee n
join manages m on m.employee_id = n.employee_id
join works_on w on e.project_id = m.project_id
group by n.employee_id
order by count(*) desc
3.
select
e.name
from employee e
join works_on w on w.employee_id = e.employee_id
join project p on p.project_id = w.employee_id
where p.project_id in (
select project_id from project where budget = (
select max(budget) from project
)
)
I have 2 tables
the 1st one name is (employee) has 3 columns (emp_id, first_name, last_name)
the 2nd table name is (works_with) has also 3 columns (emp_id, client_id, total_sales)
in (works_with) table the same emp_id can be related to different client_id
I need to extract the (first_name) and (last_name) form (employee) table and their (total_sales) of more than 30000 from (work_with) table
I used this code to give me the (emp_id)* with the (total_sales) of more than 30000
SELECT SUM(works_with.total_sales), works_with.emp_id
FROM works_with
WHERE works_with.emp_id IN (SELECT works_with.emp_id
FROM works_with
WHERE works_with.total_sales > 30000)
GROUP BY works_with.emp_id;
and I used this code to give me the (first_name) and (last_name) of those (emp_id)*
SELECT employee.first_name, employee.last_name
FROM employee
WHERE employee.emp_id IN (SELECT works_with.emp_id
FROM works_with
WHERE works_with.total_sales > 30000);
is there a way to join the 2 codes or any other way to have the result that I want
Thank you
Are you just looking for a JOIN?
SELECT e.first_name, e.last_name, SUM(ww.total_sales)
FROM works_with ww JOIN
employee e
ON ew.emp_id = ww.emp_id
WHERE ww.emp_id IN (SELECT ww2.emp_id
FROM works_with ww2
WHERE ww.total_sales > 30000
)
GROUP BY e.emp_id, e.first_name, e.last_name;
The subquery is not needed. It is implementing this logic:
SELECT e.first_name, e.last_name, SUM(ww.total_sales)
FROM works_with ww JOIN
employee e
ON ew.emp_id = ww.emp_id
GROUP BY e.emp_id, e.first_name, e.last_name
HAVING MAX(ww.total_sales) > 30000;
However, I suspect that you want:
SELECT e.first_name, e.last_name, SUM(ww.total_sales)
FROM works_with ww JOIN
employee e
ON ew.emp_id = ww.emp_id
GROUP BY e.emp_id, e.first_name, e.last_name
HAVING SUM(ww.total_sales) > 30000;
Contributing here since I noticed an error in the other answer. See here for how different types of JOINs work: (Link).
SELECT
Employee.First_Name,
Employee.Last_Name,
SUM(WW.Total_Sales)
FROM
Works_With AS WW
INNER JOIN Employee AS Employee
ON Employee.EMP_ID = WW.EMP_ID
GROUP BY
Employee.EMP_ID,
Employee.First_Name,
Employee.Last_Name
HAVING
SUM(WW.Total_Sales) > 30000;
select employee_id, first_name, last_name, department_name, location_id
from employees as e, departments as d
where d.department_id = e.department_id and (location_id)
in
(select location_id
from departments
where department_name = 'Finance')
It seems you don't need subquery to filter location_id
select employee_id, first_name, last_name, department_name, location_id
from employees as e join departments as d on d.department_id = e.department_id
where location_id in (select location_id from departments where department_name = 'Finance')
Note: it's better to use explicit join instead of comma separated join
You could use an inner join instead of an IN clause
select employee_id
, first_name
, last_name
, department_name
, d.location_id
from employees as e
INNER JOIN departments as d ON d.department_id = e.department_id
INNER JOIN ( select location_id
from departments
where department_name = 'Finance') t ON t.location_id = d.location_id
And for readability you should us explicit JOIN syntax, and not implicit syntax based on WHERE.
I have this schema:
Emp(eid: integer,ename: string,age: integer,salary: real)
Works(eid:integer,did: integer,pct_time: integer)
Dept(did:integer,budget: real,managerid:integer)
and I'm trying to print the name and salary of each employee whose salary exceeds the budget of every department that he or she works in.
I have this SQL statement but it returns an empty column for ename and I'm not sure why. Can anyone help?
select E.ename, E.Salary
from Emp E
where E.salary > all (select D.budget
from Dept D, Works W
where E.eid = W.eid and D.did = W.did)
You can do that with a normal join
SELECT E.ename
FROM Emp E
JOIN Works W ON E.eid = W.eid
JOIN Dept D ON D.did = W.did
WHERE E.salary > D.budget
I am working on a sql query to do the following:
For each project, retrieve the project number, the project name, and the number of employees from department 5 who work on the project.
So far my query looks like this:
SELECT p.PNO
, p.PNAME
, COUNT( DISTINCT w.ESSN) '# employees from Dept. 5'
FROM project p
JOIN department d
ON d.DNO = p.DNO
JOIN employee e
ON e.DNO = d.DNO
JOIN works_on w
ON w.ESSN = e.SSN
WHERE e.DNO LIKE '5'
AND p.PNO LIKE 10
Where I am testing it for project number 10 which should return the number of employees from Dept. 5 as 1, however it returns NULL. I think that I need to somehow join the project and employee tables but I am unsure
Attached is my schema
ER Diagram
Your joins are wrong.
You should be joining only projects, works_on and employee tables.
SELECT p.PNO, p.PNAME, COUNT( DISTINCT w.ESSN) '# employees from Dept. 5'
FROM project p
INNER JOIN works_on w
ON p.pno = w.pno
INNER JOIN employee e
ON w.essn = e.essn
WHERE e.DNO LIKE '5' AND p.PNO LIKE 10
And you are missing the group by at the end:
GROUP BY p.PNO, p.PNAME