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.

4 Posts
3 Users
0 Reactions
3,711 Views
Posts: 17
Topic starter
(@rahul)
Active Member
Joined: 6 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: 17
(@Deepak Raj)
Joined: 6 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
Posts: 134
Admin
(@sql-admin)
Reputable Member
Joined: 6 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: 134
Admin
(@sql-admin)
Reputable Member
Joined: 6 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_idguest_nameamount_charged
1John Doe1200
2Jane Smith1500
3Alex Johnson1500
4Chris Williams900

The output of the query will be:

guest_idguest_nameamount_charged
2Jane Smith1500
3Alex Johnson1500

Reply

Leave a reply

Author Name

Author Email

Title *

Preview 0 Revisions Saved
Share: