Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Creating SQL statements in Access Databases

Creating SQL statements in Access Databases

Scheduled Pinned Locked Moved Database
databasehelp
15 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Richard Andrew x64R Richard Andrew x64

    You could try a subquery:

    SELECT HotelNo, RoomNo FROM Room WHERE RoomNo NOT IN (SELECT RoomNo FROM Reservation) ORDER BY HotelNo, RoomNo

    The difficult we do right away... ...the impossible takes slightly longer.

    U Offline
    U Offline
    User 11474011
    wrote on last edited by
    #5

    No data is retrieved back. It's blank.

    Richard Andrew x64R 1 Reply Last reply
    0
    • U User 11474011

      No data is retrieved back. It's blank.

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #6

      My SQL may be a bit rusty. But I don't know what your data looks like either. Let's see if anyone else has an idea.

      The difficult we do right away... ...the impossible takes slightly longer.

      U 1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        My SQL may be a bit rusty. But I don't know what your data looks like either. Let's see if anyone else has an idea.

        The difficult we do right away... ...the impossible takes slightly longer.

        U Offline
        U Offline
        User 11474011
        wrote on last edited by
        #7

        It worked. I was trying to incorporate something else into the statement.

        1 Reply Last reply
        0
        • U User 11474011

          I'm being asked to create a couple of SQL statements to retrieve information about a Luxury Hotel Database. List all the rooms in each hotel that have never been reserved in order by hotel number. I have the SQL statement retrieved the rooms what have been reserved but my problem is retrieving those that have not been reserved.

          SELECT RoomNo FROM RESERVATION WHERE RoomNo ORDER BY HotelNo

          List the guests by name and the number of times each has reserved a room at one of our hotels. Arrange the list in order from most-frequent to least-frequent guest. Here are the tables we are working on. HOTEL (HotelNo, HotelName, City) ROOM_TYPE (RoomType, Descr, RoomRate) ROOM (HotelNo, RoomNo, RoomType, PhoneExt) FK1: Foreign key HotelNo references HOTEL FK2: Foreign key RoomType references ROOM_TYPE GUEST (GuestNo, FirstName, LastName, Address, City, State, ZipCode) GUEST_PHONE (PhoneNumber, GuestNo, PhoneType) FK: Foreign key GuestNo references GUEST RESERVATION (ResNum, HotelNo, RoomNo, GuestNo, ArrivalDate, DepartureDate, NumPersons) FK1: Foreign Key (HotelNo, RoomNo) references ROOM FK2: Foreign Key GuestNo references GUEST

          U Offline
          U Offline
          User 11474011
          wrote on last edited by
          #8

          List the guests by name and the number of times each has reserved a room at one of our hotels. Arrange the list in order from most-frequent to least-frequent guest. My two related tables are Guest and Reservation.I can't have the FK GuestNo be one of the attributes. This is what I have so far.

          Select FirstName, LastName, GuestNo FROM GUEST INNER JOIN RESERVATION ON GUEST.firstname = RESERVATION.guestno

          I'm not sure about GuestNo.

          1 Reply Last reply
          0
          • U User 11474011

            I'm being asked to create a couple of SQL statements to retrieve information about a Luxury Hotel Database. List all the rooms in each hotel that have never been reserved in order by hotel number. I have the SQL statement retrieved the rooms what have been reserved but my problem is retrieving those that have not been reserved.

            SELECT RoomNo FROM RESERVATION WHERE RoomNo ORDER BY HotelNo

            List the guests by name and the number of times each has reserved a room at one of our hotels. Arrange the list in order from most-frequent to least-frequent guest. Here are the tables we are working on. HOTEL (HotelNo, HotelName, City) ROOM_TYPE (RoomType, Descr, RoomRate) ROOM (HotelNo, RoomNo, RoomType, PhoneExt) FK1: Foreign key HotelNo references HOTEL FK2: Foreign key RoomType references ROOM_TYPE GUEST (GuestNo, FirstName, LastName, Address, City, State, ZipCode) GUEST_PHONE (PhoneNumber, GuestNo, PhoneType) FK: Foreign key GuestNo references GUEST RESERVATION (ResNum, HotelNo, RoomNo, GuestNo, ArrivalDate, DepartureDate, NumPersons) FK1: Foreign Key (HotelNo, RoomNo) references ROOM FK2: Foreign Key GuestNo references GUEST

            U Offline
            U Offline
            User 11474011
            wrote on last edited by
            #9

            SELECT GUEST.FirstName, RESERVATION.GuestNo
            FROM GUEST
            INNER JOIN RESERVATION ON GUEST.GuestNo = RESERVATION.GuestNo
            ORDER BY GUEST.FirstName

            This is my SQL statement that retrieves all the names of the people who made reservations with the GuestNo appearing as well. I don't know where to incorporate the Count function.

            C 1 Reply Last reply
            0
            • U User 11474011

              SELECT GUEST.FirstName, RESERVATION.GuestNo
              FROM GUEST
              INNER JOIN RESERVATION ON GUEST.GuestNo = RESERVATION.GuestNo
              ORDER BY GUEST.FirstName

              This is my SQL statement that retrieves all the names of the people who made reservations with the GuestNo appearing as well. I don't know where to incorporate the Count function.

              C Offline
              C Offline
              Chris Quinn
              wrote on last edited by
              #10

              You need to COUNT(reservation.guestNo) while GROUPing BY Guest.GuestNo

              ========================================================= I'm an optoholic - my glass is always half full of vodka. =========================================================

              1 Reply Last reply
              0
              • U User 11474011

                I'm being asked to create a couple of SQL statements to retrieve information about a Luxury Hotel Database. List all the rooms in each hotel that have never been reserved in order by hotel number. I have the SQL statement retrieved the rooms what have been reserved but my problem is retrieving those that have not been reserved.

                SELECT RoomNo FROM RESERVATION WHERE RoomNo ORDER BY HotelNo

                List the guests by name and the number of times each has reserved a room at one of our hotels. Arrange the list in order from most-frequent to least-frequent guest. Here are the tables we are working on. HOTEL (HotelNo, HotelName, City) ROOM_TYPE (RoomType, Descr, RoomRate) ROOM (HotelNo, RoomNo, RoomType, PhoneExt) FK1: Foreign key HotelNo references HOTEL FK2: Foreign key RoomType references ROOM_TYPE GUEST (GuestNo, FirstName, LastName, Address, City, State, ZipCode) GUEST_PHONE (PhoneNumber, GuestNo, PhoneType) FK: Foreign key GuestNo references GUEST RESERVATION (ResNum, HotelNo, RoomNo, GuestNo, ArrivalDate, DepartureDate, NumPersons) FK1: Foreign Key (HotelNo, RoomNo) references ROOM FK2: Foreign Key GuestNo references GUEST

                A Offline
                A Offline
                Abdulnazark
                wrote on last edited by
                #11

                SELECT HotelNo, RoomNo from ROOM
                LEFT JOIN RESERVATION ON (ROOM.HotelNo = RESERVATION.HotelNo) AND (ROOM.RoomNo = RESERVATION.RoomNo)
                WHERE (((RESERVATION.HotelNo) Is Null) AND ((RESERVATION.RoomNo) Is Null))
                ORDER BY ROOM.HotelNo

                U 1 Reply Last reply
                0
                • U User 11474011

                  I'm being asked to create a couple of SQL statements to retrieve information about a Luxury Hotel Database. List all the rooms in each hotel that have never been reserved in order by hotel number. I have the SQL statement retrieved the rooms what have been reserved but my problem is retrieving those that have not been reserved.

                  SELECT RoomNo FROM RESERVATION WHERE RoomNo ORDER BY HotelNo

                  List the guests by name and the number of times each has reserved a room at one of our hotels. Arrange the list in order from most-frequent to least-frequent guest. Here are the tables we are working on. HOTEL (HotelNo, HotelName, City) ROOM_TYPE (RoomType, Descr, RoomRate) ROOM (HotelNo, RoomNo, RoomType, PhoneExt) FK1: Foreign key HotelNo references HOTEL FK2: Foreign key RoomType references ROOM_TYPE GUEST (GuestNo, FirstName, LastName, Address, City, State, ZipCode) GUEST_PHONE (PhoneNumber, GuestNo, PhoneType) FK: Foreign key GuestNo references GUEST RESERVATION (ResNum, HotelNo, RoomNo, GuestNo, ArrivalDate, DepartureDate, NumPersons) FK1: Foreign Key (HotelNo, RoomNo) references ROOM FK2: Foreign Key GuestNo references GUEST

                  U Offline
                  U Offline
                  User 11474011
                  wrote on last edited by
                  #12

                  This is second SQL statement that retrieves everyone's name and the GuestNo who have made a reservation at the hotel. I don't know where to add the count function without getting a Syntax Error.

                  SELECT GUEST.FirstName, COUNT (RESERVATION.GuestNo)
                  FROM GUEST INNER JOIN RESERVATION ON GUEST.GuestNo =RESERVATION.GuestNo
                  ORDER BY GUEST.FirstName
                  GROUP BY GUEST.GuestNo

                  This is giving me a Syntax error in my query expression GUEST.FirstName, GROUP BY GUEST.GuestNo.

                  U 1 Reply Last reply
                  0
                  • A Abdulnazark

                    SELECT HotelNo, RoomNo from ROOM
                    LEFT JOIN RESERVATION ON (ROOM.HotelNo = RESERVATION.HotelNo) AND (ROOM.RoomNo = RESERVATION.RoomNo)
                    WHERE (((RESERVATION.HotelNo) Is Null) AND ((RESERVATION.RoomNo) Is Null))
                    ORDER BY ROOM.HotelNo

                    U Offline
                    U Offline
                    User 11474011
                    wrote on last edited by
                    #13

                    Access is giving me that HotelNo could refer to more than one table in this SQL statement.

                    A 1 Reply Last reply
                    0
                    • U User 11474011

                      This is second SQL statement that retrieves everyone's name and the GuestNo who have made a reservation at the hotel. I don't know where to add the count function without getting a Syntax Error.

                      SELECT GUEST.FirstName, COUNT (RESERVATION.GuestNo)
                      FROM GUEST INNER JOIN RESERVATION ON GUEST.GuestNo =RESERVATION.GuestNo
                      ORDER BY GUEST.FirstName
                      GROUP BY GUEST.GuestNo

                      This is giving me a Syntax error in my query expression GUEST.FirstName, GROUP BY GUEST.GuestNo.

                      U Offline
                      U Offline
                      User 11474011
                      wrote on last edited by
                      #14
                      SELECT COUNT(RESERVATION.GuestNo), GUEST.FirstName, RESERVATION.GuestNo
                      
                      FROM GUEST INNER JOIN RESERVATION on GUEST.GuestNo = RESERVATION.GuestNo
                      
                      GROUP BY GUEST.FirstName
                      

                      I've tried this combination as well but Access tells me, You tried to execute a query that does not include the specified expression 'GuestNo' as part of an aggregate functio

                      1 Reply Last reply
                      0
                      • U User 11474011

                        Access is giving me that HotelNo could refer to more than one table in this SQL statement.

                        A Offline
                        A Offline
                        Abdulnazark
                        wrote on last edited by
                        #15

                        try now I forgot to add table name

                        SELECT ROOM.HotelNo, ROOM.RoomNo from ROOM
                        LEFT JOIN RESERVATION ON (ROOM.HotelNo = RESERVATION.HotelNo) AND (ROOM.RoomNo = RESERVATION.RoomNo)
                        WHERE (((RESERVATION.HotelNo) Is Null) AND ((RESERVATION.RoomNo) Is Null))
                        ORDER BY ROOM.HotelNo

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups