SQL Query in another SQL Query = SQL Subquery


You might have wanted to do a SQL query inside an another SQL query. You can do this using SQL Subquery. It’s simple as ABC. Here’s an example :

SELECT * FROM `fdposts` WHERE user IN (SELECT friend FROM `fdfriends` WHERE user=’subins2000′)

The above code will select rows in table fdposts where user is the value of friend row in the table fdfriends.
As you can see it’s very simple. Here’s another example :

SELECT * FROM `posts` WHERE user IN (SELECT name FROM `members` WHERE user=’subins2000′)

The above code will select rows in table posts where user is the value of the row name in the table members.
Enjoy !!!