Forum

Write a query to di...
 
Share:
Notifications
Clear all

Write a query to display the guest id and guest name who has charged the highest amount in the resort. sort the output on the guest id.


Posts: 17
Topic starter
(@rahul)
Active Member
Joined: 4 years ago

Write a query to display the guest id and guest name who has charged the highest amount in the resort. sort the output on the guest id.

3 Replies
Posts: 18
Guest
(@Deepak Raj)
Active Member
Joined: 4 years ago

Hello,

Try the below query and let me know.

 

Select GuestID, name

from Guest

Where

Guest.GuestID IN(Select GuestID from Booking where totalcharge=select max(totalcharge)

from Booking Group by GuestId)

Order By guestID desc

 

Reply

Self Learning Video Tutorials - Software Course

Posts: 81
Admin
(@sql-admin)
Estimable Member
Joined: 4 years ago

Display guest details who paid total charges rs.50000 and above. write a query to fetch guest id, guest name, and the sum of total charges. give alias name to total charges as total paid. sort the result by guest id.

Select
GuestID,
GuestName,
SUM(Total_Charges) as TotalPaid
From
Guest
Group By GuestID, GuestName
Having SUM(Total_Charges)>= 50000
Order GuestID DESC

Reply
Posts: 81
Admin
(@sql-admin)
Estimable Member
Joined: 4 years ago

SQL Query to Display Guest ID and Guest Name Who Charged the Highest Amount

To display the guest_id and guest_name of the guest who has charged the highest amount in the resort and sort the output by guest_id, you can use the following SQL query:

sql
 
-- Step 1: Identify the guest(s) with the highest amount charged
SELECT guest_id, guest_name, amount_charged
FROM guests
WHERE amount_charged = (SELECT MAX(amount_charged) FROM guests)
ORDER BY guest_id;

Explanation of the Query

  1. SELECT guest_id, guest_name, amount_charged:
    • Retrieves the guest_id, guest_name, and amount_charged from the guests table.
  2. WHERE amount_charged = (SELECT MAX(amount_charged) FROM guests):
    • Finds the guest(s) with the highest amount_charged using a subquery that returns the maximum value of amount_charged from the guests table.
  3. ORDER BY guest_id:
    • Sorts the output by guest_id in ascending order.

Example Output

Assuming the guests table has the following data:

guest_id guest_name amount_charged
1 John Doe 1200
2 Jane Smith 1500
3 Alex Johnson 1500
4 Chris Williams 900

The output of the query will be:

guest_id guest_name amount_charged
2 Jane Smith 1500
3 Alex Johnson 1500
Reply

Self Learning Video Tutorials - Software Course

Leave a reply

Author Name

Author Email

Title *

Preview 0 Revisions Saved
Share: