Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • MySQL subqueries examples

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 201
    Comment on it

    Subqueries are generally similar to Normal queries in databases like -

    1. They may return one column

    2. They may return more column

    3. They may return single rows

    4. 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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: