Deleting rows referenced from another table.

Posted on 2010-01-08
Last Modified: 2012-05-08
I have a table with a ton of duplicate rows I need to remove.  I have it to the point where I have another table, with one field, ID, that contains the ID of the records I want to delete in the other table.

DELETE FROM bigtable WHERE ID = smalltable.ID

I know how I'd do this in something like C# but I'm stumped when it comes to SQL.  How do I run a loop through the smaller 'ID' table and at the same time, delete each of the associated records in the big table?  I probably need a join but how to incorporate the delete?

Question by:Meshman333
    LVL 26

    Accepted Solution

    Delete from bigtable
    FROM bigtable inner join smalltable on bigtable.ID = smalltable.ID
    LVL 75

    Assisted Solution

    by:Aneesh Retnakaran
    DELETE FROM bigtable WHERE ID IN (SELECT ID FROM  smalltable )

    Author Closing Comment

    Awesome, thanks!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    PRTG Network Monitor: Intuitive Network Monitoring

    Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

    How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
    The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now