Oracle SQL Selecting rows from one table not in another

can anyone help me with my sql statement below. What i am trying to do is retrieve rows from 1 table where they do not appear within the date range of another table. The query below at the moment returns the row that is between the date range entered. How could i modify it so that the rows not between the date range in table 2 are returned from table 1? table 1 would be homes and table 2 would be bookings

SELECT homes.home_id, homes.title, homes.description, homes.living_room_count, homes.bedroom_count, homes.bathroom_count, homes.price, homes.sqft,
listagg(features.feature_name, '\n') WITHIN GROUP(ORDER BY features.feature_name) features, home_type.type_name
FROM homes
INNER JOIN bookings ON bookings.home_id = homes.home_id
INNER JOIN home_feature ON homes.home_id = home_feature.home_id
INNER JOIN home_type ON home_type.type_code = homes.type_code
INNER JOIN features ON home_feature.feature_id = features.feature_id
WHERE bookings.booking_end < to_date('23-Jan-13')
OR bookings.booking_start > to_date('22-Jan-13')
GROUP BY homes.home_id, homes.title, homes.description, homes.living_room_count, homes.bedroom_count, homes.bathroom_count, homes.price, homes.sqft, home_type.type_name

Answers


You will want to use a LEFT JOIN to return all rows from homes that do not appear in bookings. I would also suggest moving the bookings.date filter from the WHERE clause to the JOIN condition:

SELECT homes.home_id, 
    homes.title, 
    homes.description, 
    homes.living_room_count, 
    homes.bedroom_count, 
    homes.bathroom_count, 
    homes.price, homes.sqft,
   listagg(features.feature_name, '\n') WITHIN GROUP(ORDER BY features.feature_name) features, 
    home_type.type_name
FROM homes
LEFT JOIN bookings 
   ON bookings.home_id = homes.home_id
   AND (bookings.booking_end < to_date('23-Jan-13')
    OR bookings.booking_start > to_date('22-Jan-13'))
LEFT JOIN home_feature 
  ON homes.home_id = home_feature.home_id
LEFT JOIN home_type 
  ON home_type.type_code = homes.type_code
LEFT JOIN features 
  ON home_feature.feature_id = features.feature_id
GROUP BY homes.home_id, homes.title, 
    homes.description, homes.living_room_count, 
    homes.bedroom_count, homes.bathroom_count, 
    homes.price, homes.sqft, home_type.type_name

Based on your comment, you can try a NOT EXISTS:

SELECT homes.home_id, 
    homes.title, 
    homes.description, 
    homes.living_room_count, 
    homes.bedroom_count, 
    homes.bathroom_count, 
    homes.price, homes.sqft,
   listagg(features.feature_name, '\n') WITHIN GROUP(ORDER BY features.feature_name) features, 
    home_type.type_name
FROM homes
INNER JOIN home_feature 
  ON homes.home_id = home_feature.home_id
INNER JOIN home_type 
  ON home_type.type_code = homes.type_code
INNER JOIN features 
  ON home_feature.feature_id = features.feature_id
WHERE NOT EXISTS (SELECT home_id
                  FROM bookings b
                  WHERE b.home_id = homes.home_id
                    AND (b.booking_end < to_date('23-Jan-13')
                      OR b.booking_start > to_date('22-Jan-13'))
GROUP BY homes.home_id, homes.title, 
    homes.description, homes.living_room_count, 
    homes.bedroom_count, homes.bathroom_count, 
    homes.price, homes.sqft, home_type.type_name

Need Your Help

How do I get my HTML button to delete the right list item from a SQLite database?

python html forms flask jinja2

I'm a beginner, so forgive any stupidity in advance. I'm using Flask (and by extension Jinja2) to create a simple web app -- one that basically lets you type a movie into a web form, which adds it ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.