Deleting Values from a table based on an id from another?

I have two tables:

Data
Orders

I want to delete any data which does not belong to an order.

So for example my ID's could be

Orders    |     Data
------------------------
700001   |     700001
700002   |     700002
700003   |     700003
Null         |     700004
Null         |     700005
Null         |     700006

As you can see Data contains 3 orders which do not exist in the Orders Table, this is because an order has been canceled. I would like to cleanup the Data table using a delete, but am uncertain as to how I do joins within a delete (or if it's needed).

Cheers
directxBOBAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chaitu chaituCommented:
delete from data d
where

not exists (select 'x' from orders or
where or.orderid=d.orderid
);
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
what database? (optimal) syntax can vary...
0
directxBOBAuthor Commented:
SQL on a windows mobile device
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Pratima PharandeCommented:
delete from data d
where
d.id not in
 (select orders.id from orders
);

here id means the firld whick contains 700001,700002 etc
0
directxBOBAuthor Commented:
Mobile SQL doesn't appear to like Sub Queries
0
directxBOBAuthor Commented:
More digging

select * from data d
where

not exists (select * from orders or
where or.orderid=d.orderid);

Appears to work, but it doesn't work when using a delete.
0
Pratima PharandeCommented:
delete from data
where

not exists (select * from orders
where orders.orderid=data.orderid);
0
Pratima PharandeCommented:
delete from data
where
data.id not in
 (select orders.id from orders
);
0
directxBOBAuthor Commented:
How would I rewrite this:

delete from data
where
data.id not in
 (select orders.id from orders);

using a join?
0
Pratima PharandeCommented:
As I know delete query not work with joins
0
directxBOBAuthor Commented:
http://support.microsoft.com/kb/303004

This appears to explain the issue of using subqueries within a delete.
0
Pratima PharandeCommented:
Use with(nolock) with subquery which will stop locking the table

delete from data
where
data.id not in
 (select orders.id from orders with(nolock));

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
directxBOBAuthor Commented:
Cheers for all the help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.