In my experience, the main difference between a correlated and a non-correlated subquery in SQL lies in how they are executed and their dependency on the outer query.
A non-correlated subquery is a subquery that can be executed independently of the outer query. It does not rely on any information from the outer query, and the result of the subquery can be used as an input for the main query. I like to think of it as a standalone query that is embedded within another query. For example, you might use a non-correlated subquery to find the average salary of all employees and then use that value to compare with individual employee salaries in the main query.
On the other hand, a correlated subquery is a subquery that depends on the outer query for its execution. It references one or more columns from the outer query, and it is executed once for each row in the outer query. This can lead to performance issues, as the subquery needs to be executed multiple times. One example of a correlated subquery is when you want to find all employees whose salary is above the average salary for their department. In this case, the subquery needs to calculate the average salary for each employee's department, so it must be executed for each employee.
A non-correlated subquery is a subquery that can be executed independently of the outer query. It does not rely on any information from the outer query, and the result of the subquery can be used as an input for the main query. I like to think of it as a standalone query that is embedded within another query. For example, you might use a non-correlated subquery to find the average salary of all employees and then use that value to compare with individual employee salaries in the main query.
On the other hand, a correlated subquery is a subquery that depends on the outer query for its execution. It references one or more columns from the outer query, and it is executed once for each row in the outer query. This can lead to performance issues, as the subquery needs to be executed multiple times. One example of a correlated subquery is when you want to find all employees whose salary is above the average salary for their department. In this case, the subquery needs to calculate the average salary for each employee's department, so it must be executed for each employee.