System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE hospitality_technology.properties ( property_id character varying(64) NOT NULL, property_name character varying(255) NOT NULL, address_line1 character varying(255) NOT NULL, address_line2 character varying(255), city character varying(100) NOT NULL, state_province character varying(100), postal_code character varying(20) NOT NULL, country_code character varying(3) NOT NULL, phone_number character varying(30), email_address character varying(255), property_type character varying(50) NOT NULL, star_rating numeric(2,1), timezone character varying(64) NOT NULL, manager_staff_id character varying(64), created_at timestamp with time zone NOT NULL DEFAULT current_timestamp, updated_at timestamp with time zone NOT NULL DEFAULT current_timestamp, PRIMARY KEY (property_id) )
CREATE TABLE hospitality_technology.rooms ( room_id character varying(64) NOT NULL, property_id character varying(64) NOT NULL, room_number character varying(10) NOT NULL, room_type_id character varying(64) NOT NULL, floor integer, status character varying(50) NOT NULL, is_smoking boolean NOT NULL DEFAULT false, features text, current_condition text, last_cleaned_at timestamp with time zone, PRIMARY KEY (room_id), FOREIGN KEY (property_id) REFERENCES hospitality_technology.properties(property_id), FOREIGN KEY (room_type_id) REFERENCES hospitality_technology.room_types(room_type_id) )
CREATE TABLE hospitality_technology.room_types ( room_type_id character varying(64) NOT NULL, property_id character varying(64) NOT NULL, type_name character varying(100) NOT NULL, description text, base_rate numeric(10, 2) NOT NULL, max_occupancy integer NOT NULL, bed_type character varying(50), area_sqft numeric(8,2), amenities text, PRIMARY KEY (room_type_id), FOREIGN KEY (property_id) REFERENCES hospitality_technology.properties(property_id) )
CREATE TABLE hospitality_technology.reservations ( reservation_id character varying(64) NOT NULL, property_id character varying(64) NOT NULL, guest_id character varying(64) NOT NULL, room_id character varying(64), room_type_id character varying(64) NOT NULL, check_in_date date NOT NULL, check_out_date date NOT NULL, actual_check_in_ts timestamp with time zone, actual_check_out_ts timestamp with time zone, num_adults integer NOT NULL, num_children integer DEFAULT 0, status character varying(50) NOT NULL, booking_source character varying(100), rate_plan_id character varying(64), total_amount numeric(12, 2), amount_paid numeric(12, 2) DEFAULT 0.00, special_requests text, created_at timestamp with time zone NOT NULL DEFAULT current_timestamp, updated_at timestamp with time zone NOT NULL DEFAULT current_timestamp, PRIMARY KEY (reservation_id), FOREIGN KEY (property_id) REFERENCES hospitality_technology.properties(property_id), FOREIGN KEY (guest_id) REFERENCES hospitality_technology.guests(guest_id), FOREIGN KEY (room_id) REFERENCES hospitality_technology.rooms(room_id), FOREIGN KEY (room_type_id) REFERENCES hospitality_technology.room_types(room_type_id) )
CREATE TABLE hospitality_technology.services ( service_id character varying(64) NOT NULL, property_id character varying(64) NOT NULL, service_name character varying(150) NOT NULL, description text, category character varying(100), price numeric(10, 2) NOT NULL, unit character varying(30), is_available boolean NOT NULL DEFAULT true, department character varying(100), PRIMARY KEY (service_id), FOREIGN KEY (property_id) REFERENCES hospitality_technology.properties(property_id) )
CREATE TABLE hospitality_technology.service_orders ( order_id character varying(64) NOT NULL, reservation_id character varying(64), guest_id character varying(64) NOT NULL, service_id character varying(64) NOT NULL, staff_id_ordered character varying(64), staff_id_delivered character varying(64), order_timestamp timestamp with time zone NOT NULL DEFAULT current_timestamp, delivery_timestamp timestamp with time zone, quantity integer NOT NULL DEFAULT 1, unit_price numeric(10, 2) NOT NULL, total_price numeric(12, 2) NOT NULL, status character varying(50) NOT NULL, notes text, bill_id character varying(64), PRIMARY KEY (order_id), FOREIGN KEY (reservation_id) REFERENCES hospitality_technology.reservations(reservation_id), FOREIGN KEY (guest_id) REFERENCES hospitality_technology.guests(guest_id), FOREIGN KEY (service_id) REFERENCES hospitality_technology.services(service_id), FOREIGN KEY (staff_id_ordered) REFERENCES hospitality_technology.staff(staff_id), FOREIGN KEY (staff_id_delivered) REFERENCES hospitality_technology.staff(staff_id) )
CREATE TABLE hospitality_technology.payments ( payment_id character varying(64) NOT NULL, reservation_id character varying(64), guest_id character varying(64) NOT NULL, bill_id character varying(64), amount numeric(12,2) NOT NULL, currency_code character varying(3) NOT NULL, payment_method character varying(50) NOT NULL, card_type character varying(50), card_last_four character varying(4), transaction_ref character varying(128), payment_timestamp timestamp with time zone NOT NULL DEFAULT current_timestamp, status character varying(50) NOT NULL, staff_id_processed character varying(64), notes text, PRIMARY KEY (payment_id), FOREIGN KEY (reservation_id) REFERENCES hospitality_technology.reservations(reservation_id), FOREIGN KEY (guest_id) REFERENCES hospitality_technology.guests(guest_id), FOREIGN KEY (staff_id_processed) REFERENCES hospitality_technology.staff(staff_id) )
CREATE TABLE hospitality_technology.reviews ( review_id character varying(64) NOT NULL, property_id character varying(64) NOT NULL, reservation_id character varying(64) UNIQUE, guest_id character varying(64) NOT NULL, rating_overall integer, rating_cleanliness integer, rating_service integer, rating_location integer, rating_value integer, comment text, review_source character varying(100), review_date timestamp with time zone NOT NULL DEFAULT current_timestamp, is_public boolean DEFAULT true, staff_response text, response_staff_id character varying(64), response_date timestamp with time zone, PRIMARY KEY (review_id), FOREIGN KEY (property_id) REFERENCES hospitality_technology.properties(property_id), FOREIGN KEY (reservation_id) REFERENCES hospitality_technology.reservations(reservation_id), FOREIGN KEY (guest_id) REFERENCES hospitality_technology.guests(guest_id), FOREIGN KEY (response_staff_id) REFERENCES hospitality_technology.staff(staff_id) )

[User] List all reservations along with the guest's full name and the property name for reservations made in properties outside the USA.

[Assistant]
SELECT r.reservation_id, g.first_name || ' ' || g.last_name AS guest_full_name, p.property_name
FROM hospitality_technology.reservations r
JOIN hospitality_technology.guests g ON r.guest_id = g.guest_id
JOIN hospitality_technology.properties p ON r.property_id = p.property_id
WHERE p.country_code <> 'USA';

[User] Only include reservations with a total amount greater than 800, and sort the results by total_amount descending.

[Assistant]
SELECT r.reservation_id, g.first_name || ' ' || g.last_name AS guest_full_name, p.property_name, r.total_amount
FROM hospitality_technology.reservations r
JOIN hospitality_technology.guests g ON r.guest_id = g.guest_id
JOIN hospitality_technology.properties p ON r.property_id = p.property_id
WHERE p.country_code <> 'USA'
  AND r.total_amount > 800
ORDER BY r.total_amount DESC;


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE hospitality_technology.properties ( property_id text, property_name text, address_line1 text, address_line2 text, city text, state_province text, postal_code text, country_code text, phone_number text, email_address text, property_type text, star_rating numeric, timezone text, manager_staff_id text, created_at timestamp, updated_at timestamp )
CREATE TABLE hospitality_technology.payments (
    payment_id character varying(64) NOT NULL,  -- unique identifier for the payment transaction
    reservation_id character varying(64), -- FK to reservations (if payment is for a stay)
    guest_id character varying(64) NOT NULL, -- FK to guests (who paid)
    bill_id character varying(64), -- FK to bills/invoices this payment applies to
    amount numeric(12,2) NOT NULL, -- amount of the payment
    currency_code character varying(3) NOT NULL, -- ISO currency code (e.g., USD, EUR)
    payment_method character varying(50) NOT NULL, -- e.g., CreditCard, Cash, BankTransfer, Points, Voucher
    card_type character varying(50), -- e.g., Visa, MasterCard, Amex (if CC)
    card_last_four character varying(4), -- last 4 digits of CC (if CC)
    transaction_ref character varying(128), -- reference ID from payment gateway or processor
    payment_timestamp timestamp with time zone NOT NULL DEFAULT current_timestamp, -- when the payment occurred
    status character varying(50) NOT NULL, -- e.g., Pending, Succeeded, Failed, Refunded, Disputed
    staff_id_processed character varying(64), -- FK to staff (who processed the payment)
    notes text, -- notes about the payment
    PRIMARY KEY (payment_id),
    FOREIGN KEY (reservation_id) REFERENCES hospitality_technology.reservations(reservation_id),
    FOREIGN KEY (guest_id) REFERENCES hospitality_technology.guests(guest_id),
    FOREIGN KEY (staff_id_processed) REFERENCES hospitality_technology.staff(staff_id)
)

[User] Show me the payment amount and the name of the property associated with each payment record.

[Assistant]
SELECT
  p.amount,
  prop.property_name
FROM hospitality_technology.payments AS p
JOIN hospitality_technology.reservations AS r ON p.reservation_id = r.reservation_id -- Assuming reservations links payments and properties
JOIN hospitality_technology.properties AS prop ON r.property_id = prop.property_id;

[User] Now, filter this list to only include payments with a status of 'Succeeded' and sort by payment amount in descending order.

[Assistant]
SELECT
  p.amount,
  prop.property_name
FROM hospitality_technology.payments AS p
JOIN hospitality_technology.reservations AS r ON p.reservation_id = r.reservation_id
JOIN hospitality_technology.properties AS prop ON r.property_id = prop.property_id
WHERE
  p.status = 'Succeeded'
ORDER BY
  p.amount DESC;


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE hospitality_technology.properties ( property_id character varying(64) NOT NULL, property_name character varying(255) NOT NULL, address_line1 character varying(255) NOT NULL, address_line2 character varying(255), city character varying(100) NOT NULL, state_province character varying(100), postal_code character varying(20) NOT NULL, country_code character varying(3) NOT NULL, phone_number character varying(30), email_address character varying(255), property_type character varying(50) NOT NULL, star_rating numeric(2,1), timezone character varying(64) NOT NULL, manager_staff_id character varying(64), created_at timestamp with time zone NOT NULL DEFAULT current_timestamp, updated_at timestamp with time zone NOT NULL DEFAULT current_timestamp, PRIMARY KEY (property_id) )
CREATE TABLE hospitality_technology.room_types ( room_type_id.text, property_id.text, type_name.text, description.text, base_rate.numeric, max_occupancy.integer, bed_type.text, area_sqft.numeric, amenities.text )
CREATE TABLE hospitality_technology.reservations ( reservation_id character varying(64) NOT NULL, property_id character varying(64) NOT NULL, guest_id character varying(64) NOT NULL, room_id character varying(64), room_type_id character varying(64) NOT NULL, check_in_date date NOT NULL, check_out_date date NOT NULL, actual_check_in_ts timestamp with time zone, actual_check_out_ts timestamp with time zone, num_adults integer NOT NULL, num_children integer DEFAULT 0, status character varying(50) NOT NULL, booking_source character varying(100), rate_plan_id character varying(64), total_amount numeric(12, 2), amount_paid numeric(12, 2) DEFAULT 0.00, special_requests text, created_at timestamp with time zone NOT NULL DEFAULT current_timestamp, updated_at timestamp with time zone NOT NULL DEFAULT current_timestamp, PRIMARY KEY (reservation_id), FOREIGN KEY (property_id) REFERENCES hospitality_technology.properties(property_id), FOREIGN KEY (guest_id) REFERENCES hospitality_technology.guests(guest_id), FOREIGN KEY (room_id) REFERENCES hospitality_technology.rooms(room_id), FOREIGN KEY (room_type_id) REFERENCES hospitality_technology.room_types(room_type_id) )
CREATE TABLE hospitality_technology.service_orders ( order_id text PRIMARY KEY, reservation_id text, guest_id text, service_id text, staff_id_ordered text, staff_id_delivered text, order_timestamp timestamp, delivery_timestamp timestamp, quantity int, unit_price numeric, total_price numeric, status text, notes text, bill_id text )

[User] Show me all service orders with their order ID, guest ID, reservation ID, and the name of the property where the order was made.

[Assistant]
SELECT so.order_id, so.guest_id, so.reservation_id, p.property_name
FROM hospitality_technology.service_orders so
JOIN hospitality_technology.reservations r ON so.reservation_id = r.reservation_id
JOIN hospitality_technology.properties p ON r.property_id = p.property_id;

[User] Only include orders where the total price is over 100.

[Assistant]
SELECT so.order_id, so.guest_id, so.reservation_id, p.property_name
FROM hospitality_technology.service_orders so
JOIN hospitality_technology.reservations r ON so.reservation_id = r.reservation_id
JOIN hospitality_technology.properties p ON r.property_id = p.property_id
WHERE so.total_price > 100;


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE hospitality_technology.properties ( property_id character varying(64) NOT NULL, property_name character varying(255) NOT NULL, address_line1 character varying(255) NOT NULL, address_line2 character varying(255), city character varying(100) NOT NULL, state_province character varying(100), postal_code character varying(20) NOT NULL, country_code character varying(3) NOT NULL, phone_number character varying(30), email_address character varying(255), property_type character varying(50) NOT NULL, star_rating numeric(2,1), timezone character varying(64) NOT NULL, manager_staff_id character varying(64), created_at timestamp with time zone NOT NULL DEFAULT current_timestamp, updated_at timestamp with time zone NOT NULL DEFAULT current_timestamp, PRIMARY KEY (property_id) )
CREATE TABLE hospitality_technology.rooms ( room_id character varying(64) NOT NULL, property_id character varying(64) NOT NULL, room_number character varying(10) NOT NULL, room_type_id character varying(64) NOT NULL, floor integer, status character varying(50) NOT NULL, is_smoking boolean NOT NULL DEFAULT false, features text, current_condition text, last_cleaned_at timestamp with time zone, PRIMARY KEY (room_id), FOREIGN KEY (property_id) REFERENCES hospitality_technology.properties(property_id), FOREIGN KEY (room_type_id) REFERENCES hospitality_technology.room_types(room_type_id) )
CREATE TABLE hospitality_technology.room_types ( room_type_id character varying(64) NOT NULL, property_id character varying(64) NOT NULL, type_name character varying(100) NOT NULL, description text, base_rate numeric(10, 2) NOT NULL, max_occupancy integer NOT NULL, bed_type character varying(50), area_sqft numeric(8,2), amenities text, PRIMARY KEY (room_type_id), FOREIGN KEY (property_id) REFERENCES hospitality_technology.properties(property_id) )
CREATE TABLE hospitality_technology.guests ( guest_id character varying(64) NOT NULL, first_name character varying(100) NOT NULL, last_name character varying(100) NOT NULL, email character varying(255) UNIQUE, phone_number character varying(30), date_of_birth date, address_line1 character varying(255), city character varying(100), state_province character varying(100), postal_code character varying(20), country_code character varying(3), preferences text, loyalty_member_id character varying(64) UNIQUE, communication_consent boolean DEFAULT true, created_at timestamp with time zone NOT NULL DEFAULT current_timestamp, updated_at timestamp with time zone NOT NULL DEFAULT current_timestamp, PRIMARY KEY (guest_id) )
CREATE TABLE hospitality_technology.reservations ( reservation_id character varying(64) NOT NULL, property_id character varying(64) NOT NULL, guest_id character varying(64) NOT NULL, room_id character varying(64), room_type_id character varying(64) NOT NULL, check_in_date date NOT NULL, check_out_date date NOT NULL, actual_check_in_ts timestamp with time zone, actual_check_out_ts timestamp with time zone, num_adults integer NOT NULL, num_children integer DEFAULT 0, status character varying(50) NOT NULL, booking_source character varying(100), rate_plan_id character varying(64), total_amount numeric(12, 2), amount_paid numeric(12, 2) DEFAULT 0.00, special_requests text, created_at timestamp with time zone NOT NULL DEFAULT current_timestamp, updated_at timestamp with time zone NOT NULL DEFAULT current_timestamp, PRIMARY KEY (reservation_id), FOREIGN KEY (property_id) REFERENCES hospitality_technology.properties(property_id), FOREIGN KEY (guest_id) REFERENCES hospitality_technology.guests(guest_id), FOREIGN KEY (room_id) REFERENCES hospitality_technology.rooms(room_id), FOREIGN KEY (room_type_id) REFERENCES hospitality_technology.room_types(room_type_id) )
CREATE TABLE hospitality_technology.services ( service_id character varying(64) NOT NULL, property_id character varying(64) NOT NULL, service_name character varying(150) NOT NULL, description text, category character varying(100), price numeric(10, 2) NOT NULL, unit character varying(30), is_available boolean NOT NULL DEFAULT true, department character varying(100), PRIMARY KEY (service_id), FOREIGN KEY (property_id) REFERENCES hospitality_technology.properties(property_id) )
CREATE TABLE hospitality_technology.service_orders ( order_id character varying(64) NOT NULL, reservation_id character varying(64), guest_id character varying(64) NOT NULL, service_id character varying(64) NOT NULL, staff_id_ordered character varying(64), staff_id_delivered character varying(64), order_timestamp timestamp with time zone NOT NULL DEFAULT current_timestamp, delivery_timestamp timestamp with time zone, quantity integer NOT NULL DEFAULT 1, unit_price numeric(10, 2) NOT NULL, total_price numeric(12, 2) NOT NULL, status character varying(50) NOT NULL, notes text, bill_id character varying(64), PRIMARY KEY (order_id), FOREIGN KEY (reservation_id) REFERENCES hospitality_technology.reservations(reservation_id), FOREIGN KEY (guest_id) REFERENCES hospitality_technology.guests(guest_id), FOREIGN KEY (service_id) REFERENCES hospitality_technology.services(service_id), FOREIGN KEY (staff_id_ordered) REFERENCES hospitality_technology.staff(staff_id), FOREIGN KEY (staff_id_delivered) REFERENCES hospitality_technology.staff(staff_id) )
CREATE TABLE hospitality_technology.payments ( payment_id character varying(64) NOT NULL, reservation_id character varying(64), guest_id character varying(64) NOT NULL, bill_id character varying(64), amount numeric(12,2) NOT NULL, currency_code character varying(3) NOT NULL, payment_method character varying(50) NOT NULL, card_type character varying(50), card_last_four character varying(4), transaction_ref character varying(128), payment_timestamp timestamp with time zone NOT NULL DEFAULT current_timestamp, status character varying(50) NOT NULL, staff_id_processed character varying(64), notes text, PRIMARY KEY (payment_id), FOREIGN KEY (reservation_id) REFERENCES hospitality_technology.reservations(reservation_id), FOREIGN KEY (guest_id) REFERENCES hospitality_technology.guests(guest_id), FOREIGN KEY (staff_id_processed) REFERENCES hospitality_technology.staff(staff_id) )
CREATE TABLE hospitality_technology.reviews ( review_id character varying(64) NOT NULL, property_id character varying(64) NOT NULL, reservation_id character varying(64) UNIQUE, guest_id character varying(64) NOT NULL, rating_overall integer, rating_cleanliness integer, rating_service integer, rating_location integer, rating_value integer, comment text, review_source character varying(100), review_date timestamp with time zone NOT NULL DEFAULT current_timestamp, is_public boolean DEFAULT true, staff_response text, response_staff_id character varying(64), response_date timestamp with time zone, PRIMARY KEY (review_id), FOREIGN KEY (property_id) REFERENCES hospitality_technology.properties(property_id), FOREIGN KEY (reservation_id) REFERENCES hospitality_technology.reservations(reservation_id), FOREIGN KEY (guest_id) REFERENCES hospitality_technology.guests(guest_id), FOREIGN KEY (response_staff_id) REFERENCES hospitality_technology.staff(staff_id) )

[User] List the names of services and the total number of times each service has been ordered. Only include services that have been ordered at least once.

[Assistant] SELECT T1.service_name, count(T2.order_id) AS order_count FROM hospitality_technology.services AS T1 JOIN hospitality_technology.service_orders AS T2 ON T1.service_id = T2.service_id GROUP BY T1.service_name;

[User] Now, filter this list to only show services from the 'Food & Beverage' or 'Activities' categories, and sort by the order count in descending order.

[Assistant] SELECT T1.service_name, count(T2.order_id) AS order_count FROM hospitality_technology.services AS T1 JOIN hospitality_technology.service_orders AS T2 ON T1.service_id = T2.service_id WHERE T1.category IN ('Food & Beverage', 'Activities') GROUP BY T1.service_name ORDER BY order_count DESC;


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE hospitality_technology.rooms (
    room_id character varying(64) NOT NULL,  -- unique identifier for the room
    property_id character varying(64) NOT NULL, -- FK to properties table
    room_number character varying(10) NOT NULL, -- room number identifier (e.g., '101', 'Suite 2B')
    room_type_id character varying(64) NOT NULL, -- FK to room_types table
    floor integer, -- floor number the room is on
    status character varying(50) NOT NULL, -- e.g., Available, Occupied, Cleaning, OutOfOrder, Maintenance
    is_smoking boolean NOT NULL DEFAULT false, -- smoking allowed flag
    features text, -- description of features (e.g., Balcony, Sea View, Jacuzzi)
    current_condition text, -- notes on room condition
    last_cleaned_at timestamp with time zone, -- timestamp of last cleaning
    PRIMARY KEY (room_id),
    FOREIGN KEY (property_id) REFERENCES hospitality_technology.properties(property_id),
    FOREIGN KEY (room_type_id) REFERENCES hospitality_technology.room_types(room_type_id)
)
CREATE TABLE hospitality_technology.room_types ( room_type_id text PRIMARY KEY, property_id text, type_name text, description text, base_rate numeric, max_occupancy int, bed_type text, area_sqft numeric, amenities text )
CREATE TABLE hospitality_technology.guests ( guest_id text PRIMARY KEY, first_name text, last_name text, email text, phone_number text, date_of_birth date, address_line1 text, city text, state_province text, postal_code text, country_code text, preferences text, loyalty_member_id text, communication_consent boolean, created_at timestamp, updated_at timestamp )
CREATE TABLE hospitality_technology.reservations ( reservation_id text, property_id text, guest_id text, room_id text, room_type_id text, check_in_date date, check_out_date date, actual_check_in_ts timestamp, actual_check_out_ts timestamp, num_adults integer, num_children integer, status text, booking_source text, rate_plan_id text, total_amount numeric, amount_paid numeric, special_requests text, created_at timestamp, updated_at timestamp )
CREATE TABLE hospitality_technology.reviews ( review_id character varying(64) NOT NULL, property_id character varying(64) NOT NULL, reservation_id character varying(64) UNIQUE, guest_id character varying(64) NOT NULL, rating_overall integer, rating_cleanliness integer, rating_service integer, rating_location integer, rating_value integer, comment text, review_source character varying(100), review_date timestamp with time zone NOT NULL DEFAULT current_timestamp, is_public boolean DEFAULT true, staff_response text, response_staff_id character varying(64), response_date timestamp with time zone, PRIMARY KEY (review_id), FOREIGN KEY (property_id) REFERENCES hospitality_technology.properties(property_id), FOREIGN KEY (reservation_id) REFERENCES hospitality_technology.reservations(reservation_id), FOREIGN KEY (guest_id) REFERENCES hospitality_technology.guests(guest_id), FOREIGN KEY (response_staff_id) REFERENCES hospitality_technology.staff(staff_id) )

[User] Retrieve the guest_id, first_name, last_name, and loyalty_member_id for all guests who have communication_consent enabled.

[Assistant]
SELECT guest_id, first_name, last_name, loyalty_member_id
FROM hospitality_technology.guests
WHERE communication_consent = true;

[User] Now, only include those guests with a non-null loyalty_member_id and sort by last_name ascending.

[Assistant]
SELECT guest_id, first_name, last_name, loyalty_member_id
FROM hospitality_technology.guests
WHERE communication_consent = true
  AND loyalty_member_id IS NOT NULL
ORDER BY last_name ASC;


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE hospitality_technology.properties ( property_id character varying(64) NOT NULL, property_name character varying(255) NOT NULL, address_line1 character varying(255) NOT NULL, address_line2 character varying(255), city character varying(100) NOT NULL, state_province character varying(100), postal_code character varying(20) NOT NULL, country_code character varying(3) NOT NULL, phone_number character varying(30), email_address character varying(255), property_type character varying(50) NOT NULL, star_rating numeric(2,1), timezone character varying(64) NOT NULL, manager_staff_id character varying(64), created_at timestamp with time zone NOT NULL DEFAULT current_timestamp, updated_at timestamp with time zone NOT NULL DEFAULT current_timestamp, PRIMARY KEY (property_id) )
CREATE TABLE hospitality_technology.rooms ( room_id text, property_id text, room_number text, room_type_id text, floor integer, status text, is_smoking boolean, features text, current_condition text, last_cleaned_at timestamp )
CREATE TABLE hospitality_technology.room_types (
    room_type_id character varying(64) NOT NULL, -- unique identifier for the room type
    property_id character varying(64) NOT NULL, -- FK to properties table (type might be property-specific)
    type_name character varying(100) NOT NULL, -- e.g., Standard King, Deluxe Queen, Executive Suite
    description text, -- description of the room type
    base_rate numeric(10, 2) NOT NULL, -- standard price per night
    max_occupancy integer NOT NULL, -- maximum number of guests
    bed_type character varying(50), -- e.g., King, Queen, 2 Doubles, Twin
    area_sqft numeric(8,2), -- size of the room in square feet
    amenities text, -- comma-separated list or JSON of amenities
    PRIMARY KEY (room_type_id),
    FOREIGN KEY (property_id) REFERENCES hospitality_technology.properties(property_id)
)
CREATE TABLE hospitality_technology.guests ( guest_id text PRIMARY KEY, first_name text, last_name text, email text, phone_number text, date_of_birth date, address_line1 text, city text, state_province text, postal_code text, country_code text, preferences text, loyalty_member_id text, communication_consent boolean, created_at timestamp, updated_at timestamp )
CREATE TABLE hospitality_technology.staff ( staff_id text, property_id text, first_name text, last_name text, position text, work_email text, work_phone text, hire_date date, termination_date date, is_active boolean, access_level integer, manager_id text, created_at timestamp )
CREATE TABLE hospitality_technology.services ( service_id character varying(64) NOT NULL, property_id character varying(64) NOT NULL, service_name character varying(150) NOT NULL, description text, category character varying(100), price numeric(10, 2) NOT NULL, unit character varying(30), is_available boolean NOT NULL DEFAULT true, department character varying(100), PRIMARY KEY (service_id), FOREIGN KEY (property_id) REFERENCES hospitality_technology.properties(property_id) )
CREATE TABLE hospitality_technology.service_orders ( order_id.text, reservation_id.text, guest_id.text, service_id.text, staff_id_ordered.text, staff_id_delivered.text, order_timestamp.timestamp, delivery_timestamp.timestamp, quantity.integer, unit_price.numeric, total_price.numeric, status.text, notes.text, bill_id.text )
CREATE TABLE hospitality_technology.reviews ( review_id text, property_id text, reservation_id text, guest_id text, rating_overall integer, rating_cleanliness integer, rating_service integer, rating_location integer, rating_value integer, comment text, review_source text, review_date timestamp, is_public boolean, staff_response text, response_staff_id text, response_date timestamp )

[User] List all available rooms with their room number, property_name, type_name, and the number of days since they were last cleaned.

[Assistant]
SELECT p.property_name,
       r.room_number,
       rt.type_name,
       date_part('day', current_timestamp - r.last_cleaned_at) AS days_since_cleaned
FROM hospitality_technology.rooms r
JOIN hospitality_technology.properties p ON r.property_id = p.property_id
JOIN hospitality_technology.room_types rt ON r.room_type_id = rt.room_type_id
WHERE r.status = 'Available';


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE project_fundraising_record ( id.text, project_id.text, project_name.text, rounds_name.text, investment_value.numeric, investment_date.text, introduction.text, valuation.numeric )
CREATE TABLE token_basic_info ( coin_id.text, token_name.text, token_symbol.text, token_address.text, chain.text, price.numeric, price_change_percentage_1h.numeric, price_change_percentage_24h.numeric, price_change_percentage_7d.numeric, highest_price_24h.numeric, lowest_price_24h.numeric, market_cap.numeric, market_cap_rank.numeric, market_cap_change_percentage_24h.text, total_volume.text, narrative.text, trend_rank.numeric )
CREATE TABLE narrative_info ( narrative.text, top_3_coins.text, price_change_percentage_1h.numeric, price_change_percentage_24h.numeric, price_change_percentage_7d.numeric, market_cap.numeric, volume_24h.numeric, trend_rank.numeric, token_num.numeric )
CREATE TABLE token_dexpool_info ( pair_address.text, token_name.text, token_symbol.text, chain.text, price.numeric, price_change_percentage_1h.numeric, price_change_percentage_6h.numeric, price_change_percentage_24h.numeric, makers_24h.numeric, volume_24h.numeric, liquidity.numeric, market_cap.numeric, trend_rank.numeric )
CREATE TABLE cex_token_balance ( snapshot_time.text, chain.text, address.text, exchange.text, token_address.text, token_symbol.text, balance.numeric, usd_value.numeric, price.numeric )
CREATE TABLE smart_money_profit ( address.text, rank.numeric, profit.numeric, roi.numeric, realized_ratio.numeric, most_profit_token.text, last_trade_time.text, chain.text )
CREATE TABLE smart_money_token_inflow ( token_address.text, token_symbol.text, token_amount.numeric, usd_value.numeric, avg_price.numeric, type.numeric, address_amount.numeric, narrative.text, chain.text )

[User] List all projects that raised funds in March 2023. For each project, show its name, amount raised, fundraising round, and the token symbol and price of its associated token.

[Assistant]
SELECT p.project_name, p.investment_value, p.rounds_name, t.token_symbol, t.price
FROM project_fundraising_record p
LEFT JOIN token_basic_info t ON LOWER(p.project_name) = LOWER(t.token_name)
WHERE p.investment_date LIKE '2023-03%'

[User] Can you also include the current market cap and chain of each token in the result?

[Assistant]
SELECT p.project_name, p.investment_value, p.rounds_name, t.token_symbol, t.price, t.market_cap, t.chain
FROM project_fundraising_record p
LEFT JOIN token_basic_info t ON LOWER(p.project_name) = LOWER(t.token_name)
WHERE p.investment_date LIKE '2023-03%'

[User] I only want to see projects that raised more than $5,000,000. Please update the query.

[Assistant]
SELECT p.project_name, p.investment_value, p.rounds_name, t.token_symbol, t.price, t.market_cap, t.chain
FROM project_fundraising_record p
LEFT JOIN token_basic_info t ON LOWER(p.project_name) = LOWER(t.token_name)
WHERE p.investment_date LIKE '2023-03%' AND p.investment_value > 5000000

[User] It seems some tokens are not matched because names are not identical. Can you try joining where the token_name contains the project_name (case-insensitive)?

[Assistant]
SELECT p.project_name, p.investment_value, p.rounds_name, t.token_symbol, t.price, t.market_cap, t.chain
FROM project_fundraising_record p
LEFT JOIN token_basic_info t ON LOWER(t.token_name) LIKE '%' || LOWER(p.project_name) || '%'
WHERE p.investment_date LIKE '2023-03%' AND p.investment_value > 5000000


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE token_basic_info ( coin_id text, token_name text, token_symbol text, token_address text, chain text, price numeric, price_change_percentage_1h numeric, price_change_percentage_24h numeric, price_change_percentage_7d numeric, highest_price_24h numeric, lowest_price_24h numeric, market_cap numeric, market_cap_rank numeric, market_cap_change_percentage_24h text, total_volume text, narrative text, trend_rank numeric )

[User] Find tokens on the 'Polygon' chain with a market cap greater than $1,000,000,000 and a positive price change in the last 24 hours. List their symbol, name, market cap, and 24h price change.

[Assistant] SELECT token_symbol, token_name, market_cap, price_change_percentage_24h FROM token_basic_info WHERE chain = 'Polygon' AND market_cap > 1000000000 AND price_change_percentage_24h > 0;

[User] Also include their current price and 7-day price change percentage. Order the results by market cap descending.

[Assistant] SELECT token_symbol, token_name, market_cap, price_change_percentage_24h, price, price_change_percentage_7d FROM token_basic_info WHERE chain = 'Polygon' AND market_cap > 1000000000 AND price_change_percentage_24h > 0 ORDER BY market_cap DESC;


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE smart_money_token_performance ( token_address character varying(100) NOT NULL, token_symbol character varying(100), pnl numeric(38,2), net_flow numeric(38,2), avg_cost numeric(38,16), price double precision, realized_ratio numeric(10,2), roi numeric(10,2), chain character varying(100) )

[User] Show the tokens with a negative net flow this month on all chains.

[Assistant] SELECT token_symbol, chain, net_flow FROM smart_money_token_performance WHERE net_flow < 0 AND date_trunc('month', CURRENT_DATE) = date_trunc('month', now());

[User] Order them by net flow ascending and only show the bottom 5.

[Assistant] SELECT token_symbol, chain, net_flow FROM smart_money_token_performance WHERE net_flow < 0 AND date_trunc('month', CURRENT_DATE) = date_trunc('month', now()) ORDER BY net_flow ASC LIMIT 5;


[User] Show me the top 3 tokens with the highest 7-day price change percentage. Display their symbol, chain, and 7d price change.

[Assistant]
SELECT token_symbol, chain, price_change_percentage_7d
FROM token_basic_info
ORDER BY price_change_percentage_7d DESC
LIMIT 3


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE smart_money_token_performance ( token_address.text, token_symbol.text, pnl.numeric, net_flow.numeric, avg_cost.numeric, price.numeric, realized_ratio.numeric, roi.numeric, chain.text )

[User] List all smart money addresses that have a negative PNL but a positive Realized Ratio. Show the address, chain, PNL, and Realized Ratio.

[Assistant] SELECT token_address, chain, pnl, realized_ratio FROM smart_money_token_performance WHERE pnl < 0 AND realized_ratio > 0

[User] Can you refine the results to only include addresses where the absolute value of PNL is greater than 1000?

[Assistant] SELECT token_address, chain, pnl, realized_ratio FROM smart_money_token_performance WHERE pnl < -1000 AND realized_ratio > 0


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE token_dexpool_info ( pair_address character varying(512) NOT NULL, token_name character varying(100) NOT NULL, token_symbol character varying(100) NOT NULL, chain text, price double precision, price_change_percentage_1h double precision, price_change_percentage_6h double precision, price_change_percentage_24h double precision, makers_24h double precision, volume_24h double precision, liquidity double precision, market_cap double precision, trend_rank bigint )

[User] Show me the top 5 tokens by 24-hour trading volume on the Ethereum chain, along with their price and 24-hour price change percentage.

[Assistant] SELECT token_name, token_symbol, price, price_change_percentage_24h, volume_24h FROM token_dexpool_info WHERE chain = 'Ethereum' ORDER BY volume_24h DESC LIMIT 5;

[User] Can you also include the market cap and liquidity for these tokens?

[Assistant] SELECT token_name, token_symbol, price, price_change_percentage_24h, volume_24h, market_cap, liquidity FROM token_dexpool_info WHERE chain = 'Ethereum' ORDER BY volume_24h DESC LIMIT 5;


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE smart_money_profit ( address text, rank numeric, profit numeric, roi numeric, realized_ratio numeric, most_profit_token text, last_trade_time text, chain text )

[User] For each chain, show the total number of smart money addresses and the count of addresses with positive profit.

[Assistant] SELECT chain, COUNT(address) AS total_addresses, COUNT(CASE WHEN profit > 0 THEN address END) AS positive_profit_addresses FROM smart_money_profit GROUP BY chain

[User] Filter the results to only include chains where the total number of addresses is greater than 100. Also, include the percentage of addresses with positive profit for each chain.

[Assistant] SELECT chain, COUNT(address) AS total_addresses, COUNT(CASE WHEN profit > 0 THEN address END) AS positive_profit_addresses, CAST(COUNT(CASE WHEN profit > 0 THEN address END) AS REAL) * 100 / COUNT(address) AS positive_profit_percentage FROM smart_money_profit GROUP BY chain HAVING COUNT(address) > 100


[User] Which are the top 5 smart money addresses with the highest ROI on the BSC chain, and what are their profit, realized ratio, and most profitable token?

[Assistant] SELECT address, profit, roi, realized_ratio, most_profit_token FROM smart_money_profit WHERE chain = 'BSC' ORDER BY roi DESC LIMIT 5;

[User] Can you also include their rank and last trade time in the output?

[Assistant] SELECT address, profit, roi, realized_ratio, most_profit_token, rank, last_trade_time FROM smart_money_profit WHERE chain = 'BSC' ORDER BY roi DESC LIMIT 5;

[User] I only want to see addresses with a profit above $10,000 and a realized ratio higher than 0.8.

[Assistant] SELECT address, profit, roi, realized_ratio, most_profit_token, rank, last_trade_time FROM smart_money_profit WHERE chain = 'BSC' AND profit > 10000 AND realized_ratio > 0.8 ORDER BY roi DESC LIMIT 5;

[User] Now, show the top one among these by profit instead of ROI.

[Assistant] SELECT address, profit, roi, realized_ratio, most_profit_token, rank, last_trade_time FROM smart_money_profit WHERE chain = 'BSC' AND profit > 10000 AND realized_ratio > 0.8 ORDER BY profit DESC LIMIT 1;


[User] List token symbols on any chain that have both a realized ratio above 0.8 and an ROI greater than 5. Show their symbol, chain, realized ratio, and ROI.

[Assistant] SELECT token_symbol, chain, realized_ratio, roi FROM smart_money_token_performance WHERE realized_ratio > 0.8 AND roi > 5

[User] Can you also include their PNL and Net Flow in the result?

[Assistant] SELECT token_symbol, chain, realized_ratio, roi, pnl, net_flow FROM smart_money_token_performance WHERE realized_ratio > 0.8 AND roi > 5


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE smart_money_profit ( address.text, rank.numeric, profit.numeric, roi.numeric, realized_ratio.numeric, most_profit_token.text, last_trade_time.text, chain.text )

[User] How many addresses have a realized_ratio below 0.5?

[Assistant] SELECT COUNT(*) AS address_count FROM smart_money_profit WHERE realized_ratio < 0.5;


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE insurance.policies ( policy_id character varying(64) NOT NULL, customer_id character varying(64) NOT NULL, product_id character varying(64) NOT NULL, agent_id character varying(64), policy_number character varying(100) NOT NULL UNIQUE, start_date date NOT NULL, end_date date NOT NULL, premium_amount numeric(12, 2) NOT NULL, coverage_limit numeric(15, 2) NOT NULL, deductible numeric(10, 2) NOT NULL, status character varying(30) NOT NULL, issued_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, cancellation_date date, cancellation_reason text, PRIMARY KEY (policy_id) )

[User] For each customer, find pairs of policies where one policy’s premium is at least $5000 higher than the other. Show customer_id, lower_premium_policy_id, higher_premium_policy_id, and the premium_difference.

[Assistant]
SELECT p1.customer_id,
       p1.policy_id AS lower_premium_policy_id,
       p2.policy_id AS higher_premium_policy_id,
       (p2.premium_amount - p1.premium_amount) AS premium_difference
FROM insurance.policies p1
JOIN insurance.policies p2
  ON p1.customer_id = p2.customer_id
  AND p2.premium_amount - p1.premium_amount >= 5000;

[User] Now only include those policy pairs where both policies are active.

[Assistant]
SELECT p1.customer_id,
       p1.policy_id AS lower_premium_policy_id,
       p2.policy_id AS higher_premium_policy_id,
       (p2.premium_amount - p1.premium_amount) AS premium_difference
FROM insurance.policies p1
JOIN insurance.policies p2
  ON p1.customer_id = p2.customer_id
  AND p2.premium_amount - p1.premium_amount >= 5000
WHERE p1.status = 'active'
  AND p2.status = 'active';


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE healthcare.prescriptions ( prescription_id.text, record_id.text, patient_id.text, doctor_id.text, medication_id.text, dosage.text, frequency.text, route.text, start_date.date, end_date.date, quantity_dispensed.numeric, refills_allowed.integer, prescription_date.timestamp )

[User] Find pairs of prescriptions for the same patient with overlapping periods. Show p1.prescription_id, p2.prescription_id, patient_id, and the overlap duration in days where the overlap is greater than 15 days.

[Assistant]
SELECT p1.prescription_id AS prescription_id_1,
       p2.prescription_id AS prescription_id_2,
       p1.patient_id,
       (LEAST(p1.end_date, p2.end_date) - GREATEST(p1.start_date, p2.start_date)) AS overlap_days
FROM healthcare.prescriptions p1
JOIN healthcare.prescriptions p2
  ON p1.patient_id = p2.patient_id
 AND p1.prescription_id < p2.prescription_id
WHERE LEAST(p1.end_date, p2.end_date) >= GREATEST(p1.start_date, p2.start_date)
  AND (LEAST(p1.end_date, p2.end_date) - GREATEST(p1.start_date, p2.start_date)) > 15;

[User] Actually, only consider pairs where both prescriptions allow refills.

[Assistant]
SELECT p1.prescription_id AS prescription_id_1,
       p2.prescription_id AS prescription_id_2,
       p1.patient_id,
       (LEAST(p1.end_date, p2.end_date) - GREATEST(p1.start_date, p2.start_date)) AS overlap_days
FROM healthcare.prescriptions p1
JOIN healthcare.prescriptions p2
  ON p1.patient_id = p2.patient_id
 AND p1.prescription_id < p2.prescription_id
WHERE LEAST(p1.end_date, p2.end_date) >= GREATEST(p1.start_date, p2.start_date)
  AND (LEAST(p1.end_date, p2.end_date) - GREATEST(p1.start_date, p2.start_date)) > 15
  AND p1.refills_allowed > 0
  AND p2.refills_allowed > 0;

[User] Also, limit the results to overlaps that start in 2024 and order by overlap_days descending.

[Assistant]
SELECT p1.prescription_id AS prescription_id_1,
       p2.prescription_id AS prescription_id_2,
       p1.patient_id,
       (LEAST(p1.end_date, p2.end_date) - GREATEST(p1.start_date, p2.start_date)) AS overlap_days
FROM healthcare.prescriptions p1
JOIN healthcare.prescriptions p2
  ON p1.patient_id = p2.patient_id
 AND p1.prescription_id < p2.prescription_id
WHERE LEAST(p1.end_date, p2.end_date) >= GREATEST(p1.start_date, p2.start_date)
  AND (LEAST(p1.end_date, p2.end_date) - GREATEST(p1.start_date, p2.start_date)) > 15
  AND p1.refills_allowed > 0
  AND p2.refills_allowed > 0
  AND GREATEST(p1.start_date, p2.start_date) >= '2024-01-01'
ORDER BY overlap_days DESC;


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE insurance.policies ( policy_id text PRIMARY KEY, customer_id text, product_id text, agent_id text, policy_number text, start_date date, end_date date, premium_amount numeric, coverage_limit numeric, deductible numeric, status text, issued_at timestamp, cancellation_date date, cancellation_reason text )

[User] List policy pairs for the same customer where one policy was cancelled and another active policy started within 30 days after the cancellation. Show customer_id, cancelled policy_number, cancellation_date, active policy_number, start_date.

[Assistant]
SELECT p1.customer_id,
       p1.policy_number AS cancelled_policy_number,
       p1.cancellation_date,
       p2.policy_number AS active_policy_number,
       p2.start_date
FROM insurance.policies p1
JOIN insurance.policies p2
  ON p1.customer_id = p2.customer_id
 AND p1.status = 'cancelled'
 AND p2.status = 'active'
 AND p2.start_date BETWEEN p1.cancellation_date AND p1.cancellation_date + INTERVAL '30 days';

[User] Also include the number of days between cancellation and new policy start as days_to_new.

[Assistant]
SELECT p1.customer_id,
       p1.policy_number AS cancelled_policy_number,
       p1.cancellation_date,
       p2.policy_number AS active_policy_number,
       p2.start_date,
       EXTRACT(DAY FROM (p2.start_date - p1.cancellation_date)) AS days_to_new
FROM insurance.policies p1
JOIN insurance.policies p2
  ON p1.customer_id = p2.customer_id
 AND p1.status = 'cancelled'
 AND p2.status = 'active'
 AND p2.start_date BETWEEN p1.cancellation_date AND p1.cancellation_date + INTERVAL '30 days';


[User] Show me all pairs of active policies for the same customer where their coverage periods overlap. Return customer_id, policy1_id, policy2_id, overlap_start, overlap_end.

[Assistant]
SELECT p1.customer_id,
       p1.policy_id AS policy1_id,
       p2.policy_id AS policy2_id,
       GREATEST(p1.start_date, p2.start_date) AS overlap_start,
       LEAST(p1.end_date, p2.end_date)   AS overlap_end
FROM insurance.policies p1
JOIN insurance.policies p2
  ON p1.customer_id = p2.customer_id
 AND p1.policy_id < p2.policy_id
 AND p1.status = 'active'
 AND p2.status = 'active'
 AND p1.start_date <= p2.end_date
 AND p2.start_date <= p1.end_date;

[User] I only want overlaps longer than 30 days and display the overlap days as overlap_days.

[Assistant]
SELECT p1.customer_id,
       p1.policy_id AS policy1_id,
       p2.policy_id AS policy2_id,
       GREATEST(p1.start_date, p2.start_date) AS overlap_start,
       LEAST(p1.end_date, p2.end_date)   AS overlap_end,
       (LEAST(p1.end_date, p2.end_date) - GREATEST(p1.start_date, p2.start_date)) AS overlap_days
FROM insurance.policies p1
JOIN insurance.policies p2
  ON p1.customer_id = p2.customer_id
 AND p1.policy_id < p2.policy_id
 AND p1.status = 'active'
 AND p2.status = 'active'
 AND p1.start_date <= p2.end_date
 AND p2.start_date <= p1.end_date
HAVING (LEAST(p1.end_date, p2.end_date) - GREATEST(p1.start_date, p2.start_date)) > 30;

[User] Order the results by overlap_days descending and limit to top 10.

[Assistant]
SELECT p1.customer_id,
       p1.policy_id AS policy1_id,
       p2.policy_id AS policy2_id,
       GREATEST(p1.start_date, p2.start_date) AS overlap_start,
       LEAST(p1.end_date, p2.end_date)   AS overlap_end,
       (LEAST(p1.end_date, p2.end_date) - GREATEST(p1.start_date, p2.start_date)) AS overlap_days
FROM insurance.policies p1
JOIN insurance.policies p2
  ON p1.customer_id = p2.customer_id
 AND p1.policy_id < p2.policy_id
 AND p1.status = 'active'
 AND p2.status = 'active'
 AND p1.start_date <= p2.end_date
 AND p2.start_date <= p1.end_date
GROUP BY p1.customer_id, p1.policy_id, p2.policy_id
HAVING (LEAST(p1.end_date, p2.end_date) - GREATEST(p1.start_date, p2.start_date)) > 30
ORDER BY overlap_days DESC
LIMIT 10;


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE transportation.passengers ( passenger_id character varying(64) NOT NULL, first_name character varying(128) NOT NULL, last_name character varying(128) NOT NULL, email character varying(128) UNIQUE, phone_number character varying(32) UNIQUE, date_of_birth date, loyalty_program_id character varying(64), created_at timestamp with time zone NOT NULL, PRIMARY KEY (passenger_id) )

[User] For each passenger, show their passenger_id, first_name, last_name, and the count of other passengers in the same loyalty_program_id.

[Assistant]
SELECT p1.passenger_id, p1.first_name, p1.last_name, COUNT(p2.passenger_id) AS peer_count
FROM transportation.passengers p1
LEFT JOIN transportation.passengers p2
  ON p1.loyalty_program_id = p2.loyalty_program_id
  AND p1.passenger_id <> p2.passenger_id
GROUP BY p1.passenger_id, p1.first_name, p1.last_name;

[User] Only include passengers who have more than 3 peers in their loyalty program.

[Assistant]
SELECT p1.passenger_id, p1.first_name, p1.last_name, COUNT(p2.passenger_id) AS peer_count
FROM transportation.passengers p1
LEFT JOIN transportation.passengers p2
  ON p1.loyalty_program_id = p2.loyalty_program_id
  AND p1.passenger_id <> p2.passenger_id
GROUP BY p1.passenger_id, p1.first_name, p1.last_name
HAVING COUNT(p2.passenger_id) > 3;

[User] Also include the loyalty_program_id in the results.

[Assistant]
SELECT p1.passenger_id, p1.first_name, p1.last_name, p1.loyalty_program_id, COUNT(p2.passenger_id) AS peer_count
FROM transportation.passengers p1
LEFT JOIN transportation.passengers p2
  ON p1.loyalty_program_id = p2.loyalty_program_id
  AND p1.passenger_id <> p2.passenger_id
GROUP BY p1.passenger_id, p1.first_name, p1.last_name, p1.loyalty_program_id
HAVING COUNT(p2.passenger_id) > 3;


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE insurance.insurance_products ( product_id text PRIMARY KEY, product_name text, product_type text, description text, base_premium numeric, coverage_details text, underwriting_rules text, is_active boolean, created_at timestamp, updated_at timestamp )

[User] List all pairs of insurance products that share the same product_type but have differing active statuses. Return both product_ids, the product_type, and each is_active flag.

[Assistant]
SELECT p1.product_id AS product_id_1, p2.product_id AS product_id_2, p1.product_type, p1.is_active AS is_active_1, p2.is_active AS is_active_2
FROM insurance.insurance_products p1
JOIN insurance.insurance_products p2
  ON p1.product_type = p2.product_type
  AND p1.is_active <> p2.is_active
WHERE p1.product_id < p2.product_id;


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE gaming_technology.players ( player_id uuid NOT NULL DEFAULT gen_random_uuid(), username character varying(100) NOT NULL UNIQUE, email character varying(255) NOT NULL UNIQUE, password_hash text NOT NULL, registration_date timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, last_login timestamp with time zone, country_code character(2), status character varying(20) NOT NULL DEFAULT 'active', profile_data jsonb, PRIMARY KEY (player_id) )

[User] Find pairs of players from the same country who have different last login dates. Show their usernames, country code, and last login dates. Exclude pairs where either last login date is null.

[Assistant] SELECT p1.username AS player1_username, p2.username AS player2_username, p1.country_code, p1.last_login AS player1_last_login, p2.last_login AS player2_last_login FROM gaming_technology.players p1 JOIN gaming_technology.players p2 ON p1.country_code = p2.country_code AND p1.player_id < p2.player_id WHERE p1.last_login IS NOT NULL AND p2.last_login IS NOT NULL AND p1.last_login != p2.last_login;

[User] Filter the results to only include pairs where the last login date difference is more than 30 days.

[Assistant] SELECT p1.username AS player1_username, p2.username AS player2_username, p1.country_code, p1.last_login AS player1_last_login, p2.last_login AS player2_last_login FROM gaming_technology.players p1 JOIN gaming_technology.players p2 ON p1.country_code = p2.country_code AND p1.player_id < p2.player_id WHERE p1.last_login IS NOT NULL AND p2.last_login IS NOT NULL AND p1.last_login != p2.last_login AND ABS(EXTRACT(EPOCH FROM (p1.last_login - p2.last_login))) > 30 * 86400;

[User] Exclude pairs where the country code is NULL.

[Assistant] SELECT p1.username AS player1_username, p2.username AS player2_username, p1.country_code, p1.last_login AS player1_last_login, p2.last_login AS player2_last_login FROM gaming_technology.players p1 JOIN gaming_technology.players p2 ON p1.country_code = p2.country_code AND p1.player_id < p2.player_id WHERE p1.last_login IS NOT NULL AND p2.last_login IS NOT NULL AND p1.last_login != p2.last_login AND ABS(EXTRACT(EPOCH FROM (p1.last_login - p2.last_login))) > 30 * 86400 AND p1.country_code IS NOT NULL;

[User] Order the results by the country code and then by the larger of the two last login dates in descending order. Limit to the top 10 results.

[Assistant] SELECT p1.username AS player1_username, p2.username AS player2_username, p1.country_code, p1.last_login AS player1_last_login, p2.last_login AS player2_last_login FROM gaming_technology.players p1 JOIN gaming_technology.players p2 ON p1.country_code = p2.country_code AND p1.player_id < p2.player_id WHERE p1.last_login IS NOT NULL AND p2.last_login IS NOT NULL AND p1.last_login != p2.last_login AND ABS(EXTRACT(EPOCH FROM (p1.last_login - p2.last_login))) > 30 * 86400 AND p1.country_code IS NOT NULL ORDER BY p1.country_code, GREATEST(p1.last_login, p2.last_login) DESC LIMIT 10;


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE hospitality_technology.payments (
    payment_id character varying(64) NOT NULL,  -- unique identifier for the payment transaction
    reservation_id character varying(64), -- FK to reservations (if payment is for a stay)
    guest_id character varying(64) NOT NULL, -- FK to guests (who paid)
    bill_id character varying(64), -- FK to bills/invoices this payment applies to
    amount numeric(12,2) NOT NULL, -- amount of the payment
    currency_code character varying(3) NOT NULL, -- ISO currency code (e.g., USD, EUR)
    payment_method character varying(50) NOT NULL, -- e.g., CreditCard, Cash, BankTransfer, Points, Voucher
    card_type character varying(50), -- e.g., Visa, MasterCard, Amex (if CC)
    card_last_four character varying(4), -- last 4 digits of CC (if CC)
    transaction_ref character varying(128), -- reference ID from payment gateway or processor
    payment_timestamp timestamp with time zone NOT NULL DEFAULT current_timestamp, -- when the payment occurred
    status character varying(50) NOT NULL, -- e.g., Pending, Succeeded, Failed, Refunded, Disputed
    staff_id_processed character varying(64), -- FK to staff (who processed the payment)
    notes text, -- notes about the payment
    PRIMARY KEY (payment_id),
    FOREIGN KEY (reservation_id) REFERENCES hospitality_technology.reservations(reservation_id),
    FOREIGN KEY (guest_id) REFERENCES hospitality_technology.guests(guest_id),
    FOREIGN KEY (staff_id_processed) REFERENCES hospitality_technology.staff(staff_id)
)

[User] List all reservations that have made multiple payment transactions. Return reservation_id and payment_id for each occurrence.

[Assistant]
SELECT DISTINCT p1.reservation_id, p1.payment_id
FROM hospitality_technology.payments p1
JOIN hospitality_technology.payments p2
  ON p1.reservation_id = p2.reservation_id
  AND p1.payment_id != p2.payment_id
ORDER BY p1.reservation_id, p1.payment_id;

[User] Now show only those reservation groups where at least one payment has status 'Failed'.

[Assistant]
SELECT DISTINCT p1.reservation_id, p1.payment_id
FROM hospitality_technology.payments p1
JOIN hospitality_technology.payments p2
  ON p1.reservation_id = p2.reservation_id
WHERE EXISTS (
    SELECT 1 FROM hospitality_technology.payments pm
    WHERE pm.reservation_id = p1.reservation_id AND pm.status = 'Failed'
)
ORDER BY p1.reservation_id, p1.payment_id;


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE insurance.premium_payments ( payment_id text PRIMARY KEY, policy_id text, customer_id text, payment_date timestamp, amount_paid numeric, payment_method text, transaction_reference text, due_date date, payment_period_start date, payment_period_end date, status text )

[User] Show the customer_id and pairs of payment_ids for any customers who have overlapping payment periods.

[Assistant]
SELECT p1.customer_id,
       p1.payment_id AS payment1_id,
       p2.payment_id AS payment2_id
FROM insurance.premium_payments p1
JOIN insurance.premium_payments p2
  ON p1.customer_id = p2.customer_id
 AND p1.payment_id < p2.payment_id
 AND p1.payment_period_start <= p2.payment_period_end
 AND p1.payment_period_end   >= p2.payment_period_start;


[User] List customers who have both a canceled policy and an active policy. Show customer_id, canceled_policy_id, and active_policy_id.

[Assistant]
SELECT p1.customer_id,
       p1.policy_id AS canceled_policy_id,
       p2.policy_id AS active_policy_id
FROM insurance.policies p1
JOIN insurance.policies p2
  ON p1.customer_id = p2.customer_id
WHERE p1.status = 'canceled'
  AND p2.status = 'active';

[User] Only include cases where the cancellation_date is within the last 30 days.

[Assistant]
SELECT p1.customer_id,
       p1.policy_id AS canceled_policy_id,
       p2.policy_id AS active_policy_id
FROM insurance.policies p1
JOIN insurance.policies p2
  ON p1.customer_id = p2.customer_id
WHERE p1.status = 'canceled'
  AND p2.status = 'active'
  AND p1.cancellation_date >= CURRENT_DATE - INTERVAL '30 days';


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE transportation.locations ( location_id.text, name.text, type.text, address_line1.text, city.text, state_province.text, postal_code.text, country.text, latitude.numeric, longitude.numeric, timezone.text )

[User] List pairs of distinct locations in the same city that share the same location type. Show both location IDs, names, city, and the shared type.

[Assistant] SELECT L1.location_id AS location1_id, L1.name AS location1_name, L2.location_id AS location2_id, L2.name AS location2_name, L1.city AS shared_city, L1.type AS shared_type FROM transportation.locations AS L1 JOIN transportation.locations AS L2 ON L1.city = L2.city AND L1.type = L2.type WHERE L1.location_id < L2.location_id;


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE transportation.drivers ( driver_id.text, employee_id.text, first_name.text, last_name.text, license_number.text, license_expiry_date.date, contact_number.text, email.text, hire_date.date, status.text )
CREATE TABLE transportation.passengers ( passenger_id.text, first_name.text, last_name.text, email.text, phone_number.text, date_of_birth.date, loyalty_program_id.text, created_at.timestamp )

[User] How many drivers and passengers have email addresses ending with 'company.com'? Return the count for each group.

[Assistant]
SELECT 'drivers' AS role, COUNT(*) AS count
FROM transportation.drivers
WHERE email LIKE '%@company.com'
UNION ALL
SELECT 'passengers' AS role, COUNT(*) AS count
FROM transportation.passengers
WHERE email LIKE '%@company.com';

[User] Could you refine it further and only include active drivers in the result?

[Assistant]
SELECT 'drivers' AS role, COUNT(*) AS count
FROM transportation.drivers
WHERE email LIKE '%@company.com' AND status = 'Active'
UNION ALL
SELECT 'passengers' AS role, COUNT(*) AS count
FROM transportation.passengers
WHERE email LIKE '%@company.com';


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE transportation.vehicles ( vehicle_id.text, license_plate.text, vin.text, make.text, model.text, year.integer, type.text, capacity.integer, fuel_type.text, status.text, depot_id.text, acquisition_date.date )
CREATE TABLE transportation.drivers ( driver_id.text, employee_id.text, first_name.text, last_name.text, license_number.text, license_expiry_date.date, contact_number.text, email.text, hire_date.date, status.text )
CREATE TABLE transportation.locations ( location_id.text, name.text, type.text, address_line1.text, city.text, state_province.text, postal_code.text, country.text, latitude.numeric, longitude.numeric, timezone.text )
CREATE TABLE transportation.fuel_logs ( fuel_log_id.text, vehicle_id.text, trip_id.text, log_timestamp.timestamp, fuel_type.text, quantity.numeric, unit.text, cost_per_unit.numeric, total_cost.numeric, odometer_reading.integer, location_id.text )
CREATE TABLE transportation.bookings ( booking_id.text, passenger_id.text, trip_id.text, seat_number.text, booking_time.timestamp, status.text, fare.numeric, payment_transaction_id.text, special_requests.text )

[User] Can you provide the total number of fuel refueling events and the sum of total_cost at each fueling location, including the location name and city?

[Assistant]
SELECT l.location_id, l.name, l.city, COUNT(fl.fuel_log_id) AS num_refuels, SUM(fl.total_cost) AS total_cost
FROM transportation.fuel_logs fl
JOIN transportation.locations l ON fl.location_id = l.location_id
GROUP BY l.location_id, l.name, l.city;


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE transportation.drivers ( driver_id text PRIMARY KEY, employee_id text, first_name text, last_name text, license_number text, license_expiry_date date, contact_number text, email text, hire_date date, status text )

[User] Find all pairs of drivers who share the same status. Include both driver_ids, full names, and the shared status.

[Assistant] SELECT d1.driver_id AS driver1_id, d1.first_name || ' ' || d1.last_name AS driver1_name, d2.driver_id AS driver2_id, d2.first_name || ' ' || d2.last_name AS driver2_name, d1.status FROM transportation.drivers d1 JOIN transportation.drivers d2 ON d1.status = d2.status AND d1.driver_id < d2.driver_id;


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE transportation.drivers ( driver_id text, employee_id text, first_name text, last_name text, license_number text, license_expiry_date date, contact_number text, email text, hire_date date, status text )

[User] I need a report showing the count of drivers per status.

[Assistant]
SELECT status, COUNT(*) AS driver_count
FROM transportation.drivers
GROUP BY status;

[User] Actually, only include statuses that have at least 2 drivers.

[Assistant]
SELECT status, COUNT(*) AS driver_count
FROM transportation.drivers
GROUP BY status
HAVING COUNT(*) >= 2;

[User] Also, order the results by driver_count descending.

[Assistant]
SELECT status, COUNT(*) AS driver_count
FROM transportation.drivers
GROUP BY status
HAVING COUNT(*) >= 2
ORDER BY driver_count DESC;

[User] Limit to the top 3 statuses.

[Assistant]
SELECT status, COUNT(*) AS driver_count
FROM transportation.drivers
GROUP BY status
HAVING COUNT(*) >= 2
ORDER BY driver_count DESC
LIMIT 3;


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE transportation.drivers ( driver_id text, employee_id text, first_name text, last_name text, license_number text, license_expiry_date date, contact_number text, email text, hire_date date, status text )
CREATE TABLE transportation.trips ( trip_id text, route_id text, vehicle_id text, driver_id text, scheduled_departure timestamp, actual_departure timestamp, scheduled_arrival timestamp, actual_arrival timestamp, origin_location_id text, destination_location_id text, status text, passenger_count integer, cargo_manifest_id text )
CREATE TABLE transportation.maintenance_logs ( log_id text, vehicle_id text, service_date date, odometer_reading integer, service_type text, description text, cost numeric, mechanic_name text, next_service_due_date date )
CREATE TABLE transportation.fuel_logs ( fuel_log_id text, vehicle_id text, trip_id text, log_timestamp timestamp, fuel_type text, quantity numeric, unit text, cost_per_unit numeric, total_cost numeric, odometer_reading integer, location_id text )
CREATE TABLE transportation.passengers ( passenger_id text, first_name text, last_name text, email text, phone_number text, date_of_birth date, loyalty_program_id text, created_at timestamp )

[User] For each fueling event, show the fuel_log_id, vehicle_id, the full name of the assigned driver for the trip during that fueling event, and the log_timestamp.

[Assistant]
SELECT fl.fuel_log_id, fl.vehicle_id, d.first_name || ' ' || d.last_name AS driver_full_name, fl.log_timestamp
FROM transportation.fuel_logs fl
JOIN transportation.trips t ON fl.trip_id = t.trip_id
JOIN transportation.drivers d ON t.driver_id = d.driver_id;


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE transportation.vehicles ( vehicle_id.text, license_plate.text, vin.text, make.text, model.text, year.integer, type.text, capacity.integer, fuel_type.text, status.text, depot_id.text, acquisition_date.date )
CREATE TABLE transportation.drivers ( driver_id.text, employee_id.text, first_name.text, last_name.text, license_number.text, license_expiry_date.date, contact_number.text, email.text, hire_date.date, status.text )
CREATE TABLE transportation.locations ( location_id.text, name.text, type.text, address_line1.text, city.text, state_province.text, postal_code.text, country.text, latitude.numeric, longitude.numeric, timezone.text )
CREATE TABLE transportation.maintenance_logs ( log_id.text, vehicle_id.text, service_date.date, odometer_reading.integer, service_type.text, description.text, cost.numeric, mechanic_name.text, next_service_due_date.date )
CREATE TABLE transportation.passengers ( passenger_id.text, first_name.text, last_name.text, email.text, phone_number.text, date_of_birth.date, loyalty_program_id.text, created_at.timestamp )

[User] Show me all vehicles that had maintenance in the past month, including their make, model, maintenance service_date, and depot name.

[Assistant]
SELECT v.vehicle_id,
       v.make,
       v.model,
       m.service_date,
       l.name AS depot_name
FROM transportation.maintenance_logs AS m
JOIN transportation.vehicles AS v
  ON m.vehicle_id = v.vehicle_id
LEFT JOIN transportation.locations AS l
  ON v.depot_id = l.location_id
WHERE m.service_date >= CURRENT_DATE - INTERVAL '1 month';

[User] Now, include only those records where the maintenance cost is greater than 500, and order the results by cost descending.

[Assistant]
SELECT v.vehicle_id,
       v.make,
       v.model,
       m.service_date,
       m.cost,
       l.name AS depot_name
FROM transportation.maintenance_logs AS m
JOIN transportation.vehicles AS v
  ON m.vehicle_id = v.vehicle_id
LEFT JOIN transportation.locations AS l
  ON v.depot_id = l.location_id
WHERE m.service_date >= CURRENT_DATE - INTERVAL '1 month'
  AND m.cost > 500
ORDER BY m.cost DESC;


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE transportation.routes ( route_id.text, route_number.text, description.text, origin_location_id.text, destination_location_id.text, distance_km.numeric, estimated_duration_minutes.integer, type.text )
CREATE TABLE transportation.trips ( trip_id.text, route_id.text, vehicle_id.text, driver_id.text, scheduled_departure.timestamp, actual_departure.timestamp, scheduled_arrival.timestamp, actual_arrival.timestamp, origin_location_id.text, destination_location_id.text, status.text, passenger_count.integer, cargo_manifest_id.text )
CREATE TABLE transportation.passengers ( passenger_id.text, first_name.text, last_name.text, email.text, phone_number.text, date_of_birth.date, loyalty_program_id.text, created_at.timestamp )

[User] List all trips with their ID, vehicle ID, and driver ID.

[Assistant] SELECT trip_id, vehicle_id, driver_id FROM transportation.trips;

[User] Can you also include the scheduled departure and actual arrival times?

[Assistant] SELECT trip_id, vehicle_id, driver_id, scheduled_departure, actual_arrival FROM transportation.trips;

[User] Filter this to only show trips with status 'Completed'.

[Assistant] SELECT trip_id, vehicle_id, driver_id, scheduled_departure, actual_arrival FROM transportation.trips WHERE status = 'Completed';

[User] Order the results by actual arrival time in descending order.

[Assistant] SELECT trip_id, vehicle_id, driver_id, scheduled_departure, actual_arrival FROM transportation.trips WHERE status = 'Completed' ORDER BY actual_arrival DESC;


System:
You are an expert in writing database language. You can write a corresponding database language query to answer the user's question based on the table structure described by the table creation statement.
##Table Structure
CREATE TABLE transportation.vehicles ( vehicle_id.text, license_plate.text, vin.text, make.text, model.text, year.integer, type.text, capacity.integer, fuel_type.text, status.text, depot_id.text, acquisition_date.date )
CREATE TABLE transportation.routes ( route_id.text, route_number.text, description.text, origin_location_id.text, destination_location_id.text, distance_km.numeric, estimated_duration_minutes.integer, type.text )
CREATE TABLE transportation.trips ( trip_id.text, route_id.text, vehicle_id.text, driver_id.text, scheduled_departure.timestamp, actual_departure.timestamp, scheduled_arrival.timestamp, actual_arrival.timestamp, origin_location_id.text, destination_location_id.text, status.text, passenger_count.integer, cargo_manifest_id.text )
CREATE TABLE transportation.fuel_logs ( fuel_log_id.text, vehicle_id.text, trip_id.text, log_timestamp.timestamp, fuel_type.text, quantity.numeric, unit.text, cost_per_unit.numeric, total_cost.numeric, odometer_reading.integer, location_id.text )
CREATE TABLE transportation.passengers ( passenger_id.text, first_name.text, last_name.text, email.text, phone_number.text, date_of_birth.date, loyalty_program_id.text, created_at.timestamp )

[User] Get the total fuel cost per route, showing route_number and the sum of total_cost for all fuel logs associated with trips on that route.

[Assistant]
SELECT r.route_number,
       SUM(fl.total_cost) AS total_fuel_cost
FROM transportation.routes r
JOIN transportation.trips t ON r.route_id = t.route_id
JOIN transportation.fuel_logs fl ON t.trip_id = fl.trip_id
GROUP BY r.route_number;


[User] Find pairs of drivers where the first driver was hired after the second driver, but the first driver's license expires before the second driver's. Show driver IDs, hire dates, and license expiry dates for both drivers in each pair.

[Assistant]
SELECT
  d1.driver_id AS driver1_id,
  d1.hire_date AS driver1_hire_date,
  d1.license_expiry_date AS driver1_license_expiry_date,
  d2.driver_id AS driver2_id,
  d2.hire_date AS driver2_hire_date,
  d2.license_expiry_date AS driver2_license_expiry_date
FROM transportation.drivers AS d1
JOIN transportation.drivers AS d2
  ON d1.driver_id <> d2.driver_id
WHERE
  d1.hire_date > d2.hire_date AND d1.license_expiry_date < d2.license_expiry_date
ORDER BY
  d1.hire_date, d2.hire_date;

[User] For those pairs, only show the ones where both drivers have an 'Active' status.

[Assistant]
SELECT
  d1.driver_id AS driver1_id,
  d1.hire_date AS driver1_hire_date,
  d1.license_expiry_date AS driver1_license_expiry_date,
  d2.driver_id AS driver2_id,
  d2.hire_date AS driver2_hire_date,
  d2.license_expiry_date AS driver2_license_expiry_date
FROM transportation.drivers AS d1
JOIN transportation.drivers AS d2
  ON d1.driver_id <> d2.driver_id
WHERE
  d1.hire_date > d2.hire_date AND d1.license_expiry_date < d2.license_expiry_date AND d1.status = 'Active' AND d2.status = 'Active'
ORDER BY
  d1.hire_date, d2.hire_date;