SELECT * FROM users WHERE users.id IN (SELECT vip_users.id FROM vip_users WHERE vip_users.id = users.id)
The inner query depends on the values from the outer query.
Moreover theIN (...)
statement cannot be executed by itself because it depends on the values from users table. Such query is called a correlated subquery.
SELECT employee_number, name
FROM employees AS Bob
WHERE salary > (
SELECT AVG(salary)
FROM employees
WHERE department = Bob.department);
In the above query the outer query is
SELECT employee_number, name
FROM employees AS Bob
WHERE salary > ...
and the inner query (the correlated subquery) is
SELECT AVG(salary)
FROM employees
WHERE department = Bob.department
SELECT
employee_number,
name,
(SELECT AVG(salary)
FROM employees
WHERE department = Bob.department) AS department_average
FROM employees AS Bob
group by employee_number,name ;
Login in to like
Login in to comment