# Help understanding these two outputs from correlated sub-query exercise

**URL:** <https://forum.codewithmosh.com/t/help-understanding-these-two-outputs-from-correlated-sub-query-exercise/71708>\
**Category:** SQL\
**Created:** [October 19, 2025, 9:46am UTC](https://forum.codewithmosh.com/t/help-understanding-these-two-outputs-from-correlated-sub-query-exercise/71708 "2025-10-19T09:46:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vesred](https://avatars.discourse-cdn.com/v4/letter/v/bc79bd/32.png) [@vesred](https://forum.codewithmosh.com/u/vesred)\
**Post date:** [October 19, 2025, 9:46am UTC](https://forum.codewithmosh.com/t/help-understanding-these-two-outputs-from-correlated-sub-query-exercise/71708/1 "2025-10-19T09:46:03Z")

</div>

Hello,

I need some help understanding the difference between these two queries.

The first query i’m not using a correlated sub-query but still a sub-query non-the-less. I believe I am asking

- list the employees who have a salary greater than the average of all employees.

SELECT \*  
FROM employees  
WHERE salary \> (  
SELECT AVG(salary)  
FROM employees)

 ![Nocorrelated](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/1/15af9305df1ae6caf4fb8ad4f674e8457de43fe9.png)

Below I am using a correlated sub-query version of the above but my expectation is that the two would bring back the same number of employees. Both outputs miss out some employees with greater than avarage salaries.

SELECT \*  
FROM employees e  
WHERE salary \> (  
SELECT AVG(salary)  
FROM employees  
WHERE office\_id = e.office\_id)

| employee\_id | first\_name | last\_name | job\_title | salary | reports\_to | office\_id |
| --- | --- | --- | --- | --- | --- | --- |
| 37851 | Sayer | Matterson | Statistician III | 98926 | 37270 | 1 |
| 40448 | Mindy | Crissil | Staff Scientist | 94860 | 37270 | 1 |
| 56274 | Keriann | Alloisi | VP Marketing | 110150 | 37270 | 1 |
| 67009 | North | de Clerc | VP Product Management | 114257 | 37270 | 2 |
| 67370 | Elladine | Rising | Social Worker | 96767 | 37270 | 2 |
| 72540 | Guthrey | Iacopetti | Office Assistant I | 117690 | 37270 | 3 |
| 76196 | Mirilla | Janowski | Cost Accountant | 119241 | 37270 | 3 |
| 84791 | Hazel | Tarbert | General Manager | 93760 | 37270 | 4 |
| 95213 | Cole | Kesterton | Pharmacist | 86119 | 37270 | 4 |
| 98374 | Estrellita | Daleman | Staff Accountant IV | 70187 | 37270 | 5 |
| 115357 | Ivy | Fearey | Structural Engineer | 92710 | 37270 | 5 |

In addition, the correlated sub-query seems to return a value LESS than the average of all employees, Estrellita Daleman has a salary of 70,187 but a simple AVG(salary) FROM employees tells me that the average is 82,502. What am I not getting here?

Thank you.

---

<div class="post-metadata">

**Author:** ![vesred](https://avatars.discourse-cdn.com/v4/letter/v/bc79bd/32.png) [@vesred](https://forum.codewithmosh.com/u/vesred)\
**Post date:** [October 19, 2025, 10:21am UTC](https://forum.codewithmosh.com/t/help-understanding-these-two-outputs-from-correlated-sub-query-exercise/71708/2 "2025-10-19T10:21:25Z")

</div>

I’ve figured it out i think with the help of co-pilot. Mosh didn’t explain this one very well.

What wasn’t clear is that the specific command “WHERE office\_id = e.office\_id” means that the average will be calculated based on the office\_id of each employee record the query looks at. This of course saves doing a long drawn out number of sub-queries that calculate the average of each office\_id.
