Search code examples
sqlgoogle-bigquerygoogle-cloud-functionsbigquery-udf

Bigquery delete rows that are present in another table


I want to delete all the rows from table t1 which are present in table t2.

table_1 is as follows

a   b   c
1   4   3
3   334 3
5   4   5
6   5   4
4   85  3
7   332 54
8   46  6
45  42  5
7   576 6

and table 2 is as follows

a   b   c
7   332 54
3   334 3
7   576 6

as mentioned I would like to delete all the rows from table t1 which are present in table t2.

So I used the code

DELETE `projectname.datasetname.table1` t
WHERE t IN (SELECT * from `projectname.datasetname.table2`)

but it doesn't work, what would be the ideal solution here? My desired result is

a   b   c
1   4   3
5   4   5
6   5   4
4   85  3
8   46  6
45  42  5

Thanks


Solution

  • If you need to look at the entire record, you can use:

    DELETE `projectname.datasetname.table1` t
    WHERE EXISTS (SELECT 1
                  FROM `projectname.datasetname.table2` t2
                  WHERE TO_JSON_STRING(t2) = TO_JSON_STRING(t)
                 );
    

    However, normally a comparison of a simple id column of some sort is usually sufficient for such comparisons.