A Subquery is a query written inside another SQL query. It is also known as:
- Inner Query
- Nested Query
- Subquery
A subquery is used to return data that will be used by the main query as a condition to further restrict the data to be retrieved.
-
Subqueries must be enclosed within parentheses
(). -
A subquery can have only one column in the
SELECTclause unless multiple columns are used in the main query. -
ORDER BYcannot be used in a subquery. However,GROUP BYcan be used when required. -
Subqueries that return multiple rows can only be used with operators such as:
INANYALLEXISTS
Write a query to display the last name, first name, and salary of all employees who earn more than Pandey.
SELECT l_name,
f_name,
salary
FROM emp
WHERE salary >
(
SELECT salary
FROM emp
WHERE l_name = 'Pandey'
);| L_NAME | F_NAME | SALARY |
|---|---|---|
| Ganeshan | Prabhakaran | 45000 |
| Nair | DEEP | 55000 |
| K | Ashwin | 36000 |
| Raj | Shashi | 85000 |
| Sam | Adam | 53000 |
| Devraj | Meghana | 53000 |
| Bhupathi | Braven | 45500 |
| T | Mamatha | 46000 |
| patel | Pankaj | 39500 |
Write a query to display the department id and first name of all employees who work in the same department in which 'Prabhakaran' works.
SELECT dept_id,
f_name
FROM emp
WHERE dept_id =
(
SELECT dept_id
FROM emp
WHERE f_name = 'Prabhakaran'
);| DEPT_ID | F_NAME |
|---|---|
| 22 | Prabhakaran |
| 22 | Shashi |
| 22 | Andy |
| 22 | Braven |
| 22 | Pankaj |
| 22 | Janardhan |
Write a query to display the department id, first name, and job id for all employees who work in the Administration department.
SELECT dept_id,
f_name,
job_id
FROM emp
WHERE dept_id IN
(
SELECT dept_id
FROM dept
WHERE dept_name = 'Admin'
);| Result |
|---|
| No Data Found |
Write a query to display the employee id of all employees whose department id in employee table is equal to the department id in department table.
SELECT emp_id
FROM emp
WHERE dept_id IN
(
SELECT dept_id
FROM dept
);| EMP_ID |
|---|
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 7 |
| 8 |
| 9 |
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
| 15 |
| 16 |
| 17 |
| 18 |
| 19 |
| 20 |
Write a query to display the last name and job id whose job id is similar to 'King' and whose salary is greater than Singh's salary.
SELECT l_name,
job_id
FROM emp
WHERE job_id =
(
SELECT job_id
FROM emp
WHERE l_name = 'King'
)
AND salary >
(
SELECT salary
FROM emp
WHERE l_name = 'Singh'
);| Result |
|---|
| No Data Found |
| Point | Description |
|---|---|
| Single Row Subquery | Returns only one row |
| Multiple Row Subquery | Returns more than one row |
| IN Operator | Used with multiple row subqueries |
| Subquery | Executed first |
| Main Query | Executed after subquery |