Simple SQL query

  • MOTEL (motelID, motelName, town)

    ROOM (roomIDmotelNo, type, cost)

    BOOKING (motelNoguestIDdateFrom, dateTo, roomID)

    GUEST (guestID, guestName, guestAddress)

     

    For the above SQL database relation, I have two questions that I'm trying to answer:

    Select all guestIDs that have the person's first name as 'John', and have a booking with an undefined dateTo. No explicit or implicit joins are allowed.

    So far, I've done it using explicit joins to see how I might be able to avoid using joins. I don't know how to not use joins for this query, so that's what I'm asking for help on firstly.

    My code so far:

    SELECT GUEST.GUESTID FROM GUESTINNER JOIN BOOKING ON BOOKING.GUESTID=GUEST.GUESTID WHERE BOOKING.DATETO IS NULLAND GUEST.GUESTNAME='John%';

    Select the cost and roomID of all rooms at the motel named 'Jollife'. If the room is occupied (according to the database), state the guestName of the room occupier.

    All dates are in the format yyyymmdd. My code so far:

    SELECT COST.PRICE, ROOM.ROOMID FROM ROOMINNER JOIN MOTEL ON MOTEL.MOTELID=ROOM.MOTELIDFULL OUTER JOIN BOOKING ON ROOM.MOTELID=BOOKING.MOTELID

    I've attempted to start this question by doing an inner join of 'ROOM' and 'MOTEL' based on the motelID, and then a full outer join with 'BOOKING' so that the 'dateTo' can be checked. I'm not sure where to go from here. This is in postgresql.

  • Thanks for posting your issue and hopefully someone will answer soon.

    This is an automated bump to increase visibility of your question.

  • For (1):

    SELECT GUEST.GUESTID 
    FROM GUEST
    WHERE GUEST.GUESTNAME='John %' /*space added because don't want name 'Johnathan' or 'Johnny'*/ AND
    EXISTS(SELECT 1 FROM BOOKING WHERE BOOKING.GUESTID=GUEST.GUESTID AND BOOKING.DATETO IS NULL)

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

  • Just now noticed the Postgre sql qualification.  I answered for SQL Server; code may or may not work on Postgre.

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply