So im trying to list the department locations, the project names associated with each department and the number of employees that work on each project.
There is a DeptLocations table(Attributes: Dnum, DLoc)
A Project table(Attributes:PName, Pnum, PLoc, DNum)
An Employee table (Attributes: FNAME, M, LNAME, SSN, BDATE, ADDRESS, S, SALARY, SUPERSSN, DNO)
And a Works_On table (Attributes: ESSN, PNO, HOURS).
This is my SQL query:
select DeptLocations.DLocation, Project.PName, count(ESSN)
from Works_On, DeptLocations, Project, Department
where DeptLocations.DLocation = Project.PLocation and Project.PNumber = Works_On.PNo
For some reason it only yields 1 record, when clearly there should be plenty more. Any help would be awesome.
You need to add group by clause - as you've used aggregated function:
select
DeptLocations.DLocation,
Project.PName,
count(ESSN)
from
Works_On
inner join Project on Project.PNumber = Works_On.PNo
inner join DeptLocations on DeptLocations.DLocation = Project.PLocation
group by
DeptLocations.DLocation, Project.PName
Note: It's better to use explicit join instead of comma separated join.
Related
I am trying to write a query to display the last name, department
number, and department name for all the employees.
And this my working code:
SELECT
last_name,
department_id,
department_name
FROM
employees
JOIN departments USING(DEPARTMENT_ID);
When I was trying to make the query using JOIN ON ,I faced an error saying that
Column 'department_id' in field list is ambiguous through this code
query using JOIN ON:
SELECT
last_name,
department_id,
department_name
FROM
employees
JOIN departments ON(
employees.DEPARTMENT_ID = departments.DEPARTMENT_ID
)
I want to know why it is not working .
The ambiguity should be resolved in select clause
SELECT
last_name,
employees.department_id,
department_name
FROM employees
JOIN departments USING(DEPARTMENT_ID);
I'm still learning the MySQl.
This is the relational DBMS :
CUSTOMER (CustID, CustName, AnnualRevenue)
TRUCK (TruckNumber, DriverName)
CITY (CityName, Population)
SHIPMENT (ShipmentNumber, CustID, Weight, Year, TruckNumber, CityName)
Now, I have to formulate for these two queries:
Total weight of shipments per year for each city.
Drivers who drove shipments to London but not Paris.
These are the queries i have came up with:
1.
select sum(s.weight), s.year , c.city
from shipment s, city c
INNER JOIN CITY
on s.CityName = c.CityName
You are mixing and old way to JOIN table (which you should avoid because the joining columns are not explicitly stated and it is confusing for others):
FROM shipment s, city c
You should group columns in the select that are not aggregated (year, city). Also it is better to use an alias for the aggregated column (AS total_weight)
select sum(s.weight) AS total_weight, s.year , c.city
from shipment s
INNER JOIN CITY as c
on s.CityName = c.CityName
GROUP BY s.year, c.city
Try to solve the second query and come back if you have a problem.
I have been asked this question in an interview. I have tried so hard, but unfortunately was not able to get it right.can anybody help me with this?
Retrieve the last name, first name, dept name of that employee using these tables.I am writing down the tables and columns.however, i am not writing that dummy data.
Employee- (id, last name, first name, DOB, SSN) and some other columns(not useful).
Dept - (D_id, dept name)
Emp_Dept - (id, D_id)
You can try this solution for your problem :
Query :
SELECT E.last_name, E.first_name, D.dept_name
FROM Employee AS E
-- Get employee dept
INNER JOIN Emp_Dept AS ED
ON ED.id = E.id
-- get dept data
INNER JOIN Dept AS D
ON D.id = ED.D_id
I hope it will help you.
But this query doesn't return value of employee without dept. If this is necessary you should use LEFT JOIN instead of INNER JOIN
There are two relevant tables: works_on and project. Project contains the following attributes: PName, PNo, PLocation and DNo (department number). Works_on contains the following attributes: SSN, PNo, hours.
I want to only count the SSNs that appear more than twice and then provide the count value, PName and PNo.
This is my attempt so far:
SELECT
P.PNo, P.PName,
COUNT(W.SSN) AS no_employees
FROM
project AS P INNER JOIN works_on AS W ON P.PNO = W.PNo
WHERE W.SSN IN (SELECT SSN FROM WORKS_ON GROUP BY SSN HAVING COUNT(SSN) > 2)
GROUP BY P.PNo
but I get the wrong PNo's and the wrong number of employees. I have been trying to figure out why this code will not give me projects with more than two employees. Please help me figure out what I am doing wrong.
You don't need inner query, group by with having should do, e.g.:
SELECT p.no, p.name, COUNT(w.ssn) as employees
FROM project p JOIN works_on w ON p.pno = w.pno
GROUP BY p.no, p.name
HAVING employees > 2;
I am working on a sql query to do the following:
For each project, retrieve the project number, the project name, the number of employees who work on that project.
Here is what I have so far:
select pno, pname,
count(select fname from
employee inner join works_on
on employee.ssn=works_on.essn
inner join project
on works_on.pno=project.pno)
as num_emp from project
Which gives me this error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select fname from employee inner join works_on on employee.ssn=works_on.essn inn' at line 1
Which I am assuming means I can't put a select statement inside a count function, but I do not see how else to do this
Attached is my schema
I'd do it like this, grouping on the project details for which you want the employee count:
SELECT
pno,
pname,
COUNT(employee.snn) AS num_emp
FROM
project
INNER JOIN
works_on
ON works_on.pno = project.pno
INNER JOIN
employee
ON employee.ssn = works_on.essn
GROUP BY
pno,
pname;
EDIT:
Actually, if you want to list projects with no employees assigned then you could make your original query more correct by doing something like:
SELECT
pno,
pname,
(
SELECT
COUNT(fname)
FROM
employee
INNER JOIN
works_on
ON employee.ssn = works_on.essn
WHERE
works_on.pno = project.pno
) AS num_emp
FROM
project;
You don't need the sub query
Count(employee ID) and group by the other fields
You can do it like this:
SELECT pno, pname,count(fname) AS Cnt
FROM employee, works_on, project
WHERE employee.ssn=works_on.essn
AND works_on.pno=project.pno
GROUP BY pno,pname
Use a COUNT(*) with an ordinary join and GROUP BY.
select p.pno, p.pname, COUNT(*) AS num_emp
FROM project AS p
JOIN works_on AS w ON w.pno = p.pno
GROUP BY p.pno
You don't need to join the employee table because you're not using any information from that table -- the information is all in works_on.
If you need to include projects with no employees working on them, you need to use a left join.
select p.pno, p.pname, COUNT(w.pno) AS num_emp
FROM project AS p
LEFT JOIN works_on AS w ON w.pno = p.pno
GROUP BY p.pno
In the unlikely case that there can be multiple entries in works_on for the same project+employee, use COUNT(DISTINCT w.essn) in the queries so they're not counted multiple times.