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.post_hashtags ( post_hashtag_id.text, post_id.text, hashtag_id.text )
CREATE TABLE social_media.conversation_participants ( participant_id text PRIMARY KEY, conversation_id text, user_id text, joined_at timestamp, last_read_at timestamp )

[User] List all conversation participants along with post_hashtag details where the participant's user_id matches the hashtag_id.

[Assistant]
SELECT cp.conversation_id, cp.user_id, ph.post_id, ph.hashtag_id
FROM social_media.conversation_participants cp
INNER JOIN social_media.post_hashtags ph
  ON cp.user_id = ph.hashtag_id;

[User] Only include participants who joined after '2024-01-01'.

[Assistant]
SELECT cp.conversation_id, cp.user_id, ph.post_id, ph.hashtag_id
FROM social_media.conversation_participants cp
INNER JOIN social_media.post_hashtags ph
  ON cp.user_id = ph.hashtag_id
WHERE cp.joined_at > '2024-01-01';

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

[Assistant]
SELECT cp.conversation_id, cp.user_id, ph.post_id, ph.hashtag_id
FROM social_media.conversation_participants cp
INNER JOIN social_media.post_hashtags ph
  ON cp.user_id = ph.hashtag_id
WHERE cp.joined_at > '2024-01-01'
ORDER BY cp.joined_at DESC;

[User] Finally, limit the output to the first 10 rows.

[Assistant]
SELECT cp.conversation_id, cp.user_id, ph.post_id, ph.hashtag_id
FROM social_media.conversation_participants cp
INNER JOIN social_media.post_hashtags ph
  ON cp.user_id = ph.hashtag_id
WHERE cp.joined_at > '2024-01-01'
ORDER BY cp.joined_at 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.posts ( post_id text, user_id text, content text, media_url text, media_type text, created_at timestamp, updated_at timestamp, visibility text, location_lat numeric, location_lon numeric )
CREATE TABLE social_media.comments ( comment_id text PRIMARY KEY, post_id text, user_id text, parent_comment_id text, content text, created_at timestamp, updated_at timestamp )
CREATE TABLE social_media.likes ( like_id character varying(64) NOT NULL, user_id character varying(64) NOT NULL, post_id character varying(64), comment_id character varying(64), created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (like_id), CONSTRAINT unique_user_like UNIQUE (user_id, post_id, comment_id), CONSTRAINT check_like_target CHECK (post_id IS NOT NULL OR comment_id IS NOT NULL) )
CREATE TABLE social_media.follows ( follow_id.text, follower_user_id.text, following_user_id.text, created_at.timestamp )
CREATE TABLE social_media.hashtags ( hashtag_id text PRIMARY KEY, tag_name text, created_at timestamp )
CREATE TABLE social_media.post_hashtags ( post_hashtag_id character varying(64) NOT NULL, post_id character varying(64) NOT NULL, hashtag_id character varying(64) NOT NULL, PRIMARY KEY (post_hashtag_id), CONSTRAINT unique_post_hashtag UNIQUE (post_id, hashtag_id) )
CREATE TABLE social_media.conversations ( conversation_id.text, created_at.timestamp, last_message_at.timestamp, is_group_chat.boolean, group_name.text )
CREATE TABLE social_media.conversation_participants ( participant_id.text, conversation_id.text, user_id.text, joined_at.timestamp, last_read_at.timestamp )
CREATE TABLE social_media.notifications ( notification_id text PRIMARY KEY, recipient_user_id text, sender_user_id text, notification_type text, target_entity_id text, target_entity_type text, created_at timestamp, read_at timestamp )
CREATE TABLE social_media.user_settings ( setting_id text PRIMARY KEY, user_id text, theme text, email_notifications_enabled bool, push_notifications_enabled bool, private_profile bool, updated_at timestamp )

[User] List the usernames of users who liked the post with ID 'P123'.

[Assistant] SELECT DISTINCT T1.username FROM social_media.users AS T1 JOIN social_media.likes AS T2 ON T1.user_id = T2.user_id WHERE T2.post_id = 'P123';

[User] Also, only include those users who have enabled email notifications.

[Assistant] SELECT DISTINCT T1.username FROM social_media.users AS T1 JOIN social_media.likes AS T2 ON T1.user_id = T2.user_id JOIN social_media.user_settings AS T3 ON T1.user_id = T3.user_id WHERE T2.post_id = 'P123' AND T3.email_notifications_enabled = TRUE;

[User] Further filter the list to only show users with a public profile.

[Assistant] SELECT DISTINCT T1.username FROM social_media.users AS T1 JOIN social_media.likes AS T2 ON T1.user_id = T2.user_id JOIN social_media.user_settings AS T3 ON T1.user_id = T3.user_id WHERE T2.post_id = 'P123' AND T3.email_notifications_enabled = TRUE AND T3.private_profile = FALSE;

[User] Please order the results by username alphabetically.

[Assistant] SELECT DISTINCT T1.username FROM social_media.users AS T1 JOIN social_media.likes AS T2 ON T1.user_id = T2.user_id JOIN social_media.user_settings AS T3 ON T1.user_id = T3.user_id WHERE T2.post_id = 'P123' AND T3.email_notifications_enabled = TRUE AND T3.private_profile = FALSE ORDER BY T1.username 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 social_media.users ( user_id character varying(64) NOT NULL, username character varying(50) NOT NULL UNIQUE, email character varying(128) NOT NULL UNIQUE, password_hash text NOT NULL, full_name character varying(100), profile_picture_url text, bio text, location character varying(100), created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, is_verified boolean NOT NULL DEFAULT false, PRIMARY KEY (user_id) )
CREATE TABLE social_media.posts ( post_id.text, user_id.text, content.text, media_url.text, media_type.text, created_at.timestamp, updated_at.timestamp, visibility.text, location_lat.numeric, location_lon.numeric )
CREATE TABLE social_media.likes ( like_id text PRIMARY KEY, user_id text, post_id text, comment_id text, created_at timestamp )
CREATE TABLE social_media.hashtags ( hashtag_id.text, tag_name.text, created_at.timestamp )
CREATE TABLE social_media.post_hashtags ( post_hashtag_id.text, post_id.text, hashtag_id.text )
CREATE TABLE social_media.conversations ( conversation_id character varying(64) NOT NULL, created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, last_message_at timestamp, is_group_chat boolean NOT NULL DEFAULT false, group_name character varying(100), PRIMARY KEY (conversation_id) )
CREATE TABLE social_media.conversation_participants ( participant_id text PRIMARY KEY, conversation_id text, user_id text, joined_at timestamp, last_read_at timestamp )

[User] List all users who have participated in group conversations created in April 2024, showing their username, conversation ID, and the group name.

[Assistant]
SELECT DISTINCT u.username, c.conversation_id, c.group_name
FROM social_media.conversations c
JOIN social_media.conversation_participants cp ON c.conversation_id = cp.conversation_id
JOIN social_media.users u ON cp.user_id = u.user_id
WHERE c.is_group_chat = true
  AND c.created_at >= '2024-04-01' AND c.created_at < '2024-05-01';

[User] Instead of all users, show only those who joined the group conversation after it was created.

[Assistant]
SELECT DISTINCT u.username, c.conversation_id, c.group_name
FROM social_media.conversations c
JOIN social_media.conversation_participants cp ON c.conversation_id = cp.conversation_id
JOIN social_media.users u ON cp.user_id = u.user_id
WHERE c.is_group_chat = true
  AND c.created_at >= '2024-04-01' AND c.created_at < '2024-05-01'
  AND cp.joined_at > c.created_at;

[User] I only care about group conversations where the group name contains the word 'Study'.

[Assistant]
SELECT DISTINCT u.username, c.conversation_id, c.group_name
FROM social_media.conversations c
JOIN social_media.conversation_participants cp ON c.conversation_id = cp.conversation_id
JOIN social_media.users u ON cp.user_id = u.user_id
WHERE c.is_group_chat = true
  AND c.created_at >= '2024-04-01' AND c.created_at < '2024-05-01'
  AND cp.joined_at > c.created_at
  AND c.group_name ILIKE '%Study%';


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.posts ( post_id.text, user_id.text, content.text, media_url.text, media_type.text, created_at.timestamp, updated_at.timestamp, visibility.text, location_lat.numeric, location_lon.numeric )
CREATE TABLE social_media.follows ( follow_id.text, follower_user_id.text, following_user_id.text, created_at.timestamp )
CREATE TABLE social_media.messages ( message_id character varying(64) NOT NULL, conversation_id character varying(64) NOT NULL, sender_user_id character varying(64) NOT NULL, content text, media_url text, media_type character varying(20), sent_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, read_at timestamp, PRIMARY KEY (message_id) )
CREATE TABLE social_media.conversations ( conversation_id text PRIMARY KEY, created_at timestamp, last_message_at timestamp, is_group_chat bool, group_name text )
CREATE TABLE social_media.user_settings ( setting_id text PRIMARY KEY, user_id text, theme text, email_notifications_enabled bool, push_notifications_enabled bool, private_profile bool, updated_at timestamp )

[User] I want to get the list of users and their total number of public posts made in the last month, including user_id and post_count.

[Assistant]
SELECT user_id, COUNT(*) AS post_count
FROM social_media.posts
WHERE visibility = 'public'
  AND created_at >= CURRENT_DATE - INTERVAL '1 month'
GROUP BY user_id;

[User] Actually, include only users whose email_notifications_enabled is true.

[Assistant]
SELECT p.user_id, COUNT(*) AS post_count
FROM social_media.posts p
JOIN social_media.user_settings u
  ON p.user_id = u.user_id
WHERE p.visibility = 'public'
  AND p.created_at >= CURRENT_DATE - INTERVAL '1 month'
  AND u.email_notifications_enabled = true
GROUP BY p.user_id;

[User] Also only consider posts that have media_type 'image'.

[Assistant]
SELECT p.user_id, COUNT(*) AS post_count
FROM social_media.posts p
JOIN social_media.user_settings u
  ON p.user_id = u.user_id
WHERE p.visibility = 'public'
  AND p.created_at >= CURRENT_DATE - INTERVAL '1 month'
  AND u.email_notifications_enabled = true
  AND p.media_type = 'image'
GROUP BY p.user_id;

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

[Assistant]
SELECT p.user_id, COUNT(*) AS post_count
FROM social_media.posts p
JOIN social_media.user_settings u
  ON p.user_id = u.user_id
WHERE p.visibility = 'public'
  AND p.created_at >= CURRENT_DATE - INTERVAL '1 month'
  AND u.email_notifications_enabled = true
  AND p.media_type = 'image'
GROUP BY p.user_id
ORDER BY post_count 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.messages ( message_id.text, conversation_id.text, sender_user_id.text, content.text, media_url.text, media_type.text, sent_at.timestamp, read_at.timestamp )
CREATE TABLE social_media.notifications ( notification_id text, recipient_user_id text, sender_user_id text, notification_type text, target_entity_id text, target_entity_type text, created_at timestamp, read_at timestamp )

[User] List all messages that haven’t triggered any notification; show message_id, sender_user_id, and sent_at.

[Assistant]
SELECT m.message_id, m.sender_user_id, m.sent_at
FROM social_media.messages AS m
LEFT JOIN social_media.notifications AS n
  ON n.target_entity_type = 'message'
  AND n.target_entity_id = m.message_id
WHERE n.notification_id IS NULL;

[User] Actually, only include those sent before '2024-06-01'.

[Assistant]
SELECT m.message_id, m.sender_user_id, m.sent_at
FROM social_media.messages AS m
LEFT JOIN social_media.notifications AS n
  ON n.target_entity_type = 'message'
  AND n.target_entity_id = m.message_id
WHERE n.notification_id IS NULL
  AND m.sent_at < '2024-06-01';


[User] Show the post ID and hashtags for all posts containing the word 'conference' in their content.

[Assistant]
SELECT p.post_id, h.hashtag_id, h.tag_name
FROM social_media.posts p
JOIN social_media.post_hashtags ph ON p.post_id = ph.post_id
JOIN social_media.hashtags h ON ph.hashtag_id = h.hashtag_id
WHERE p.content ILIKE '%conference%';


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.posts ( post_id text, user_id text, content text, media_url text, media_type text, created_at timestamp, updated_at timestamp, visibility text, location_lat numeric, location_lon numeric )
CREATE TABLE social_media.follows ( follow_id text, follower_user_id text, following_user_id text, created_at timestamp )

[User] Count how many follow relationships were created in June 2024.

[Assistant] SELECT COUNT(follow_id) FROM social_media.follows WHERE created_at >= '2024-06-01' AND created_at < '2024-07-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 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.posts ( post_id.text, user_id.text, content.text, media_url.text, media_type.text, created_at.timestamp, updated_at.timestamp, visibility.text, location_lat.numeric, location_lon.numeric )
CREATE TABLE social_media.likes ( like_id text, user_id text, post_id text, comment_id text, created_at timestamp )
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.hashtags ( hashtag_id character varying(64) NOT NULL, tag_name character varying(100) NOT NULL UNIQUE, created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (hashtag_id) )
CREATE TABLE social_media.post_hashtags ( post_hashtag_id character varying(64) NOT NULL, post_id character varying(64) NOT NULL, hashtag_id character varying(64) NOT NULL, PRIMARY KEY (post_hashtag_id), CONSTRAINT unique_post_hashtag UNIQUE (post_id, hashtag_id) )
CREATE TABLE social_media.user_settings ( setting_id text PRIMARY KEY, user_id text, theme text, email_notifications_enabled bool, push_notifications_enabled bool, private_profile bool, updated_at timestamp )

[User] I want to know the usernames of users who have followed more than three different people, along with the date they created their accounts.

[Assistant]
SELECT u.username, u.created_at, COUNT(DISTINCT f.following_user_id) AS following_count
FROM social_media.users u
JOIN social_media.follows f ON u.user_id = f.follower_user_id
GROUP BY u.user_id, u.username, u.created_at
HAVING COUNT(DISTINCT f.following_user_id) > 3;

[User] Also include the usernames of up to two users they are following for each user in the result.

[Assistant]
SELECT u.username AS follower_username, u.created_at, uf.username AS following_username
FROM social_media.users u
JOIN social_media.follows f ON u.user_id = f.follower_user_id
JOIN social_media.users uf ON f.following_user_id = uf.user_id
WHERE u.user_id IN (
  SELECT f1.follower_user_id
  FROM social_media.follows f1
  GROUP BY f1.follower_user_id
  HAVING COUNT(DISTINCT f1.following_user_id) > 3
)
ORDER BY u.username, f.created_at
LIMIT 2;

[User] Can you instead provide all usernames they follow, not just two, and include the follow creation date as well?

[Assistant]
SELECT u.username AS follower_username, u.created_at AS follower_created_at, uf.username AS following_username, f.created_at AS follow_created_at
FROM social_media.users u
JOIN social_media.follows f ON u.user_id = f.follower_user_id
JOIN social_media.users uf ON f.following_user_id = uf.user_id
WHERE u.user_id IN (
  SELECT f1.follower_user_id
  FROM social_media.follows f1
  GROUP BY f1.follower_user_id
  HAVING COUNT(DISTINCT f1.following_user_id) > 3
)
ORDER BY u.username, f.created_at;


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.posts ( post_id text, user_id text, content text, media_url text, media_type text, created_at timestamp, updated_at timestamp, visibility text, location_lat numeric, location_lon numeric )
CREATE TABLE social_media.comments ( comment_id text, post_id text, user_id text, parent_comment_id text, content text, created_at timestamp, updated_at timestamp )
CREATE TABLE social_media.likes ( like_id text, user_id text, post_id text, comment_id text, created_at timestamp )
CREATE TABLE social_media.follows ( follow_id text, follower_user_id text, following_user_id text, created_at timestamp )
CREATE TABLE social_media.hashtags ( hashtag_id text, tag_name text, created_at timestamp )
CREATE TABLE social_media.messages ( message_id text, conversation_id text, sender_user_id text, content text, media_url text, media_type text, sent_at timestamp, read_at timestamp )
CREATE TABLE social_media.conversations ( conversation_id text, created_at timestamp, last_message_at timestamp, is_group_chat boolean, group_name text )
CREATE TABLE social_media.conversation_participants ( participant_id text, conversation_id text, user_id text, joined_at timestamp, last_read_at timestamp )
CREATE TABLE social_media.user_settings ( setting_id text, user_id text, theme text, email_notifications_enabled boolean, push_notifications_enabled boolean, private_profile boolean, updated_at timestamp )

[User] List all comments on a given post along with the commenter’s username and the comment time.

[Assistant]
SELECT c.comment_id, c.content, u.username, c.created_at
FROM social_media.comments c
JOIN social_media.users u ON c.user_id = u.user_id
WHERE c.post_id = '<POST_ID_PLACEHOLDER>';

[User] Please also indicate if the comment is a reply by showing the parent_comment_id as well.

[Assistant]
SELECT c.comment_id, c.content, u.username, c.created_at, c.parent_comment_id
FROM social_media.comments c
JOIN social_media.users u ON c.user_id = u.user_id
WHERE c.post_id = '<POST_ID_PLACEHOLDER>';

[User] Filter to only show the top-level comments (those without a parent_comment_id).

[Assistant]
SELECT c.comment_id, c.content, u.username, c.created_at, c.parent_comment_id
FROM social_media.comments c
JOIN social_media.users u ON c.user_id = u.user_id
WHERE c.post_id = '<POST_ID_PLACEHOLDER>'
  AND c.parent_comment_id IS 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 social_media.conversations ( conversation_id text PRIMARY KEY, created_at timestamp, last_message_at timestamp, is_group_chat bool, group_name text )

[User] How many group conversations are there?

[Assistant] SELECT COUNT(*) FROM social_media.conversations WHERE is_group_chat = 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 web3.contracts ( contract_id character varying(64) NOT NULL, address character varying(128) NOT NULL, abi text NOT NULL, bytecode text NOT NULL, deployer_address character varying(128) NOT NULL, deployed_at timestamp NOT NULL, network_id character varying(64) NOT NULL, status character varying(32) NOT NULL, PRIMARY KEY (contract_id) )
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 character varying(64) NOT NULL, from_address character varying(128) NOT NULL, to_address character varying(128), value numeric(38,14) NOT NULL, gas_used integer NOT NULL, gas_price numeric(38,14) NOT NULL, block_number integer NOT NULL, timestamp timestamp NOT NULL, input_data text, status character varying(32) NOT NULL, nonce integer NOT NULL, PRIMARY KEY (transaction_id) )
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.wallets ( wallet_id text, user_id text, address text, private_key text, created_at timestamp )
CREATE TABLE web3.users ( user_id text, username text, email text, password_hash text, created_at timestamp )

[User] For each block, get the count of failed transactions and the total gas used by these failed transactions. Show block_number, failed_tx_count, and total_failed_gas.

[Assistant]
SELECT block_number,
       COUNT(*) FILTER (WHERE status = 'failed') AS failed_tx_count,
       SUM(gas_used) FILTER (WHERE status = 'failed') AS total_failed_gas
FROM web3.transactions
GROUP BY block_number;

[User] Actually, I need only blocks with failed_tx_count greater than 10.

[Assistant]
SELECT block_number,
       COUNT(*) FILTER (WHERE status = 'failed') AS failed_tx_count,
       SUM(gas_used) FILTER (WHERE status = 'failed') AS total_failed_gas
FROM web3.transactions
GROUP BY block_number
HAVING COUNT(*) FILTER (WHERE status = 'failed') > 10;


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.maintenance_records ( record_id character varying(64) NOT NULL, asset_id character varying(64) NOT NULL, asset_type character varying(32) NOT NULL, maintenance_type character varying(128) NOT NULL, staff_id character varying(64), start_datetime timestamp NOT NULL, end_datetime timestamp, description text NOT NULL, cost numeric(12, 2), status character varying(32) NOT NULL DEFAULT 'Completed', PRIMARY KEY (record_id), FOREIGN KEY (staff_id) REFERENCES train_station_management.staff(staff_id) )

[User] What is the reason behind the cost variation for maintenance of asset_id 'ASSET456' over the past year?

[Assistant] I'm sorry, but I can't provide information on the reasons behind cost variations for maintenance. The data in the maintenance_records table includes only the cost amount and other basic details of each maintenance record, but it does not contain explanations or factors influencing cost changes.


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 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) )

[User] Find all guests who live in the same city and list each guest's name along with the name of another guest in that city.

[Assistant]
SELECT g1.first_name || ' ' || g1.last_name AS guest_name,
       g2.first_name || ' ' || g2.last_name AS other_guest_name,
       g1.city
FROM guests g1
JOIN guests g2
  ON g1.city = g2.city
 AND g1.guest_id <> g2.guest_id;

[User] Now restrict to only cities with more than two guests and order by city ascending.

[Assistant]
SELECT pairs.city,
       pairs.guest_name,
       pairs.other_guest_name
FROM (
  SELECT g1.first_name || ' ' || g1.last_name AS guest_name,
         g2.first_name || ' ' || g2.last_name AS other_guest_name,
         g1.city
  FROM guests g1
  JOIN guests g2
    ON g1.city = g2.city
   AND g1.guest_id <> g2.guest_id
) AS pairs
JOIN (
  SELECT city
  FROM guests
  GROUP BY city
  HAVING COUNT(*) > 2
) AS multi
  ON pairs.city = multi.city
ORDER BY pairs.city 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 project_fundraising_record ( id character varying(32) NOT NULL, project_id character varying(32) NOT NULL, project_name character varying(128) NOT NULL, rounds_name character varying(32), investment_value numeric(38,14), investment_date character varying(128), introduction character varying(128), valuation numeric(38,14) )

[User] I want to see pairs of fundraising rounds for projects that have at least two rounds. Please list project_name, earlier_round, earlier_investment, later_round, and later_investment.

[Assistant]
SELECT
  earlier.project_name,
  earlier.rounds_name AS earlier_round,
  earlier.investment_value AS earlier_investment,
  later.rounds_name AS later_round,
  later.investment_value AS later_investment
FROM project_fundraising_record AS earlier
JOIN project_fundraising_record AS later
  ON earlier.project_id = later.project_id
  AND earlier.investment_date < later.investment_date;

[User] Only include pairs where later_investment is greater than earlier_investment.

[Assistant]
SELECT
  earlier.project_name,
  earlier.rounds_name AS earlier_round,
  earlier.investment_value AS earlier_investment,
  later.rounds_name AS later_round,
  later.investment_value AS later_investment
FROM project_fundraising_record AS earlier
JOIN project_fundraising_record AS later
  ON earlier.project_id = later.project_id
  AND earlier.investment_date < later.investment_date
WHERE later.investment_value > earlier.investment_value;

[User] Also calculate the valuation change between the two rounds as valuation_diff.

[Assistant]
SELECT
  earlier.project_name,
  earlier.rounds_name AS earlier_round,
  earlier.investment_value AS earlier_investment,
  later.rounds_name AS later_round,
  later.investment_value AS later_investment,
  (later.valuation - earlier.valuation) AS valuation_diff
FROM project_fundraising_record AS earlier
JOIN project_fundraising_record AS later
  ON earlier.project_id = later.project_id
  AND earlier.investment_date < later.investment_date
WHERE later.investment_value > earlier.investment_value;

[User] Order by the investment increase descending and limit to top 5.

[Assistant]
SELECT
  earlier.project_name,
  earlier.rounds_name AS earlier_round,
  earlier.investment_value AS earlier_investment,
  later.rounds_name AS later_round,
  later.investment_value AS later_investment,
  (later.valuation - earlier.valuation) AS valuation_diff
FROM project_fundraising_record AS earlier
JOIN project_fundraising_record AS later
  ON earlier.project_id = later.project_id
  AND earlier.investment_date < later.investment_date
WHERE later.investment_value > earlier.investment_value
ORDER BY (later.investment_value - earlier.investment_value) 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 trading_platform.assets ( asset_id character varying(64) NOT NULL, name character varying(128) NOT NULL, symbol character varying(32) NOT NULL, type character varying(32) NOT NULL, PRIMARY KEY (asset_id) )

[User] Show me all assets whose symbol starts with 'BTC' or 'ETH', along with their full names and types.

[Assistant] SELECT asset_id, name, symbol, type FROM trading_platform.assets WHERE symbol LIKE 'BTC%' OR symbol LIKE 'ETH%';


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 character varying(64) NOT NULL, license_plate character varying(32) NOT NULL UNIQUE, vin character varying(17) UNIQUE, make character varying(64) NOT NULL, model character varying(64) NOT NULL, year integer NOT NULL, type character varying(32) NOT NULL, capacity integer, fuel_type character varying(32) NOT NULL, status character varying(32) NOT NULL, depot_id character varying(64), acquisition_date date, PRIMARY KEY (vehicle_id) )

[User] List all pairs of vehicles with the same make and model but different manufacture years. Include each vehicle's ID, make, model, and both years.

[Assistant]
SELECT v1.vehicle_id AS vehicle1_id, v2.vehicle_id AS vehicle2_id, v1.make, v1.model, v1.year AS year1, v2.year AS year2
FROM transportation.vehicles v1
JOIN transportation.vehicles v2
  ON v1.make = v2.make
  AND v1.model = v2.model
  AND v1.year <> v2.year
  AND v1.vehicle_id < v2.vehicle_id
ORDER BY v1.make, v1.model, v1.year;


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 trading_platform.trades ( trade_id character varying(64) NOT NULL, order_id character varying(64) NOT NULL, buyer_id character varying(64) NOT NULL, seller_id character varying(64) NOT NULL, asset_id character varying(64) NOT NULL, quantity numeric(38,14) NOT NULL, price numeric(38,14) NOT NULL, traded_at timestamp NOT NULL, PRIMARY KEY (trade_id) )

[User] List all trades for asset 'BTC' where the trade quantity is greater than 5.

[Assistant] SELECT trade_id, order_id, buyer_id, seller_id, quantity, price, traded_at FROM trading_platform.trades WHERE asset_id = 'BTC' AND quantity > 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 web3.contracts ( contract_id character varying(64) NOT NULL, address character varying(128) NOT NULL, abi text NOT NULL, bytecode text NOT NULL, deployer_address character varying(128) NOT NULL, deployed_at timestamp NOT NULL, network_id character varying(64) NOT NULL, status character varying(32) NOT NULL, PRIMARY KEY (contract_id) )

[User] Provide a count of contracts for each unique combination of network ID and status.

[Assistant] SELECT network_id, status, COUNT(*) FROM web3.contracts GROUP BY network_id, status;

[User] Now, only show the network IDs from the previous result that have contracts in more than one distinct status.

[Assistant] SELECT network_id FROM web3.contracts GROUP BY network_id HAVING COUNT(DISTINCT status) > 1;


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.transactions ( transaction_id character varying(64) NOT NULL, from_address character varying(128) NOT NULL, to_address character varying(128), value numeric(38,14) NOT NULL, gas_used integer NOT NULL, gas_price numeric(38,14) NOT NULL, block_number integer NOT NULL, timestamp timestamp NOT NULL, input_data text, status character varying(32) NOT NULL, nonce integer NOT NULL, PRIMARY KEY (transaction_id) )

[User] Provide a count of transactions grouped by their status.

[Assistant] SELECT status, COUNT(*) FROM web3.transactions GROUP BY status;

[User] Now, only show the statuses that have more than 50 transactions.

[Assistant] SELECT status, COUNT(*) FROM web3.transactions GROUP BY status HAVING COUNT(*) > 50;


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 character varying(64) NOT NULL, contract_id character varying(64) NOT NULL, event_name character varying(128) NOT NULL, data text NOT NULL, emitted_at timestamp NOT NULL, transaction_id character varying(64) NOT NULL, log_index integer NOT NULL, PRIMARY KEY (event_id) )

[User] For each event, display its event_id, the event_id of the previous event in the same transaction (if any), and the log_index of both events.

[Assistant]
SELECT curr.event_id   AS current_event_id,
       prev.event_id   AS previous_event_id,
       curr.log_index  AS current_log_index,
       prev.log_index  AS previous_log_index,
       curr.transaction_id
FROM web3.events curr
LEFT JOIN web3.events prev
  ON curr.transaction_id = prev.transaction_id
 AND curr.log_index = prev.log_index + 1;


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 character varying(64) NOT NULL, train_id character varying(64) NOT NULL, car_number character varying(10) NOT NULL, car_type character varying(64) NOT NULL, class character varying(32), capacity integer, features text, PRIMARY KEY (car_id), FOREIGN KEY (train_id) REFERENCES train_station_management.trains(train_id) )

[User] What is the maintenance schedule for the train car with car_id 'CAR456'?

[Assistant] I'm sorry, but I can't provide information about the maintenance schedule for train cars as it is not included in the data available in the train_cars table.


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 character varying(64) NOT NULL, contract_id character varying(64) NOT NULL, event_name character varying(128) NOT NULL, data text NOT NULL, emitted_at timestamp NOT NULL, transaction_id character varying(64) NOT NULL, log_index integer NOT NULL, PRIMARY KEY (event_id) )
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.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)
)
CREATE TABLE web3.users (
    user_id character varying(64) NOT NULL,  -- unique identifier for the user
    username character varying(128) NOT NULL, -- username of the user
    email character varying(128) NOT NULL, -- email address of the user
    password_hash text NOT NULL, -- hashed password of the user
    created_at timestamp NOT NULL, -- time when the user account was created
    PRIMARY KEY (user_id)
)

[User] List the username, registration date, and the creation date of the first wallet created by each user who owns at least one wallet.

[Assistant] SELECT u.username, u.created_at AS registration_date, MIN(w.created_at) AS first_wallet_creation_date FROM web3.users AS u JOIN web3.wallets AS w ON u.user_id = w.user_id GROUP BY u.user_id, u.username, u.created_at;


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 character varying(64) NOT NULL, -- Unique identifier for the train car
    train_id character varying(64) NOT NULL, -- Foreign key referencing trains
    car_number character varying(10) NOT NULL, -- Identifier of the car within the train set
    car_type character varying(64) NOT NULL, -- e.g., Locomotive, Passenger, Sleeper, Dining, Baggage
    class character varying(32), -- e.g., First, Business, Economy (null for non-passenger cars)
    capacity integer, -- Passenger capacity of this specific car (null for non-passenger cars)
    features text, -- Comma-separated list of features (e.g., 'WiFi', 'Power Outlet', 'Accessible Restroom')
    PRIMARY KEY (car_id),
    FOREIGN KEY (train_id) REFERENCES train_station_management.trains(train_id)
)

[User] What is the current location of a specific train car with car_id 'TC123'?

[Assistant] I'm sorry, but I can't provide the current location of a specific train car as this information is not stored in the database table. The table only contains static details about train cars such as car type, capacity, and features. For real-time location data, please refer to the train tracking system or contact the relevant department.


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] Show all fuel logs for vehicle_id 'VH123', including the timestamp, fuel type, quantity, and the location_id. Sort the list by timestamp descending.

[Assistant]
SELECT fuel_log_id, log_timestamp, fuel_type, quantity, location_id
FROM transportation.fuel_logs
WHERE vehicle_id = 'VH123'
ORDER BY log_timestamp DESC;

[User] Can you also include the odometer reading and total cost for each fuel log in the result?

[Assistant]
SELECT fuel_log_id, log_timestamp, fuel_type, quantity, location_id, odometer_reading, total_cost
FROM transportation.fuel_logs
WHERE vehicle_id = 'VH123'
ORDER BY log_timestamp DESC;

[User] I only want to see records where the total_cost is above 100.

[Assistant]
SELECT fuel_log_id, log_timestamp, fuel_type, quantity, location_id, odometer_reading, total_cost
FROM transportation.fuel_logs
WHERE vehicle_id = 'VH123'
  AND total_cost > 100
ORDER BY log_timestamp DESC;

[User] Actually, please limit the results to just the 5 most recent logs.

[Assistant]
SELECT fuel_log_id, log_timestamp, fuel_type, quantity, location_id, odometer_reading, total_cost
FROM transportation.fuel_logs
WHERE vehicle_id = 'VH123'
  AND total_cost > 100
ORDER BY log_timestamp 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 insurance.policies (
    policy_id character varying(64) NOT NULL, -- unique identifier for the policy
    customer_id character varying(64) NOT NULL, -- identifier for the policyholder (FK to customers)
    product_id character varying(64) NOT NULL, -- identifier for the insurance product (FK to insurance_products)
    agent_id character varying(64), -- identifier for the agent who sold the policy (FK to agents)
    policy_number character varying(100) NOT NULL UNIQUE, -- customer-facing policy number
    start_date date NOT NULL, -- date the policy coverage begins
    end_date date NOT NULL, -- date the policy coverage ends
    premium_amount numeric(12, 2) NOT NULL, -- total premium for the policy term
    coverage_limit numeric(15, 2) NOT NULL, -- maximum amount covered
    deductible numeric(10, 2) NOT NULL, -- amount customer pays before coverage kicks in
    status character varying(30) NOT NULL, -- e.g., Active, Expired, Cancelled, Pending
    issued_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, -- when the policy was issued
    cancellation_date date,
    cancellation_reason text,
    PRIMARY KEY (policy_id)
)

[User] Show me a list of all policy numbers that have a deductible exactly equal to 0.

[Assistant]
SELECT policy_number
FROM insurance.policies
WHERE deductible = 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 project_fundraising_record ( id character varying(32) NOT NULL, project_id character varying(32) NOT NULL, project_name character varying(128) NOT NULL, rounds_name character varying(32), investment_value numeric(38,14), investment_date character varying(128), introduction character varying(128), valuation numeric(38,14) )
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 )
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) )
CREATE TABLE smart_money_token_inflow ( token_address character varying(100) NOT NULL, token_symbol character varying(100), token_amount numeric(38,2), usd_value numeric(38,2), avg_price numeric(38,6), type int, address_amount integer, narrative character varying(100), chain character varying(100) )

[User] Show me the top 5 projects by total investment value, along with their latest fundraising round name and valuation.

[Assistant] SELECT project_name, rounds_name, valuation, SUM(investment_value) AS total_investment_value FROM project_fundraising_record GROUP BY project_name, rounds_name, valuation ORDER BY total_investment_value DESC LIMIT 5;

[User] Can you also include the investment date for these projects?

[Assistant] SELECT project_name, rounds_name, valuation, investment_date, SUM(investment_value) AS total_investment_value FROM project_fundraising_record GROUP BY project_name, rounds_name, valuation, investment_date ORDER BY total_investment_value DESC LIMIT 5;


[User] Count the number of contracts for each network_id.

[Assistant] SELECT network_id, COUNT(*) FROM web3.contracts GROUP BY network_id;

[User] Now, only show networks with more than 100 contracts.

[Assistant] SELECT network_id, COUNT(*) FROM web3.contracts GROUP BY network_id 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.claims (
    claim_id character varying(64) NOT NULL, -- unique identifier for the claim
    policy_id character varying(64) NOT NULL, -- identifier of the policy under which the claim is made (FK to policies)
    customer_id character varying(64) NOT NULL, -- identifier of the customer making the claim (FK to customers)
    incident_date timestamp NOT NULL, -- date and time of the incident leading to the claim
    reported_date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, -- date the claim was reported
    description text NOT NULL, -- detailed description of the incident and claim
    estimated_loss numeric(15, 2), -- initial estimated cost of the loss
    status character varying(30) NOT NULL, -- e.g., Filed, Processing, Under Review, Approved, Denied, Settled
    settlement_amount numeric(15, 2), -- final amount paid out
    settlement_date date,
    claim_handler_id character varying(64), -- identifier for the employee handling the claim (FK to employees/adjusters)
    police_report_number character varying(100),
    PRIMARY KEY (claim_id)
)
CREATE TABLE insurance.policy_beneficiaries (
    beneficiary_id character varying(64) NOT NULL, -- unique identifier for the beneficiary entry
    policy_id character varying(64) NOT NULL, -- identifier of the life/relevant policy (FK to policies)
    customer_id character varying(64) NOT NULL, -- identifier of the beneficiary (could be FK to customers or store info directly)
    first_name character varying(100) NOT NULL, -- beneficiary first name (if not linking to customers table)
    last_name character varying(100) NOT NULL, -- beneficiary last name
    relationship character varying(50) NOT NULL, -- relationship to the policyholder (e.g., Spouse, Child, Parent)
    percentage_share numeric(5, 2) NOT NULL CHECK (percentage_share > 0 AND percentage_share <= 100), -- percentage of payout
    contact_info text, -- contact details for the beneficiary
    added_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (beneficiary_id),
    UNIQUE (policy_id, customer_id) -- Ensure a person is listed only once per policy
)

[User] For each claim, count the number of beneficiaries associated with its policy. Show claim_id and beneficiary_count, only for claims with more than 2 beneficiaries.

[Assistant]
SELECT c.claim_id, COUNT(pb.beneficiary_id) AS beneficiary_count
FROM insurance.claims c
JOIN insurance.policy_beneficiaries pb ON c.policy_id = pb.policy_id
GROUP BY c.claim_id
HAVING COUNT(pb.beneficiary_id) > 2;

[User] Now restrict the result to claims reported in 2023.

[Assistant]
SELECT c.claim_id, COUNT(pb.beneficiary_id) AS beneficiary_count
FROM insurance.claims c
JOIN insurance.policy_beneficiaries pb ON c.policy_id = pb.policy_id
WHERE c.reported_date >= '2023-01-01' AND c.reported_date < '2024-01-01'
GROUP BY c.claim_id
HAVING COUNT(pb.beneficiary_id) > 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 transportation.vehicles (
    vehicle_id character varying(64) NOT NULL,  -- unique identifier for the vehicle
    license_plate character varying(32) NOT NULL UNIQUE, -- vehicle license plate
    vin character varying(17) UNIQUE, -- vehicle identification number
    make character varying(64) NOT NULL, -- manufacturer of the vehicle
    model character varying(64) NOT NULL, -- model of the vehicle
    year integer NOT NULL, -- manufacturing year
    type character varying(32) NOT NULL, -- type of vehicle (e.g., Bus, Train, Truck, Car, Van)
    capacity integer, -- passenger or cargo capacity
    fuel_type character varying(32) NOT NULL, -- e.g., Diesel, Gasoline, Electric, Hydrogen
    status character varying(32) NOT NULL, -- e.g., Active, Maintenance, Decommissioned
    depot_id character varying(64), -- identifier of the home depot/base
    acquisition_date date, -- date the vehicle was acquired
    PRIMARY KEY (vehicle_id)
)
CREATE TABLE transportation.drivers (
    driver_id character varying(64) NOT NULL,  -- unique identifier for the driver
    employee_id character varying(64) NOT NULL UNIQUE, -- employee reference number
    first_name character varying(128) NOT NULL, -- driver's first name
    last_name character varying(128) NOT NULL, -- driver's last name
    license_number character varying(64) NOT NULL UNIQUE, -- driving license number
    license_expiry_date date NOT NULL, -- expiry date of the license
    contact_number character varying(32), -- phone number
    email character varying(128) UNIQUE, -- email address
    hire_date date NOT NULL, -- date the driver was hired
    status character varying(32) NOT NULL, -- e.g., Active, On Leave, Terminated
    PRIMARY KEY (driver_id)
)
CREATE TABLE transportation.routes (
    route_id character varying(64) NOT NULL,  -- unique identifier for the route
    route_number character varying(32) NOT NULL UNIQUE, -- public-facing route number/name
    description text, -- description of the route
    origin_location_id character varying(64) NOT NULL, -- identifier of the starting location
    destination_location_id character varying(64) NOT NULL, -- identifier of the ending location
    distance_km numeric(10, 2), -- estimated distance in kilometers
    estimated_duration_minutes integer, -- estimated travel time in minutes
    type character varying(32) NOT NULL, -- e.g., City Bus, Intercity Train, Cargo Delivery
    PRIMARY KEY (route_id)
)
CREATE TABLE transportation.trips (
    trip_id character varying(64) NOT NULL,  -- unique identifier for a specific trip instance
    route_id character varying(64), -- identifier of the route being followed (can be null for ad-hoc trips)
    vehicle_id character varying(64) NOT NULL, -- identifier of the vehicle used
    driver_id character varying(64) NOT NULL, -- identifier of the driver operating
    scheduled_departure timestamp with time zone NOT NULL, -- planned departure time
    actual_departure timestamp with time zone, -- actual departure time
    scheduled_arrival timestamp with time zone NOT NULL, -- planned arrival time
    actual_arrival timestamp with time zone, -- actual arrival time
    origin_location_id character varying(64) NOT NULL, -- actual starting location (could differ from route origin)
    destination_location_id character varying(64) NOT NULL, -- actual ending location (could differ from route destination)
    status character varying(32) NOT NULL, -- e.g., Scheduled, In Progress, Completed, Delayed, Cancelled
    passenger_count integer, -- number of passengers (if applicable)
    cargo_manifest_id character varying(64), -- link to cargo details (if applicable)
    PRIMARY KEY (trip_id)
)
CREATE TABLE transportation.maintenance_logs (
    log_id character varying(64) NOT NULL,  -- unique identifier for the maintenance record
    vehicle_id character varying(64) NOT NULL, -- identifier of the vehicle serviced
    service_date date NOT NULL, -- date the service was performed
    odometer_reading integer, -- vehicle mileage at time of service
    service_type character varying(128) NOT NULL, -- type of service (e.g., Oil Change, Tire Rotation, Engine Repair)
    description text NOT NULL, -- detailed description of work done
    cost numeric(10, 2), -- cost of the service
    mechanic_name character varying(128), -- name or ID of the mechanic/shop
    next_service_due_date date, -- recommended date for next service
    PRIMARY KEY (log_id)
)
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)
)
CREATE TABLE transportation.passengers (
    passenger_id character varying(64) NOT NULL,  -- unique identifier for the passenger
    first_name character varying(128) NOT NULL, -- passenger's first name
    last_name character varying(128) NOT NULL, -- passenger's last name
    email character varying(128) UNIQUE, -- email address
    phone_number character varying(32) UNIQUE, -- phone number
    date_of_birth date, -- passenger's date of birth
    loyalty_program_id character varying(64), -- identifier for loyalty program membership
    created_at timestamp with time zone NOT NULL, -- time when the passenger record was created
    PRIMARY KEY (passenger_id)
)

[User] Show me all drivers and the vehicles they are assigned for trips that have status 'Delayed'. Include driver first_name, driver last_name, vehicle license_plate, trip_id, and actual_departure.

[Assistant]
SELECT d.first_name, d.last_name, v.license_plate, t.trip_id, t.actual_departure
FROM transportation.trips t
JOIN transportation.drivers d ON t.driver_id = d.driver_id
JOIN transportation.vehicles v ON t.vehicle_id = v.vehicle_id
WHERE t.status = 'Delayed';

[User] Now limit to delays where the actual_departure is more than 15 minutes after scheduled_departure and order by the delay duration in descending order.

[Assistant]
SELECT d.first_name, d.last_name, v.license_plate, t.trip_id, t.actual_departure,
       EXTRACT(EPOCH FROM (t.actual_departure - t.scheduled_departure))/60 AS delay_minutes
FROM transportation.trips t
JOIN transportation.drivers d ON t.driver_id = d.driver_id
JOIN transportation.vehicles v ON t.vehicle_id = v.vehicle_id
WHERE t.status = 'Delayed'
  AND (t.actual_departure - t.scheduled_departure) > INTERVAL '15 minutes'
ORDER BY delay_minutes 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 character varying(64) NOT NULL,  -- unique identifier for the vehicle
    license_plate character varying(32) NOT NULL UNIQUE, -- vehicle license plate
    vin character varying(17) UNIQUE, -- vehicle identification number
    make character varying(64) NOT NULL, -- manufacturer of the vehicle
    model character varying(64) NOT NULL, -- model of the vehicle
    year integer NOT NULL, -- manufacturing year
    type character varying(32) NOT NULL, -- type of vehicle (e.g., Bus, Train, Truck, Car, Van)
    capacity integer, -- passenger or cargo capacity
    fuel_type character varying(32) NOT NULL, -- e.g., Diesel, Gasoline, Electric, Hydrogen
    status character varying(32) NOT NULL, -- e.g., Active, Maintenance, Decommissioned
    depot_id character varying(64), -- identifier of the home depot/base
    acquisition_date date, -- date the vehicle was acquired
    PRIMARY KEY (vehicle_id)
)
CREATE TABLE transportation.drivers (
    driver_id character varying(64) NOT NULL,  -- unique identifier for the driver
    employee_id character varying(64) NOT NULL UNIQUE, -- employee reference number
    first_name character varying(128) NOT NULL, -- driver's first name
    last_name character varying(128) NOT NULL, -- driver's last name
    license_number character varying(64) NOT NULL UNIQUE, -- driving license number
    license_expiry_date date NOT NULL, -- expiry date of the license
    contact_number character varying(32), -- phone number
    email character varying(128) UNIQUE, -- email address
    hire_date date NOT NULL, -- date the driver was hired
    status character varying(32) NOT NULL, -- e.g., Active, On Leave, Terminated
    PRIMARY KEY (driver_id)
)
CREATE TABLE transportation.routes (
    route_id character varying(64) NOT NULL,  -- unique identifier for the route
    route_number character varying(32) NOT NULL UNIQUE, -- public-facing route number/name
    description text, -- description of the route
    origin_location_id character varying(64) NOT NULL, -- identifier of the starting location
    destination_location_id character varying(64) NOT NULL, -- identifier of the ending location
    distance_km numeric(10, 2), -- estimated distance in kilometers
    estimated_duration_minutes integer, -- estimated travel time in minutes
    type character varying(32) NOT NULL, -- e.g., City Bus, Intercity Train, Cargo Delivery
    PRIMARY KEY (route_id)
)
CREATE TABLE transportation.trips (
    trip_id character varying(64) NOT NULL,  -- unique identifier for a specific trip instance
    route_id character varying(64), -- identifier of the route being followed (can be null for ad-hoc trips)
    vehicle_id character varying(64) NOT NULL, -- identifier of the vehicle used
    driver_id character varying(64) NOT NULL, -- identifier of the driver operating
    scheduled_departure timestamp with time zone NOT NULL, -- planned departure time
    actual_departure timestamp with time zone, -- actual departure time
    scheduled_arrival timestamp with time zone NOT NULL, -- planned arrival time
    actual_arrival timestamp with time zone, -- actual arrival time
    origin_location_id character varying(64) NOT NULL, -- actual starting location (could differ from route origin)
    destination_location_id character varying(64) NOT NULL, -- actual ending location (could differ from route destination)
    status character varying(32) NOT NULL, -- e.g., Scheduled, In Progress, Completed, Delayed, Cancelled
    passenger_count integer, -- number of passengers (if applicable)
    cargo_manifest_id character varying(64), -- link to cargo details (if applicable)
    PRIMARY KEY (trip_id)
)
CREATE TABLE transportation.maintenance_logs (
    log_id character varying(64) NOT NULL,  -- unique identifier for the maintenance record
    vehicle_id character varying(64) NOT NULL, -- identifier of the vehicle serviced
    service_date date NOT NULL, -- date the service was performed
    odometer_reading integer, -- vehicle mileage at time of service
    service_type character varying(128) NOT NULL, -- type of service (e.g., Oil Change, Tire Rotation, Engine Repair)
    description text NOT NULL, -- detailed description of work done
    cost numeric(10, 2), -- cost of the service
    mechanic_name character varying(128), -- name or ID of the mechanic/shop
    next_service_due_date date, -- recommended date for next service
    PRIMARY KEY (log_id)
)
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] Show the most recent maintenance service for each vehicle, including the vehicle's make and model.

[Assistant]
SELECT m.vehicle_id, v.make, v.model, m.service_date, m.service_type, m.odometer_reading
FROM transportation.maintenance_logs m
JOIN transportation.vehicles v ON m.vehicle_id = v.vehicle_id
WHERE m.service_date = (
  SELECT MAX(m2.service_date)
  FROM transportation.maintenance_logs m2
  WHERE m2.vehicle_id = m.vehicle_id
);

[User] Can you also add the mechanic's name and the maintenance cost in the result?

[Assistant]
SELECT m.vehicle_id, v.make, v.model, m.service_date, m.service_type, m.odometer_reading, m.mechanic_name, m.cost
FROM transportation.maintenance_logs m
JOIN transportation.vehicles v ON m.vehicle_id = v.vehicle_id
WHERE m.service_date = (
  SELECT MAX(m2.service_date)
  FROM transportation.maintenance_logs m2
  WHERE m2.vehicle_id = m.vehicle_id
);

[User] Actually, only include records where the maintenance cost is above 200.

[Assistant]
SELECT m.vehicle_id, v.make, v.model, m.service_date, m.service_type, m.odometer_reading, m.mechanic_name, m.cost
FROM transportation.maintenance_logs m
JOIN transportation.vehicles v ON m.vehicle_id = v.vehicle_id
WHERE m.service_date = (
  SELECT MAX(m2.service_date)
  FROM transportation.maintenance_logs m2
  WHERE m2.vehicle_id = m.vehicle_id
)
AND m.cost > 200;

[User] Please order the result by maintenance cost in descending order.

[Assistant]
SELECT m.vehicle_id, v.make, v.model, m.service_date, m.service_type, m.odometer_reading, m.mechanic_name, m.cost
FROM transportation.maintenance_logs m
JOIN transportation.vehicles v ON m.vehicle_id = v.vehicle_id
WHERE m.service_date = (
  SELECT MAX(m2.service_date)
  FROM transportation.maintenance_logs m2
  WHERE m2.vehicle_id = m.vehicle_id
)
AND m.cost > 200
ORDER BY m.cost DESC;


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.stations ( station_id character varying(64) NOT NULL, station_name character varying(128) NOT NULL, city character varying(100) NOT NULL, state_province character varying(100), country_code character varying(3) NOT NULL, address text, timezone character varying(64) NOT NULL, number_of_platforms integer NOT NULL DEFAULT 0, created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, last_updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (station_id) )

[User] What are the ticket prices for trains departing from the station with ID 'ST456'?

[Assistant] I'm sorry, but I can't provide information on ticket prices as this data is not included in the station management database. For ticket pricing, please refer to the ticketing system or contact customer service.


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.maintenance_logs (
    log_id character varying(64) NOT NULL,  -- unique identifier for the maintenance record
    vehicle_id character varying(64) NOT NULL, -- identifier of the vehicle serviced
    service_date date NOT NULL, -- date the service was performed
    odometer_reading integer, -- vehicle mileage at time of service
    service_type character varying(128) NOT NULL, -- type of service (e.g., Oil Change, Tire Rotation, Engine Repair)
    description text NOT NULL, -- detailed description of work done
    cost numeric(10, 2), -- cost of the service
    mechanic_name character varying(128), -- name or ID of the mechanic/shop
    next_service_due_date date, -- recommended date for next service
    PRIMARY KEY (log_id)
)

[User] Show me all vehicles that are due for their next service before the end of this month, along with their last service type and date.

[Assistant] SELECT vehicle_id, service_type, service_date FROM transportation.maintenance_logs WHERE next_service_due_date <= DATE_TRUNC('month', CURRENT_DATE) + INTERVAL '1 month' - INTERVAL '1 day';

[User] Can you include the mechanic who performed the last service and order by how soon the next service is due?

[Assistant] SELECT vehicle_id, service_type, service_date, mechanic_name, next_service_due_date FROM transportation.maintenance_logs WHERE next_service_due_date <= DATE_TRUNC('month', CURRENT_DATE) + INTERVAL '1 month' - INTERVAL '1 day' ORDER BY next_service_due_date ASC;

[User] There seems to be a typo in your SQL - it should be 'INTERVAL' not 'INTERVAL'. Also, can you include the cost of the last service?

[Assistant] SELECT vehicle_id, service_type, service_date, mechanic_name, next_service_due_date, cost FROM transportation.maintenance_logs WHERE next_service_due_date <= DATE_TRUNC('month', CURRENT_DATE) + INTERVAL '1 month' - INTERVAL '1 day' ORDER BY next_service_due_date 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 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)
)

[User] List all token transfers between two distinct addresses, showing transfer_id, from_address, to_address, value, and transaction_id.

[Assistant]
SELECT transfer_id, from_address, to_address, value, transaction_id
FROM web3.token_transfers
WHERE from_address <> to_address;

[User] Actually, I only want to see transfers where the value transferred is greater than 1000 tokens.

[Assistant]
SELECT transfer_id, from_address, to_address, value, transaction_id
FROM web3.token_transfers
WHERE from_address <> to_address AND value > 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 transportation.vehicles ( vehicle_id text PRIMARY KEY, license_plate text, vin text, make text, model text, year int, type text, capacity int, fuel_type text, status text, depot_id text, acquisition_date date )
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.passengers (
    passenger_id character varying(64) NOT NULL,  -- unique identifier for the passenger
    first_name character varying(128) NOT NULL, -- passenger's first name
    last_name character varying(128) NOT NULL, -- passenger's last name
    email character varying(128) UNIQUE, -- email address
    phone_number character varying(32) UNIQUE, -- phone number
    date_of_birth date, -- passenger's date of birth
    loyalty_program_id character varying(64), -- identifier for loyalty program membership
    created_at timestamp with time zone NOT NULL, -- time when the passenger record was created
    PRIMARY KEY (passenger_id)
)
CREATE TABLE transportation.bookings ( booking_id character varying(64) NOT NULL, passenger_id character varying(64) NOT NULL, trip_id character varying(64) NOT NULL, seat_number character varying(16), booking_time timestamp with time zone NOT NULL, status character varying(32) NOT NULL, fare numeric(10, 2) NOT NULL, payment_transaction_id character varying(128), special_requests text, PRIMARY KEY (booking_id) )

[User] Provide a list of passengers including their passenger_id, first_name, last_name, phone_number, and the total number of bookings they have made. Sort the result by booking count in descending order.

[Assistant]
SELECT p.passenger_id,
       p.first_name,
       p.last_name,
       p.phone_number,
       COUNT(b.booking_id) AS booking_count
FROM transportation.passengers p
LEFT JOIN transportation.bookings b
  ON p.passenger_id = b.passenger_id
GROUP BY p.passenger_id,
         p.first_name,
         p.last_name,
         p.phone_number
ORDER BY booking_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 web3.events ( event_id character varying(64) NOT NULL, contract_id character varying(64) NOT NULL, event_name character varying(128) NOT NULL, data text NOT NULL, emitted_at timestamp NOT NULL, transaction_id character varying(64) NOT NULL, log_index integer NOT NULL, PRIMARY KEY (event_id) )
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.token_transfers ( transfer_id character varying(64) NOT NULL, token_id character varying(64) NOT NULL, from_address character varying(128) NOT NULL, to_address character varying(128) NOT NULL, value numeric(38,14) NOT NULL, transaction_id character varying(64) NOT NULL, log_index integer NOT NULL, 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)
)
CREATE TABLE web3.users (
    user_id character varying(64) NOT NULL,  -- unique identifier for the user
    username character varying(128) NOT NULL, -- username of the user
    email character varying(128) NOT NULL, -- email address of the user
    password_hash text NOT NULL, -- hashed password of the user
    created_at timestamp NOT NULL, -- time when the user account was created
    PRIMARY KEY (user_id)
)

[User] Show all token transfers including the token symbol and the address of the contract that created each token. List the transfer id, token id, value, transaction id, the contract address, and the token symbol.

[Assistant]
SELECT tt.transfer_id, tt.token_id, tt.value, tt.transaction_id, t.contract_id AS contract_address, t.token_symbol
FROM web3.token_transfers tt
JOIN web3.tokens t ON tt.token_id = t.token_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 financial_services.customers ( customer_id character varying(64) NOT NULL, first_name character varying(100) NOT NULL, last_name character varying(100) NOT NULL, date_of_birth date NOT NULL, ssn_hash character varying(128), address text NOT NULL, city character varying(100) NOT NULL, state character varying(50) NOT NULL, zip_code character varying(20) NOT NULL, country character varying(50) NOT NULL, email character varying(128) UNIQUE, phone_number character varying(32), created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, status character varying(32) NOT NULL, risk_profile character varying(50), PRIMARY KEY (customer_id) )
CREATE TABLE financial_services.accounts ( account_id character varying(64) NOT NULL, customer_id character varying(64) NOT NULL, account_number character varying(128) NOT NULL UNIQUE, account_type character varying(50) NOT NULL, balance numeric(19, 4) NOT NULL DEFAULT 0.00, currency character varying(3) NOT NULL, opened_date date NOT NULL, status character varying(32) NOT NULL, interest_rate numeric(5, 4), last_updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (account_id), FOREIGN KEY (customer_id) REFERENCES financial_services.customers(customer_id) )
CREATE TABLE financial_services.securities ( security_id character varying(64) NOT NULL, ticker_symbol character varying(20) NOT NULL UNIQUE, isin_code character varying(12) UNIQUE, cusip_code character varying(9) UNIQUE, security_name text NOT NULL, security_type character varying(50) NOT NULL, exchange character varying(50), currency character varying(3) NOT NULL, issuer_name character varying(255), sector character varying(100), last_updated timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (security_id) )
CREATE TABLE financial_services.trades ( trade_id character varying(64) NOT NULL, account_id character varying(64) NOT NULL, security_id character varying(64) NOT NULL, order_id character varying(64), trade_type character varying(10) NOT NULL, quantity numeric(19, 8) NOT NULL, price_per_unit numeric(19, 8) NOT NULL, total_amount numeric(19, 4) NOT NULL, commission numeric(10, 4), trade_timestamp timestamp NOT NULL, settlement_date date, status character varying(32) NOT NULL, execution_venue character varying(100), PRIMARY KEY (trade_id), FOREIGN KEY (account_id) REFERENCES financial_services.accounts(account_id), FOREIGN KEY (security_id) REFERENCES financial_services.securities(security_id) )
CREATE TABLE financial_services.market_data ( data_point_id bigserial NOT NULL, security_id character varying(64) NOT NULL, data_timestamp timestamp NOT NULL, open_price numeric(19, 8), high_price numeric(19, 8), low_price numeric(19, 8), close_price numeric(19, 8) NOT NULL, adjusted_close_price numeric(19, 8), volume bigint, source character varying(50), PRIMARY KEY (data_point_id), UNIQUE (security_id, data_timestamp), FOREIGN KEY (security_id) REFERENCES financial_services.securities(security_id) )
CREATE TABLE financial_services.employees ( employee_id character varying(64) NOT NULL, first_name character varying(100) NOT NULL, last_name character varying(100) NOT NULL, job_title character varying(100) NOT NULL, department character varying(100), branch_id character varying(64), email character varying(128) NOT NULL UNIQUE, phone_extension character varying(20), hire_date date NOT NULL, termination_date date, manager_id character varying(64), status character varying(32) NOT NULL, PRIMARY KEY (employee_id), FOREIGN KEY (branch_id) REFERENCES financial_services.branches(branch_id), FOREIGN KEY (manager_id) REFERENCES financial_services.employees(employee_id) )
CREATE TABLE financial_services.loans ( loan_id character varying(64) NOT NULL, account_id character varying(64) NOT NULL UNIQUE, loan_type character varying(50) NOT NULL, principal_amount numeric(19, 4) NOT NULL, current_principal_balance numeric(19, 4) NOT NULL, interest_rate numeric(6, 4) NOT NULL, term_months integer NOT NULL, origination_date date NOT NULL, maturity_date date NOT NULL, monthly_payment numeric(12, 4), collateral_description text, status character varying(32) NOT NULL, PRIMARY KEY (loan_id), FOREIGN KEY (account_id) REFERENCES financial_services.accounts(account_id) )

[User] Show the average close price and total volume for market data, grouped by source.

[Assistant] SELECT source, AVG(close_price), SUM(volume) FROM financial_services.market_data GROUP BY source;

[User] Only include data points with volume greater than 100000 and exclude sources where the total volume is less than 10000000.


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 text PRIMARY KEY, medication_name text, generic_name text, manufacturer text, ndc_code text, description text )

[User] I want to see pairs of medications that share the same manufacturer. Show each medication's name and its peer's medication_name along with the manufacturer.

[Assistant] SELECT t1.medication_name AS med1_name, t2.medication_name AS med2_name, t1.manufacturer FROM healthcare.medications t1 JOIN healthcare.medications t2 ON t1.manufacturer = t2.manufacturer;

[User] Exclude any rows where a medication is paired with itself.

[Assistant] SELECT t1.medication_name AS med1_name, t2.medication_name AS med2_name, t1.manufacturer FROM healthcare.medications t1 JOIN healthcare.medications t2 ON t1.manufacturer = t2.manufacturer AND t1.medication_id <> t2.medication_id;

[User] Now only include manufacturers 'Pfizer' or 'Moderna'.

[Assistant] SELECT t1.medication_name AS med1_name, t2.medication_name AS med2_name, t1.manufacturer FROM healthcare.medications t1 JOIN healthcare.medications t2 ON t1.manufacturer = t2.manufacturer AND t1.medication_id <> t2.medication_id WHERE t1.manufacturer IN ('Pfizer','Moderna');

[User] I’m seeing duplicate pairs for reversed order. Remove reversed duplicates.

[Assistant] SELECT t1.medication_name AS med1_name, t2.medication_name AS med2_name, t1.manufacturer FROM healthcare.medications t1 JOIN healthcare.medications t2 ON t1.manufacturer = t2.manufacturer AND t1.medication_id < t2.medication_id WHERE t1.manufacturer IN ('Pfizer','Moderna');


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 PRIMARY KEY, route_number text, description text, origin_location_id text, destination_location_id text, distance_km numeric, estimated_duration_minutes int, type 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 vehicle that appears in both the maintenance logs and fuel logs tables, calculate the total maintenance cost and the total fuel cost. Display the vehicle ID and these two total costs.

[Assistant] SELECT T1.vehicle_id ,  SUM(T1.cost) AS total_maintenance_cost ,  SUM(T2.total_cost) AS total_fuel_cost FROM transportation.maintenance_logs AS T1 JOIN transportation.fuel_logs AS T2 ON T1.vehicle_id  =  T2.vehicle_id GROUP BY T1.vehicle_id

[User] Now, only include vehicles where the total maintenance cost exceeds 5000.

[Assistant] SELECT T1.vehicle_id ,  SUM(T1.cost) AS total_maintenance_cost ,  SUM(T2.total_cost) AS total_fuel_cost FROM transportation.maintenance_logs AS T1 JOIN transportation.fuel_logs AS T2 ON T1.vehicle_id  =  T2.vehicle_id GROUP BY T1.vehicle_id HAVING SUM(T1.cost)  >  5000

[User] Also, only consider fuel log entries from the year 2023 for the fuel cost calculation.

[Assistant] SELECT T1.vehicle_id ,  SUM(T1.cost) AS total_maintenance_cost ,  SUM(CASE WHEN STRFTIME('%Y', T2.log_timestamp) = '2023' THEN T2.total_cost ELSE 0 END) AS total_fuel_cost FROM transportation.maintenance_logs AS T1 JOIN transportation.fuel_logs AS T2 ON T1.vehicle_id  =  T2.vehicle_id GROUP BY T1.vehicle_id HAVING SUM(T1.cost)  >  5000


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.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.servers (
    server_id uuid NOT NULL DEFAULT gen_random_uuid(), -- unique identifier for the game server instance
    game_id uuid NOT NULL, -- FK linking to the game this server hosts
    server_name character varying(150), -- display name for the server
    region character varying(50) NOT NULL, -- geographical region (e.g., us-east-1, eu-west-2)
    ip_address character varying(45), -- IP address (IPv4 or IPv6)
    port integer, -- network port number
    current_players integer NOT NULL DEFAULT 0, -- number of players currently connected
    max_players integer NOT NULL, -- maximum player capacity
    status character varying(30) NOT NULL DEFAULT 'offline', -- server status (e.g., online, offline, maintenance)
    last_heartbeat timestamp with time zone, -- last time the server reported its status
    PRIMARY KEY (server_id)
    -- FOREIGN KEY (game_id) REFERENCES gaming_technology.games(game_id)
)
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_inventory ( inventory_id uuid NOT NULL DEFAULT gen_random_uuid(), player_id uuid NOT NULL, item_id uuid NOT NULL, quantity integer NOT NULL DEFAULT 1, acquired_at timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, instance_data jsonb, PRIMARY KEY (inventory_id) )

[User] Show the game title and the count of hidden achievements for each game.

[Assistant] SELECT T1.title , COUNT(T2.achievement_id) FROM games AS T1 JOIN achievements AS T2 ON T1.game_id  =  T2.game_id WHERE T2.hidden  =  TRUE GROUP BY T1.title

[User] Filter the results to only include games that have at least one hidden achievement.

[Assistant] SELECT T1.title , COUNT(T2.achievement_id) FROM games AS T1 JOIN achievements AS T2 ON T1.game_id  =  T2.game_id WHERE T2.hidden  =  TRUE GROUP BY T1.title HAVING COUNT(T2.achievement_id)  >=  1

[User] Order the results by the count of hidden achievements descending.

[Assistant] SELECT T1.title , COUNT(T2.achievement_id) FROM games AS T1 JOIN achievements AS T2 ON T1.game_id  =  T2.game_id WHERE T2.hidden  =  TRUE GROUP BY T1.title HAVING COUNT(T2.achievement_id)  >=  1 ORDER BY COUNT(T2.achievement_id) 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 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)
)

[User] List the names of all open cases, the name of the client for each case, and the name of the lead lawyer, but only for cases where the lead lawyer specializes in 'Family Law'.

[Assistant] SELECT T1.case_name ,  T2.first_name ,  T2.last_name ,  T3.first_name ,  T3.last_name FROM legal_services.cases AS T1 JOIN legal_services.clients AS T2 ON T1.client_id  =  T2.client_id JOIN legal_services.lawyers AS T3 ON T1.lead_lawyer_id  =  T3.lawyer_id WHERE T1.status  =  'Open' AND T3.specialization  =  'Family Law'

[User] Actually, could you change the specialization filter to 'Criminal Law' and include the case number? Also, please sort the results by the client's last name.

[Assistant] SELECT T1.case_number ,  T1.case_name ,  T2.first_name ,  T2.last_name ,  T3.first_name ,  T3.last_name FROM legal_services.cases AS T1 JOIN legal_services.clients AS T2 ON T1.client_id  =  T2.client_id JOIN legal_services.lawyers AS T3 ON T1.lead_lawyer_id  =  T3.lawyer_id WHERE T1.status  =  'Open' AND T3.specialization  =  'Criminal Law' ORDER BY T2.last_name

[User] Okay, now just show the cases that were opened after '2023-07-01'.

[Assistant] SELECT T1.case_number ,  T1.case_name ,  T2.first_name ,  T2.last_name ,  T3.first_name ,  T3.last_name FROM legal_services.cases AS T1 JOIN legal_services.clients AS T2 ON T1.client_id  =  T2.client_id JOIN legal_services.lawyers AS T3 ON T1.lead_lawyer_id  =  T3.lawyer_id WHERE T1.status  =  'Open' AND T3.specialization  =  'Criminal Law' AND T1.date_opened  >  '2023-07-01' ORDER BY T2.last_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 token_dexpool_info ( pair_address character varying(512) NOT NULL, -- the id of dexpool token_name character varying(100) NOT NULL, -- the name of the token in the dexpool token_symbol character varying(100) NOT NULL, -- the token's symbol chain text, -- the chain that the dexpool resides price double precision, -- the current price of the token price_change_percentage_1h double precision, -- change in value divided by the absolute price value of the original token price in 1 hour price_change_percentage_6h double precision, -- change in value divided by the absolute price value of the original token price in 6 hours price_change_percentage_24h double precision, -- change in value divided by the absolute price value of the original token price in 24 hours makers_24h double precision, -- the makers of the dex pool in 24 hours volume_24h double precision, -- the trade volume of the dex pool in 24 hours liquidity double precision, -- the liquidity of the dex pool market_cap double precision, -- the market cap of the token trend_rank bigint, -- the rank of token in the dex pool of recent trending, value 1 expresses the most popular token. ,PRIMARY KEY (pair_address, chain) )

[User] Which two tokens on each chain have the greatest difference in their 24h price change percentage? Show token_symbol A, token_symbol B, chain, and the absolute difference.

[Assistant]
SELECT DISTINCT ON (t1.chain)
  t1.token_symbol AS symbol_a,
  t2.token_symbol AS symbol_b,
  t1.chain,
  ABS(t1.price_change_percentage_24h - t2.price_change_percentage_24h) AS diff_24h
FROM token_dexpool_info t1
JOIN token_dexpool_info t2
  ON t1.chain = t2.chain
  AND t1.token_symbol < t2.token_symbol
ORDER BY t1.chain, diff_24h DESC;

[User] Actually, I want only the chains where this top difference is at least 15% and show the top 2 pairs per chain.

[Assistant]
SELECT symbol_a, symbol_b, chain, diff_24h
FROM (
  SELECT
    t1.token_symbol AS symbol_a,
    t2.token_symbol AS symbol_b,
    t1.chain,
    ABS(t1.price_change_percentage_24h - t2.price_change_percentage_24h) AS diff_24h,
    ROW_NUMBER() OVER(
      PARTITION BY t1.chain
      ORDER BY ABS(t1.price_change_percentage_24h - t2.price_change_percentage_24h) DESC
    ) AS rn
  FROM token_dexpool_info t1
  JOIN token_dexpool_info t2
    ON t1.chain = t2.chain
    AND t1.token_symbol < t2.token_symbol
  WHERE ABS(t1.price_change_percentage_24h - t2.price_change_percentage_24h) >= 15
) sub
WHERE rn <= 2
ORDER BY chain, diff_24h DESC;

[User] Also sort the overall result by diff_24h descending and limit to the top 5 records.

[Assistant]
SELECT *
FROM (
  SELECT symbol_a, symbol_b, chain, diff_24h
  FROM (
    SELECT
      t1.token_symbol AS symbol_a,
      t2.token_symbol AS symbol_b,
      t1.chain,
      ABS(t1.price_change_percentage_24h - t2.price_change_percentage_24h) AS diff_24h,
      ROW_NUMBER() OVER(
        PARTITION BY t1.chain
        ORDER BY ABS(t1.price_change_percentage_24h - t2.price_change_percentage_24h) DESC
      ) AS rn
    FROM token_dexpool_info t1
    JOIN token_dexpool_info t2
      ON t1.chain = t2.chain
      AND t1.token_symbol < t2.token_symbol
    WHERE ABS(t1.price_change_percentage_24h - t2.price_change_percentage_24h) >= 15
  ) sub1
  WHERE rn <= 2
) sub2
ORDER BY diff_24h DESC
LIMIT 5;

[User] Could you include the current price of both tokens in the output as well?

[Assistant]
SELECT symbol_a, symbol_b, chain, price_a, price_b, diff_24h
FROM (
  SELECT
    t1.token_symbol AS symbol_a,
    t2.token_symbol AS symbol_b,
    t1.chain,
    t1.price AS price_a,
    t2.price AS price_b,
    ABS(t1.price_change_percentage_24h - t2.price_change_percentage_24h) AS diff_24h,
    ROW_NUMBER() OVER(
      PARTITION BY t1.chain
      ORDER BY ABS(t1.price_change_percentage_24h - t2.price_change_percentage_24h) DESC
    ) AS rn
  FROM token_dexpool_info t1
  JOIN token_dexpool_info t2
    ON t1.chain = t2.chain
    AND t1.token_symbol < t2.token_symbol
  WHERE ABS(t1.price_change_percentage_24h - t2.price_change_percentage_24h) >= 15
) sub
WHERE rn <= 2
ORDER BY diff_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 transportation.trips (
    trip_id character varying(64) NOT NULL,  -- unique identifier for a specific trip instance
    route_id character varying(64), -- identifier of the route being followed (can be null for ad-hoc trips)
    vehicle_id character varying(64) NOT NULL, -- identifier of the vehicle used
    driver_id character varying(64) NOT NULL, -- identifier of the driver operating
    scheduled_departure timestamp with time zone NOT NULL, -- planned departure time
    actual_departure timestamp with time zone, -- actual departure time
    scheduled_arrival timestamp with time zone NOT NULL, -- planned arrival time
    actual_arrival timestamp with time zone, -- actual arrival time
    origin_location_id character varying(64) NOT NULL, -- actual starting location (could differ from route origin)
    destination_location_id character varying(64) NOT NULL, -- actual ending location (could differ from route destination)
    status character varying(32) NOT NULL, -- e.g., Scheduled, In Progress, Completed, Delayed, Cancelled
    passenger_count integer, -- number of passengers (if applicable)
    cargo_manifest_id character varying(64), -- link to cargo details (if applicable)
    PRIMARY KEY (trip_id)
)

[User] Can you show me all pairs of trips that use the same vehicle but have different drivers? Please include both trip IDs, vehicle_id, and the driver_id for each trip.

[Assistant]
SELECT t1.trip_id AS trip_id_1, t2.trip_id AS trip_id_2, t1.vehicle_id, t1.driver_id AS driver_id_1, t2.driver_id AS driver_id_2
FROM transportation.trips t1
JOIN transportation.trips t2
  ON t1.vehicle_id = t2.vehicle_id
 AND t1.trip_id < t2.trip_id
 AND t1.driver_id <> t2.driver_id;

[User] Actually, I also want to see only those pairs where both trips have a status of 'Completed'.

[Assistant]
SELECT t1.trip_id AS trip_id_1, t2.trip_id AS trip_id_2, t1.vehicle_id, t1.driver_id AS driver_id_1, t2.driver_id AS driver_id_2
FROM transportation.trips t1
JOIN transportation.trips t2
  ON t1.vehicle_id = t2.vehicle_id
 AND t1.trip_id < t2.trip_id
 AND t1.driver_id <> t2.driver_id
WHERE t1.status = 'Completed' AND t2.status = 'Completed';


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 blockchain.nodes (
    node_id character varying(64) NOT NULL,  -- unique identifier for the node
    ip_address character varying(15) NOT NULL, -- IP address of the node
    port integer NOT NULL, -- port number of the node
    last_seen timestamp NOT NULL, -- last time the node was active
    PRIMARY KEY (node_id)
)
CREATE TABLE blockchain.wallets ( wallet_id.text, user_id.text, address_id.text, private_key.text )
CREATE TABLE blockchain.users ( user_id character varying(64) NOT NULL, username character varying(128) NOT NULL, email character varying(128) NOT NULL, password_hash text NOT NULL, PRIMARY KEY (user_id) )
CREATE TABLE blockchain.smart_contracts ( contract_id character varying(64) NOT NULL, creator_address character varying(128) NOT NULL, code text NOT NULL, timestamp timestamp NOT NULL, PRIMARY KEY (contract_id) )

[User] Retrieve all users and their associated wallet identifiers.

[Assistant] SELECT T1.username ,  T2.wallet_id FROM blockchain.users AS T1 JOIN blockchain.wallets AS T2 ON T1.user_id  =  T2.user_id

[User] Only show users whose username starts with 'J'.

[Assistant] SELECT T1.username ,  T2.wallet_id FROM blockchain.users AS T1 JOIN blockchain.wallets AS T2 ON T1.user_id  =  T2.user_id WHERE T1.username LIKE 'J%'

[User] Further filter to only include wallets where the address_id contains 'abc'.

[Assistant] SELECT T1.username ,  T2.wallet_id FROM blockchain.users AS T1 JOIN blockchain.wallets AS T2 ON T1.user_id  =  T2.user_id WHERE T1.username LIKE 'J%' AND T2.address_id LIKE '%abc%'

[User] For the filtered users and wallets, count how many wallets each user has.

[Assistant] SELECT T1.username ,  COUNT(T2.wallet_id) FROM blockchain.users AS T1 JOIN blockchain.wallets AS T2 ON T1.user_id  =  T2.user_id WHERE T1.username LIKE 'J%' AND T2.address_id LIKE '%abc%' GROUP BY T1.username


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] List all pairs of token transfers occurring within the same transaction where one transferred value is greater than the other, showing both transfer IDs, the transaction ID, and both values.

[Assistant]
SELECT t1.transaction_id,
       t1.transfer_id AS transfer_id_1,
       t2.transfer_id AS transfer_id_2,
       t1.value       AS value_1,
       t2.value       AS value_2
FROM web3.token_transfers t1
JOIN web3.token_transfers t2
  ON t1.transaction_id = t2.transaction_id
 AND t1.value > t2.value;


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.conversations ( conversation_id text PRIMARY KEY, created_at timestamp, last_message_at timestamp, is_group_chat bool, group_name text )

[User] List group conversation names and the count of messages sent in each conversation after '2024-06-01'.

[Assistant] SELECT T1.group_name, COUNT(T2.message_id) FROM social_media.conversations AS T1 JOIN social_media.messages AS T2 ON T1.conversation_id = T2.conversation_id WHERE T1.is_group_chat = TRUE AND T2.sent_at > '2024-06-01' GROUP BY T1.conversation_id, T1.group_name;

[User] Only show conversations that have more than 5 messages sent after that date.

[Assistant] SELECT T1.group_name, COUNT(T2.message_id) FROM social_media.conversations AS T1 JOIN social_media.messages AS T2 ON T1.conversation_id = T2.conversation_id WHERE T1.is_group_chat = TRUE AND T2.sent_at > '2024-06-01' GROUP BY T1.conversation_id, T1.group_name HAVING COUNT(T2.message_id) > 5;