Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Combining Results of two or more SELECT Statements

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 461
    Comment on it

    Combining Results of two or SELECT Statements using UNION OPERATOR:

    UNION Operator is used to combine the results of two or more SELECT statements. But UNION operator does not return duplicate rows so to return duplicate rows UNION ALL operator is used.

    Some important points while using UNION Operator:

    • All SELECT statement must have same number of columns to be fetched .
    • All SELECT statement columns to be fetched must have similar dataypes .

    Syntax for UNION operator:

    SELECT columnname(s)
    FROM tablename
    WHERE [condition]
    UNION
    SELECT columnname(s)
    FROM tablename
    WHERE [condition];
    

    Syntax for UNION ALL operator:

    SELECT columnname(s)
    FROM tablename
    WHERE [condition]
    UNION ALL
    SELECT columnname(s)
    FROM tablename 
    WHERE [condition];
    

    Example:

    SELECT City FROM Employee
    UNION
    SELECT City FROM Trainee;
    

    The above query will fetch all different cities from Employee and Trainee tables.

 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: