User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 422,949 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,650 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser: Programming Forums

Problem with Rewriting Subqueries as Joins

Join Date: Jun 2005
Posts: 12
Reputation: relawson is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
relawson relawson is offline Offline
Newbie Poster

Re: Problem with Rewriting Subqueries as Joins

  #3  
Jun 11th, 2005
First, you need to add some unique primary keys. That will make joining much easier and the code more readable. Look up "normalization" in google and try to get to 3NF. Second, use "not exists" instead of "not in". They both work, but "not in" does a table scan. If your database gets large, that isn't going to work quickly. Not a big deal for lookup tables and small databases, just FYI.

Now for the join:

You tried this:

Select *
From room R, booking B
Where R.room_no = B.room_no
And R.room_type = ‘single’
And B.arrivel_date = ‘2005-04-23’
And B.departure_date=’2005-04-30’ ;

Try something like this:

Select * From Room R
Join Booking B
On R.room_no = B.room_no
Where R.room_type = ‘single’
And B.arrivel_date = ‘2005-04-23’
And B.departure_date=’2005-04-30’

Also, look up on Google the different types of joins and use what you want. Inner is the default in MS SQL; I should have been explicit in my code but now you know. Good Luck.
Reply With Quote  
All times are GMT -4. The time now is 2:28 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC