Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7399375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:03:10+00:00 2026-05-29T04:03:10+00:00

I want make sql query which will insert values from one table to another

  • 0

I want make sql query which will insert values from one table to another table by checking where condition on 1st table.

I have to check is that row present previously in 1st table or not. If not present then add otherwise don’t add.

There is query “insert into select from” pattern in sql.

I have tried following query. But it inserts many duplicates.

INSERT INTO 
    company_location (company_id, country_id, city_id)
SELECT 
    ci.company_id, hq_location, hq_city
FROM 
    company_info ci, company_location cl 
WHERE  
    ci.company_id <> cl.company_id
    AND cl.country_id <> ci.hq_location
    AND cl.city_id <> ci.hq_city;

Duplicate avoiding means that tuple (company_id, country_id, city_id) shouldn’t added again. And I have to add from more 4 tables into these table.

Also I require query for removing duplicates from company_location. i.e. combination of (company_id, country_id, city_id) should exist only single time. Keep only one tuple and remove other rows.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:03:11+00:00Added an answer on May 29, 2026 at 4:03 am

    I hope this untested Script helps! It inserts every combination just once.

    INSERT INTO company_location 
        (company_id,country_id,city_id)
    SELECT  distinct ci.company_id, 
            ci.hq_location, 
            ci.hq_city
    FROM    company_info ci  
    WHERE  ci.company_id NOT IN 
            (SELECT cl1.company_id FROM company_location cl1
                WHERE cl1.country_id = ci.hq_location
                AND cl1.city_id = ci.hq_city
                AND cl1.company_id = ci.company_id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make a table in SqlServer that will add, on insert, a
Hi I want to design a class using Groovy Sql which I will be
I want to make many inserts in a single SQL statement, pretty much like
If we want in our WPF application to let a user make a SQL
I have a MS SQL view that I want to make available as a
Given this table structure, I want to flatten out the many-to-many relationships and make
My job is the maintain one application which heavy use SQL server (MSSQL2005). Until
I am fetching records from one table with count of one field with other
I have a table where I'm storing Lat/Long coordinates, and I want to make
I need help to build SQL query. I have 2 tables table PROJECT and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.