Problem solve Get help with specific problems with your technologies, process and projects.

Basic three-table join with a condition

I have to join three tables: the Rental table, that has the First and Last Name of the Renter, the Rental Unit table that has the Rental Address, and the Active Rentals table that has the Start Date and Length. The information must come from the Active Rentals table. Only active rentals can be listed. Also I have to display all units with an ocean view. These come from Yes/No fields. I want to use True and False as criterion.

Let's take the active rentals first. You said the Active Rentals table has Start Date and Length. I will assume that Length is in days, and that an "active" rental is one where the Start Date is less than today, and if you added Length days, it would make the duration of the rental go from the Start Date to at least today, or beyond today into the future. The join is the easy part.

select U.RentalAddress
     , R.FirstName
     , R.LastName
     , A.StartDate
     , A.Length
  from ActiveRentals A 
  join RentalUnit U
    on A.UnitID = U.UnitID 
  join Rental R
    on A.RentalID = R.RentalID
 where A.StartDate + Length days
    >= current_date

You will probably have to change this syntax to match your particular database. For example, in Microsoft Access, you would need to parenthesize one of the inner joins and change the WHERE condition to

 where DATEADD("d",Length,StartDate) >= DATE()

For your second question, units with an ocean view,

select RentalAddress
  from RentalUnit 
 where OceanView <> 0

For more on Yes/No fields, see Boolean Columns.

Dig Deeper on Oracle and SQL

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.