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
    • 364
    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

    1. SELECT column(s)
    2. FROM table
    3. WHERE [condition]
    4. UNION
    5. SELECT column(s)
    6. FROM table
    7. WHERE [condition];

    eg;-

    Below is a selection from the "Customers" table:

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

    And a selection from the "Suppliers" table:

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

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

    1. SELECT City FROM Customers
    2. UNION
    3. SELECT City FROM Suppliers
    4. ORDER BY City;

    OUTPUT

    1. city
    2. rishikesh
    3. bahadurgarh
    4. dehradun
    5. lucknow
    6. delhi
    7. agra

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: