0

I was trying to solve this question given below by using the subquery in MySQL but can't find a solution as it's showing subquery returns more than one row.

Problem Statement: Display the emp name with their salaries and their managers and managers salary.
Display those only employees whose managers were hired after them.

select w.ename employee, w.SAL woSal, m.ename manager, m.SAL mSal
from emp w, emp m
where w.mgr = m.empno and
(SELECT E1.ENAME  
FROM EMP E1, EMP E2
WHERE E1.MGR=E2.EMPNO and ( E1.hiredate<E2.hiredate));
2
  • It simply means that your sub-query needs more conditions in the where clause so that it returns one row. for any further clarifications please don't hesitate to ask me again Commented Sep 26, 2017 at 19:07
  • 1
    what is 'mgr' column contains. Please add the schema and sample data Commented Sep 26, 2017 at 19:27

1 Answer 1

2

You don't need a subquery - just add the condition on the hiredate to the join condition. Also note that implicit joins (using more than one table in the from clause) is considered deprecated, and you'd probably be better off rewriting it as an explicit join:

SELECT w.ename employee, w.SAL woSal, m.ename manager, m.SAL mSal
FROM   emp w
JOIN   emp m
WHERE  w.mgr = m.empno and w.hiredate < m.hiredate
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.