Results 1 to 17 of 17

Thread: help in SQL simple queries ?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Location
    at my home
    Posts
    10

    help in SQL simple queries ?

    Hello All..

    I have a database which include those tables

    Hotel(hotelno,hotelname,city)
    guest(guestno,guestname,guestadress)
    room(roomno,hotelno,type,price)
    booking(hotelno,guestno,datefrom,dateto,roomno)

    pks are underlined..

    and i have to find those queries

    1- find those guests who booked one room or more during this current month ?

    2- find out the rooms no. with their hotels name which are empty (not booked) today (current day) ?

    can anyone help me solving them ?

  2. #2
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    Re: help in SQL simple queries ?

    Please give us what have you tried yet ? So that we can tell you what is the issue ?
    thanks
    amrita

  3. #3
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: help in SQL simple queries ?

    Just to be clear, we don't do people's homework for them and this looks suspiciously like a homework assignment. If you want to get an answer you'll need to have a go at it yourslef and then come back to us with specific problems that you were unable to resolve.

    If you need a hand getting started on querying then check the faqs at the head of this forum. There are some excellent ones on SQL which should give you all you need. W3Schools also offer an excellent set of explanations of the various SQL commands.

    If this is some commercial work that you're hoping to get done then there is a section where you post a work request but people will want paying for the work they do.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Location
    at my home
    Posts
    10

    Re: help in SQL simple queries ?

    Quote Originally Posted by amrita View Post
    Please give us what have you tried yet ? So that we can tell you what is the issue ?
    well that was my answer but it didnt work

    1- select distinct count(g.guestno) , b.datefrom
    from guest g ,booking b
    where g.guestno=b.guestno
    and b.datefrom=( select ..........???

    im new to the sql and i really dont know how to deal with the date queries
    im sure that i can get the soluation using the subquery but i couldnt apply it ..

    2- select distinct hotelname ,b.roomno
    from hotel , booking b ,room r
    where r.roomno=b.roomno
    and b.datefrom<>sysdate

    the result wasnot as expected it gives me all the rooms no. in each hotel


    any advice please ?

  5. #5
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: help in SQL simple queries ?

    What is the database?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Location
    at my home
    Posts
    10

    Re: help in SQL simple queries ?

    Quote Originally Posted by FunkyDexter View Post
    Just to be clear, we don't do people's homework for them and this looks suspiciously like a homework assignment. If you want to get an answer you'll need to have a go at it yourslef and then come back to us with specific problems that you were unable to resolve.

    If you need a hand getting started on querying then check the faqs at the head of this forum. There are some excellent ones on SQL which should give you all you need. W3Schools also offer an excellent set of explanations of the various SQL commands.

    If this is some commercial work that you're hoping to get done then there is a section where you post a work request but people will want paying for the work they do.

    Thank you Sir, for clarification but there is a misunderstanding of my question
    it Is not obligatory home work or business at all, but I try to study the material as selfstudy and faced with this exercise, I could not find out the right solution
    It was my fault from the beginning that i did not put my attempts in the solution and now I wrote put it down in my privuos post..
    I repeat my thanks to you with sincere appreciation and respect for all tips and advices you offer here ..

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Location
    at my home
    Posts
    10

    Re: help in SQL simple queries ?

    Quote Originally Posted by GaryMazzone View Post
    What is the database?

    dunno what u mean in your question
    but if u mean my database
    it is (myproject) which include all the above tables
    sorry if i didnt get what u mean

  8. #8
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: help in SQL simple queries ?

    No... he means SQL Server, Access, MySQL, Oracle etc. Which one?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  9. #9
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: help in SQL simple queries ?

    It was my fault...
    That's OK. I didn't mean to tell you off. It's just that you'll get a much better response round here if people can see you've had a go yourself.

    For question 1, what does 'in the current month' actually mean? Does it mean the from date is in the current month, or the to date, or both, or either? You'll need to clarify that before you can design a query to resolve it. Looking at your query so far it looks like you're basing it on the from date only, which means you'll want something like:-
    Code:
    Select *
    From Booking
    Where Month(DateFrom) = Month(GetDate())
    The syntax might change a bit depending on which database system you're using. The point is you check that the month of the from date is the same as the month of the current date. If you want to base it on both dates in some way then it's going to get more complicated.

    For question 2 there's a couple of ways you can do it but I personally like the NOT EXISTS clause because it's the easiest to understand.
    Code:
    Select * From Room
    Where NOT EXISTS
      (Select * From Booking
      Where Booking.RoomID = Room.RoomID
      And Booking.HotelID = Booking.HotelID
      And FromDate <= GetDate()
      And ToDate >= GetDate())

    Hope that helps as a starter. Try those queries out and, if they don't give you what you want, let us know what the problem is and we'll be able to help further.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  10. #10
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: help in SQL simple queries ?

    This was my go at it in SQL Server:

    1:
    sql Code:
    1. SELECT
    2.     GuestName,Count(Booking.*)
    3. FROM
    4.     Guest
    5. INNER JOIN Booking
    6.     ON Booking.Guestno = Guest.GuestNo
    7. WHERE
    8.     (MONTH(Booking.DateFrom) = MONTH(GETDATE()) AND YEAR(Booking.DateFrom) = YEAR(GETDATE())
    9.     OR
    10.     (MONTH(Booking.DateTo) = MONTH(GETDATE()) AND YEAR(Booking.DateTo) = YEAR(GETDATE())


    2:
    sql Code:
    1. SELECT
    2.     Hotel.HotelName,
    3.     Hotel.City,
    4.     Room.RoomNo,
    5.     Room.Type
    6. FROM HOTEL
    7. INNER JOIN ROOM
    8.     ON HOTEL.HotelNo = Room.HotelNo
    9. LEFT OUTER JOIN BOOKING
    10.     ON Room.RoomNo = Booking.RoomNo
    11. WHERE
    12.     GETDATE() BETWEEN Booking.DateFrom AND Booking.DateTo
    13.     AND Booking.RoomNo IS NULL
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  11. #11

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Location
    at my home
    Posts
    10

    Re: help in SQL simple queries ?

    Quote Originally Posted by Pradeep1210 View Post
    No... he means SQL Server, Access, MySQL, Oracle etc. Which one?
    well , im using Oracle ..

  12. #12

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Location
    at my home
    Posts
    10

    Re: help in SQL simple queries ?

    Quote Originally Posted by FunkyDexter View Post
    That's OK. I didn't mean to tell you off. It's just that you'll get a much better response round here if people can see you've had a go yourself.

    For question 1, what does 'in the current month' actually mean? Does it mean the from date is in the current month, or the to date, or both, or either? You'll need to clarify that before you can design a query to resolve it. Looking at your query so far it looks like you're basing it on the from date only, which means you'll want something like:-
    Code:
    Select *
    From Booking
    Where Month(DateFrom) = Month(GetDate())
    The syntax might change a bit depending on which database system you're using. The point is you check that the month of the from date is the same as the month of the current date. If you want to base it on both dates in some way then it's going to get more complicated.

    For question 2 there's a couple of ways you can do it but I personally like the NOT EXISTS clause because it's the easiest to understand.
    Code:
    Select * From Room
    Where NOT EXISTS
      (Select * From Booking
      Where Booking.RoomID = Room.RoomID
      And Booking.HotelID = Booking.HotelID
      And FromDate <= GetDate()
      And ToDate >= GetDate())

    Hope that helps as a starter. Try those queries out and, if they don't give you what you want, let us know what the problem is and we'll be able to help further.

    I do appreciate ur help
    and for question 1 .. i think it means
    during the current month means that date from
    is included..
    logically your select statment is more close to the correct answer
    but i dont know if i can use the function(Getdate()) with the Oracle

    ..as well as in the second query
    it sounds really great using the NOTEXIST clause

    ..thank u again .. i will rebuild mt answer i will be back if i got new problem
    and i wish that isnot going to happen..

  13. #13

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Location
    at my home
    Posts
    10

    Re: help in SQL simple queries ?

    Quote Originally Posted by GaryMazzone View Post
    This was my go at it in SQL Server:

    1:
    sql Code:
    1. SELECT
    2.     GuestName,Count(Booking.*)
    3. FROM
    4.     Guest
    5. INNER JOIN Booking
    6.     ON Booking.Guestno = Guest.GuestNo
    7. WHERE
    8.     (MONTH(Booking.DateFrom) = MONTH(GETDATE()) AND YEAR(Booking.DateFrom) = YEAR(GETDATE())
    9.     OR
    10.     (MONTH(Booking.DateTo) = MONTH(GETDATE()) AND YEAR(Booking.DateTo) = YEAR(GETDATE())


    2:
    sql Code:
    1. SELECT
    2.     Hotel.HotelName,
    3.     Hotel.City,
    4.     Room.RoomNo,
    5.     Room.Type
    6. FROM HOTEL
    7. INNER JOIN ROOM
    8.     ON HOTEL.HotelNo = Room.HotelNo
    9. LEFT OUTER JOIN BOOKING
    10.     ON Room.RoomNo = Booking.RoomNo
    11. WHERE
    12.     GETDATE() BETWEEN Booking.DateFrom AND Booking.DateTo
    13.     AND Booking.RoomNo IS NULL

    really im speachless about that
    even i know no thing about sql server but obviously ..it sounds so close to the oracle sql
    ..one day i will use ur code
    and it may be my start with the sql server
    thank u again

  14. #14
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: help in SQL simple queries ?

    In oracle you would use SYSDATE instead of GETDATE(). Try that.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  15. #15
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: help in SQL simple queries ?

    If you're using my queries as a basis I'd recommend you do a quick double check on Gary's queries because he picked up something I didn't. Checking the month matches isn't enough, you need to check that the year also matches.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  16. #16
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: help in SQL simple queries ?

    I just noticed the I also missed the group by in the first query. There should be a GROUP BY GuestName after the WHERE clause.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  17. #17
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: help in SQL simple queries ?

    meh, we'll call it a team effort
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width