subquery

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”,

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.