Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • UNION CLAUSE IN SQL

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 295
    Comment on it

    UNION CLAUSE

    The union operator in sql is used to combined two different queries into a singel table. For eg:-we have one table which have information about employee, also have another table that have information about their department, and their is no relationship between them. Suppose that both employee and department table have one similar fields that have information about employee_id, and we want to get all information about employee_id from both the tables at the same time. We can do this by using union clause.

    The important thing which have to be in mind while using union clause is that.

    - All SELECT statement that we use to combine in union query must have same number of columns to be fetched

    Syntax of union clause

    SELECT column(s)
    FROM table
    WHERE [condition]
    UNION
    SELECT column(s)
    FROM table
    WHERE [condition];
    

    eg;-

    Below is a selection from the "Customers" table:

    CustomerID       CustomerName             Address              City           PostalCode     
    1                     suresh             tilak road            rishikesh      240098 
    2                     amit               survey road           bahadurgarh    293400
    3                     saurav            neshvilla road         dehardun       248001
    

    And a selection from the "Suppliers" table:

    SupplierID        SupplierName       Address           City        PostalCode   
    1                 ram                kalidas road     lucknow       289100
    2                 shyam              dobhalwala       delhi         200001
    3                 radha               cant            agra          230001
    

    The following SQL statement give all the different cities from the "Customers" and the "Suppliers" tables:-

    SELECT City FROM Customers
    UNION
    SELECT City FROM Suppliers
    ORDER BY City;
    

    OUTPUT

    city
    rishikesh
    bahadurgarh
    dehradun
    lucknow
    delhi
    agra
    

 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: