Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8102871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:21:29+00:00 2026-06-05T23:21:29+00:00

I have this enormous database : # # Dropping tables # DROP TABLE IF

  • 0

I have this enormous database :

#
# Dropping tables
#

DROP TABLE IF EXISTS hotels;
DROP TABLE IF EXISTS guests;
DROP TABLE IF EXISTS rooms;
DROP TABLE IF EXISTS reservation;

#
# Table structure for tables hotels, guests, rooms and reservation
#

CREATE TABLE hotels(
hotel_code int(4) unsigned NOT NULL default '0',
hotel_name char(20) default NULL,
city char(20) default NULL,
address char(20) default NULL,
hotel_rank int(1) unsigned NOT NULL default '0',
phone char(10) default NULL,
number_of_rooms int(4) unsigned NOT NULL default '0',
PRIMARY KEY  (hotel_code));

CREATE TABLE guests(
id char(10) default NULL,
name char(20) default NULL,
city char(20) default NULL,
address char(20) default NULL,
state char(20) default NULL,
PRIMARY KEY  (id));

CREATE TABLE rooms(
hotel_code int(4) unsigned NOT NULL default '0',
room_number int(4) unsigned NOT NULL default '0',
type char(20) default NULL,
beds int(4) unsigned NOT NULL default '0',
price int(4) unsigned NOT NULL default '0',
PRIMARY KEY  (hotel_code, room_number));

CREATE TABLE reservation(
hotel_code int(4) unsigned NOT NULL default '0',
room_number int(4) unsigned NOT NULL default '0',
from_date char(20) default NULL,
to_date char(20) default NULL,
payment_method char(20) default NULL,
id char(10) default NULL,
PRIMARY KEY  (hotel_code, room_number, from_date));

#
# Dumping data for tables hotels, guests, rooms and reservation
#

INSERT INTO hotels VALUES
(1, 'Meridian', 'Tel Aviv', 'Shinkin 24', 3, '03-6527384', 230),
(2, 'Meridian', 'Tveria', 'Kineret 22', 5, '04-7369944', 320),
(3, 'King David', 'Jerusalem', 'Kineret 22', 5, '02-7435334', 200),
(4, 'Hilton', 'Jerusalem', 'Kineret 22', 4, '02-4566324', 180),
(5, 'Herods', 'Eilat', 'Dolphin 14', 4, '09-7333245', 190),
(6, 'Hof Yam', 'Haifa', 'Kasam 39', 5, '04-7344545', 150),
(7, 'Ha Tsuk', 'Haifa', 'Lohem 17', 3, '04-7345678', 110),
(8, 'Ha Nesiha', 'Eilat', 'Suf 4', 5, '09-6543543', 170),
(9, 'King David', 'Tveria', 'Kineret 55', 5, '04-7445544', 270);

INSERT INTO guests VALUES
('00000001-0', 'Adam', 'Gan Eden', 'Mango 3', 'Israel'),
('00000002-0', 'Eve', 'Gan Eden', 'Avucado 12', 'Israel'),
('04352244-4', 'Dani Mor', 'Tel Aviv', 'Magen 3', 'Israel'),
('06112343-6', 'Amiad Rozenberg', 'Petah Tikva', 'Zahal 14', 'Israel'),
('12345678-0', 'Ehud Olmert', 'Jerusalem', 'Money 99', 'Israel'),
('87654321-0', 'Moris Talenski', 'Jerusalem', 'Money 100', 'Israel'),
('04256372-0', 'Uri Meser', 'Holyland', 'Money 101', 'Israel'),
('02637482-0', 'Shula Zaken', 'Jerusalem', 'Money 102', 'Israel'),
('00000000-0', 'Gimel', 'Mosad', 'Secret 007', 'Dubai');

INSERT INTO rooms VALUES
(1, 100, 'basic', 2, 100),
(1, 101, 'basic', 2, 100),
(1, 102, 'basic', 2, 100),
(1, 200, 'double', 2, 100),
(1, 201, 'double', 2, 100),
(1, 202, 'double', 2, 100),
(1, 300, 'truple', 3, 180),
(1, 400, 'honeymoon', 2, 270),
(2, 100, 'basic', 2, 100),
(2, 200, 'double', 4, 200),
(2, 201, 'double', 4, 200),
(2, 202, 'double', 4, 200),
(2, 300, 'truple', 5, 220),
(2, 400, 'honeymoon', 3, 230),
(2, 401, 'honeymoon', 3, 230),
(3, 100, 'basic', 2, 100),
(3, 200, 'double', 2, 210),
(3, 201, 'double', 2, 210),
(3, 202, 'double', 2, 210),
(3, 300, 'truple', 4, 220),
(3, 400, 'honeymoon', 3, 230),
(4, 100, 'basic', 2, 100),
(4, 200, 'double', 1, 500),
(4, 300, 'truple', 4, 120),
(4, 301, 'truple', 4, 120),
(4, 400, 'honeymoon', 3, 230),
(4, 401, 'honeymoon', 3, 230),
(4, 402, 'honeymoon', 3, 230),
(4, 403, 'honeymoon', 3, 230),
(5, 100, 'basic', 2, 100),
(5, 200, 'double', 4, 150),
(5, 300, 'truple', 4, 340),
(5, 400, 'honeymoon', 3, 210),
(6, 100, 'basic', 2, 100),
(6, 101, 'basic', 2, 100),
(6, 200, 'double', 3, 110),
(6, 201, 'double', 3, 110),
(6, 300, 'truple', 4, 310),
(6, 400, 'honeymoon', 1, 240),
(7, 100, 'basic', 2, 100),
(7, 101, 'basic', 2, 100),
(7, 102, 'basic', 2, 100),
(7, 200, 'double', 4, 130),
(7, 300, 'truple', 2, 250),
(7, 301, 'truple', 2, 250),
(7, 302, 'truple', 2, 250),
(7, 400, 'honeymoon', 3, 280),
(8, 100, 'basic', 2, 90),
(8, 200, 'double', 4, 290),
(8, 300, 'truple', 2, 430),
(8, 400, 'honeymoon', 3, 150),
(9, 100, 'basic', 2, 130),
(9, 200, 'double', 4, 150),
(9, 300, 'truple', 2, 280),
(9, 400, 'honeymoon', 3, 270);

INSERT INTO reservation VALUES
(6, 300, '2010-06-15', '2010-06-29', 'cash', '00000001-0'),
(5, 400, '2010-01-02', '2010-01-13', 'credit', '00000001-0'),
(4, 300, '2010-06-15', '2010-07-01', 'cash', '00000002-0'),
(5, 400, '2010-01-01', '2010-01-13', 'credit', '00000002-0'),
(3, 200, '2010-06-15', '2010-08-01', 'cash', '04352244-4'),
(2, 100, '2010-04-15', '2010-06-21', 'cash', '06112343-6'),
(1, 100, '2010-06-23', '2010-07-01', 'cash', '06112343-6'),
(8, 300, '2010-04-12', '2010-04-15', 'cash', '12345678-0'),
(8, 200, '2010-04-12', '2010-04-15', 'cash', '87654321-0'),
(8, 100, '2010-04-12', '2010-04-15', 'cash', '04256372-0'),
(8, 200, '2010-04-17', '2010-04-25', 'cash', '04256372-0'),
(8, 200, '2010-04-25', '2010-04-29', 'cash', '02637482-0');

And I want to write the query :

present a list of the hotels with more than 2 rooms , where their price is 
bigger than 186 . 
Order it by : hotel name ,city ,number of rooms . Sort by number of rooms .

So my query is :

SELECT DISTINCT hotel_name, city, rooms_number
FROM hotels JOIN
(SELECT DISTINCT hotel_code, COUNT(*) as rooms_number
FROM rooms
WHERE price > 186
GROUP BY hotel_code
) as rooms_count
ON (hotels.hotel_code = rooms_count.hotel_code AND rooms_number > 2)
ORDER BY rooms_number;

But this query is way too long , is it possible to write it in another way ,that would
fit the question’s needs ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T23:21:31+00:00Added an answer on June 5, 2026 at 11:21 pm

    You can avoid the subselect by utilizing the HAVING clause.

    SELECT
        a.hotel_name,
        a.city,
        COUNT(*) AS room_count
    FROM 
        hotels a
    INNER JOIN
        rooms b ON a.hotel_code = b.hotel_code AND b.price > 186
    GROUP BY
        a.hotel_name,
        a.city
    HAVING
        COUNT(*) > 2
    ORDER BY
        room_count DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to choose the structure of a database that will store content types
So I have a page with an enormous table in a CRUD interface of
I have a table with around 115k rows. Something like this: Table: People Column:
The database I am working with currently does have two tables, one holding the
Have this query: SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
Have this self-made slider: http://jsfiddle.net/wyc3P/4/ What it does: takes min and max values in
I have this string 2012-06-27 16:17:06 and I want to convert it to GMT
I have this application where I implement the ActionBar Fragment interface. Underlying the interface,
I have this form, in which i need to populate a combo box with
I have this form: <%= form_tag posts_path, :method => :get, :class => search_nav do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.