Chloe Conlon
Chloe Conlon

Reputation: 7

Microsoft Access SQL Query - get rid of certain results

I want to find lecture rooms (their room code) which are of capacity 100 or more and are free (i.e. not booked) on Tuesday at 12pm.

In the room table, there are rCode & capacity. In the roomBooking table, there are dayReq, timeReq & roomCode.

The SQL Query I have so far is

SELECT rCode, capacity 
FROM room
INNER JOIN roomBooking
ON room.rCode = roomBooking.roomCode
WHERE capacity >= 100;

I want to get rid of the results that involve dayReq = "Tuesday" AND timeReq = "12:00:00" but I don't know the correct operator to do so.

Upvotes: 0

Views: 55

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269843

To get what you want, you are going to need a left join (or a subquery in the where clause). The idea is to find get all rooms along with any possible matches the room has to the booking criteria. You then want the rooms where there is no match:

SELECT room.rCode, room.capacity 
FROM room LEFT JOIN
     roomBooking
     ON room.rCode = roomBooking.roomCode AND
        dayReq = 'Tuesday' AND timeReq = '12:00:00'
WHERE room.capacity >= 100 AND
      roomBooking.roomCode IS NULL;

MS Access is really weird about many things, including joins with multiple keys. Just do this with a correlated subquery:

SELECT room.rCode, room.capacity 
FROM room
WHERE NOT EXISTS (SELECT 1
                  FROM roomBooking
                  WHERE room.rCode = roomBooking.roomCode AND
                        dayReq = "Tuesday" AND timeReq = "12:00:00"
                 ) AND
      room.capacity >= 100;

Upvotes: 1

Related Questions