How to join 2 tables without duplicates (SQL server)?


  • QA Trainee

    I have two following tables:

    table1:

    id name city subject

    1 name1 city1 subject1
    2 name2 city2 subject2
    3 name3 city3 subject3
    4 name4 city4 subject4
    ...

    table2:

    id name city subject

    1 name1 city1 subject1
    2 name2 city2 subject2
    3 name3 city3 subject3
    4 name4 city4 subject4
    ...

    How to avoid duplication's when joining these 2 tables?



  • You can use distinct function



  • UNION checks for duplicates and only distinct rows will be returned

    SELECT * FROM table1
    UNION SELECT * FROM table2
    

Log in to reply
 

Suggested Topics

  • 2
  • 1
  • 2