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
)
)
Related
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
I have two tables, for example the Employee and Project tables:
Employee (id, dept, joining_date)
Project (emp_id, project)
With Project having foreign key from Employee table.
I have to query on project and dept and return Employee in the order of their joining_date. Which query will work faster on big data set on the queries below?
select * from Employee where id in (select p.emp_id from Project p join Employee e on p.emp_id = e.id where p.project = 'project1' and e.dept = 'dept1') order by joining_date
select * from Employee where id in (select p.emp_id from Project p join Employee e on p.emp_id = e.id where p.project = 'project1' and e.dept = 'dept1') and dept = 'dept1' order by joining_date
Or is there any better and simpler way to do so?
The outer query using the IN() expression serves no purpose is entirely unnecessary. This will produce the output you need:
select e.*
from Project p
inner join Employee e on p.emp_id = e.id
where p.project = 'project1' and e.dept = 'dept1'
order by joining_date
SELECT
e.lname AS employee_name
, s.lname AS supervisor_name
, e.superssn AS supervisor_ssn
FROM employee e INNER JOIN employee s
WHERE e.superssn=s.ssn
I actually have two questions. The first one is, the above statement works just fine. However right now it will show bunch of employees with their supervisor. Not well organized. By the way there are three supervisors. Is there a way to show let's say supervisor A with employees A and supervisor B with employees B and so on?
The second problem is that I also tried to just count the number of employees for each supervisor rather than showing their name with COUNT(), I tried several different ones but non worked.
A few things
1) It is good practice to keep your join predicate with your join. So change:
FROM employee e INNER JOIN employee s WHERE e.superssn = s.ssn
To:
FROM employee e INNER JOIN employee s ON e.superssn = s.ssn
2) To keep them together by supervisor, just use an order by:
SELECT
e.lname AS employee_name
, s.lname AS supervisor_name
, e.superssn AS supervisor_ssn
FROM employee e INNER JOIN employee s ON e.superssn = s.ssn
ORDER BY s.lname
3) To do a count of employees for each supervisor use a group by and the COUNT aggregate function
SELECT
s.lname AS supervisor_name,
COUNT(*) AS employee_count
FROM employee e INNER JOIN employee s ON e.superssn = s.ssn
GROUP BY s.lname
ORDER BY s.lname
SELECT pnumber, pname, COUNT(*)
FROM project
INNER JOIN works_on ON pno=pnumber
INNER JOIN department ON dnumber=dnum
GROUP BY pnumber
Right now, mysql will return the project number and name, along with the number of employees who are working on it. What I want to do is that mysql only count employees from department 5. In other words there are people from different departments working on projects, but I want only the ones from department 5 to be counted and not all.
If Barmar's and Shafeeq's suggestion of using a WHERE clause won't work for you (for example, if you need to include all departments in your result, but only count people from department 5) you can use this:
SUM(IF(dnum=5, 1, 0)) AS CountFromDepartment5
So we'd have:
SELECT pnumber, pname, COUNT(*) AS TotalCount, SUM(IF(dnum=5, 1, 0)) AS CountFromDepartment5
FROM project
INNER JOIN works_on ON pno=pnumber
INNER JOIN department ON dnumber=dnum
GROUP BY pnumber
You can use WHERE
SELECT pnumber, pname, COUNT(*)
FROM project
JOIN works_on ON pno = pnumber
JOIN department ON dnumber=dnum
WHERE dnumber = 5
GROUP BY pnumber
EDIT
SELECT pnumber, pname, COUNT(*),SUM(IF(dnumber=5, 1, 0)) AS count5
FROM project
JOIN works_on ON pno = pnumber
JOIN department ON dnumber=dnum
GROUP BY pnumber
Use a WHERE clause that specifies the department you want.
SELECT pnumber, pname, COUNT(*)
FROM project
INNER JOIN works_on ON pno=pnumber
WHERE dnumber = 5
GROUP BY pnumber
You don't need to join with department, since the department number also exists as a foreign key in the works_on table.
I am trying to select the classes with maximum enrollment in each department from a table with the following structure:
Courses: cid, dept, name
Enrollment: cid, sid
The idea would be to count all the enrollments in each class, and find the maximum in each department. Unfortunately, I'm having trouble with the maximum-count combination.
My query was going to look something like:
SELECT c.name, MAX(COUNT(*) FROM Enrollment E WHERE E.cid = C.cid)
FROM Courses C
GROUP BY C.dept
But I can't figure out how to get the MAX-COUNT syntax to work properly. I've tried a lot of different examples from Google and StackOverflow and none of them worked for me. MySQL says the syntax is wrong.
I like nested queries for this kind of problem. First select the enrollment counts grouped per class. Then find the max enrollment count grouped per department:
SELECT MAX(cnt) cnt, dept FROM
(
SELECT COUNT(*) cnt, dept, C.cid
FROM Courses C
INNER JOIN Enrollment E on C.cid = E.cid
GROUP BY cid, dept
) a
GROUP BY dept
There is no declaration of E, so you can't use E.cid
So, either you do this
SELECT c.name, COUNT(c.*) as count
FROM Courses C
GROUP BY C.dept
Or,
SELECT c.name, MAX(SELECT COUNT(*) FROM Enrollment E WHERE E.cid = C.cid)
FROM Courses C
GROUP BY C.dept
SELECT MAX(cnt) FROM (
SELECT c.name, COUNT(E.*) AS cnt FROM Enrollment E
INNER JOIN Courses C ON E.cid = C.cid
GROUP BY C.dept
) final