subquery
A subquery is a SELECT statement that is written inside another SQL statement . To distinguish the subquery from its enclosing query , it must be enclosed within parentheses.
SELECT * FROM users WHERE uid IN — Main Query
(SELECT uid FROM loans WHERE amount>10000); — Sub query
This Sub query will return all user id from loan table who has loan amount greater than 10000
than the main query selects all the user details of the returned user ids
We can also use = for condition
like
SELECT * FROM users WHERE uid = — Main Query
(SELECT uid FROM loans WHERE amount>10000); — Sub query
But here we must make sure the subquery must not return more than one user id
else we will be fired with an error message like
“subquery returns more than one row”,
