Question 1
Question 1
Provide an
example showing how a nested select statement can be used to retrieve
data from multiple tables
### Example:
**Table: employees**
|-------------|-----------|---------------|--------|
|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
SELECT name
FROM employees
FROM departments
FROM employees
FROM departments
```
### Explanation:
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.