Delete based on a subquery

Question

In our previous exercises, we deleted a specific member who had never made a booking. How can we make that more general, to delete all members who have never made a booking?

Expected Results

memidsurnamefirstnameaddresszipcodetelephonerecommendedbyjoindate
0GUESTGUESTGUEST0(000) 000-0000NULLSun Jul 01 2012 00:00:00 GMT+0000 (Coordinated Universal Time)
1SmithDarren8 Bloomsbury Close, Boston4321555-555-5555NULLMon Jul 02 2012 12:02:05 GMT+0000 (Coordinated Universal Time)
2SmithTracy8 Bloomsbury Close, New York4321555-555-5555NULLMon Jul 02 2012 12:08:23 GMT+0000 (Coordinated Universal Time)
3RownamTim23 Highway Way, Boston23423(844) 693-0723NULLTue Jul 03 2012 09:32:15 GMT+0000 (Coordinated Universal Time)
4JopletteJanice20 Crossing Road, New York234(833) 942-47101Tue Jul 03 2012 10:25:05 GMT+0000 (Coordinated Universal Time)
5ButtersGerald1065 Huntingdon Avenue, Boston56754(844) 078-41301Mon Jul 09 2012 10:44:09 GMT+0000 (Coordinated Universal Time)
6TracyBurton3 Tunisia Drive, Boston45678(822) 354-9973NULLSun Jul 15 2012 08:52:55 GMT+0000 (Coordinated Universal Time)
7DareNancy6 Hunting Lodge Way, Boston10383(833) 776-40014Wed Jul 25 2012 08:59:12 GMT+0000 (Coordinated Universal Time)
8BootheTim3 Bloomsbury Close, Reading, 00234234(811) 433-25473Wed Jul 25 2012 16:02:35 GMT+0000 (Coordinated Universal Time)
9StibbonsPonder5 Dragons Way, Winchester87630(833) 160-39006Wed Jul 25 2012 17:09:05 GMT+0000 (Coordinated Universal Time)
10OwenCharles52 Cheshire Grove, Winchester, 2856328563(855) 542-52511Fri Aug 03 2012 19:42:37 GMT+0000 (Coordinated Universal Time)
11JonesDavid976 Gnats Close, Reading33862(844) 536-80364Mon Aug 06 2012 16:32:55 GMT+0000 (Coordinated Universal Time)
12BakerAnne55 Powdery Street, Boston80743844-076-51419Fri Aug 10 2012 14:23:22 GMT+0000 (Coordinated Universal Time)
13FarrellJemima103 Firth Avenue, North Reading57392(855) 016-0163NULLFri Aug 10 2012 14:28:01 GMT+0000 (Coordinated Universal Time)
14SmithJack252 Binkington Way, Boston69302(822) 163-32541Fri Aug 10 2012 16:22:05 GMT+0000 (Coordinated Universal Time)
15BaderFlorence264 Ursula Drive, Westford84923(833) 499-35279Fri Aug 10 2012 17:52:03 GMT+0000 (Coordinated Universal Time)
16BakerTimothy329 James Street, Reading58393833-941-082413Wed Aug 15 2012 10:34:25 GMT+0000 (Coordinated Universal Time)
17PinkerDavid5 Impreza Road, Boston65332811 409-673413Thu Aug 16 2012 11:32:47 GMT+0000 (Coordinated Universal Time)
20GentingMatthew4 Nunnington Place, Wingfield, Boston52365(811) 972-13775Sun Aug 19 2012 14:55:55 GMT+0000 (Coordinated Universal Time)
21MackenzieAnna64 Perkington Lane, Reading64577(822) 661-28981Sun Aug 26 2012 09:32:05 GMT+0000 (Coordinated Universal Time)
22CoplinJoan85 Bard Street, Bloomington, Boston43533(822) 499-223216Wed Aug 29 2012 08:32:41 GMT+0000 (Coordinated Universal Time)
24SarwinRamnaresh12 Bullington Lane, Boston65464(822) 413-147015Sat Sep 01 2012 08:44:42 GMT+0000 (Coordinated Universal Time)
26JonesDouglas976 Gnats Close, Reading11986844 536-803611Sun Sep 02 2012 18:43:05 GMT+0000 (Coordinated Universal Time)
27RumneyHenrietta3 Burkington Plaza, Boston78533(822) 989-887620Wed Sep 05 2012 08:42:35 GMT+0000 (Coordinated Universal Time)
28FarrellDavid437 Granite Farm Road, Westford43532(855) 755-9876NULLSat Sep 15 2012 08:22:05 GMT+0000 (Coordinated Universal Time)
29Worthington-SmythHenry55 Jagbi Way, North Reading97676(855) 894-37582Mon Sep 17 2012 12:27:15 GMT+0000 (Coordinated Universal Time)
30PurviewMillicent641 Drudgery Close, Burnington, Boston34232(855) 941-97862Tue Sep 18 2012 19:04:01 GMT+0000 (Coordinated Universal Time)
33TupperwareHyacinth33 Cheerful Plaza, Drake Road, Westford68666(822) 665-5327NULLTue Sep 18 2012 19:32:05 GMT+0000 (Coordinated Universal Time)
35HuntJohn5 Bullington Lane, Boston54333(899) 720-697830Wed Sep 19 2012 11:32:45 GMT+0000 (Coordinated Universal Time)
36CrumpetEricaCrimson Road, North Reading75655(811) 732-48162Sat Sep 22 2012 08:36:38 GMT+0000 (Coordinated Universal Time)

Your Answer

Your Results

Loading database...

Answers and Discussion

delete from cd.members where memid not in (select memid from cd.bookings);

We can use subqueries to determine whether a row should be deleted or not. There's a couple of standard ways to do this. In our featured answer, the subquery produces a list of all the different member ids in the cd.bookings table. If a row in the table isn't in the list generated by the subquery, it gets deleted.

An alternative is to use a correlated subquery. Where our previous example runs a large subquery once, the correlated approach instead specifies a smaller subqueryto run against every row.

delete from cd.members mems where not exists (select 1 from cd.bookings where memid = mems.memid);

The two different forms can have different performance characteristics. Under the hood, your database engine is free to transform your query to execute it in a correlated or uncorrelated fashion, though, so things can be a little hard to predict.