Skip to content
Advertisement

SQL Query Not Getting Results

What I am doing wrong here? Can anyone please help?

1. Use non-correlated sub-query, find the names of employees who are not working on any projects

Answer:

SELECT * 
FROM employee_name 
WHERE ID IN (SELECT employee_id FROM projectinfo 
             WHERE project IS NOT NULL);

2. Use correlated sub-query, find the names of employees who are not working on any projects.

Answer:

  SELECT * 
  FROM employee_name
  WHERE EXISTS (SELECT * FROM project 
                WHERE project.employee_id = project_id 
                  AND projectinfo.project IS NOT NULL);

Advertisement

Answer

You can sue the LEFT JOIN as follows for non-correlated sub-query as follows:

SELECT e.* FROM employee_name  E -- is employee_name a table? if no, then use proper table name
LEFT JOIN project P
ON E.ID = P.employee_id
WHERE P.project_id IS NULL;

Using correlated sub-query

SELECT * FROM employee_name E 
  WHERE NOT EXISTS (SELECT 1 FROM project p
  WHERE p.employee_id= e.employee_id);

Note: I have considered that the employee record is not available in the project table if the employee has no project assigned.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement