Trying to query a database of a hotel to return the id of the unoccupied rooms on a specific date on SQLZOO










0















The question I'm trying to answer wants me to query the DB for:
List the rooms that are free on the day 25th Nov 2016.



This is what I have so far,



SELECT room_no FROM booking b
JOIN room ON b.room_no = room.id
WHERE booking_date = '2016-11-25' AND occupants IS NULL


I've modified it to:



SELECT room_no FROM booking b
JOIN room ON b.room_no = room.id
JOIN guest ON guest_id = guest.id
WHERE booking_date = '2016-11-25' AND guest_id IS NULL


And I'm still getting the table header with no values, when I should be getting room ids 207, 210, 304.



I'm sure I'm missing something but I just can't see what, any help is much appreciated.



https://sqlzoo.net/wiki/Guest_House_Assessment_Hard



Thanks :)



UML Diagram










share|improve this question



















  • 2





    occupants IS NULL!

    – jarlh
    Nov 12 '18 at 12:52











  • Please tag with the database you are using.

    – Gordon Linoff
    Nov 12 '18 at 12:52











  • "Answers to questions tagged with SQL should use ISO/IEC standard SQL" - Should be doable for this one.

    – GolezTrol
    Nov 12 '18 at 12:53











  • SQLzoo has a strange naming convention. Use plural for table names (a table stores data about many items.) Use singular for column names.

    – jarlh
    Nov 12 '18 at 13:12















0















The question I'm trying to answer wants me to query the DB for:
List the rooms that are free on the day 25th Nov 2016.



This is what I have so far,



SELECT room_no FROM booking b
JOIN room ON b.room_no = room.id
WHERE booking_date = '2016-11-25' AND occupants IS NULL


I've modified it to:



SELECT room_no FROM booking b
JOIN room ON b.room_no = room.id
JOIN guest ON guest_id = guest.id
WHERE booking_date = '2016-11-25' AND guest_id IS NULL


And I'm still getting the table header with no values, when I should be getting room ids 207, 210, 304.



I'm sure I'm missing something but I just can't see what, any help is much appreciated.



https://sqlzoo.net/wiki/Guest_House_Assessment_Hard



Thanks :)



UML Diagram










share|improve this question



















  • 2





    occupants IS NULL!

    – jarlh
    Nov 12 '18 at 12:52











  • Please tag with the database you are using.

    – Gordon Linoff
    Nov 12 '18 at 12:52











  • "Answers to questions tagged with SQL should use ISO/IEC standard SQL" - Should be doable for this one.

    – GolezTrol
    Nov 12 '18 at 12:53











  • SQLzoo has a strange naming convention. Use plural for table names (a table stores data about many items.) Use singular for column names.

    – jarlh
    Nov 12 '18 at 13:12













0












0








0








The question I'm trying to answer wants me to query the DB for:
List the rooms that are free on the day 25th Nov 2016.



This is what I have so far,



SELECT room_no FROM booking b
JOIN room ON b.room_no = room.id
WHERE booking_date = '2016-11-25' AND occupants IS NULL


I've modified it to:



SELECT room_no FROM booking b
JOIN room ON b.room_no = room.id
JOIN guest ON guest_id = guest.id
WHERE booking_date = '2016-11-25' AND guest_id IS NULL


And I'm still getting the table header with no values, when I should be getting room ids 207, 210, 304.



I'm sure I'm missing something but I just can't see what, any help is much appreciated.



https://sqlzoo.net/wiki/Guest_House_Assessment_Hard



Thanks :)



UML Diagram










share|improve this question
















The question I'm trying to answer wants me to query the DB for:
List the rooms that are free on the day 25th Nov 2016.



This is what I have so far,



SELECT room_no FROM booking b
JOIN room ON b.room_no = room.id
WHERE booking_date = '2016-11-25' AND occupants IS NULL


I've modified it to:



SELECT room_no FROM booking b
JOIN room ON b.room_no = room.id
JOIN guest ON guest_id = guest.id
WHERE booking_date = '2016-11-25' AND guest_id IS NULL


And I'm still getting the table header with no values, when I should be getting room ids 207, 210, 304.



I'm sure I'm missing something but I just can't see what, any help is much appreciated.



https://sqlzoo.net/wiki/Guest_House_Assessment_Hard



Thanks :)



UML Diagram







sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 12:57







CB17

















asked Nov 12 '18 at 12:51









CB17CB17

225




225







  • 2





    occupants IS NULL!

    – jarlh
    Nov 12 '18 at 12:52











  • Please tag with the database you are using.

    – Gordon Linoff
    Nov 12 '18 at 12:52











  • "Answers to questions tagged with SQL should use ISO/IEC standard SQL" - Should be doable for this one.

    – GolezTrol
    Nov 12 '18 at 12:53











  • SQLzoo has a strange naming convention. Use plural for table names (a table stores data about many items.) Use singular for column names.

    – jarlh
    Nov 12 '18 at 13:12












  • 2





    occupants IS NULL!

    – jarlh
    Nov 12 '18 at 12:52











  • Please tag with the database you are using.

    – Gordon Linoff
    Nov 12 '18 at 12:52











  • "Answers to questions tagged with SQL should use ISO/IEC standard SQL" - Should be doable for this one.

    – GolezTrol
    Nov 12 '18 at 12:53











  • SQLzoo has a strange naming convention. Use plural for table names (a table stores data about many items.) Use singular for column names.

    – jarlh
    Nov 12 '18 at 13:12







2




2





occupants IS NULL!

– jarlh
Nov 12 '18 at 12:52





occupants IS NULL!

– jarlh
Nov 12 '18 at 12:52













Please tag with the database you are using.

– Gordon Linoff
Nov 12 '18 at 12:52





Please tag with the database you are using.

– Gordon Linoff
Nov 12 '18 at 12:52













"Answers to questions tagged with SQL should use ISO/IEC standard SQL" - Should be doable for this one.

– GolezTrol
Nov 12 '18 at 12:53





"Answers to questions tagged with SQL should use ISO/IEC standard SQL" - Should be doable for this one.

– GolezTrol
Nov 12 '18 at 12:53













SQLzoo has a strange naming convention. Use plural for table names (a table stores data about many items.) Use singular for column names.

– jarlh
Nov 12 '18 at 13:12





SQLzoo has a strange naming convention. Use plural for table names (a table stores data about many items.) Use singular for column names.

– jarlh
Nov 12 '18 at 13:12












2 Answers
2






active

oldest

votes


















1














Although your SQL query is not correct (you can check if occupants IS NULL but not compare them with the = operator), you are doing an INNER JOIN, which will only return rows matching in both tables.



Rooms without a booking for that day should not have a booking associated because nobody made a booking. You have to get the rooms that do not have a booking starting before Nov 25th or that date, and ending later than that.



You can get what you want with a subquery:



SELECT id FROM room 
WHERE id NOT IN
(SELECT room_no FROM booking
WHERE (DATE_ADD(booking_date, INTERVAL nights DAY) > '2016-11-25'
AND booking_date <= '2016-11-25'))


I assumed you are using MariaDB (therefore I used DATE_ADD function) as that is the DBMS that SQLZOO appears to be based on.






share|improve this answer

























  • Thank you so much

    – CB17
    Nov 12 '18 at 13:13


















0














You are not taking the number of nights into account, so the logic is more like this:



SELECT room_no
FROM room r LEFT JOIN
booking b
ON b.room_no = room.id AND
'2016-11-25' < booking_date + interval nights day AND
'2016-11-25' >= booking_date
WHERE b.room_no IS NULL;





share|improve this answer

























  • That is not correct. Although it is syntactically incorrect (lack of an AND and IS NULL without the space) and uses wrong field names, you should be adding nights (not subtracting) and using < on your first expression and >= on the second one.

    – Antonio Alvarez
    Nov 12 '18 at 13:27











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53262579%2ftrying-to-query-a-database-of-a-hotel-to-return-the-id-of-the-unoccupied-rooms-o%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Although your SQL query is not correct (you can check if occupants IS NULL but not compare them with the = operator), you are doing an INNER JOIN, which will only return rows matching in both tables.



Rooms without a booking for that day should not have a booking associated because nobody made a booking. You have to get the rooms that do not have a booking starting before Nov 25th or that date, and ending later than that.



You can get what you want with a subquery:



SELECT id FROM room 
WHERE id NOT IN
(SELECT room_no FROM booking
WHERE (DATE_ADD(booking_date, INTERVAL nights DAY) > '2016-11-25'
AND booking_date <= '2016-11-25'))


I assumed you are using MariaDB (therefore I used DATE_ADD function) as that is the DBMS that SQLZOO appears to be based on.






share|improve this answer

























  • Thank you so much

    – CB17
    Nov 12 '18 at 13:13















1














Although your SQL query is not correct (you can check if occupants IS NULL but not compare them with the = operator), you are doing an INNER JOIN, which will only return rows matching in both tables.



Rooms without a booking for that day should not have a booking associated because nobody made a booking. You have to get the rooms that do not have a booking starting before Nov 25th or that date, and ending later than that.



You can get what you want with a subquery:



SELECT id FROM room 
WHERE id NOT IN
(SELECT room_no FROM booking
WHERE (DATE_ADD(booking_date, INTERVAL nights DAY) > '2016-11-25'
AND booking_date <= '2016-11-25'))


I assumed you are using MariaDB (therefore I used DATE_ADD function) as that is the DBMS that SQLZOO appears to be based on.






share|improve this answer

























  • Thank you so much

    – CB17
    Nov 12 '18 at 13:13













1












1








1







Although your SQL query is not correct (you can check if occupants IS NULL but not compare them with the = operator), you are doing an INNER JOIN, which will only return rows matching in both tables.



Rooms without a booking for that day should not have a booking associated because nobody made a booking. You have to get the rooms that do not have a booking starting before Nov 25th or that date, and ending later than that.



You can get what you want with a subquery:



SELECT id FROM room 
WHERE id NOT IN
(SELECT room_no FROM booking
WHERE (DATE_ADD(booking_date, INTERVAL nights DAY) > '2016-11-25'
AND booking_date <= '2016-11-25'))


I assumed you are using MariaDB (therefore I used DATE_ADD function) as that is the DBMS that SQLZOO appears to be based on.






share|improve this answer















Although your SQL query is not correct (you can check if occupants IS NULL but not compare them with the = operator), you are doing an INNER JOIN, which will only return rows matching in both tables.



Rooms without a booking for that day should not have a booking associated because nobody made a booking. You have to get the rooms that do not have a booking starting before Nov 25th or that date, and ending later than that.



You can get what you want with a subquery:



SELECT id FROM room 
WHERE id NOT IN
(SELECT room_no FROM booking
WHERE (DATE_ADD(booking_date, INTERVAL nights DAY) > '2016-11-25'
AND booking_date <= '2016-11-25'))


I assumed you are using MariaDB (therefore I used DATE_ADD function) as that is the DBMS that SQLZOO appears to be based on.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 '18 at 13:11

























answered Nov 12 '18 at 12:56









Antonio AlvarezAntonio Alvarez

147115




147115












  • Thank you so much

    – CB17
    Nov 12 '18 at 13:13

















  • Thank you so much

    – CB17
    Nov 12 '18 at 13:13
















Thank you so much

– CB17
Nov 12 '18 at 13:13





Thank you so much

– CB17
Nov 12 '18 at 13:13













0














You are not taking the number of nights into account, so the logic is more like this:



SELECT room_no
FROM room r LEFT JOIN
booking b
ON b.room_no = room.id AND
'2016-11-25' < booking_date + interval nights day AND
'2016-11-25' >= booking_date
WHERE b.room_no IS NULL;





share|improve this answer

























  • That is not correct. Although it is syntactically incorrect (lack of an AND and IS NULL without the space) and uses wrong field names, you should be adding nights (not subtracting) and using < on your first expression and >= on the second one.

    – Antonio Alvarez
    Nov 12 '18 at 13:27
















0














You are not taking the number of nights into account, so the logic is more like this:



SELECT room_no
FROM room r LEFT JOIN
booking b
ON b.room_no = room.id AND
'2016-11-25' < booking_date + interval nights day AND
'2016-11-25' >= booking_date
WHERE b.room_no IS NULL;





share|improve this answer

























  • That is not correct. Although it is syntactically incorrect (lack of an AND and IS NULL without the space) and uses wrong field names, you should be adding nights (not subtracting) and using < on your first expression and >= on the second one.

    – Antonio Alvarez
    Nov 12 '18 at 13:27














0












0








0







You are not taking the number of nights into account, so the logic is more like this:



SELECT room_no
FROM room r LEFT JOIN
booking b
ON b.room_no = room.id AND
'2016-11-25' < booking_date + interval nights day AND
'2016-11-25' >= booking_date
WHERE b.room_no IS NULL;





share|improve this answer















You are not taking the number of nights into account, so the logic is more like this:



SELECT room_no
FROM room r LEFT JOIN
booking b
ON b.room_no = room.id AND
'2016-11-25' < booking_date + interval nights day AND
'2016-11-25' >= booking_date
WHERE b.room_no IS NULL;






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 '18 at 13:30

























answered Nov 12 '18 at 12:55









Gordon LinoffGordon Linoff

763k35296400




763k35296400












  • That is not correct. Although it is syntactically incorrect (lack of an AND and IS NULL without the space) and uses wrong field names, you should be adding nights (not subtracting) and using < on your first expression and >= on the second one.

    – Antonio Alvarez
    Nov 12 '18 at 13:27


















  • That is not correct. Although it is syntactically incorrect (lack of an AND and IS NULL without the space) and uses wrong field names, you should be adding nights (not subtracting) and using < on your first expression and >= on the second one.

    – Antonio Alvarez
    Nov 12 '18 at 13:27

















That is not correct. Although it is syntactically incorrect (lack of an AND and IS NULL without the space) and uses wrong field names, you should be adding nights (not subtracting) and using < on your first expression and >= on the second one.

– Antonio Alvarez
Nov 12 '18 at 13:27






That is not correct. Although it is syntactically incorrect (lack of an AND and IS NULL without the space) and uses wrong field names, you should be adding nights (not subtracting) and using < on your first expression and >= on the second one.

– Antonio Alvarez
Nov 12 '18 at 13:27


















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53262579%2ftrying-to-query-a-database-of-a-hotel-to-return-the-id-of-the-unoccupied-rooms-o%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20