I am learning MySQL using a next.tech course that relies on the following schema:
My current task is to find the average number of hours worked on one specific
In the above Query, you are trying to compare employee_id and project_id, i think that might be the issue. Make sure that you are using JOIN
correctly with attributes.
Thank you for reading!! Happy Learning!!
The where clause must come after join, not before
You need specific wich join you using, there's INNER,LEFT,RIGHT,FULL,OUTER So try these
SELECT AVG(hours) FROM project_employees
INNER JOIN projects
ON project_employees.employee_id = projects.id
WHERE name = 'Washington Avenue Barber';
The syntax of your SQL query seems to be the main problem behind the first error.
you are doing SELECT-FROM-WHERE-JOIN
, but the actual sequence should be SELECT-FROM-JOIN-WHERE
.
Your second query (which is returning a wrong value) has the correct syntax, which is why it returns something.
Secondly, you should be comparing projects.id
with project_employees.project_id
, not project_employees.employee_id
. Therefore, your query should be something like -
SELECT AVG(hours) FROM project_employees
JOIN projects
ON project_employees.project_id = projects.id
WHERE name = 'Washington Avenue Barber';