Search code examples
sqlsubquerywhere-clause

How do I remove rows based off of cell value? - SQL


Trying to see if anyone can help if this is possible.

I'm trying to find a way that I could have SQL identify anything listed as Aluminum and then remove all of the rows associated with that brand. So where Coors has Aluminum, it eliminates all Coors rows.

Sample:

Brand       Alcohol  Container
Absolute     Vodka    Glass
Absolute     Vodka    Plastic
Coors        Beer     Glass
Coors        Beer     Aluminum
Coors        Beer     Plastic
Gallo        Wine     Glass
Yellowtail   Wine     Glass
Yellowtail   Wine     Aluminum
Yellowtail   Wine     Plastic
Corona       Beer     Plastic
Corona       Beer     Glass

Finish Product:

Brand       Alcohol  Container
Absolute     Vodka    Glass
Absolute     Vodka    Plastic
Gallo        Wine     Glass
Corona       Beer     Plastic
Corona       Beer     Glass

Thanks, Matt


Solution

  • You can use not exists:

    select t.*
    from mytable t
    where not exists (
        select 1 
        from mytable t1 
        where t1.brand = t.brand and t1.container = 'Aluminium'
    )