delete table from table in databricks

Shambhu Rai 1,411 Reputation points
2023-09-12T19:21:25.1766667+00:00

Hi Expert,

how to use delete table from table in databricks like below sql example

DELETE FROM Sales.SalesPersonQuotaHistory   
FROM Sales.SalesPersonQuotaHistory AS spqh  
INNER JOIN Sales.SalesPerson AS sp  
ON spqh.BusinessEntityID = sp.BusinessEntityID  
WHERE sp.SalesYTD > 2500000.00; 
Azure Databricks
Azure Databricks
An Apache Spark-based analytics platform optimized for Azure.
2,518 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Marella, Lavanya 0 Reputation points
    2023-09-12T20:40:18.3433333+00:00

    Hi @Shambhu Rai

    Try something like this

    %sql 
    DELETE FROM Sales.SalesPersonQuotaHistory   
    WHERE BusinessEntityID in (
      SELECT spqh.BusinessEntityID 
      FROM Sales.SalesPersonQuotaHistory AS spqh  
    INNER JOIN Sales.SalesPerson AS sp  
    ON spqh.BusinessEntityID = sp.BusinessEntityID  
    WHERE sp.SalesYTD > 2500000.00)
    

  2. AnnuKumari-MSFT 34,556 Reputation points Microsoft Employee Moderator
    2023-09-15T15:45:13.65+00:00

    Hi @Shambhu Rai ,

    Are you trying to look for solution for dataframe or on sql table?

    Alternatively, you can try merge statement like below:

    %sql
    MERGE INTO Sales.SalesPersonQuotaHistory AS spqh
    USING (
      SELECT sqh.*
      FROM Sales.SalesPersonQuotaHistory AS sqh
      JOIN Sales.SalesPerson AS sp
      ON sqh.BusinessEntityID = sp.BusinessEntityID
      WHERE sp.SalesYTD <= 2500000.00
    ) AS sq
    ON spqh.BusinessEntityID = sq.BusinessEntityID
    WHEN MATCHED THEN
      DELETE;
    
    
    

    Hope it helps. Kindly accept the answer by clicking on Accept answer button. Thankyou

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.