Results 1 to 8 of 8

Thread: [RESOLVED] I don't like RED (DELETE FROM TABLE WHERE COLOR = 'RED' .. but not all of them

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2018
    Location
    Krähental
    Posts
    64

    Resolved [RESOLVED] I don't like RED (DELETE FROM TABLE WHERE COLOR = 'RED' .. but not all of them

    Assume I have a table containing a list of my Clothes like this:

    Code:
    Item	Color
    SHIRT	RED
    SHIRT	BLUE
    SHIRT	WHITE
    PANTS	RED
    PANTS	GREEN
    HAT	RED
    SHOES	RED
    SHOES	BLACK
    SHOES	BROWN
    SOCKS	RED
    COAT	BLUE
    and now I don't like RED so I want to delete all the RED ones. This SQL would do that

    Code:
    DELETE FROM Wardrobe WHERE Color = 'RED'
    But some of the items are only available in one color. For example I don't want to delete my HAT even though it's RED because I've only got one HAT.

    So I want to delete all the red items except in cases where there's no alternative color.

    This seems to work.

    Code:
    DELETE FROM Wardrobe WHERE Color = 'RED'
    AND EXISTS (SELECT ITEM FROM Wardrobe AS CLOTHES WHERE CLOTHES.Item = Wardrobe.Item AND CLOTHES.Color  <> 'RED')
    I figured it out by trial and error and that seems to work but I'm still nervous.

    Is it reliable?
    is there a better way?
    Although it's just one line does it put a load on the server?
    or what?

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: I don't like RED (DELETE FROM TABLE WHERE COLOR = 'RED' .. but not all of them

    Yes - this is an acceptable approach - one I would have probably used myself.

    If this is MS SQL my syntax would have been

    Code:
    Delete From Wardrobe
       From Wardrobe WX
       Where WX.Color='RED' 
          and Exists(Select * From Wardrobe WX2 Where WX2.Item=WX.Item and WX2.Color<>'RED')
    I use this syntax so I can comment out --Delete From Wardrobe and change it to SELECT * (for testing).

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2018
    Location
    Krähental
    Posts
    64

    Re: I don't like RED (DELETE FROM TABLE WHERE COLOR = 'RED' .. but not all of them

    Thank you

  4. #4
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,902

    Re: [RESOLVED] I don't like RED (DELETE FROM TABLE WHERE COLOR = 'RED' .. but not all

    The only downside I can see with that approach is that it uses a corelated subquery. If this is being run across very large volumes of data that can be a bit of a performance hog because it needs to be resolved for each row individually. Usually you will get a better result if you can express the query using a join:-

    SQL Code:
    1. Create Table #Wardrobe (Item Varchar(8), Color Varchar(8))
    2.  
    3. Insert into #Wardrobe
    4. Values  ('SHIRT','RED'),
    5.         ('SHIRT','BLUE'),
    6.         ('SHIRT','WHITE'),
    7.         ('PANTS','RED'),
    8.         ('PANTS','GREEN'),
    9.         ('HAT','RED'),
    10.         ('SHOES','RED'),
    11.         ('SHOES','BLACK'),
    12.         ('SHOES','BROWN'),
    13.         ('SOCKS','RED'),
    14.         ('COAT','BLUE')
    15.  
    16.  
    17. Delete RedClothes
    18. From #Wardrobe as RedClothes
    19. Join #Wardrobe as NonRedClothes
    20.     on RedClothes.Item = NonRedClothes.Item
    21. where RedClothes.Color = 'RED'
    22. And NonRedClothes.Color <> 'RED'
    23.  
    24. Drop Table #Wardrobe

    I say usually because nothing is certain with the Query Engine. You might find that the sub query is actually better in your case so I'd suggest giving it a quick test with either approach.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2018
    Location
    Krähental
    Posts
    64

    Re: [RESOLVED] I don't like RED (DELETE FROM TABLE WHERE COLOR = 'RED' .. but not all

    @FunkyDexter

    Do all DB's support that JOIN syntax? It's been a while since I worked with JET but I seem to remember it couldn't delete from a JOIN'ed table?

  6. #6
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,902

    Re: [RESOLVED] I don't like RED (DELETE FROM TABLE WHERE COLOR = 'RED' .. but not all

    I don't think you mentioned the platform and, since you were talking about server performance, I guessed at SQL Server or MySQL, either of which would support it.

    I quick Google search says that Access does not I'm afraid
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2018
    Location
    Krähental
    Posts
    64

    Re: [RESOLVED] I don't like RED (DELETE FROM TABLE WHERE COLOR = 'RED' .. but not all

    Quote Originally Posted by FunkyDexter View Post
    I don't think you mentioned the platform and, since you were talking about server performance, I guessed at SQL Server or MySQL, either of which would support it.

    I quick Google search says that Access does not I'm afraid
    You're right. I didn't mention the platform - Sorry. I kind if forgot about jet till you mentioned that join syntax and it reminded me. My application uses IDBConnection and can connect to either MSSQL or MySQL so I try to use a common syntax with a function that fixes up things like "TOP n vs LIMIT n" etc. And, unfortunately, I still need to be able to connect to Jet for backward compatibility.

    I'm trying to drop support for JET. As soon as I can do that I'll come back and look at that join syntax. Thanks.

  8. #8
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,902

    Re: [RESOLVED] I don't like RED (DELETE FROM TABLE WHERE COLOR = 'RED' .. but not all

    Yeah, the delete from syntax isn't actually covered by the ANSII standard. Just about every current engine supports it... except JET. Kinda sucks but there you have it I'm afraid.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width