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_metas ( block_timestamp timestamp, block_number bigint, transaction_hash text, transaction_index bigint, from_address text, contract_address text, symbol text, total_supply text, name text, decimals bigint, is_erc20 integer, is_erc721 integer, is_erc1155 integer, is_proxy integer, extra_info text, pk integer )
CREATE TABLE erc20_balances ( block_number bigint, contract_address text, wallet_address text, amount text, pk text )
CREATE TABLE erc20_balances_historical ( block_number bigint, contract_address text, wallet_address text, amount text, pk integer )
CREATE TABLE nft_holders ( block_number bigint, contract_address text, wallet_address text, token_id text, contract_type text, amount text, pk text )
CREATE TABLE nft_holder_historical ( block_number bigint, contract_address text, wallet_address text, token_id text, contract_type text, amount text, pk integer )
CREATE TABLE sui_coins ( object_id text, status text, coin_type text, balance text, owner text )
CREATE TABLE sui_nfts ( object_id text, status text, collection_type text, package_id text, previous_transaction text, display text, owner text, name text )
CREATE TABLE nft_metadata ( blockchain text, contract_address text, token_id text, contract_name text, erc_type text, amount bigint, minter text, owner text, mint_block_number bigint, mint_time bigint, mint_transaction_hash text, mint_price text, token_uri text, metadata text, name text, image_uri text, traits text, updated_at bigint, status text, rarity_score real, rarity_rank integer, unique_attribute_count integer, pk integer )
CREATE TABLE nft_collections ( blockchain text, contract_address text, name text, symbol text, deploy_block_number bigint, deploy_block_time bigint, erc_type text, owner text, traits text, description text, slug text, media_info text, image_url text, banner_image_url text, updated_at bigint, banner text, token_count bigint, floor_price text, floor_price_symbol text, floor_price_contract text, pk integer )
CREATE TABLE token_prices_latest ( blockchain text, address text, decimals bigint, symbol text, price real, timestamp timestamp )
CREATE TABLE erc20_approval ( block_number bigint, block_timestamp timestamp, transaction_index integer, transaction_hash text, log_index integer, contract_address text, pk integer )
CREATE TABLE token_balances ( block_number bigint, block_timestamp timestamp, tick text, wallet_address text, total_supply decimal, amount decimal )
CREATE TABLE token_info ( block_timestamp timestamp, block_number bigint, tx_index integer, tx_hash text, tick text, max_supply decimal, lim decimal, wlim decimal, dec integer, creator text )
CREATE TABLE token_activities ( block_timestamp timestamp, block_number bigint, tx_index integer, tx_hash text, log_index integer, type text, tick text, id text, amt decimal, from_address text, to_address text )
CREATE TABLE mud_holesky_redstone ( address text, table_id text, key_bytes text, key0 text, key1 text, static_data text, encoded_lengths text, dynamic_data text, is_deleted boolean, block_number bigint, log_index bigint )
CREATE TABLE token_balances_bitcoin ( block_number bigint, block_timestamp timestamp, tick text, wallet_address text, total_supply text, amount text )
CREATE TABLE token_info_bitcoin ( block_timestamp timestamp, block_number bigint, tx_index integer, tx_hash text, tick text, max_supply decimal, lim decimal, wlim decimal, dec integer, creator text )
CREATE TABLE token_activities_bitcoin ( block_timestamp timestamp, block_number bigint, tx_index integer, tx_hash text, log_index integer, type text, tick text, id text, amt decimal, from_address text, to_address text )
CREATE TABLE token_activities_avalanche ( block_timestamp timestamp, block_number bigint, tx_index integer, tx_hash text, log_index integer, type text, tick text, id text, amt decimal, from_address text, to_address text )
CREATE TABLE token_balances_avalanche ( block_number bigint, block_timestamp timestamp, tick text, wallet_address text, total_supply decimal, amount decimal )
CREATE TABLE token_info_avalanche ( block_timestamp timestamp, block_number bigint, tx_index integer, tx_hash text, tick text, max_supply decimal, lim decimal, wlim decimal, dec integer, creator text, minted decimal, holders decimal, txs decimal, updated_timestamp timestamp, completed_timestamp timestamp, id text )

[User] List all wallet addresses that hold SUI coins with a balance greater than 1,000,000. Please show the coin_type as well.

[Assistant]
SELECT owner, coin_type, balance
FROM sui_coins
WHERE CAST(balance AS DECIMAL) > 1000000;

[User] Please add the coin symbol and name by joining with sui_coins_metadata, and only show coins that have 'Utility' in their description.

[Assistant]
SELECT sc.owner, sc.coin_type, sc.balance, scm.symbol, scm.name
FROM sui_coins sc
JOIN sui_coins_metadata scm ON sc.coin_type = scm.coin_type
WHERE CAST(sc.balance AS DECIMAL) > 1000000 AND scm.description LIKE '%Utility%';

[User] Show only coins with status 'active' and limit results to 10, sorted by balance descending.

[Assistant]
SELECT sc.owner, sc.coin_type, sc.balance, scm.symbol, scm.name
FROM sui_coins sc
JOIN sui_coins_metadata scm ON sc.coin_type = scm.coin_type
WHERE CAST(sc.balance AS DECIMAL) > 1000000 AND scm.description LIKE '%Utility%' AND sc.status = 'active'
ORDER BY CAST(sc.balance AS DECIMAL) DESC
LIMIT 10;

[User] Please add the coin description in the result as well.

[Assistant]
SELECT sc.owner, sc.coin_type, sc.balance, scm.symbol, scm.name, scm.description
FROM sui_coins sc
JOIN sui_coins_metadata scm ON sc.coin_type = scm.coin_type
WHERE CAST(sc.balance AS DECIMAL) > 1000000 AND scm.description LIKE '%Utility%' AND sc.status = 'active'
ORDER BY CAST(sc.balance AS DECIMAL) 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 social_media.users ( user_id text, username text, email text, password_hash text, full_name text, profile_picture_url text, bio text, location text, created_at timestamp, updated_at timestamp, is_verified boolean )
CREATE TABLE social_media.follows ( follow_id character varying(64) NOT NULL, follower_user_id character varying(64) NOT NULL, following_user_id character varying(64) NOT NULL, created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (follow_id), CONSTRAINT unique_follow UNIQUE (follower_user_id, following_user_id) )
CREATE TABLE social_media.messages (
    message_id character varying(64) NOT NULL, -- unique identifier for the message
    conversation_id character varying(64) NOT NULL, -- identifier for the conversation thread (FK to conversations.conversation_id)
    sender_user_id character varying(64) NOT NULL, -- identifier of the sender (FK to users.user_id)
    content text, -- textual content of the message
    media_url text, -- URL to attached media (image/video)
    media_type character varying(20), -- type of media ('image', 'video', null)
    sent_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, -- timestamp when message was sent
    read_at timestamp, -- timestamp when message was read by recipient(s)
    PRIMARY KEY (message_id)
)
CREATE TABLE social_media.conversation_participants ( participant_id.text, conversation_id.text, user_id.text, joined_at.timestamp, last_read_at.timestamp )

[User] Show me the usernames and email addresses of all users who are participants in conversations where user 'U333' has sent at least one message.

[Assistant]
SELECT DISTINCT u.username, u.email
FROM social_media.conversation_participants cp
JOIN social_media.messages m ON cp.conversation_id = m.conversation_id
JOIN social_media.users u ON cp.user_id = u.user_id
WHERE m.sender_user_id = 'U333';

[User] Exclude user 'U333' from the results.

[Assistant]
SELECT DISTINCT u.username, u.email
FROM social_media.conversation_participants cp
JOIN social_media.messages m ON cp.conversation_id = m.conversation_id
JOIN social_media.users u ON cp.user_id = u.user_id
WHERE m.sender_user_id = 'U333' AND u.user_id <> 'U333';

[User] Now only include participants who joined the conversation after March 1, 2024.

[Assistant]
SELECT DISTINCT u.username, u.email
FROM social_media.conversation_participants cp
JOIN social_media.messages m ON cp.conversation_id = m.conversation_id
JOIN social_media.users u ON cp.user_id = u.user_id
WHERE m.sender_user_id = 'U333' AND u.user_id <> 'U333'
  AND cp.joined_at > '2024-03-01';


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 erc20_balances ( block_number.bigint, contract_address.text, wallet_address.text, amount.text, pk.text )
CREATE TABLE nft_holders (
    block_number bigint NOT NULL, -- Block number containing the transaction
    contract_address varchar NOT NULL, -- Event contract address
    wallet_address varchar NOT NULL, -- The wallet address for the contract(with bloom filter)
    token_id varchar NOT NULL, -- NFT token id
    contract_type varchar NOT NULL, -- NFT contract type
    amount varchar NOT NULL, -- Number of held NFTs
    pk varchar NOT NULL  -- The last string partition of the contract.
)
CREATE TABLE sui_nfts ( object_id varchar, status varchar, collection_type varchar, package_id varchar, previous_transaction varchar, display varchar, owner varchar, name varchar )
CREATE TABLE token_prices ( uuid varchar, blockchain varchar, address varchar, decimals bigint, symbol varchar, price real, timestamp timestamp )
CREATE TABLE token_prices_latest (
    blockchain varchar(36) NOT NULL, -- blockchain fields for the functionType  in contract
    address varchar(42) NOT NULL, -- address fields for the functionType  in contract
    decimals bigint NOT NULL, -- decimals fields for the functionType  in contract
    symbol varchar(36) NOT NULL, -- symbol fields for the functionType  in contract
    price real NOT NULL, -- price fields for the functionType  in contract
    timestamp timestamp(6) NOT NULL  -- timestamp fields for the functionType  in contract
)
CREATE TABLE erc20_approval ( block_number bigint, block_timestamp timestamp, transaction_index integer, transaction_hash text, log_index integer, contract_address text, pk integer )
CREATE TABLE token_balances ( block_number bigint, block_timestamp timestamp, tick text, wallet_address text, total_supply decimal, amount decimal )
CREATE TABLE token_info (
    block_timestamp timestamp(3) NOT NULL, -- Timestamp of the block containing the inscription (matches block_timestamp in transactions table)
    block_number bigint NOT NULL, -- Block number containing the inscription (matches block_number in transactions table)
    tx_index integer NOT NULL, -- Index of the transaction containing the inscription (matches transaction_index in transactions table)
    tx_hash varchar(66) NOT NULL, -- Unique identifier of the transaction containing the inscription (matches hash in transactions table)
    tick varchar NOT NULL, -- Token tick
    max_supply decimal(38,0) NOT NULL, -- Max supply
    lim decimal(38,0) NOT NULL, -- Limit of each mint
    wlim decimal(38,0) NOT NULL, -- Limit of each address can maximum mint
    dec integer NOT NULL, -- Decimal for minimum divie
    creator varchar(42) NOT NULL  -- Address originating the inscription (matches from_address in transactions table)
)
CREATE TABLE token_info_bitcoin ( block_timestamp timestamp(3) NOT NULL, block_number bigint NOT NULL, tx_index integer NOT NULL, tx_hash varchar(66) NOT NULL, tick varchar(255) NOT NULL, max_supply decimal(38,0) NOT NULL, lim decimal(38,0) NOT NULL, wlim decimal(38,0) NOT NULL, dec integer NOT NULL, creator varchar(42) NOT NULL )
CREATE TABLE token_activities_avalanche ( block_timestamp timestamp(3) NOT NULL, block_number bigint NOT NULL, tx_index integer NOT NULL, tx_hash varchar(66) NOT NULL, log_index integer NOT NULL, type varchar(20) NOT NULL, tick varchar(255) NOT NULL, id varchar(255) NOT NULL, amt decimal(38,0) NOT NULL, from_address varchar(42) NOT NULL, to_address varchar(42) NOT NULL )
CREATE TABLE token_balances_avalanche ( block_number bigint, block_timestamp timestamp, tick text, wallet_address text, total_supply decimal, amount decimal )

[User] Find all wallets that hold at least one NFT and also hold ERC-20 tokens.

[Assistant]
SELECT DISTINCT nh.wallet_address
FROM nft_holders nh
JOIN erc20_balances eb
  ON nh.wallet_address = eb.wallet_address;

[User] Now show the count of distinct NFTs and count of distinct ERC-20 token contracts held by each such wallet.

[Assistant]
SELECT nh.wallet_address,
       COUNT(DISTINCT nh.contract_address || '-' || nh.token_id) AS nft_count,
       COUNT(DISTINCT eb.contract_address) AS erc20_count
FROM nft_holders nh
JOIN erc20_balances eb
  ON nh.wallet_address = eb.wallet_address
GROUP BY nh.wallet_address;

[User] Finally, only include wallets where erc20_count > 1 and nft_count > 2, and order by erc20_count descending.

[Assistant]
SELECT nh.wallet_address,
       COUNT(DISTINCT nh.contract_address || '-' || nh.token_id) AS nft_count,
       COUNT(DISTINCT eb.contract_address) AS erc20_count
FROM nft_holders nh
JOIN erc20_balances eb
  ON nh.wallet_address = eb.wallet_address
GROUP BY nh.wallet_address
HAVING COUNT(DISTINCT eb.contract_address) > 1
   AND COUNT(DISTINCT nh.contract_address || '-' || nh.token_id) > 2
ORDER BY erc20_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 gaming_technology.achievements ( achievement_id.uuid, game_id.uuid, name.text, description.text, icon_url.text, points.integer, hidden.boolean )

[User] Which players have unlocked any achievement?

[Assistant] SELECT DISTINCT player_id FROM player_achievements;

[User] Show me the player ID and the total number of points they've accumulated from all unlocked achievements.

[Assistant] SELECT pa.player_id, SUM(a.points) AS total_points FROM player_achievements pa JOIN achievements a ON pa.achievement_id = a.achievement_id GROUP BY pa.player_id;

[User] Can you filter this to only include players whose total points exceed 1000?

[Assistant] SELECT pa.player_id, SUM(a.points) AS total_points FROM player_achievements pa JOIN achievements a ON pa.achievement_id = a.achievement_id GROUP BY pa.player_id HAVING SUM(a.points) > 1000;


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.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 PRIMARY KEY, 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 int, num_children int, 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.staff ( staff_id text PRIMARY KEY, 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 int, manager_id text, created_at timestamp )
CREATE TABLE hospitality_technology.services ( service_id text PRIMARY KEY, property_id text, service_name text, description text, category text, price numeric, unit text, is_available boolean, department text )
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 )
CREATE TABLE hospitality_technology.reviews ( review_id text PRIMARY KEY, property_id text, reservation_id text, guest_id text, rating_overall int, rating_cleanliness int, rating_service int, rating_location int, rating_value int, comment text, review_source text, review_date timestamp, is_public boolean, staff_response text, response_staff_id text, response_date timestamp )

[User] List all future reservations (where check_in_date > current_date) including guest full name, room_id, property_id, and the number of days until check-in.

[Assistant]
SELECT r.reservation_id, r.property_id, r.room_id,
       g.first_name || ' ' || g.last_name AS guest_name,
       (r.check_in_date - CURRENT_DATE) AS days_until_check_in
FROM hospitality_technology.reservations r
JOIN hospitality_technology.guests g ON r.guest_id = g.guest_id
WHERE r.check_in_date > CURRENT_DATE;


System:
You are an expert in writing database language and you can refuse the user's question if the question is not related to the table structure. 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 train_station_management.train_cars ( car_id text PRIMARY KEY, train_id text, car_number text, car_type text, class text, capacity int, features text )

[User] What is the current operational status of the train car with car_id 'CAR123'?

[Assistant] I'm sorry, but I can't provide information on the current operational status of the train car with car_id 'CAR123'. The data in the train_cars table does not include real-time status updates or operational conditions.


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 web3.token_transfers ( transfer_id text PRIMARY KEY, token_id text, from_address text, to_address text, value numeric, transaction_id text, log_index int )

[User] For each recipient address, count how many token transfers they have received.

[Assistant] SELECT to_address, COUNT(*) AS received_transfer_count FROM web3.token_transfers GROUP BY to_address;

[User] Only show addresses that have received more than 100 transfers.

[Assistant] SELECT to_address, COUNT(*) AS received_transfer_count FROM web3.token_transfers GROUP BY to_address HAVING COUNT(*) > 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 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;


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 web3.tokens (
    token_id character varying(64) NOT NULL,  -- unique identifier for the token
    contract_address character varying(128) NOT NULL, -- address of the token contract
    token_name character varying(128) NOT NULL, -- name of the token
    token_symbol character varying(32) NOT NULL, -- symbol of the token
    decimals integer NOT NULL, -- number of decimals for the token
    total_supply numeric(38,14) NOT NULL, -- total supply of the token
    PRIMARY KEY (token_id)
)
CREATE TABLE web3.token_transfers (
    transfer_id character varying(64) NOT NULL,  -- unique identifier for the transfer
    token_id character varying(64) NOT NULL, -- identifier of the token being transferred
    from_address character varying(128) NOT NULL, -- address of the sender
    to_address character varying(128) NOT NULL, -- address of the receiver
    value numeric(38,14) NOT NULL, -- amount of tokens transferred
    transaction_id character varying(64) NOT NULL, -- identifier of the transaction containing the transfer
    log_index integer NOT NULL, -- index of the transfer log in the transaction
    PRIMARY KEY (transfer_id)
)
CREATE TABLE web3.wallets (
    wallet_id character varying(64) NOT NULL,  -- unique identifier for the wallet
    user_id character varying(64) NOT NULL, -- identifier of the user owning the wallet
    address character varying(128) NOT NULL, -- address of the wallet
    private_key text NOT NULL, -- private key of the wallet
    created_at timestamp NOT NULL, -- time when the wallet was created
    PRIMARY KEY (wallet_id)
)

[User] List each wallet that has received tokens of more than one distinct token type. Show wallet_id, address, the number of distinct tokens received as distinct_token_count, and the total received value, ordered by total_received_value descending.

[Assistant]
SELECT w.wallet_id,
       w.address,
       COUNT(DISTINCT t.token_id) AS distinct_token_count,
       SUM(tt.value)            AS total_received_value
FROM web3.wallets w
JOIN web3.token_transfers tt
  ON w.address = tt.to_address
JOIN web3.tokens t
  ON tt.token_id = t.token_id
GROUP BY w.wallet_id, w.address
HAVING COUNT(DISTINCT t.token_id) > 1
ORDER BY total_received_value 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.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 )

[User] Provide the monthly reservation count and total revenue for reservations made through 'Online Travel Agency' in 2023.

[Assistant]
SELECT DATE_TRUNC('month', check_in_date) AS month,
       COUNT(*) AS reservation_count,
       SUM(total_amount) AS total_revenue
FROM hospitality_technology.reservations
WHERE booking_source = 'Online Travel Agency'
  AND check_in_date BETWEEN '2023-01-01' AND '2023-12-31'
GROUP BY DATE_TRUNC('month', check_in_date)
ORDER BY month;

[User] Now show only those months where total_revenue exceeds 50000 and sort by total_revenue descending.

[Assistant]
SELECT month,
       reservation_count,
       total_revenue
FROM (
  SELECT DATE_TRUNC('month', check_in_date) AS month,
         COUNT(*) AS reservation_count,
         SUM(total_amount) AS total_revenue
  FROM hospitality_technology.reservations
  WHERE booking_source = 'Online Travel Agency'
    AND check_in_date BETWEEN '2023-01-01' AND '2023-12-31'
  GROUP BY DATE_TRUNC('month', check_in_date)
) t
WHERE total_revenue > 50000
ORDER BY total_revenue 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 gaming_technology.players (
    player_id uuid NOT NULL DEFAULT gen_random_uuid(), -- unique identifier for the player
    username character varying(100) NOT NULL UNIQUE, -- player's chosen display name
    email character varying(255) NOT NULL UNIQUE, -- player's email address for login/recovery
    password_hash text NOT NULL, -- securely hashed password
    registration_date timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, -- when the player account was created
    last_login timestamp with time zone, -- timestamp of the last successful login
    country_code character(2), -- player's country (ISO 3166-1 alpha-2)
    status character varying(20) NOT NULL DEFAULT 'active', -- account status (e.g., active, banned, suspended)
    profile_data jsonb, -- flexible field for avatar url, bio, preferences etc.
    PRIMARY KEY (player_id)
)
CREATE TABLE gaming_technology.games (
    game_id uuid NOT NULL DEFAULT gen_random_uuid(), -- unique identifier for the game
    title character varying(200) NOT NULL, -- official title of the game
    developer_id uuid NOT NULL, -- FK linking to the developers table
    publisher_id uuid, -- FK linking to a potential publishers table (optional)
    genre character varying(100), -- primary genre (e.g., RPG, FPS, Strategy)
    release_date date, -- official release date
    description text, -- short description or synopsis of the game
    platform character varying(50) NOT NULL, -- primary platform (e.g., PC, PlayStation 5, Xbox Series X, Mobile)
    engine character varying(100), -- game engine used (e.g., Unreal Engine 5, Unity)
    website character varying(255), -- official website URL
    PRIMARY KEY (game_id)
    -- FOREIGN KEY (developer_id) REFERENCES gaming_technology.developers(developer_id) -- Example FK constraint
)
CREATE TABLE gaming_technology.achievements (
    achievement_id uuid NOT NULL DEFAULT gen_random_uuid(), -- unique identifier for an achievement definition
    game_id uuid NOT NULL, -- FK linking to the game the achievement belongs to
    name character varying(150) NOT NULL, -- name of the achievement
    description text NOT NULL, -- how to unlock the achievement
    icon_url character varying(255), -- URL for the achievement's icon
    points integer DEFAULT 0, -- points awarded for unlocking (optional)
    hidden boolean DEFAULT false, -- whether the achievement details are hidden until unlocked
    PRIMARY KEY (achievement_id)
    -- FOREIGN KEY (game_id) REFERENCES gaming_technology.games(game_id)
)
CREATE TABLE gaming_technology.player_achievements (
    player_achievement_id uuid NOT NULL DEFAULT gen_random_uuid(), -- unique identifier for this unlock instance
    player_id uuid NOT NULL, -- FK linking to the player who unlocked it
    achievement_id uuid NOT NULL, -- FK linking to the achievement definition
    unlocked_at timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, -- when the achievement was unlocked
    PRIMARY KEY (player_achievement_id),
    UNIQUE (player_id, achievement_id) -- Ensure a player can only unlock an achievement once
    -- FOREIGN KEY (player_id) REFERENCES gaming_technology.players(player_id),
    -- FOREIGN KEY (achievement_id) REFERENCES gaming_technology.achievements(achievement_id)
)
CREATE TABLE gaming_technology.in_game_items (
    item_id uuid NOT NULL DEFAULT gen_random_uuid(), -- unique identifier for the item definition
    game_id uuid NOT NULL, -- FK linking to the game this item belongs to
    item_name character varying(150) NOT NULL, -- name of the item
    item_type character varying(50) NOT NULL, -- type (e.g., Cosmetic, Consumable, Weapon, Armor)
    description text, -- description of the item
    rarity character varying(30), -- item rarity (e.g., Common, Rare, Epic, Legendary)
    cost_currency character varying(10), -- currency code if purchasable (e.g., USD, EUR, GOLD)
    cost_amount numeric(12, 2), -- price if purchasable
    attributes jsonb, -- flexible field for item stats or properties
    PRIMARY KEY (item_id)
    -- FOREIGN KEY (game_id) REFERENCES gaming_technology.games(game_id)
)

[User] List all in-game items' names, types, and rarity along with their total cost in USD, for items that belong to games released on or after 2021-01-01.

[Assistant]
SELECT i.item_name, i.item_type, i.rarity, i.cost_amount
FROM gaming_technology.in_game_items i
JOIN gaming_technology.games g ON i.game_id = g.game_id
WHERE g.release_date >= '2021-01-01' AND i.cost_currency = 'USD'
ORDER BY i.item_name;

[User] Please add the game's title to the results.

[Assistant]
SELECT i.item_name, i.item_type, i.rarity, i.cost_amount, g.title AS game_title
FROM gaming_technology.in_game_items i
JOIN gaming_technology.games g ON i.game_id = g.game_id
WHERE g.release_date >= '2021-01-01' AND i.cost_currency = 'USD'
ORDER BY i.item_name;

[User] Now, show only items of rarity 'Epic' or 'Legendary'.

[Assistant]
SELECT i.item_name, i.item_type, i.rarity, i.cost_amount, g.title AS game_title
FROM gaming_technology.in_game_items i
JOIN gaming_technology.games g ON i.game_id = g.game_id
WHERE g.release_date >= '2021-01-01' AND i.cost_currency = 'USD' AND i.rarity IN ('Epic', 'Legendary')
ORDER BY i.item_name;

[User] I got an error because some items have null cost_amount. Can you exclude those from the results?

[Assistant]
SELECT i.item_name, i.item_type, i.rarity, i.cost_amount, g.title AS game_title
FROM gaming_technology.in_game_items i
JOIN gaming_technology.games g ON i.game_id = g.game_id
WHERE g.release_date >= '2021-01-01' AND i.cost_currency = 'USD' AND i.rarity IN ('Epic', 'Legendary') AND i.cost_amount IS NOT NULL
ORDER BY i.item_name;


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.services ( service_id.text, property_id.text, service_name.text, description.text, category.text, price.numeric, unit.text, is_available.boolean, department.text )

[User] Can you show me the total number of available services and the average price for each department?

[Assistant] SELECT department, COUNT(*) AS available_service_count, AVG(price) AS average_price FROM hospitality_technology.services WHERE is_available = true GROUP BY department;

[User] Now, include only departments where the average price is greater than 60 and sort by average_price descending.

[Assistant] SELECT department, COUNT(*) AS available_service_count, AVG(price) AS average_price FROM hospitality_technology.services WHERE is_available = true GROUP BY department HAVING AVG(price) > 60 ORDER BY average_price 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 government_services.applications ( application_id text, citizen_id text, service_id text, submitted_at timestamp, status text, last_updated_at timestamp, assigned_employee_id text, decision_date date, notes text, reference_number text )

[User] List the reference_number and status for all applications assigned to employee 'emp200', along with the submitted_at date of each application.

[Assistant] SELECT reference_number, status, submitted_at FROM government_services.applications WHERE assigned_employee_id = 'emp200';

[User] Show only those applications that have status 'Pending' and were submitted after January 1, 2024.

[Assistant] SELECT reference_number, status, submitted_at FROM government_services.applications WHERE assigned_employee_id = 'emp200' AND status = 'Pending' AND submitted_at > '2024-01-01';


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.fuel_logs (
    fuel_log_id character varying(64) NOT NULL,  -- unique identifier for the fuel log entry
    vehicle_id character varying(64) NOT NULL, -- identifier of the vehicle fueled
    trip_id character varying(64), -- optional: identifier of the trip during which fueling occurred
    log_timestamp timestamp with time zone NOT NULL, -- time when fueling occurred
    fuel_type character varying(32) NOT NULL, -- type of fuel added (matches vehicle's fuel type)
    quantity numeric(10, 3) NOT NULL, -- amount of fuel added (e.g., Liters, Gallons, kWh)
    unit character varying(16) NOT NULL, -- unit of quantity (e.g., L, gal, kWh)
    cost_per_unit numeric(8, 4), -- price per unit of fuel
    total_cost numeric(10, 2) NOT NULL, -- total cost of the fuel purchase
    odometer_reading integer, -- vehicle mileage at time of fueling
    location_id character varying(64), -- identifier of the fueling station/location
    PRIMARY KEY (fuel_log_id)
)

[User] For each fueling event, show its fuel_log_id, vehicle_id, log_timestamp, and the previous fueling event's fuel_log_id, log_timestamp, and quantity for the same vehicle.

[Assistant]
SELECT fl.fuel_log_id,
       fl.vehicle_id,
       fl.log_timestamp AS current_timestamp,
       fl.quantity AS current_quantity,
       prev.fuel_log_id AS previous_fuel_log_id,
       prev.log_timestamp AS previous_timestamp,
       prev.quantity AS previous_quantity
FROM transportation.fuel_logs fl
LEFT JOIN transportation.fuel_logs prev
  ON fl.vehicle_id = prev.vehicle_id
  AND prev.log_timestamp = (
    SELECT MAX(log_timestamp)
    FROM transportation.fuel_logs
    WHERE vehicle_id = fl.vehicle_id
      AND log_timestamp < fl.log_timestamp
  );


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 web3.events ( event_id text, contract_id text, event_name text, data text, emitted_at timestamp, transaction_id text, log_index integer )
CREATE TABLE web3.transactions ( transaction_id text, from_address text, to_address text, value numeric, gas_used integer, gas_price numeric, block_number integer, timestamp timestamp, input_data text, status text, nonce integer )
CREATE TABLE web3.tokens ( token_id text, contract_address text, token_name text, token_symbol text, decimals integer, total_supply numeric )
CREATE TABLE web3.token_transfers ( transfer_id text, token_id text, from_address text, to_address text, value numeric, transaction_id text, log_index integer )
CREATE TABLE web3.wallets ( wallet_id text, user_id text, address text, private_key text, created_at timestamp )

[User] For each wallet, show the wallet address and the total value of tokens they have received across all token transfers.

[Assistant]
SELECT w.address, COALESCE(SUM(tt.value), 0) AS total_received_value
FROM web3.wallets w
LEFT JOIN web3.token_transfers tt ON w.address = tt.to_address
GROUP BY w.address;


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 social_media.likes (
    like_id character varying(64) NOT NULL, -- unique identifier for the like
    user_id character varying(64) NOT NULL, -- identifier of the user who liked (FK to users.user_id)
    post_id character varying(64), -- identifier of the post being liked (FK to posts.post_id, nullable)
    comment_id character varying(64), -- identifier of the comment being liked (FK to comments.comment_id, nullable)
    created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, -- timestamp of when the like occurred
    PRIMARY KEY (like_id),
    CONSTRAINT unique_user_like UNIQUE (user_id, post_id, comment_id), -- prevent duplicate likes
    CONSTRAINT check_like_target CHECK (post_id IS NOT NULL OR comment_id IS NOT NULL) -- ensure it likes something
)
CREATE TABLE social_media.conversations (
    conversation_id character varying(64) NOT NULL, -- unique identifier for the conversation
    created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, -- timestamp when conversation was initiated
    last_message_at timestamp, -- timestamp of the last message in the conversation
    is_group_chat boolean NOT NULL DEFAULT false, -- indicates if it's a group chat or direct message
    group_name character varying(100), -- name of the group chat (if applicable)
    PRIMARY KEY (conversation_id)
)
CREATE TABLE social_media.notifications (
    notification_id character varying(64) NOT NULL, -- unique identifier for the notification
    recipient_user_id character varying(64) NOT NULL, -- user receiving the notification (FK to users.user_id)
    sender_user_id character varying(64), -- user triggering the notification (FK to users.user_id, can be null for system notifications)
    notification_type character varying(30) NOT NULL, -- type of notification ('like', 'comment', 'follow', 'mention', 'message')
    target_entity_id character varying(64), -- ID of the related entity (post_id, comment_id, user_id, conversation_id)
    target_entity_type character varying(20), -- type of the related entity ('post', 'comment', 'user', 'conversation')
    created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, -- timestamp notification was created
    read_at timestamp, -- timestamp notification was marked as read
    PRIMARY KEY (notification_id)
)

[User] Which users have received both a comment notification and a like notification? List the recipient_user_id and the total number of such users.

[Assistant]
SELECT COUNT(DISTINCT recipient_user_id) AS user_count
FROM (
  SELECT recipient_user_id
  FROM social_media.notifications
  WHERE notification_type = 'comment'
  INTERSECT
  SELECT recipient_user_id
  FROM social_media.notifications
  WHERE notification_type = 'like'
) AS users_with_both;


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] List customers who have more than one policy. Show the customer ID and the count of policies.

[Assistant] SELECT customer_id, COUNT(*) AS policy_count FROM insurance.policies GROUP BY customer_id HAVING COUNT(*) > 1;

[User] For these customers, list their policy numbers and status.

[Assistant] SELECT T1.customer_id, T1.policy_number, T1.status FROM insurance.policies AS T1 INNER JOIN ( SELECT customer_id FROM insurance.policies GROUP BY customer_id HAVING COUNT(*) > 1 ) AS T2 ON T1.customer_id = T2.customer_id;

[User] Actually, only show policy numbers and statuses for policies where the status is 'Active' or 'Expired'.

[Assistant] SELECT T1.customer_id, T1.policy_number, T1.status FROM insurance.policies AS T1 INNER JOIN ( SELECT customer_id FROM insurance.policies GROUP BY customer_id HAVING COUNT(*) > 1 ) AS T2 ON T1.customer_id = T2.customer_id WHERE T1.status IN ('Active', 'Expired');


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, username.text, email.text, password_hash.text, registration_date.timestamptz, last_login.timestamptz, country_code.text, status.text, profile_data.jsonb )
CREATE TABLE gaming_technology.games ( game_id.uuid, title.text, developer_id.uuid, publisher_id.uuid, genre.text, release_date.date, description.text, platform.text, engine.text, website.text )
CREATE TABLE gaming_technology.game_sessions ( session_id.uuid, player_id.uuid, game_id.uuid, server_id.uuid, start_time.timestamptz, end_time.timestamptz, duration_seconds.integer, score.integer, session_data.jsonb )
CREATE TABLE gaming_technology.servers ( server_id.uuid, game_id.uuid, server_name.text, region.text, ip_address.text, port.integer, current_players.integer, max_players.integer, status.text, last_heartbeat.timestamptz )
CREATE TABLE gaming_technology.in_game_items ( item_id.uuid, game_id.uuid, item_name.text, item_type.text, description.text, rarity.text, cost_currency.text, cost_amount.numeric, attributes.jsonb )
CREATE TABLE gaming_technology.player_inventory ( inventory_id.uuid, player_id.uuid, item_id.uuid, quantity.integer, acquired_at.timestamptz, instance_data.jsonb )

[User] Which servers are currently 'online' and located in the 'us-west-2' region, and what is the title of the game each server is hosting?

[Assistant]
SELECT s.server_id, s.server_name, s.region, g.title AS game_title
FROM gaming_technology.servers s
JOIN gaming_technology.games g ON s.game_id = g.game_id
WHERE s.status = 'online' AND s.region = 'us-west-2';

[User] Can you also include each server's current_players count and the game's genre in the result?

[Assistant]
SELECT s.server_id, s.server_name, s.region, s.current_players, g.title AS game_title, g.genre
FROM gaming_technology.servers s
JOIN gaming_technology.games g ON s.game_id = g.game_id
WHERE s.status = 'online' AND s.region = 'us-west-2';


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.claims ( claim_id text PRIMARY KEY, policy_id text, customer_id text, incident_date timestamp, reported_date timestamp, description text, estimated_loss numeric, status text, settlement_amount numeric, settlement_date date, claim_handler_id text, police_report_number text )
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 )
CREATE TABLE insurance.claim_documents ( document_id text PRIMARY KEY, claim_id text, document_type text, file_name text, file_path text, file_size_bytes int, mime_type text, uploaded_by text, uploaded_at timestamp, description text )

[User] List all active insurance products, including their product name, type, and base premium.

[Assistant] SELECT product_name, product_type, base_premium FROM insurance.insurance_products WHERE is_active = TRUE;


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.medications (
    medication_id character varying(64) NOT NULL, -- unique identifier for the medication type
    medication_name character varying(255) NOT NULL, -- common or brand name
    generic_name character varying(255), -- generic chemical name
    manufacturer character varying(100), -- drug manufacturer
    ndc_code character varying(20) UNIQUE, -- National Drug Code (optional but useful)
    description text, -- description of the medication
    PRIMARY KEY (medication_id)
)

[User] List the medication_id, medication_name, and description for all medications whose generic_name starts with 'A'.

[Assistant]
SELECT medication_id, medication_name, description
FROM healthcare.medications
WHERE generic_name LIKE 'A%';


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.customers ( customer_id.text, first_name.text, last_name.text, date_of_birth.date, ssn_hash.text, address_line1.text, address_line2.text, city.text, state.text, zip_code.text, country.text, phone_number.text, email.text, created_at.timestamp, updated_at.timestamp, status.text )

[User] List the first and last names, email, and phone number of all customers whose status is not 'Active'.

[Assistant]
SELECT first_name, last_name, email, phone_number
FROM insurance.customers
WHERE status <> 'Active';

[User] Now, only show those who live in the state of California.

[Assistant]
SELECT first_name, last_name, email, phone_number
FROM insurance.customers
WHERE status <> 'Active' AND state = 'CA';

[User] I got some records where the phone_number is missing. Please exclude customers with a NULL phone number.

[Assistant]
SELECT first_name, last_name, email, phone_number
FROM insurance.customers
WHERE status <> 'Active'
  AND state = 'CA'
  AND phone_number IS NOT NULL;

[User] Finally, sort the results by last_name ascending and limit the output to 20 records.

[Assistant]
SELECT first_name, last_name, email, phone_number
FROM insurance.customers
WHERE status <> 'Active'
  AND state = 'CA'
  AND phone_number IS NOT NULL
ORDER BY last_name ASC
LIMIT 20;


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) )
CREATE TABLE gaming_technology.games ( game_id uuid PRIMARY KEY, title text, developer_id uuid, publisher_id uuid, genre text, release_date date, description text, platform text, engine text, website text )
CREATE TABLE gaming_technology.game_sessions ( session_id uuid NOT NULL DEFAULT gen_random_uuid(), player_id uuid NOT NULL, game_id uuid NOT NULL, server_id uuid, start_time timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, end_time timestamp with time zone, duration_seconds integer, score integer, session_data jsonb, PRIMARY KEY (session_id) )
CREATE TABLE gaming_technology.servers ( server_id uuid PRIMARY KEY, game_id uuid, server_name text, region text, ip_address text, port int, current_players int, max_players int, status text, last_heartbeat timestamptz )
CREATE TABLE gaming_technology.achievements ( achievement_id.uuid, game_id.uuid, name.text, description.text, icon_url.text, points.integer, hidden.boolean )
CREATE TABLE gaming_technology.player_achievements ( player_achievement_id uuid PRIMARY KEY, player_id uuid, achievement_id uuid, unlocked_at timestamptz )
CREATE TABLE gaming_technology.player_inventory ( inventory_id uuid PRIMARY KEY, player_id uuid, item_id uuid, quantity int, acquired_at timestamptz, instance_data jsonb )

[User] Show each player's username and the number of distinct games they have played sessions on.

[Assistant]
SELECT
  p.username,
  COUNT(DISTINCT gs.game_id) AS games_played
FROM gaming_technology.players p
JOIN gaming_technology.game_sessions gs
  ON p.player_id = gs.player_id
GROUP BY p.username;

[User] Now include each player's total number of achievements unlocked.

[Assistant]
SELECT
  p.username,
  COUNT(DISTINCT gs.game_id) AS games_played,
  COUNT(pa.achievement_id)      AS achievements_unlocked
FROM gaming_technology.players p
JOIN gaming_technology.game_sessions gs
  ON p.player_id = gs.player_id
LEFT JOIN gaming_technology.player_achievements pa
  ON p.player_id = pa.player_id
GROUP BY p.username;

[User] Exclude players who have played fewer than 3 games or have not unlocked any achievements.

[Assistant]
SELECT
  p.username,
  COUNT(DISTINCT gs.game_id) AS games_played,
  COUNT(pa.achievement_id)      AS achievements_unlocked
FROM gaming_technology.players p
JOIN gaming_technology.game_sessions gs
  ON p.player_id = gs.player_id
LEFT JOIN gaming_technology.player_achievements pa
  ON p.player_id = pa.player_id
GROUP BY p.username
HAVING COUNT(DISTINCT gs.game_id) >= 3
   AND COUNT(pa.achievement_id) > 0;

[User] Also include the first session start_time as first_play and the first achievement unlocked date as first_achievement.

[Assistant]
SELECT
  p.username,
  COUNT(DISTINCT gs.game_id)      AS games_played,
  COUNT(pa.achievement_id)         AS achievements_unlocked,
  MIN(gs.start_time)               AS first_play,
  MIN(pa.unlocked_at)              AS first_achievement
FROM gaming_technology.players p
JOIN gaming_technology.game_sessions gs
  ON p.player_id = gs.player_id
LEFT JOIN gaming_technology.player_achievements pa
  ON p.player_id = pa.player_id
GROUP BY p.username
HAVING COUNT(DISTINCT gs.game_id) >= 3
   AND COUNT(pa.achievement_id) > 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 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 gaming_technology.game_sessions ( session_id.uuid, player_id.uuid, game_id.uuid, server_id.uuid, start_time.timestamptz, end_time.timestamptz, duration_seconds.integer, score.integer, session_data.jsonb )
CREATE TABLE gaming_technology.servers ( server_id.uuid, game_id.uuid, server_name.text, region.text, ip_address.text, port.integer, current_players.integer, max_players.integer, status.text, last_heartbeat.timestamptz )
CREATE TABLE gaming_technology.achievements ( achievement_id.uuid, game_id.uuid, name.text, description.text, icon_url.text, points.integer, hidden.boolean )
CREATE TABLE gaming_technology.in_game_items ( item_id.uuid, game_id.uuid, item_name.text, item_type.text, description.text, rarity.text, cost_currency.text, cost_amount.numeric, attributes.jsonb )
CREATE TABLE gaming_technology.player_inventory ( inventory_id.uuid, player_id.uuid, item_id.uuid, quantity.integer, acquired_at.timestamptz, instance_data.jsonb )

[User] Show all in-game items (name, rarity) that belong to games where the average session score across all sessions for that game is greater than 300.

[Assistant] SELECT ii.item_name, ii.rarity FROM gaming_technology.in_game_items AS ii WHERE ii.game_id IN (SELECT game_id FROM gaming_technology.game_sessions GROUP BY game_id HAVING AVG(score) > 300);

[User] Additionally, include the total number of sessions for each of these items' games.

[Assistant] SELECT ii.item_name, ii.rarity, gs.total_sessions FROM gaming_technology.in_game_items AS ii JOIN (SELECT game_id, COUNT(*) AS total_sessions, AVG(score) AS avg_score FROM gaming_technology.game_sessions GROUP BY game_id) AS gs ON ii.game_id = gs.game_id WHERE gs.avg_score > 300;


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 erc1155_approval_for_all ( block_number bigint, block_timestamp timestamp, transaction_index integer, transaction_hash text, log_index integer, contract_address text )
CREATE TABLE erc20_balances ( block_number bigint, contract_address text, wallet_address text, amount text, pk text )
CREATE TABLE erc20_balances_historical ( block_number bigint, contract_address text, wallet_address text, amount text, pk integer )
CREATE TABLE nft_holders ( block_number bigint, contract_address text, wallet_address text, token_id text, contract_type text, amount text, pk text )
CREATE TABLE sui_coins ( object_id text, status text, coin_type text, balance text, owner text )
CREATE TABLE sui_nfts ( object_id text, status text, collection_type text, package_id text, previous_transaction text, display text, owner text, name text )
CREATE TABLE nft_metadata ( blockchain text, contract_address text, token_id text, contract_name text, erc_type text, amount bigint, minter text, owner text, mint_block_number bigint, mint_time bigint, mint_transaction_hash text, mint_price text, token_uri text, metadata text, name text, image_uri text, traits text, updated_at bigint, status text, rarity_score real, rarity_rank integer, unique_attribute_count integer, pk integer )
CREATE TABLE token_prices ( uuid text, blockchain text, address text, decimals bigint, symbol text, price real, timestamp timestamp )
CREATE TABLE token_prices_latest ( blockchain text, address text, decimals bigint, symbol text, price real, timestamp timestamp )
CREATE TABLE token_balances ( block_number bigint, block_timestamp timestamp, tick text, wallet_address text, total_supply decimal, amount decimal )
CREATE TABLE token_info ( block_timestamp timestamp, block_number bigint, tx_index integer, tx_hash text, tick text, max_supply decimal, lim decimal, wlim decimal, dec integer, creator text )
CREATE TABLE mud_holesky_redstone ( address text, table_id text, key_bytes text, key0 text, key1 text, static_data text, encoded_lengths text, dynamic_data text, is_deleted boolean, block_number bigint, log_index bigint )
CREATE TABLE token_info_avalanche ( block_timestamp timestamp, block_number bigint, tx_index integer, tx_hash text, tick text, max_supply decimal, lim decimal, wlim decimal, dec integer, creator text, minted decimal, holders decimal, txs decimal, updated_timestamp timestamp, completed_timestamp timestamp, id text )

[User] Show me all wallet addresses that hold more than 3 different NFTs from any ERC1155 contract. For each wallet, list the contract address and the count of distinct token_ids.

[Assistant]
SELECT contract_address, wallet_address, COUNT(DISTINCT token_id) AS token_id_count
FROM nft_holders
WHERE contract_type = 'ERC1155'
GROUP BY contract_address, wallet_address
HAVING COUNT(DISTINCT token_id) > 3;

[User] Can you also tell me the total amount of NFTs held by each of these wallets for the contracts they qualify for?

[Assistant]
SELECT contract_address, wallet_address, COUNT(DISTINCT token_id) AS token_id_count, SUM(CAST(amount AS numeric)) AS total_amount
FROM nft_holders
WHERE contract_type = 'ERC1155'
GROUP BY contract_address, wallet_address
HAVING COUNT(DISTINCT token_id) > 3;

[User] Actually, only include results for contracts where the total number of NFTs held by all wallets is over 1000. Show the contract address in the output.

[Assistant]
WITH contract_totals AS (
  SELECT contract_address, SUM(CAST(amount AS numeric)) AS contract_total_amount
  FROM nft_holders
  WHERE contract_type = 'ERC1155'
  GROUP BY contract_address
  HAVING SUM(CAST(amount AS numeric)) > 1000
)
SELECT n.contract_address, n.wallet_address, COUNT(DISTINCT n.token_id) AS token_id_count, SUM(CAST(n.amount AS numeric)) AS total_amount
FROM nft_holders n
JOIN contract_totals c ON n.contract_address = c.contract_address
WHERE n.contract_type = 'ERC1155'
GROUP BY n.contract_address, n.wallet_address
HAVING COUNT(DISTINCT n.token_id) > 3;

[User] Also include the name of the NFT for each token_id if available from nft_metadata. Only show NFTs on the Ethereum blockchain.

[Assistant]
WITH contract_totals AS (
  SELECT contract_address, SUM(CAST(amount AS numeric)) AS contract_total_amount
  FROM nft_holders
  WHERE contract_type = 'ERC1155'
  GROUP BY contract_address
  HAVING SUM(CAST(amount AS numeric)) > 1000
),
wallet_tokens AS (
  SELECT n.contract_address, n.wallet_address, n.token_id, SUM(CAST(n.amount AS numeric)) AS amount
  FROM nft_holders n
  JOIN contract_totals c ON n.contract_address = c.contract_address
  WHERE n.contract_type = 'ERC1155'
  GROUP BY n.contract_address, n.wallet_address, n.token_id
)
SELECT w.contract_address, w.wallet_address, w.token_id, w.amount, m.name AS nft_name
FROM wallet_tokens w
LEFT JOIN nft_metadata m
  ON m.blockchain = 'Ethereum' AND m.contract_address = w.contract_address AND m.token_id = w.token_id
WHERE w.amount > 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 legal_services.clients (
    client_id character varying(64) NOT NULL,  -- unique identifier for the client
    first_name character varying(100) NOT NULL, -- client's first name
    last_name character varying(100) NOT NULL, -- client's last name
    company_name character varying(200), -- company name if applicable
    email character varying(128) UNIQUE NOT NULL, -- client's primary email
    phone character varying(32), -- client's contact phone number
    address text, -- client's full address
    date_registered timestamp NOT NULL, -- when the client was first registered
    status character varying(32) NOT NULL, -- client status (e.g., active, inactive, prospect)
    PRIMARY KEY (client_id)
)
CREATE TABLE legal_services.law_firms (
    firm_id character varying(64) NOT NULL,  -- unique identifier for the law firm
    firm_name character varying(200) NOT NULL, -- official name of the law firm
    address text NOT NULL, -- primary address of the firm
    phone character varying(32), -- firm's main phone number
    website character varying(255), -- firm's website URL
    founded_date date, -- date the firm was established
    managing_partner_id character varying(64), -- FK to lawyers table (optional)
    PRIMARY KEY (firm_id)
)
CREATE TABLE legal_services.cases (
    case_id character varying(64) NOT NULL,  -- unique identifier for the case/matter
    case_number character varying(100) UNIQUE, -- official case number (court or internal)
    case_name text NOT NULL, -- descriptive name or title for the case
    client_id character varying(64) NOT NULL, -- FK to clients table
    lead_lawyer_id character varying(64) NOT NULL, -- FK to lawyers table (primary lawyer responsible)
    case_type character varying(100) NOT NULL, -- e.g., Litigation, Corporate, Family Law, IP
    status character varying(50) NOT NULL, -- e.g., Open, Closed, Pending, On Hold, Appealed
    date_opened timestamp NOT NULL, -- date the case was formally opened
    date_closed timestamp, -- date the case was closed (nullable)
    description text, -- detailed description of the case
    court_jurisdiction character varying(128), -- court or jurisdiction if applicable
    PRIMARY KEY (case_id)
)
CREATE TABLE legal_services.documents (
    document_id character varying(64) NOT NULL,  -- unique identifier for the document
    case_id character varying(64) NOT NULL, -- FK to cases table
    document_name character varying(255) NOT NULL, -- name of the document file
    document_type character varying(100), -- e.g., Pleading, Contract, Evidence, Correspondence
    storage_path text NOT NULL, -- path or reference to the document storage location
    uploaded_by_lawyer_id character varying(64), -- FK to lawyers table (who uploaded it)
    upload_timestamp timestamp NOT NULL, -- time the document was uploaded
    version integer DEFAULT 1, -- document version number
    description text, -- brief description or notes about the document
    PRIMARY KEY (document_id)
)
CREATE TABLE legal_services.time_entries (
    entry_id character varying(64) NOT NULL,  -- unique identifier for the time entry
    case_id character varying(64) NOT NULL, -- FK to cases table
    lawyer_id character varying(64) NOT NULL, -- FK to lawyers table (who performed the work)
    date_worked date NOT NULL, -- date the work was performed
    hours_spent numeric(5, 2) NOT NULL CHECK (hours_spent > 0), -- duration of work in hours (e.g., 1.5 for 1h 30m)
    billing_rate numeric(10, 2) NOT NULL, -- hourly rate applied for this entry
    description text NOT NULL, -- detailed description of the work performed
    is_billable boolean DEFAULT true, -- whether this time entry is billable to the client
    is_billed boolean DEFAULT false, -- whether this entry has been included in an invoice
    invoice_id character varying(64), -- FK to invoices table (once billed)
    created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, -- when the entry was recorded
    PRIMARY KEY (entry_id)
)
CREATE TABLE legal_services.appointments (
    appointment_id character varying(64) NOT NULL, -- unique identifier for the appointment
    client_id character varying(64) NOT NULL, -- FK to clients table
    lawyer_id character varying(64) NOT NULL, -- FK to lawyers table
    case_id character varying(64), -- FK to cases table (optional, if related to a specific case)
    start_time timestamp NOT NULL, -- scheduled start date and time
    end_time timestamp NOT NULL, -- scheduled end date and time
    location text, -- location of the meeting (address or virtual link)
    subject character varying(255), -- subject or purpose of the appointment
    notes text, -- additional notes for the appointment
    status character varying(50) NOT NULL, -- e.g., Scheduled, Completed, Canceled, Rescheduled
    created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (appointment_id)
)

[User] List all documents uploaded for cases handled in the 'District Court' jurisdiction, showing the document name, case number, and upload timestamp.

[Assistant]
SELECT d.document_name, cs.case_number, d.upload_timestamp
FROM legal_services.documents d
JOIN legal_services.cases cs ON d.case_id = cs.case_id
WHERE cs.court_jurisdiction = 'District Court';


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, 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] Display each product alongside the most recently updated product of the same type. Return original_id, original_updated_at, recent_id, recent_updated_at, and product_type.

[Assistant] SELECT p.product_id AS original_id, p.updated_at AS original_updated_at, r.product_id AS recent_id, r.updated_at AS recent_updated_at, p.product_type FROM insurance.insurance_products p JOIN (SELECT product_type, MAX(updated_at) AS max_updated FROM insurance.insurance_products GROUP BY product_type) m ON p.product_type = m.product_type JOIN insurance.insurance_products r ON r.product_type = p.product_type AND r.updated_at = m.max_updated;

[User] Actually, exclude entries where original or recent updated_at is null.

[Assistant] SELECT p.product_id AS original_id, p.updated_at AS original_updated_at, r.product_id AS recent_id, r.updated_at AS recent_updated_at, p.product_type FROM insurance.insurance_products p JOIN (SELECT product_type, MAX(updated_at) AS max_updated FROM insurance.insurance_products WHERE updated_at IS NOT NULL GROUP BY product_type) m ON p.product_type = m.product_type AND p.updated_at IS NOT NULL JOIN insurance.insurance_products r ON r.product_type = p.product_type AND r.updated_at = m.max_updated;

[User] Also, only include product types that have more than 2 products.

[Assistant] SELECT p.product_id AS original_id, p.updated_at AS original_updated_at, r.product_id AS recent_id, r.updated_at AS recent_updated_at, p.product_type FROM insurance.insurance_products p JOIN (SELECT product_type, MAX(updated_at) AS max_updated FROM insurance.insurance_products WHERE updated_at IS NOT NULL GROUP BY product_type) m ON p.product_type = m.product_type AND p.updated_at IS NOT NULL JOIN insurance.insurance_products r ON r.product_type = p.product_type AND r.updated_at = m.max_updated WHERE p.product_type IN (SELECT product_type FROM insurance.insurance_products GROUP BY product_type HAVING COUNT(*) > 2);

[User] Finally, sort the results by recent_updated_at descending.

[Assistant] SELECT p.product_id AS original_id, p.updated_at AS original_updated_at, r.product_id AS recent_id, r.updated_at AS recent_updated_at, p.product_type FROM insurance.insurance_products p JOIN (SELECT product_type, MAX(updated_at) AS max_updated FROM insurance.insurance_products WHERE updated_at IS NOT NULL GROUP BY product_type) m ON p.product_type = m.product_type AND p.updated_at IS NOT NULL JOIN insurance.insurance_products r ON r.product_type = p.product_type AND r.updated_at = m.max_updated WHERE p.product_type IN (SELECT product_type FROM insurance.insurance_products GROUP BY product_type HAVING COUNT(*) > 2) ORDER BY recent_updated_at DESC;


[User] How many notifications have been marked as read so far?

[Assistant]
SELECT COUNT(*) AS read_notifications_count
FROM social_media.notifications
WHERE read_at IS NOT NULL;


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_metas ( block_timestamp.timestamp, block_number.bigint, transaction_hash.text, transaction_index.bigint, from_address.text, contract_address.text, symbol.text, total_supply.text, name.text, decimals.bigint, is_erc20.integer, is_erc721.integer, is_erc1155.integer, is_proxy.integer, extra_info.text, pk.integer )
CREATE TABLE erc1155_approval_for_all ( block_number.bigint, block_timestamp.timestamp, transaction_index.integer, transaction_hash.text, log_index.integer, contract_address.text )
CREATE TABLE erc20_balances_historical ( block_number.bigint, contract_address.text, wallet_address.text, amount.text, pk.integer )
CREATE TABLE nft_holders ( block_number.bigint, contract_address.text, wallet_address.text, token_id.text, contract_type.text, amount.text, pk.text )
CREATE TABLE nft_holder_historical ( block_number.bigint, contract_address.text, wallet_address.text, token_id.text, contract_type.text, amount.text, pk.integer )
CREATE TABLE sui_coins ( object_id.text, status.text, coin_type.text, balance.text, owner.text )
CREATE TABLE sui_coins_metadata ( object_id.text, coin_type.text, status.text, decimals.bigint, description.text, icon_url.text, symbol.text, name.text )
CREATE TABLE sui_nfts ( object_id.text, status.text, collection_type.text, package_id.text, previous_transaction.text, display.text, owner.text, name.text )
CREATE TABLE nft_metadata ( blockchain.text, contract_address.text, token_id.text, contract_name.text, erc_type.text, amount.bigint, minter.text, owner.text, mint_block_number.bigint, mint_time.bigint, mint_transaction_hash.text, mint_price.text, token_uri.text, metadata.text, name.text, image_uri.text, traits.text, updated_at.bigint, status.text, rarity_score.real, rarity_rank.integer, unique_attribute_count.integer, pk.integer )
CREATE TABLE nft_collections ( blockchain.text, contract_address.text, name.text, symbol.text, deploy_block_number.bigint, deploy_block_time.bigint, erc_type.text, owner.text, traits.text, description.text, slug.text, media_info.text, image_url.text, banner_image_url.text, updated_at.bigint, banner.text, token_count.bigint, floor_price.text, floor_price_symbol.text, floor_price_contract.text, pk.integer )
CREATE TABLE token_prices_latest ( blockchain.text, address.text, decimals.bigint, symbol.text, price.real, timestamp.timestamp )
CREATE TABLE token_balances ( block_number.bigint, block_timestamp.timestamp, tick.text, wallet_address.text, total_supply.decimal, amount.decimal )
CREATE TABLE token_info ( block_timestamp.timestamp, block_number.bigint, tx_index.integer, tx_hash.text, tick.text, max_supply.decimal, lim.decimal, wlim.decimal, dec.integer, creator.text )
CREATE TABLE token_activities ( block_timestamp.timestamp, block_number.bigint, tx_index.integer, tx_hash.text, log_index.integer, type.text, tick.text, id.text, amt.decimal, from_address.text, to_address.text )
CREATE TABLE mud_holesky_redstone ( address.text, table_id.text, key_bytes.text, key0.text, key1.text, static_data.text, encoded_lengths.text, dynamic_data.text, is_deleted.boolean, block_number.bigint, log_index.bigint )
CREATE TABLE token_balances_bitcoin ( block_number.bigint, block_timestamp.timestamp, tick.text, wallet_address.text, total_supply.text, amount.text )
CREATE TABLE token_activities_bitcoin ( block_timestamp.timestamp, block_number.bigint, tx_index.integer, tx_hash.text, log_index.integer, type.text, tick.text, id.text, amt.decimal, from_address.text, to_address.text )
CREATE TABLE token_activities_avalanche ( block_timestamp.timestamp, block_number.bigint, tx_index.integer, tx_hash.text, log_index.integer, type.text, tick.text, id.text, amt.decimal, from_address.text, to_address.text )
CREATE TABLE token_balances_avalanche ( block_number.bigint, block_timestamp.timestamp, tick.text, wallet_address.text, total_supply.decimal, amount.decimal )
CREATE TABLE token_info_avalanche ( block_timestamp.timestamp, block_number.bigint, tx_index.integer, tx_hash.text, tick.text, max_supply.decimal, lim.decimal, wlim.decimal, dec.integer, creator.text, minted.decimal, holders.decimal, txs.decimal, updated_timestamp.timestamp, completed_timestamp.timestamp, id.text )

[User] Show me the total amount held and number of unique wallets for each ERC20 token with a symbol ending in 'X'.

[Assistant] SELECT tm.symbol, SUM(CAST(ebh.amount AS DECIMAL)) AS total_amount, COUNT(DISTINCT ebh.wallet_address) AS unique_wallets FROM token_metas tm JOIN erc20_balances_historical ebh ON tm.contract_address = ebh.contract_address WHERE tm.is_erc20 = 1 AND tm.symbol LIKE '%X' GROUP BY tm.symbol;

[User] Can you also include the token name and the contract address in your results?

[Assistant] SELECT tm.symbol, tm.name, tm.contract_address, SUM(CAST(ebh.amount AS DECIMAL)) AS total_amount, COUNT(DISTINCT ebh.wallet_address) AS unique_wallets FROM token_metas tm JOIN erc20_balances_historical ebh ON tm.contract_address = ebh.contract_address WHERE tm.is_erc20 = 1 AND tm.symbol LIKE '%X' GROUP BY tm.symbol, tm.name, tm.contract_address;

[User] Now, restrict the results to only those tokens where total_amount exceeds 1,000,000.

[Assistant] SELECT tm.symbol, tm.name, tm.contract_address, SUM(CAST(ebh.amount AS DECIMAL)) AS total_amount, COUNT(DISTINCT ebh.wallet_address) AS unique_wallets FROM token_metas tm JOIN erc20_balances_historical ebh ON tm.contract_address = ebh.contract_address WHERE tm.is_erc20 = 1 AND tm.symbol LIKE '%X' GROUP BY tm.symbol, tm.name, tm.contract_address HAVING SUM(CAST(ebh.amount AS DECIMAL)) > 1000000;

[User] Please sort the results by the total_amount in descending order and limit to the top 5 tokens.

[Assistant] SELECT tm.symbol, tm.name, tm.contract_address, SUM(CAST(ebh.amount AS DECIMAL)) AS total_amount, COUNT(DISTINCT ebh.wallet_address) AS unique_wallets FROM token_metas tm JOIN erc20_balances_historical ebh ON tm.contract_address = ebh.contract_address WHERE tm.is_erc20 = 1 AND tm.symbol LIKE '%X' GROUP BY tm.symbol, tm.name, tm.contract_address HAVING SUM(CAST(ebh.amount AS DECIMAL)) > 1000000 ORDER BY total_amount 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 legal_services.clients ( client_id text PRIMARY KEY, first_name text, last_name text, company_name text, email text UNIQUE, phone text, address text, date_registered timestamp, status text )
CREATE TABLE legal_services.lawyers (
    lawyer_id character varying(64) NOT NULL,  -- unique identifier for the lawyer
    first_name character varying(100) NOT NULL, -- lawyer's first name
    last_name character varying(100) NOT NULL, -- lawyer's last name
    bar_number character varying(64) UNIQUE NOT NULL, -- lawyer's bar association number
    firm_id character varying(64), -- FK to law_firms table
    email character varying(128) UNIQUE NOT NULL, -- lawyer's professional email
    phone character varying(32), -- lawyer's contact phone number
    specialization text, -- areas of legal expertise
    hire_date timestamp NOT NULL, -- date the lawyer was hired or joined
    is_partner boolean DEFAULT false, -- flag indicating if the lawyer is a partner
    PRIMARY KEY (lawyer_id)
)
CREATE TABLE legal_services.law_firms ( firm_id text PRIMARY KEY, firm_name text, address text, phone text, website text, founded_date date, managing_partner_id text )
CREATE TABLE legal_services.cases ( case_id.text, case_number.text, case_name.text, client_id.text, lead_lawyer_id.text, case_type.text, status.text, date_opened.timestamp, date_closed.timestamp, description.text, court_jurisdiction.text )
CREATE TABLE legal_services.invoices ( invoice_id.text, invoice_number.text, client_id.text, case_id.text, issue_date.date, due_date.date, total_amount.numeric, amount_paid.numeric, status.text, payment_date.timestamp, notes.text, created_at.timestamp )
CREATE TABLE legal_services.appointments (
    appointment_id character varying(64) NOT NULL, -- unique identifier for the appointment
    client_id character varying(64) NOT NULL, -- FK to clients table
    lawyer_id character varying(64) NOT NULL, -- FK to lawyers table
    case_id character varying(64), -- FK to cases table (optional, if related to a specific case)
    start_time timestamp NOT NULL, -- scheduled start date and time
    end_time timestamp NOT NULL, -- scheduled end date and time
    location text, -- location of the meeting (address or virtual link)
    subject character varying(255), -- subject or purpose of the appointment
    notes text, -- additional notes for the appointment
    status character varying(50) NOT NULL, -- e.g., Scheduled, Completed, Canceled, Rescheduled
    created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (appointment_id)
)

[User] List all cases where the client and the lead lawyer belong to companies or law firms with the same phone number. For each such case, please show the case name, client name, client company, lead lawyer name, and the shared phone number.

[Assistant]
SELECT cases.case_name, c.first_name AS client_first_name, c.last_name AS client_last_name, c.company_name, l.first_name AS lawyer_first_name, l.last_name AS lawyer_last_name, f.phone AS shared_phone
FROM legal_services.cases
JOIN legal_services.clients c ON cases.client_id = c.client_id
JOIN legal_services.lawyers l ON cases.lead_lawyer_id = l.lawyer_id
JOIN legal_services.law_firms f ON l.firm_id = f.firm_id
WHERE c.phone = f.phone;

[User] Can you include the case number and sort the results by the shared phone number?

[Assistant]
SELECT cases.case_number, cases.case_name, c.first_name AS client_first_name, c.last_name AS client_last_name, c.company_name, l.first_name AS lawyer_first_name, l.last_name AS lawyer_last_name, f.phone AS shared_phone
FROM legal_services.cases
JOIN legal_services.clients c ON cases.client_id = c.client_id
JOIN legal_services.lawyers l ON cases.lead_lawyer_id = l.lawyer_id
JOIN legal_services.law_firms f ON l.firm_id = f.firm_id
WHERE c.phone = f.phone
ORDER BY f.phone;

[User] Actually, please only show cases where the shared phone number is not null and starts with '+44'.

[Assistant]
SELECT cases.case_number, cases.case_name, c.first_name AS client_first_name, c.last_name AS client_last_name, c.company_name, l.first_name AS lawyer_first_name, l.last_name AS lawyer_last_name, f.phone AS shared_phone
FROM legal_services.cases
JOIN legal_services.clients c ON cases.client_id = c.client_id
JOIN legal_services.lawyers l ON cases.lead_lawyer_id = l.lawyer_id
JOIN legal_services.law_firms f ON l.firm_id = f.firm_id
WHERE c.phone = f.phone AND f.phone IS NOT NULL AND f.phone LIKE '+44%'
ORDER BY f.phone;


[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';


[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';