delete records which are selected from select query on the same table

12,131

Solution 1

Your query would look something like

delete from emp 
from  emp e
where exists (select * 
              from EMP 
              WHERE e.EMPNAME = EMPNAME
               AND  e.EMPSALARY = EMPSALARY 
               AND  <another Condition>)   --<-- Condition on which you want to delete rows

Solution 2

you can directly give condition in where clause

delete from emp where <conditions>;

else you can do in this way

DELETE FROM Table1 T1
WHERE EXISTS (SELECT column1 FROM table2 T2
               WHERE T1.column1 = T2.column1 
                 AND T1.Column2 = T2.column2 );
Share:
12,131
Madhuprathap
Author by

Madhuprathap

Updated on June 25, 2022

Comments

  • Madhuprathap
    Madhuprathap almost 2 years

    I need to delete few records which I got from a subquery.

    Let's say that the EMP table has columns EMPNAME, EMPSALARY and the primary key is a combination of EMPNAME,EMPSALARY.

    delete from emp 
    where exists (
            select * 
            from EMP ***** //query which fetch few records from EMP table);
    

    The above query is not working.

    Thanks for the help.