简体   繁体   中英

Selecting multiple rows with a sub-query

The question asks "Display the names of all employees whose job title is the same as anyone in the sales dept" but

SELECT name, job 
FROM Employer WHERE job=(SELECT job FROM employer WHERE dept = sales);

does not work because the sub-query returns more than one value. How do I work around this?

Try to use in

SELECT name, job 
    FROM Employer 
    WHERE job in (SELECT job FROM employer WHERE dept = sales);

@Parado has the simplest solution. Keep in mind, however, that the in keyword is really just a join . For example, your query is the same as:

SELECT e1.name, e1.job
FROM Employer e1
    JOIN Employer e2 on e1.job = e2.job
WHERE e2.dept = 'sales'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM