Category: Blog

  • Finding and Updating Duplicate IDs: #SQLNewBlogger

    Finding duplicates was an interview question for me years ago, and I’ve never forgotten it. Recently I got asked how to easily do this and delete them, so I decided to write a couple of posts on the topic. This one looks at simple, single column IDs. The next one will look at more complex situations.

    Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.

    A Simple Scenario

    Like many people, I like identity fields for primary keys. However, lots of people build tables like this:

    CREATE TABLE PurchaseOrder
    (
         poid INT IDENTITY(1, 1),
         purchaseordernumber VARCHAR(20),
         podate DATETIME,
         active INT
    )
    GO

    No nullability, and no PK constraint. People think an identity prevents duplicates. Run the query above and then the script below.

    INSERT INTO PurchaseOrder
    (
         purchaseordernumber,
         podate,
         active
    )
    VALUES
    ('PO-2023-00001', '2023-01-15 09:30:00', 1),
    ('PO-2023-00002', '2023-01-22 11:45:00', 1),
    ('PO-2023-00003', '2023-02-05 14:20:00', 1),
    ('PO-2023-00004', '2023-02-18 10:15:00', 0),
    ('PO-2023-00005', '2023-03-03 16:30:00', 1),
    ('PO-2023-00006', '2023-03-17 08:45:00', 1),
    ('PO-2023-00007', '2023-04-02 13:10:00', 0),
    ('PO-2023-00008', '2023-04-15 15:25:00', 1),
    ('PO-2023-00009', '2023-05-01 09:50:00', 1),
    ('PO-2023-00010', '2023-05-14 12:05:00', 1),
    ('PO-2023-00011', '2023-06-01 14:40:00', 0),
    ('PO-2023-00012', '2023-06-15 10:35:00', 1),
    ('PO-2023-00013', '2023-07-02 16:55:00', 1),
    ('PO-2023-00014', '2023-07-17 08:20:00', 0),
    ('PO-2023-00015', '2023-08-03 11:30:00', 1),
    ('PO-2023-00016', '2023-08-18 13:45:00', 1),
    ('PO-2023-00017', '2023-09-04 15:10:00', 1),
    ('PO-2023-00018', '2023-09-19 09:25:00', 0),
    ('PO-2023-00019', '2023-10-05 12:40:00', 1),
    ('PO-2023-00020', '2023-10-20 14:15:00', 1)
    GO
    SET IDENTITY_INSERT dbo.PurchaseOrder ON
    GO
    INSERT INTO PurchaseOrder
    ( poid,
         purchaseordernumber,
         podate,
         active
    )
    VALUES
    (19, 'PO-2023-00021', '2026-01-15 09:30:00', 1),
    (14, 'PO-2023-00022', '2026-01-22 11:45:00', 1)
    GO
    SET IDENTITY_INSERT dbo.PurchaseOrder OFF
    GO

    Now if we select all the rows from this table, we might think things are fine. After all, all the purchaseordernumber fields are unique.

    Checking Duplicates

    I’ll run this query. Notice I use a GROUP BY on the poid to list these together with a count. In the image, we see some counts that are greater than 1, which indicates a duplicate. We are grouping, or putting all the rows with the same value together.

    2026-02_0139

    I often will add a HAVING clause to this, which lets me filter the grouped items. When I do that, I just see two items.

    2026-02_0140

    Notice if I change this to purchaseordernumber, I don’t get duplicates. This is because those are unique.

    2026-02_0142

    However, a lot of people often build software that edits using the underlying key, so they can edit the PO number. Let’s do that. I’ll change the PO number for id 19. First we’ll get the current values, and then re-query.

    If we look below, we see separate purchase order numbers, but when we try to update one of them, we get two changed. Because we have duplicate hidden surrogate ID keys.

    2026-02_0143

    We want to fix this, so what can we do?

    What we want to do is find the duplicate 14s and 19s (and others) and change them.

    Fixing the Issue

    While trying to fix this, I realized that one can’t update an identity field. That actually makes the fix really, really simple.

    Since I want to give the rows new poid values, I need to find those rows which are duplicates and then re-insert them into the table. I also need a way to delete the old duplicate values as well.

    This can be tricky, as the purpose of an identity (usually) is to ensure there are not duplicate rows. It’s possible every field in the row is duplicate, which could be an issue. In this case, I’d likely just copy the data back in and delete all the “old” rows, which were the same.

    In my case, the purchaseordernumber is different, so we can use that with the date to decide which is a duplicate and which row we keep.

    SQL New Blogger

    This is a little longer post, and it somewhat got away from me, but this isn’t an easy thing to write about, nor is it short. Easy to mess this up.

    This post took me about 45 minutes to write. The code part wasn’t long, but I had to think about how to frame the issue with test code and explain that. SQL Prompt made the coding easy once I knew what I wanted. I built this over 3-4 days, working on it at 5-10 minutes at a time.

    That’s a great way to tackle complex topics.

    You could do this and impress an interviewer. Highlight this post in your resume/LinkedIn/etc.

  • Whiling away an afternoon, thinking

    I come to Heathrow often. Today is likely somewhere close to 60 trips to this airport in my lifetime. After over 19 years of working with Redgate (plus a trip in college), this is the airport I’ve visited most outside of Denver (my home).

    This is the view outside my hotel window. You can see an A320 (or a Boeing 737) taking off. This is a good sized plane I fly all over the US. It is about 35 meters (100ft) wide and carries 150-180 people.

    As it took off beside the A380 pulling into the terminal, it looked like a toy plane. Those things are massive.

    20260317_151547

    I’ve stayed here a few times after landing to try and get some work done, and to check in early to take a nap. I landed at 10am after flying all night, and I could use some rest. Rather than heading into London and getting stuck sitting in a restaurant waiting to check in, I booked a room here. Already had a nap and ready to work.

    Thinking Time

    This is good thinking time, which is sometimes rare to get. Years ago on one of our multi-times-a-week SQL Server Central calls, Andy told me that he needed time to think about his work as a DBA and manager of others. Time to just consider how things are going, why things are going well or not, and which direction to go.

    Time just thinking about a topic.

    It has stuck with me all these years that time to just think is important, and it helps me clarify how I view things.

    It’s often how I get editorials written. Turning vague ideas into something concrete. I’ll look at a note I’ve made or re-read something and think about it. When inspiration strikes, I’ll start writing.

    Today I was thinking, watching planes take off when I realized I hadn’t seen a plane in awhile. Normally they take off around every 45s at LHR. Then I saw this little vehicle doing down the runway, something I’ve never seem.

    20260317_151301

    I watched for a few minutes and realized there was another one coming the other way. They met in the middle and circled a few times before leaving the runway.

    20260317_151335

    I’m sure this is some FOD (Foreign Object Detection) process looking for pieces that might have fallen off planes (yikes) or vehicles, or somewhere. A pilot friend said that this is something that is done in the military, sometimes by grunts walking the runway looking for something that might get pulled into a jet engine.

    In any case, it was something new and unusual and inspired me.

    I don’t know that everyone needs thinking time, but if you manage or lead or are trying to develop strategy, I do believe you need some quiet thinking time when you’re not really getting anything done, but just thinking about work.

    Glad that I took the time today and grateful to have the opportunity.

    Now back to writing about AI and other things impacting the data professional.


  • Balancing Life with a Career–Slides

    I had a great time at SQL Saturday Atlanta 2026 this past weekend. I had some good comments and a few questions. I’ll write a bit more on this, but I’ve uploaded the slides (apologies for the delay).

    Balancing Life with a Career

  • Advice I Like: Knots

    Learn how to tie a bowline knot. Practice in the dark. With one hand. For the rest of your life, you’ll use this knot more times than you would ever believe.” – from Excellent Advice for Living

    I like this advice, not because I use this know a lot, but because there are things you should be able to do in the physical world, and some self-reliance and ability is handy.

    For the record I do use bowlines regularly around the ranch, but we use some horseman’s slipknots (manger tie) and square knots more.

    In my life, there are small skills like this that get used over and over. We still live in the physical world, and not only are physical skills useful, but there is a lot of satisfaction from being able to accomplish something yourself.

    I’ve been posting New Words on Fridays from a book I was reading, however, a friend thought they were a little depressing. They should be as they are obscure sorrows. I like them because they make me think.

    To counter-balance those, I’m adding in thoughts on advice, mostly from Kevin Kelley’s book. You can read all these posts under the advice tag.