Answer: Subquery in the SELECT Clause
Find a detailed explanation of using subqueries on multiple tables.
We'll cover the following...
We'll cover the following...
Solution
The solution is given below:
MySQL
/* The use of subquery to find the employees working on a project */SELECT (SELECT e.EmpName FROM Employees AS e WHERE e.EmpID = p.EmpID) AS EmpName,ProjectNameFROM Projects AS pWHERE p.EmpID IS NOT NULL;
Explanation
The explanation of the solution code is given below:
Lines 2–3: The
SELECTquery selectsEmpName(coming from the subquery) andProjectNamefromProjects, respectively. Thee.EmpNamerefers to theEmpNamecolumn from theEmployeestable (aliased ase) andp.EmpIDrefers to theEmpIDcolumn from theProjectstable (aliased asp).Line 4: The data is retrieved from the
Projectstable.Line 5: The
WHEREclause ensures that only those ...