Multiple Row Sub Query
A Multiple Row Sub Query returns a result of multiple rows to the outer/main/parent query. It includes the following operators:
- IN
- ANY
- ALL or EXISTS
Example
- SELECT e.first_name, e.salary
- FROM employees e
- WHERE salary IN ( SELECT MIN(e.salary)
- FROM employees e
- GROUP BY e.department_id);
Execute the Query, then the result will be as in the following:
Multiple Column Sub Query
Multiple Column Sub Queries are queries that return multiple columns to the outer SQL query. It uses the IN operator for the WHERE and HAVING clause.
- SELECT e.department_id, e.job_id,e.salary
- FROM employees e
- WHERE (e.job_id, e.salary) IN ( SELECT e.job_id, e.salary
- FROM employees e
- WHERE e.department_id = 50) ;
Execute the Query, then the result will be as in the following:
Note: We can use a Sub Query using a FROM clause in the main query.
- SELECT e.first_name, e.salary, e.department_id, b.salary_avg
- FROM employees e,
- (SELECT e1.department_id, AVg(e1.salary) salary_avg
- FROM employees e1
- GROUP BY e1.department_id) b
- WHERE e.department_id = b.department_id AND e.salary > b.salary_avg;

Comments
Join the conversation! Your thoughts help the community grow.