Traditional Culture Encyclopedia - Hotel reservation - Ask a difficult sql query and try your database writing level.

Ask a difficult sql query and try your database writing level.

-200 to 300 rooms

Select the maximum (hotel price), minimum (hotel price), count (room price) and * from the hotel prices.

Inner join Hotel on hotle.hotelname = hotelprice.hotelname.

Inner join Hotel on Hotelpic. HotelName = HotelPrice. HotelName

The house price is between 200 and 300.

Group by hotel. Hotel name

For the record, the above sentence is just wrapped for better looking.

Using the principle of grouping query, we can group hotels by name, so that we can classify hotels by name and find out the highest price and the lowest price in each hotel between 200 and 300.

Not tested, but the principle must be correct.

Maximum value, minimum value, count, join query and grouping query are all used.

Reasons for using grouped queries:

Maximum \ minimum \ count, etc. Is an aggregate function, which should be displayed together with other information and classified by hotel. Only group queries can be used.

But personally, the design of three tables in this database is problematic.

First of all, these three tables can only be connected by the hotel name. The id should be the identity column and the primary key, so that the three tables cannot establish the relationship between the primary key and the foreign key.

Secondly, if there is no master-foreign key relationship, how to ensure that a picture information of a hotel is added, and the hotel information also has master table data, so that this data will be redundant in the future.

In addition, the hotel prices, pictures and other information you want to count should use non-empty constraints. Otherwise, the statistics will go wrong.

Finally. You don't need to write SQL in one sentence, you can write it in multiple sentences, or you can do it through views, temporary tables, etc. Multi-statement SQL can be called by stored procedures. And the efficiency is higher than that of single sentence direct calling. (No multiple queries or temporary tables)