SQL Query - Find The Highest Number

237 views Asked by At

Could someone please help me? I need to find this query, but I cannot get them to display as one. I know I wrote it wrong but not sure how to correct it. I need it to only display the customer who booked the longest period.

SELECT CUSTOMERS.CUSTOMER_ID, CUSTOMERS.CUSTOMER_NAME, 
       ACCOMMODATION_BOOKINGS.CHECKIN_DATE, ACCOMMODATION_BOOKINGS.NIGHTS
FROM   CUSTOMERS, ACCOMMODATION_BOOKINGS
WHERE  CUSTOMERS.CUSTOMER_ID = ACCOMMODATION_BOOKINGS.CUSTOMER_ID
SELECT MAX(Nights) AS NIGHTS
FROM ACCOMMODATION_BOOKINGS, CUSTOMERS;
1

There are 1 answers

4
aRvi On

This will give you the desired result:

SELECT CUSTOMERS.CUSTOMER_ID, CUSTOMERS.CUSTOMER_NAME, 
       ACCOMMODATION_BOOKINGS.CHECKIN_DATE, ACCOMMODATION_BOOKINGS.NIGHTS
FROM CUSTOMERS
JOIN ACCOMMODATION_BOOKINGS
ON CUSTOMERS.CUSTOMER_ID = ACCOMMODATION_BOOKINGS.CUSTOMER_ID
WHERE ACCOMMODATION_BOOKINGS.NIGHTS = (
    SELECT MAX(NIGHTS) AS NIGHTS
    FROM ACCOMMODATION_BOOKINGS
);