Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

244
Views
Update where exists delete row where doesn't

I can update fine and I can delete fine, but I want to do both at once.

I have a table ORIG like this, column names are

ref,fname,lname,add1,add2,add3,add4

A1  a  b  c  d  h  j
S2  f  d  s  e  y  t
B3  j  f  s  e  o  p

Where the first column is unique

then another table DATA like this

ref,fname,lname,add1,add2

A1  b  c  d  e
B3  k  g  h  t

I want to update the first table using the second and delete any rows that don't have the unique

So the end result needs to be table ORIG like below

A1  b  c  d  e  h  j
B3  k  g  h  t  o  p

Am I able to accomplish this in one go?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I might approach this using CTEs:

with u as (
      update orig
          set b = d.b, . . .
          from data d
          where d.a = orig.a
      returning *
     )
delete from orig
    where not exists (select 1 from u where u.a = d.a);

The first CTE updates the rows. The second one does the delete.

over 4 years ago · Santiago Trujillo Report

0

You can use a correlated subquery for this:

delete
from orig o
where not exists (
        select 1
        from data d
        where d.col1 = o.col1
        )
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!