Subqueries are generally similar to Normal queries in databases like -
They may return one column
They may return more column
They may return single rows
They may return multiple rows
Or it can be any of combination from above.
SubQueries are those queries which we use within any of the Normal Queries.
Example:-
select * from users where user_id =(select user_id from profile where type='user');
In the above queries used within bracket is sub queries.
Generally there are four kind of sub-queries.
A. Scalar Subqueries: return single columan of single row.
select * from users where user_id =(select user_id from profile where type='user' limit 1);
B. Row Subqueries: They will return single row
select u1.* from users As u1 left join (select user_id,country from profile limit 1) as u2 on u2.user_id=u1.user_id and u2.country=u1.country
C. Column Subqueries: They will return one column of one or more rows.
select * from users where user_id in(select user_id from profile where type='user');
D. Table Subqueries: They will return one more col of one/more rows
select u1.* from users As u1 left join (select user_id,country from profile) as u2 on u2.user_id=u1.user_id and u2.country=u1.country
0 Comment(s)