0% found this document useful (0 votes)
5 views

Question 1

A nested select statement, or subquery, is used in SQL to retrieve data for the main query, enabling filtering, aggregation, or joining from multiple tables. An example demonstrates how to find employees in the HR department earning above the average salary using subqueries to get the department ID and average salary. This technique allows for complex queries by combining data from different tables effectively.

Uploaded by

raskeljay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Question 1

A nested select statement, or subquery, is used in SQL to retrieve data for the main query, enabling filtering, aggregation, or joining from multiple tables. An example demonstrates how to find employees in the HR department earning above the average salary using subqueries to get the department ID and average salary. This technique allows for complex queries by combining data from different tables effectively.

Uploaded by

raskeljay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1. What is the purpose of nested select statement in SQL?

Provide an
example showing how a nested select statement can be used to retrieve
data from multiple tables

A nested select statement, also known as a subquery, is a query within


another SQL query. Its purpose is to retrieve data that can be used in the
main query. This can be useful for filtering results, calculating aggregates, or
joining data from multiple tables.

### Example:

Let's consider two tables: `employees` and `departments`.

**Table: employees**

| employee_id | name | department_id | salary |

|-------------|-----------|---------------|--------|

|1 | Alice |1 | 70000 |

|2 | Bob |2 | 60000 |

|3 | Charlie | 1 | 80000 |

|4 | David |3 | 50000 |

**Table: departments**

| department_id | department_name |

|---------------|------------------|

|1 | HR |

|2 | Engineering |

|3 | Sales |
### Use Case:

Suppose you want to find the names of employees who work in the HR
department and earn more than the average salary in that department. You
can use a nested select statement to achieve this.

### SQL Query:

```sql

SELECT name

FROM employees

WHERE department_id = (SELECT department_id

FROM departments

WHERE department_name = 'HR')

AND salary > (SELECT AVG(salary)

FROM employees

WHERE department_id = (SELECT department_id

FROM departments

WHERE department_name = 'HR'));

```

### Explanation:

1. The first subquery `(SELECT department_id FROM departments WHERE


department_name = 'HR')` retrieves the `department_id` for the HR
department.

2. The second subquery `(SELECT AVG(salary) FROM employees WHERE


department_id = (SELECT department_id FROM departments WHERE
department_name = 'HR'))` calculates the average salary of employees in
the HR department.

3. The main query then selects employee names from the `employees` table
where the `department_id` matches the HR department and the salary is
greater than the average salary calculated by the subquery.

This is a powerful way to combine data from multiple tables and perform
complex queries in SQL.

You might also like