conversations
listlengths 3
3
|
---|
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total amount of all deposits made by traders who have a trading account with a balance greater than $20,000 in the year 2022?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT SUM(COALESCE(T.transaction_amount, 0))\nFROM Transactions T\nJOIN TradingAccounts TA ON T.account_id = TA.account_id\nWHERE T.transaction_type = 'Deposit'\nAND T.transaction_date BETWEEN ? AND ?\nAND TA.account_balance > 20000"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average exchange rate for all trades executed by traders who have a trading account with a balance greater than $30,000 in the year 2022?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT AVG(tr.executed_rate)\nFROM Trades tr\nJOIN Orders ord ON tr.order_id = ord.order_id\nJOIN TradingAccounts ta ON ord.account_id = ta.account_id\nWHERE ta.account_balance > 30000\n AND DATE(tr.executed_at) BETWEEN '2022-01-01' AND '2022-12-31';"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total value of all trades executed by traders who have made at least 5 trades in the year 2022 and have a trading account with a balance greater than $40,000?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT SUM(T.trade_value)\nFROM Trades T\nJOIN Orders O ON T.order_id = O.order_id\nJOIN TradingAccounts TA ON O.account_id = TA.account_id\nJOIN Traders TR ON TA.trader_id = TR.trader_id\nWHERE TR.trader_id IN (\n SELECT TR.trader_id\n FROM Trades T\n JOIN Orders O ON T.order_id = O.order_id\n JOIN TradingAccounts TA ON O.account_id = TA.account_id\n JOIN Traders TR ON TA.trader_id = TR.trader_id\n WHERE T.executed_at BETWEEN '2022-01-01' AND '2022-12-31'\n GROUP BY TR.trader_id\n HAVING COUNT(T.trade_id) >= 5\n)\nAND TA.account_balance > 40000"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the highest exchange rate for the currency pair EUR/GBP in the year 2022?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "-- Get the highest exchange rate for the currency pair EUR/GBP in the year 2022\nSELECT MAX(forex_rates.exchange_rate) \nFROM ForexRates forex_rates \nJOIN Currencies base_currency ON forex_rates.base_currency_id = base_currency.currency_id \nJOIN Currencies quote_currency ON forex_rates.quote_currency_id = quote_currency.currency_id \nWHERE forex_rates.rate_date BETWEEN '2022-01-01' AND '2022-12-31' \nAND base_currency.currency_code = 'EUR' \nAND quote_currency.currency_code = 'GBP';"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total amount of all withdrawals made by traders who have a trading account with a balance greater than $50,000 in the year 2022?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT SUM(WD.withdrawal_amount) \nFROM Withdrawals WD \nJOIN TradingAccounts TAC ON WD.account_id = TAC.account_id \nWHERE WD.withdrawal_date BETWEEN DATE('2022-01-01') AND DATE('2022-12-31') \nAND TAC.account_balance > 50000;"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average exchange rate for all trades executed by traders who have made at least 10 trades in the year 2022 and have a trading account with a balance greater than $60,000?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "WITH active_traders AS (\n SELECT TR.trader_id\n FROM Trades T\n JOIN Orders O ON T.order_id = O.order_id\n JOIN TradingAccounts TA ON O.account_id = TA.account_id\n JOIN Traders TR ON TA.trader_id = TR.trader_id\n WHERE T.executed_at BETWEEN '2022-01-01' AND '2022-12-31'\n GROUP BY TR.trader_id\n HAVING COUNT(T.trade_id) >= 10\n)\nSELECT AVG(T.executed_rate)\nFROM Trades T\nJOIN Orders O ON T.order_id = O.order_id\nJOIN TradingAccounts TA ON O.account_id = TA.account_id\nJOIN Traders TR ON TA.trader_id = TR.trader_id\nJOIN active_traders AT ON TR.trader_id = AT.trader_id\nWHERE TA.account_balance > 60000"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total value of all trades executed by traders who have a trading account with a balance greater than $70,000 and have made at least 5 trades in the year 2022?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT SUM(T.trade_value)\nFROM Trades T\nJOIN Orders O ON T.order_id = O.order_id\nJOIN TradingAccounts TA ON O.account_id = TA.account_id\nJOIN Traders TR ON TA.trader_id = TR.trader_id\nWHERE TA.account_balance > 70000\nAND TR.trader_id IN (\n SELECT TR.trader_id\n FROM Trades T\n JOIN Orders O ON T.order_id = O.order_id\n JOIN TradingAccounts TA ON O.account_id = TA.account_id\n JOIN Traders TR ON TA.trader_id = TR.trader_id\n WHERE T.executed_at BETWEEN '2022-01-01' AND '2022-12-31'\n GROUP BY TR.trader_id\n HAVING COUNT(T.trade_id) >= 5\n)"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the highest exchange rate for the currency pair USD/JPY in the year 2022?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT MAX(T.executed_rate)\nFROM Trades T\nJOIN Orders O ON T.order_id = O.order_id\nJOIN Currencies C1 ON O.base_currency_id = C1.currency_id\nJOIN Currencies C2 ON O.quote_currency_id = C2.currency_id\nWHERE T.executed_at BETWEEN '2022-01-01' AND '2022-12-31'\nAND C1.currency_code = 'USD'\nAND C2.currency_code = 'JPY'"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total amount of all deposits made by traders who have a trading account with a balance greater than $80,000 in the year 2022?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT SUM(T.transaction_amount)\nFROM Transactions T\nJOIN TradingAccounts TA ON T.account_id = TA.account_id\nWHERE T.transaction_type = 'Deposit'\nAND T.transaction_date BETWEEN '2022-01-01' AND '2022-12-31'\nAND TA.account_balance > 80000"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average exchange rate for all trades executed by traders who have made at least 15 trades in the year 2022 and have a trading account with a balance greater than $90,000?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT AVG(T.executed_rate)\nFROM Trades T\nJOIN Orders O ON T.order_id = O.order_id\nJOIN TradingAccounts TA ON O.account_id = TA.account_id\nJOIN Traders TR ON TA.trader_id = TR.trader_id\nWHERE TR.trader_id IN (\n SELECT TR.trader_id\n FROM Trades T\n JOIN Orders O ON T.order_id = O.order_id\n JOIN TradingAccounts TA ON O.account_id = TA.account_id\n JOIN Traders TR ON TA.trader_id = TR.trader_id\n WHERE T.executed_at BETWEEN '2022-01-01' AND '2022-12-31'\n GROUP BY TR.trader_id\n HAVING COUNT(T.trade_id) >= 15\n)\nAND TA.account_balance > 90000"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total value of all trades executed by traders who have a trading account with a balance greater than $100,000 and have made at least 10 trades in the year 2022?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT SUM(T.trade_value)\nFROM Trades T\nJOIN Orders O ON T.order_id = O.order_id\nJOIN TradingAccounts TA ON O.account_id = TA.account_id\nJOIN Traders TR ON TA.trader_id = TR.trader_id\nWHERE TA.account_balance > 100000\nAND TR.trader_id IN (\n SELECT TR.trader_id\n FROM Trades T\n JOIN Orders O ON T.order_id = O.order_id\n JOIN TradingAccounts TA ON O.account_id = TA.account_id\n JOIN Traders TR ON TA.trader_id = TR.trader_id\n WHERE T.executed_at BETWEEN '2022-01-01' AND '2022-12-31'\n GROUP BY TR.trader_id\n HAVING COUNT(T.trade_id) >= 10\n)"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the highest exchange rate for the currency pair EUR/CHF in the year 2022?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT MAX(T.executed_rate)\nFROM Trades T\nJOIN Orders O ON T.order_id = O.order_id\nJOIN Currencies C1 ON O.base_currency_id = C1.currency_id\nJOIN Currencies C2 ON O.quote_currency_id = C2.currency_id\nWHERE T.executed_at BETWEEN '2022-01-01' AND '2022-12-31'\nAND C1.currency_code = 'EUR'\nAND C2.currency_code = 'CHF'"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total amount of all withdrawals made by traders who have a trading account with a balance greater than $110,000 in the year 2022?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT SUM(WD.withdrawal_amount) AS total_withdrawal_amount \nFROM Withdrawals WD \nJOIN TradingAccounts TAC ON WD.account_id = TAC.account_id \nWHERE WD.withdrawal_date BETWEEN '2022-01-01' AND '2022-12-31' \nAND TAC.account_balance > 110000;"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average exchange rate for all trades executed by traders who have made at least 20 trades in the year 2022 and have a trading account with a balance greater than $120,000?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "WITH active_traders AS (\n SELECT trader_id\n FROM Trades\n JOIN Orders ON Trades.order_id = Orders.order_id\n JOIN TradingAccounts ON Orders.account_id = TradingAccounts.account_id\n WHERE Trades.executed_at >= '2022-01-01'\n AND Trades.executed_at < '2023-01-01'\n GROUP BY trader_id\n HAVING COUNT(Trades.trade_id) >= 20\n)\nSELECT AVG(Trades.executed_rate)\nFROM Trades\nJOIN Orders ON Trades.order_id = Orders.order_id\nJOIN TradingAccounts ON Orders.account_id = TradingAccounts.account_id\nJOIN Traders ON TradingAccounts.trader_id = Traders.trader_id\nWHERE Traders.trader_id IN (SELECT trader_id FROM active_traders)\n AND TradingAccounts.account_balance > 120000;"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total value of all pending orders in USD for traders who have a balance greater than $10,000 in their trading accounts, and what is the average order amount for these pending orders?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n SUM(o.order_amount * fr.exchange_rate) AS total_value, \n AVG(o.order_amount) AS avg_order_amount \nFROM \n Orders o \nJOIN \n TradingAccounts ta ON o.account_id = ta.account_id \nJOIN \n ForexRates fr ON o.quote_currency_id = fr.quote_currency_id \nWHERE \n o.order_status = 'Pending' \n AND ta.account_balance > 10000 \n AND fr.base_currency_id = (SELECT currency_id FROM Currencies WHERE currency_code = 'USD')"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: Which traders have the highest number of executed orders in the last 30 days, and what is the total value of these orders?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n tr.first_name, \n tr.last_name, \n COUNT(DISTINCT o.order_id) AS num_orders, \n SUM(o.order_amount * fr.exchange_rate) AS total_value \nFROM \n Orders o \nJOIN \n TradingAccounts ta ON o.account_id = ta.account_id \nJOIN \n Traders tr ON ta.trader_id = tr.trader_id \nJOIN \n ForexRates fr ON o.quote_currency_id = fr.quote_currency_id \n AND o.base_currency_id = fr.base_currency_id \nWHERE \n o.order_status = 'Executed' \n AND o.executed_at > CURRENT_DATE - INTERVAL 30 DAY \nGROUP BY \n tr.trader_id \nORDER BY \n num_orders DESC \nLIMIT 1;"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average execution rate for all executed orders in the last 7 days, and what is the standard deviation of these execution rates?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "-- Calculate the average and standard deviation of executed rates for orders executed in the last 7 days\nSELECT \n AVG(o.executed_rate) AS avg_execution_rate, \n STDEV(o.executed_rate) AS std_dev \nFROM \n Orders o \nWHERE \n o.order_status = 'Executed' \n AND o.executed_at > CURRENT_DATE - INTERVAL 7 DAY;"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: Which currencies have the highest number of pending orders, and what is the total value of these orders?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n c.currency_code, \n COUNT(o.order_id) AS num_orders, \n SUM(o.order_amount * fr.exchange_rate) AS total_value \nFROM \n Orders o \nJOIN \n Currencies c ON o.base_currency_id = c.currency_id \nJOIN \n ForexRates fr ON o.quote_currency_id = fr.quote_currency_id \nWHERE \n o.order_status = 'Pending' \nGROUP BY \n c.currency_code \nORDER BY \n num_orders DESC \nLIMIT 1;"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total value of all executed orders for traders who have a balance greater than $50,000 in their trading accounts, and what is the average order amount for these executed orders?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n SUM(o.order_amount * fr.exchange_rate) AS total_value, \n AVG(o.order_amount) AS avg_order_amount \nFROM \n Orders o \nJOIN \n TradingAccounts ta ON o.account_id = ta.account_id \nJOIN \n ForexRates fr ON o.quote_currency_id = fr.quote_currency_id \nWHERE \n o.order_status = 'Executed' AND ta.account_balance > 50000"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: Which traders have the highest number of pending orders, and what is the total value of these orders?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "-- Retrieve traders with the highest number of pending orders and the total value of these orders\nSELECT \n t.first_name, \n t.last_name, \n COUNT(o.order_id) AS num_orders, \n SUM(o.order_amount * forex_rates.exchange_rate) AS total_value \nFROM \n Orders o \nJOIN \n TradingAccounts ta ON o.account_id = ta.account_id \nJOIN \n Traders t ON ta.trader_id = t.trader_id \nJOIN \n ForexRates forex_rates ON o.quote_currency_id = forex_rates.quote_currency_id \n AND o.base_currency_id = forex_rates.base_currency_id \nWHERE \n o.order_status = 'Pending' \nGROUP BY \n t.trader_id \nORDER BY \n num_orders DESC \nLIMIT 1;"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average execution rate for all executed orders in the last 30 days, and what is the standard deviation of these execution rates?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "-- Calculate the average and standard deviation of executed rates for orders executed in the last 30 days\nSELECT \n AVG(o.executed_rate) AS avg_execution_rate, \n STDEV(o.executed_rate) AS std_dev \nFROM \n Orders o \nWHERE \n o.order_status = 'Executed' \n AND o.executed_at > DATE('now', '-30 day');"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: Which currencies have the highest number of executed orders, and what is the total value of these orders?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n c.currency_code, \n COUNT(o.order_id) AS num_orders, \n SUM(o.order_amount * fr.exchange_rate) AS total_value \nFROM \n Orders o \nJOIN \n Currencies c ON o.base_currency_id = c.currency_id \nJOIN \n ForexRates fr ON o.quote_currency_id = fr.quote_currency_id \nWHERE \n o.order_status = 'Executed' \nGROUP BY \n c.currency_code \nORDER BY \n num_orders DESC \nLIMIT 1;"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total value of all pending orders for traders who have a balance greater than $20,000 in their trading accounts, and what is the average order amount for these pending orders?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n ta.trader_id, \n SUM(o.order_amount * fr.exchange_rate) AS total_value, \n AVG(o.order_amount) AS avg_order_amount \nFROM \n Orders o \nJOIN \n TradingAccounts ta ON o.account_id = ta.account_id \nJOIN \n ForexRates fr ON o.quote_currency_id = fr.quote_currency_id \n AND o.base_currency_id = fr.base_currency_id \nWHERE \n o.order_status = 'Pending' \n AND ta.account_balance > 20000 \n AND ta.currency_id = (SELECT currency_id FROM Currencies WHERE currency_code = 'USD') \nGROUP BY \n ta.trader_id \nHAVING \n SUM(o.order_amount * fr.exchange_rate) > 100000"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: Which traders have the highest number of executed orders in the last 7 days, and what is the total value of these orders?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "-- Retrieve the trader with the most executed orders in the last 7 days\nSELECT \n t.first_name, \n t.last_name, \n COUNT(o.order_id) AS num_orders, \n SUM(o.order_amount * fr.exchange_rate) AS total_value \nFROM \n Orders o \nJOIN \n TradingAccounts trading_account ON o.account_id = trading_account.account_id \nJOIN \n Traders t ON trading_account.trader_id = t.trader_id \nJOIN \n ForexRates fr ON o.quote_currency_id = fr.quote_currency_id \n AND o.base_currency_id = fr.base_currency_id \nWHERE \n o.order_status = 'Executed' \n AND o.executed_at IS NOT NULL \n AND o.executed_at > DATE('now', '-7 days') \nGROUP BY \n t.trader_id \nORDER BY \n num_orders DESC \nLIMIT 1;"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average execution rate for all executed orders in the last 14 days, and what is the standard deviation of these execution rates?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "-- Calculate average execution rate and standard deviation for executed orders in the last 14 days\nSELECT \n AVG(o.executed_rate) AS avg_execution_rate, \n STDEV(o.executed_rate) AS std_dev \nFROM \n Orders o \nWHERE \n o.order_status = 'Executed' \n AND o.executed_at > DATE('now', '-14 days');"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: Which currencies have the highest number of pending orders in the last 30 days, and what is the total value of these orders?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n c.currency_code, \n COUNT(o.order_id) AS num_orders, \n SUM(o.order_amount * fr.exchange_rate) AS total_value \nFROM \n Orders o \nJOIN \n Currencies c ON o.base_currency_id = c.currency_id \nJOIN \n ForexRates fr ON o.quote_currency_id = fr.quote_currency_id \nWHERE \n o.order_status = 'Pending' \n AND o.placed_at > DATE('now', '-30 days') \nGROUP BY \n c.currency_code \nORDER BY \n num_orders DESC \nLIMIT 1;"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total value of all executed orders for traders who have a balance greater than $30,000 in their trading accounts, and what is the average order amount for these executed orders?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n SUM(o.order_amount * fr.exchange_rate) AS total_value, \n AVG(o.order_amount) AS avg_order_amount \nFROM \n Orders o \nJOIN \n TradingAccounts ta ON o.account_id = ta.account_id \nJOIN \n ForexRates fr ON o.quote_currency_id = fr.quote_currency_id \nWHERE \n o.order_status = 'Executed' AND ta.account_balance > 30000"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: Which traders have the highest number of pending orders in the last 14 days, and what is the total value of these orders?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n t.first_name, \n t.last_name, \n COUNT(o.order_id) AS num_orders, \n SUM(o.order_amount * fr.exchange_rate) AS total_value \nFROM \n Orders o \nJOIN \n TradingAccounts ta ON o.account_id = ta.account_id \nJOIN \n Traders t ON ta.trader_id = t.trader_id \nJOIN \n ForexRates fr ON o.quote_currency_id = fr.quote_currency_id \nWHERE \n o.order_status = 'Pending' \n AND o.placed_at > DATE('now', '-14 days') \nGROUP BY \n t.trader_id \nORDER BY \n num_orders DESC \nLIMIT 1;"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average execution rate for all executed orders in the last 60 days, and what is the standard deviation of these execution rates?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "-- Calculate the average and standard deviation of executed rates for orders in the last 60 days\nSELECT \n AVG(o.executed_rate) AS avg_execution_rate, \n STDEV(o.executed_rate) AS std_dev \nFROM \n Orders o \nWHERE \n o.order_status = 'Executed' \n AND o.executed_at > DATE('now', '-60 days', 'localtime');"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: Which currencies have the highest number of executed orders in the last 60 days, and what is the total value of these orders?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n c.currency_code, \n COUNT(o.order_id) AS num_orders, \n SUM(o.order_amount * fr.exchange_rate) AS total_value \nFROM \n Orders o \nJOIN \n Currencies c ON o.base_currency_id = c.currency_id \nJOIN \n ForexRates fr ON o.quote_currency_id = fr.quote_currency_id \nWHERE \n o.order_status = 'Executed' \n AND o.executed_at > DATE('now', '-60 days') \nGROUP BY \n c.currency_code \nORDER BY \n num_orders DESC \nLIMIT 1;"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total value of all pending orders for traders who have a balance greater than $40,000 in their trading accounts, and what is the average order amount for these pending orders?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n SUM(o.order_amount * fr.exchange_rate) AS total_value, \n AVG(o.order_amount) AS avg_order_amount \nFROM \n Orders o \nJOIN \n TradingAccounts ta ON o.account_id = ta.account_id \nJOIN \n ForexRates fr ON o.quote_currency_id = fr.quote_currency_id \nWHERE \n o.order_status = 'Pending' AND ta.account_balance > 40000"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: Which traders have the highest number of executed orders in the last 30 days, and what is the total value of these orders?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n tr.first_name, \n tr.last_name, \n COUNT(DISTINCT o.order_id) AS num_orders, \n SUM(o.order_amount * fr.exchange_rate) AS total_value \nFROM \n Orders o \nJOIN \n TradingAccounts ta ON o.account_id = ta.account_id \nJOIN \n Traders tr ON ta.trader_id = tr.trader_id \nJOIN \n ForexRates fr ON o.quote_currency_id = fr.quote_currency_id \n AND o.base_currency_id = fr.base_currency_id \nWHERE \n o.order_status = 'Executed' \n AND o.executed_at > CURRENT_DATE - INTERVAL 30 DAY \nGROUP BY \n tr.trader_id \nORDER BY \n num_orders DESC \nLIMIT 1;"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total amount of all executed orders for each currency pair, where the base currency is 'USD' and the order status is 'Executed', and the order was placed in the last 30 days?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n o.base_currency_id, \n o.quote_currency_id, \n SUM(o.order_amount) AS total_amount\nFROM \n Orders o\nWHERE \n o.base_currency_id = (SELECT currency_id FROM Currencies WHERE currency_code = 'USD')\n AND o.order_status = 'Executed'\n AND o.placed_at >= DATE('now', '-30 days')\nGROUP BY \n o.base_currency_id, \n o.quote_currency_id"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: Which traders have the highest number of canceled orders, and what is the total amount of these orders?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n t.trader_id, \n t.first_name, \n t.last_name, \n COUNT(o.order_id) AS num_canceled_orders, \n SUM(o.order_amount) AS total_amount\nFROM \n Traders t\nJOIN \n TradingAccounts ta ON t.trader_id = ta.trader_id\nJOIN \n Orders o ON ta.account_id = o.account_id\nWHERE \n o.order_status = 'Canceled'\nGROUP BY \n t.trader_id, \n t.first_name, \n t.last_name\nORDER BY \n num_canceled_orders DESC\nLIMIT 1"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average execution rate for each currency pair, where the order status is 'Executed' and the order was placed in the last 7 days?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n o.base_currency_id, \n o.quote_currency_id, \n AVG(o.executed_rate) AS avg_execution_rate\nFROM \n Orders o\nWHERE \n o.order_status = 'Executed'\n AND o.placed_at >= DATE('now', '-7 days')\nGROUP BY \n o.base_currency_id, \n o.quote_currency_id"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: Which trading accounts have the highest balance, and what is the total amount of all executed orders for these accounts?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "WITH ExecutedOrders AS (\n -- Calculate total amount of executed orders for each account\n SELECT \n o.account_id, \n SUM(o.order_amount) AS total_amount\n FROM \n Orders o\n WHERE \n o.order_status = 'Executed'\n GROUP BY \n o.account_id\n)\nSELECT \n ta.account_id AS account_id, \n ta.account_balance, \n eo.total_amount\nFROM \n TradingAccounts ta\nJOIN \n ExecutedOrders eo ON ta.account_id = eo.account_id\nORDER BY \n ta.account_balance DESC\nLIMIT 1;"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total amount of all pending orders for each currency pair, where the base currency is 'EUR' and the order was placed in the last 14 days?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n o.base_currency_id, \n o.quote_currency_id, \n SUM(o.order_amount) AS total_amount\nFROM \n Orders o\nWHERE \n o.base_currency_id = (SELECT currency_id FROM Currencies WHERE currency_code = 'EUR')\n AND o.order_status = 'Pending'\n AND o.placed_at >= DATE('now', '-14 days')\nGROUP BY \n o.base_currency_id, \n o.quote_currency_id"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: Which traders have the highest number of executed orders, and what is the total amount of these orders?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n t.trader_id, \n t.first_name, \n t.last_name, \n COUNT(o.order_id) AS num_executed_orders, \n SUM(o.order_amount) AS total_amount\nFROM \n Traders t\nJOIN \n TradingAccounts ta ON t.trader_id = ta.trader_id\nJOIN \n Orders o ON ta.account_id = o.account_id\nWHERE \n o.order_status = 'Executed'\nGROUP BY \n t.trader_id, \n t.first_name, \n t.last_name\nORDER BY \n num_executed_orders DESC\nLIMIT 1"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average order amount for each currency pair, where the order status is 'Executed' and the order was placed in the last 30 days?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n o.base_currency_id, \n o.quote_currency_id, \n AVG(o.order_amount) AS avg_order_amount\nFROM \n Orders o\nWHERE \n o.order_status = 'Executed'\n AND o.placed_at >= DATE('now', '-30 days')\nGROUP BY \n o.base_currency_id, \n o.quote_currency_id"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: Which trading accounts have the highest number of canceled orders, and what is the total amount of these orders?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n ta.account_id, \n COUNT(o.order_id) AS num_canceled_orders, \n SUM(o.order_amount) AS total_amount\nFROM \n TradingAccounts ta\nJOIN \n Orders o ON ta.account_id = o.account_id\nWHERE \n o.order_status = 'Canceled'\nGROUP BY \n ta.account_id\nORDER BY \n num_canceled_orders DESC\nLIMIT 1"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total amount of all executed orders for each trader, where the order status is 'Executed' and the order was placed in the last 7 days?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n t.trader_id, \n t.first_name, \n t.last_name, \n SUM(o.order_amount) AS total_amount\nFROM \n Traders t\nJOIN \n TradingAccounts ta ON t.trader_id = ta.trader_id\nJOIN \n Orders o ON ta.account_id = o.account_id\nWHERE \n o.order_status = 'Executed'\n AND o.placed_at >= DATE('now', '-7 days')\nGROUP BY \n t.trader_id, \n t.first_name, \n t.last_name"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: Which currency pairs have the highest number of executed orders, and what is the total amount of these orders?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "-- Retrieve the currency pairs with the most executed orders\nSELECT \n c1.currency_code base_currency,\n c2.currency_code quote_currency,\n COUNT(o.order_id) num_executed_orders,\n SUM(o.order_amount) total_amount\nFROM \n Orders o\n JOIN Currencies c1 ON o.base_currency_id = c1.currency_id\n JOIN Currencies c2 ON o.quote_currency_id = c2.currency_id\nWHERE \n o.order_status = 'Executed'\nGROUP BY \n c1.currency_code,\n c2.currency_code\nORDER BY \n num_executed_orders DESC\n-- Use RANK or DENSE_RANK to retrieve all pairs with the maximum number of executed orders\n-- However, since SQLite does not support these functions, we'll use a subquery instead\nLIMIT (SELECT COUNT(*) FROM (\n SELECT \n COUNT(o.order_id) num_executed_orders\n FROM \n Orders o\n WHERE \n o.order_status = 'Executed'\n GROUP BY \n o.base_currency_id,\n o.quote_currency_id\n) AS subquery WHERE num_executed_orders = (\n SELECT \n MAX(num_executed_orders)\n FROM (\n SELECT \n COUNT(o.order_id) num_executed_orders\n FROM \n Orders o\n WHERE \n o.order_status = 'Executed'\n GROUP BY \n o.base_currency_id,\n o.quote_currency_id\n ) AS subquery2\n));"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average execution rate for each trading account, where the order status is 'Executed' and the order was placed in the last 14 days?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n ta.account_id, \n AVG(o.executed_rate) AS avg_execution_rate\nFROM \n TradingAccounts ta\nJOIN \n Orders o ON ta.account_id = o.account_id\nWHERE \n o.order_status = 'Executed'\n AND o.placed_at >= DATE('now', '-14 days')\nGROUP BY \n ta.account_id"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: Which traders have the highest number of pending orders, and what is the total amount of these orders?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n t.trader_id, \n t.first_name, \n t.last_name, \n COUNT(o.order_id) AS num_pending_orders, \n SUM(o.order_amount) AS total_amount\nFROM \n Traders t\nJOIN \n TradingAccounts ta ON t.trader_id = ta.trader_id\nJOIN \n Orders o ON ta.account_id = o.account_id\nWHERE \n o.order_status = 'Pending'\nGROUP BY \n t.trader_id, \n t.first_name, \n t.last_name\nORDER BY \n num_pending_orders DESC\nLIMIT 1"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total amount of all canceled orders for each currency pair, where the base currency is 'USD' and the order was placed in the last 30 days?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n o.base_currency_id, \n o.quote_currency_id, \n SUM(o.order_amount) AS total_amount\nFROM \n Orders o\nWHERE \n o.base_currency_id = (SELECT currency_id FROM Currencies WHERE currency_code = 'USD')\n AND o.order_status = 'Canceled'\n AND o.placed_at >= DATE('now', '-30 days')\nGROUP BY \n o.base_currency_id, \n o.quote_currency_id"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: Which trading accounts have the highest balance, and what is the total amount of all pending orders for these accounts?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "-- Rank trading accounts by balance in descending order\nWITH ranked_accounts AS (\n SELECT \n account_id, \n account_balance, \n ROW_NUMBER() OVER (ORDER BY account_balance DESC) AS balance_rank\n FROM \n TradingAccounts\n)\nSELECT \n ra.*, \n COALESCE(SUM(o.order_amount), 0) AS total_pending_amount\nFROM \n ranked_accounts ra\n LEFT JOIN Orders o ON ra.account_id = o.account_id AND o.order_status = 'Pending'\nWHERE \n ra.balance_rank = 1\nGROUP BY \n ra.account_id, \n ra.account_balance"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average order amount for each trader, where the order status is 'Executed' and the order was placed in the last 7 days?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n t.trader_id, \n t.first_name, \n t.last_name, \n AVG(o.order_amount) AS avg_order_amount\nFROM \n Traders t\nJOIN \n TradingAccounts ta ON t.trader_id = ta.trader_id\nJOIN \n Orders o ON ta.account_id = o.account_id\nWHERE \n o.order_status = 'Executed'\n AND o.placed_at >= DATE('now', '-7 days')\nGROUP BY \n t.trader_id, \n t.first_name, \n t.last_name"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: Which currency pairs have the highest number of pending orders, and what is the total amount of these orders?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n o.base_currency_id, \n o.quote_currency_id, \n COUNT(o.order_id) AS num_pending_orders, \n SUM(o.order_amount) AS total_amount\nFROM \n Orders o\nWHERE \n o.order_status = 'Pending'\nGROUP BY \n o.base_currency_id, \n o.quote_currency_id\nORDER BY \n num_pending_orders DESC\nLIMIT 1"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total amount of all executed orders for each trading account, where the order status is 'Executed' and the order was placed in the last 14 days?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n ta.account_id, \n SUM(o.order_amount) AS total_amount\nFROM \n TradingAccounts ta\nJOIN \n Orders o ON ta.account_id = o.account_id\nWHERE \n o.order_status = 'Executed'\n AND o.placed_at >= DATE('now', '-14 days')\nGROUP BY \n ta.account_id"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total trade value of all trades executed by traders who have been registered for more than 6 months, and have a trade history of more than 10 trades, and have a profit to loss ratio of more than 2:1, and have a total trade value of more than $100,000?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "WITH \n TraderTradeCounts AS (\n SELECT th.trader_id, COUNT(th.trade_id) AS trade_count\n FROM TradeHistories th\n GROUP BY th.trader_id\n ),\n TraderProfitLoss AS (\n SELECT \n ta.trader_id, \n SUM(CASE WHEN t.transaction_type = 'Profit' THEN t.transaction_amount ELSE 0 END) AS profit,\n SUM(CASE WHEN t.transaction_type = 'Loss' THEN t.transaction_amount ELSE 0 END) AS loss\n FROM TradingAccounts ta\n JOIN Transactions t ON ta.account_id = t.account_id\n GROUP BY ta.trader_id\n )\nSELECT SUM(th.trade_value)\nFROM TradeHistories th\nJOIN Traders t ON th.trader_id = t.trader_id\nJOIN TraderTradeCounts ttc ON th.trader_id = ttc.trader_id\nJOIN TraderProfitLoss tpl ON th.trader_id = tpl.trader_id\nWHERE t.registration_date < DATE('now', '-6 months')\nAND ttc.trade_count > 10\nAND tpl.profit / tpl.loss > 2\nAND th.trade_value > 100000;"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average trade amount of all trades executed by traders who have a trade history of more than 5 trades, and have a profit to loss ratio of more than 1.5:1, and have a total trade value of more than $50,000?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "WITH filtered_traders AS (\n SELECT th.trader_id\n FROM TradeHistories th\n GROUP BY th.trader_id\n HAVING COUNT(th.trade_id) > 5\n AND SUM(th.trade_value) > 50000\n),\nloss_ratio AS (\n SELECT th.trader_id\n FROM TradeHistories th\n JOIN TradingAccounts ta ON th.trader_id = ta.trader_id\n JOIN Transactions trans ON ta.account_id = trans.account_id\n WHERE trans.transaction_type = 'Loss'\n GROUP BY th.trader_id\n HAVING SUM(th.trade_value) / SUM(trans.transaction_amount) > 1.5\n)\nSELECT AVG(th.trade_amount)\nFROM TradeHistories th\nJOIN filtered_traders ft ON th.trader_id = ft.trader_id\nJOIN loss_ratio lr ON th.trader_id = lr.trader_id;"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total trade value of all trades executed by traders who have a trade history of more than 10 trades, and have a profit to loss ratio of more than 2:1, and have a total trade value of more than $100,000, and have executed trades in more than 5 different currencies?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "WITH \n -- Get traders with more than 10 trades\n active_traders AS (\n SELECT th.trader_id\n FROM TradeHistories th\n GROUP BY th.trader_id\n HAVING COUNT(th.trade_id) > 10\n ),\n \n -- Get traders with a profit to loss ratio of more than 2:1\n profitable_traders AS (\n SELECT th.trader_id\n FROM TradeHistories th\n JOIN TradingAccounts ta ON th.trader_id = ta.trader_id\n JOIN Transactions t ON ta.account_id = t.account_id\n WHERE t.transaction_type = 'Loss'\n GROUP BY th.trader_id\n HAVING SUM(th.trade_value) / SUM(t.transaction_amount) > 2\n ),\n \n -- Get traders with a total trade value of more than $100,000\n high_value_traders AS (\n SELECT th.trader_id\n FROM TradeHistories th\n GROUP BY th.trader_id\n HAVING SUM(th.trade_value) > 100000\n ),\n \n -- Get traders who have executed trades in more than 5 different currencies\n diverse_traders AS (\n SELECT th.trader_id\n FROM TradeHistories th\n GROUP BY th.trader_id\n HAVING COUNT(DISTINCT th.base_currency_id) > 5\n )\n\nSELECT SUM(th.trade_value)\nFROM TradeHistories th\nWHERE th.trader_id IN (\n SELECT trader_id\n FROM active_traders\n INTERSECT\n SELECT trader_id\n FROM profitable_traders\n INTERSECT\n SELECT trader_id\n FROM high_value_traders\n INTERSECT\n SELECT trader_id\n FROM diverse_traders\n)"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average trade amount of all trades executed by traders who have a trade history of more than 5 trades, and have a profit to loss ratio of more than 1.5:1, and have a total trade value of more than $50,000, and have executed trades in more than 3 different currencies?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "WITH \n -- Get traders who meet at least one of the conditions\n eligible_traders AS (\n SELECT th.trader_id\n FROM TradeHistories th\n WHERE th.trader_id IN (\n SELECT trader_id\n FROM TradeHistories\n GROUP BY trader_id\n HAVING COUNT(trade_id) > 5\n ) OR th.trader_id IN (\n SELECT trader_id\n FROM TradeHistories th\n JOIN TradingAccounts ta ON th.trader_id = ta.trader_id\n JOIN Transactions t ON ta.account_id = t.account_id\n WHERE t.transaction_type = 'Loss'\n GROUP BY th.trader_id\n HAVING SUM(th.trade_value) / SUM(t.transaction_amount) > 1.5\n ) OR th.trader_id IN (\n SELECT trader_id\n FROM TradeHistories\n GROUP BY trader_id\n HAVING SUM(trade_value) > 50000\n ) OR th.trader_id IN (\n SELECT trader_id\n FROM TradeHistories\n GROUP BY trader_id\n HAVING COUNT(DISTINCT base_currency_id) > 3\n )\n )\n\nSELECT AVG(th.trade_amount)\nFROM TradeHistories th\nWHERE th.trader_id IN (SELECT trader_id FROM eligible_traders)"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total trade value of all trades executed by traders who have been registered for more than 3 months, and have a trade history of more than 5 trades, and have a profit to loss ratio of more than 1.5:1?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT DISTINCT SUM(th.trade_value) \nFROM TradeHistories th \nJOIN Traders t ON th.trader_id = t.trader_id \nJOIN TradingAccounts ta ON t.trader_id = ta.trader_id \nJOIN Transactions trans ON ta.account_id = trans.account_id \nWHERE t.registration_date < CURRENT_DATE - INTERVAL 3 MONTH \nAND th.trade_id > 5 \nGROUP BY th.trader_id \nHAVING SUM(CASE WHEN trans.transaction_type = 'Profit' THEN trans.transaction_amount ELSE 0 END) / \n SUM(CASE WHEN trans.transaction_type = 'Loss' THEN trans.transaction_amount ELSE 0 END) > 1.5\nORDER BY th.trader_id;"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average trade amount of all trades executed by traders who have been registered for more than 6 months, and have a trade history of more than 10 trades, and have a profit to loss ratio of more than 2:1?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "-- SQLite query\nWITH \n -- Calculate profit and loss for each trader\n trader_profit_loss AS (\n SELECT \n th.trader_id, \n SUM(CASE WHEN t.transaction_type = 'Profit' THEN t.transaction_amount ELSE 0 END) AS profit, \n SUM(CASE WHEN t.transaction_type = 'Loss' THEN t.transaction_amount ELSE 0 END) AS loss \n FROM TradeHistories th \n JOIN TradingAccounts ta ON th.trader_id = ta.trader_id \n JOIN Transactions t ON ta.account_id = t.account_id \n GROUP BY th.trader_id\n ),\n \n -- Filter traders with profit/loss ratio > 2\n filtered_traders AS (\n SELECT trader_id \n FROM trader_profit_loss \n WHERE profit / loss > 2\n )\n\n-- Calculate average trade amount for filtered traders\nSELECT AVG(th.trade_amount) \nFROM TradeHistories th \nJOIN Traders t ON th.trader_id = t.trader_id \nWHERE t.registration_date < DATE('now', '-6 months') \nAND th.trader_id IN (SELECT trader_id FROM filtered_traders)\nAND th.trader_id IN (\n SELECT trader_id \n FROM TradeHistories \n GROUP BY trader_id \n HAVING COUNT(trade_id) > 10\n)"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total trade value of all trades executed by traders who have a trade history of more than 5 trades, and have a profit to loss ratio of more than 1.5:1, and have executed trades in more than 3 different currencies?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "delimiter:"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average trade amount of all trades executed by traders who have a trade history of more than 10 trades, and have a profit to loss ratio of more than 2:1, and have executed trades in more than 5 different currencies?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "WITH \n -- Get traders with more than 10 trades\n active_traders AS (\n SELECT th.trader_id\n FROM TradeHistories th\n GROUP BY th.trader_id\n HAVING COUNT(th.trade_id) > 10\n ),\n \n -- Get traders with a profit to loss ratio of more than 2:1\n profitable_traders AS (\n SELECT th.trader_id\n FROM TradeHistories th\n JOIN TradingAccounts ta ON th.trader_id = ta.trader_id\n JOIN Transactions t ON ta.account_id = t.account_id\n WHERE t.transaction_type = 'Loss' AND th.trade_amount > 0\n GROUP BY th.trader_id\n HAVING SUM(th.trade_value) / SUM(t.transaction_amount) > 2\n ),\n \n -- Get traders who have executed trades in more than 5 different currencies\n diverse_traders AS (\n SELECT th.trader_id\n FROM TradeHistories th\n GROUP BY th.trader_id\n HAVING COUNT(DISTINCT th.base_currency_id) > 5\n )\n\nSELECT AVG(th.trade_amount)\nFROM TradeHistories th\nWHERE th.trader_id IN (\n SELECT trader_id\n FROM active_traders\n INTERSECT\n SELECT trader_id\n FROM profitable_traders\n INTERSECT\n SELECT trader_id\n FROM diverse_traders\n) AND th.trade_amount > 0"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total trade value of all trades executed by traders who have been registered for more than 6 months, and have a trade history of more than 10 trades, and have a profit to loss ratio of more than 2:1, and have executed trades in more than 5 different currencies?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "WITH \n Elig AS (\n SELECT trader_id, registration_date \n FROM Traders \n WHERE registration_date < DATE('now', '-6 months')\n ),\n \n Active AS (\n SELECT th.trader_id \n FROM TradeHistories th \n GROUP BY th.trader_id \n HAVING COUNT(th.trade_id) > 10\n ),\n \n Profitable AS (\n SELECT th.trader_id \n FROM TradeHistories th \n JOIN TradingAccounts ta ON th.trader_id = ta.trader_id \n JOIN Transactions trans ON ta.account_id = trans.account_id \n WHERE trans.transaction_type = 'Loss' \n GROUP BY th.trader_id \n HAVING SUM(trans.transaction_amount) != 0 AND SUM(th.trade_value) / SUM(trans.transaction_amount) > 2\n ),\n \n Diverse AS (\n SELECT th.trader_id \n FROM TradeHistories th \n GROUP BY th.trader_id \n HAVING COUNT(DISTINCT th.base_currency_id) > 5\n )\n\nSELECT SUM(th.trade_value) AS total_trade_value \nFROM TradeHistories th \nINNER JOIN Elig e ON th.trader_id = e.trader_id \nINNER JOIN Active a ON th.trader_id = a.trader_id \nINNER JOIN Profitable p ON th.trader_id = p.trader_id \nINNER JOIN Diverse d ON th.trader_id = d.trader_id;"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average trade amount of all trades executed by traders who have been registered for more than 3 months, and have a trade history of more than 5 trades, and have a profit to loss ratio of more than 1.5:1, and have executed trades in more than 3 different currencies?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "WITH ProfitLossRatio AS (\n SELECT \n ta.trader_id, \n SUM(CASE WHEN tx.transaction_type = 'Profit' THEN tx.transaction_amount ELSE 0 END) AS profit, \n SUM(CASE WHEN tx.transaction_type = 'Loss' THEN tx.transaction_amount ELSE 0 END) AS loss \n FROM TradingAccounts ta \n JOIN Transactions tx ON ta.account_id = tx.account_id \n GROUP BY ta.trader_id\n),\nTraderStats AS (\n SELECT \n th.trader_id, \n COUNT(DISTINCT th.base_currency_id) AS num_currencies, \n COUNT(th.trade_id) AS num_trades \n FROM TradeHistories th \n GROUP BY th.trader_id\n)\nSELECT AVG(th.trade_amount) \nFROM TradeHistories th \nJOIN TraderStats ts ON th.trader_id = ts.trader_id \nJOIN ProfitLossRatio plr ON th.trader_id = plr.trader_id \nWHERE th.registration_date < DATE('now', '-3 months') \nAND ts.num_trades > 5 \nAND plr.profit / plr.loss > 1.5 \nAND ts.num_currencies > 3;"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total trade value of all trades executed by traders who have a trade history of more than 10 trades, and have a profit to loss ratio of more than 2:1, and have executed trades in more than 5 different currencies, and have a total trade value of more than $100,000?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "WITH \n -- Get traders who meet at least one of the conditions\n eligible_traders AS (\n SELECT th.trader_id\n FROM TradeHistories th\n WHERE th.trader_id IN (\n SELECT trader_id\n FROM TradeHistories\n GROUP BY trader_id\n HAVING COUNT(trade_id) > 10\n ) OR th.trader_id IN (\n SELECT trader_id\n FROM TradeHistories th\n JOIN Transactions t ON th.trader_id = t.trader_id\n WHERE t.transaction_type = 'Loss'\n GROUP BY th.trader_id\n HAVING SUM(th.trade_value) / SUM(t.transaction_amount) > 2\n ) OR th.trader_id IN (\n SELECT trader_id\n FROM TradeHistories\n GROUP BY trader_id\n HAVING COUNT(DISTINCT base_currency_id) > 5\n ) OR th.trader_id IN (\n SELECT trader_id\n FROM TradeHistories\n GROUP BY trader_id\n HAVING SUM(trade_value) > 100000\n )\n )\n\nSELECT SUM(th.trade_value)\nFROM TradeHistories th\nWHERE th.trader_id IN (SELECT trader_id FROM eligible_traders)"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average trade amount of all trades executed by traders who have a trade history of more than 5 trades, and have a profit to loss ratio of more than 1.5:1, and have executed trades in more than 3 different currencies, and have a total trade value of more than $50,000?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "WITH \n -- Define a CTE to calculate trade counts and values for each trader\n TraderTradeSummary AS (\n SELECT \n th.trader_id,\n COUNT(DISTINCT th.trade_id) AS trade_count,\n SUM(th.trade_value) AS total_trade_value\n FROM \n TradeHistories th\n GROUP BY \n th.trader_id\n ),\n \n -- Define a CTE to calculate profit-to-loss ratios for each trader\n TraderProfitLossRatio AS (\n SELECT \n th.trader_id,\n SUM(th.trade_value) / SUM(tr.transaction_amount) AS profit_loss_ratio\n FROM \n TradeHistories th\n JOIN \n Transactions tr ON th.trader_id = tr.account_id\n WHERE \n tr.transaction_type = 'Loss'\n GROUP BY \n th.trader_id\n ),\n \n -- Define a CTE to calculate currency counts for each trader\n TraderCurrencyCount AS (\n SELECT \n th.trader_id,\n COUNT(DISTINCT th.base_currency_id) AS currency_count\n FROM \n TradeHistories th\n GROUP BY \n th.trader_id\n )\n\n-- Main query to calculate average trade amount\nSELECT \n AVG(th.trade_amount) \nFROM \n TradeHistories th\nWHERE \n th.trader_id IN (\n SELECT \n trader_id \n FROM \n TraderTradeSummary \n WHERE \n trade_count > 5 AND total_trade_value > 50000\n )\n AND th.trader_id IN (\n SELECT \n trader_id \n FROM \n TraderProfitLossRatio \n WHERE \n profit_loss_ratio > 1.5\n )\n AND th.trader_id IN (\n SELECT \n trader_id \n FROM \n TraderCurrencyCount \n WHERE \n currency_count > 3\n );"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total trade value of all trades executed by traders who have been registered for more than 6 months, and have a trade history of more than 10 trades, and have a profit to loss ratio of more than 2:1, and have executed trades in more than 5 different currencies, and have a total trade value of more than $100,000?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "DELIMITER //\n\nSELECT SUM(th.trade_value) \nFROM TradeHistories th \nJOIN (\n SELECT trader_id, \n SUM(CASE WHEN t.transaction_type = 'Profit' THEN t.transaction_amount ELSE 0 END) AS profit, \n SUM(CASE WHEN t.transaction_type = 'Loss' THEN t.transaction_amount ELSE 0 END) AS loss \n FROM TradeHistories th \n JOIN TradingAccounts ta ON th.trader_id = ta.trader_id \n JOIN Transactions t ON ta.account_id = t.account_id \n GROUP BY th.trader_id\n) AS subquery ON th.trader_id = subquery.trader_id \nWHERE th.trader_id IN (\n SELECT trader_id \n FROM TradeHistories \n GROUP BY trader_id \n HAVING COUNT(trade_id) > 10\n) \nAND subquery.profit / subquery.loss > 2 \nAND th.trader_id IN (\n SELECT trader_id \n FROM TradeHistories \n GROUP BY trader_id \n HAVING COUNT(DISTINCT base_currency_id) > 5\n) \nAND th.trade_value > 100000 \nAND th.trader_id IN (\n SELECT trader_id \n FROM Traders \n WHERE registration_date < DATE('now', '-6 months')\n);\n\n//"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average trade amount of all trades executed by traders who have been registered for more than 3 months, and have a trade history of more than 5 trades, and have a profit to loss ratio of more than 1.5:1, and have executed trades in more than 3 different currencies, and have a total trade value of more than $50,000?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "WITH \n -- Define a CTE to calculate trade frequency\n TradeFrequency AS (\n SELECT trader_id, COUNT(trade_id) AS trade_count\n FROM TradeHistories\n GROUP BY trader_id\n ),\n \n -- Define a CTE to calculate profit-to-loss ratio\n ProfitLossRatio AS (\n SELECT \n th.trader_id, \n SUM(CASE WHEN t.transaction_type = 'Profit' THEN t.transaction_amount ELSE 0 END) AS profit, \n SUM(CASE WHEN t.transaction_type = 'Loss' THEN t.transaction_amount ELSE 0 END) AS loss \n FROM TradeHistories th \n JOIN TradingAccounts ta ON th.trader_id = ta.trader_id \n JOIN Transactions t ON ta.account_id = t.account_id \n GROUP BY th.trader_id\n ),\n \n -- Define a CTE to calculate currency diversity\n CurrencyDiversity AS (\n SELECT trader_id, COUNT(DISTINCT base_currency_id) AS currency_count\n FROM TradeHistories\n GROUP BY trader_id\n ),\n \n -- Define a CTE to calculate total trade value\n TotalTradeValue AS (\n SELECT trader_id, SUM(trade_value) AS total_value\n FROM TradeHistories\n GROUP BY trader_id\n )\n\nSELECT AVG(th.trade_amount) \nFROM TradeHistories th \nJOIN Traders t ON th.trader_id = t.trader_id \nJOIN TradeFrequency tf ON th.trader_id = tf.trader_id \nJOIN ProfitLossRatio plr ON th.trader_id = plr.trader_id \nJOIN CurrencyDiversity cd ON th.trader_id = cd.trader_id \nJOIN TotalTradeValue ttv ON th.trader_id = ttv.trader_id \nWHERE t.registration_date < DATE('now', '-3 months') \n AND tf.trade_count > 5 \n AND plr.profit / plr.loss > 1.5 \n AND cd.currency_count > 3 \n AND ttv.total_value > 50000;"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total trade value of all trades executed by traders who have a trade history of more than 10 trades, and have a profit to loss ratio of more than 2:1, and have executed trades in more than 5 different currencies, and have a total trade value of more than $100,000, and have a average trade amount of more than $10,000?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "WITH \n -- Get traders with more than 10 trades\n active_traders AS (\n SELECT th.trader_id\n FROM TradeHistories th\n GROUP BY th.trader_id\n HAVING COUNT(th.trade_id) > 10\n ),\n \n -- Get traders with profit to loss ratio of more than 2:1\n profitable_traders AS (\n SELECT ta.trader_id\n FROM TradingAccounts ta\n JOIN Transactions t ON ta.account_id = t.account_id\n GROUP BY ta.trader_id\n HAVING SUM(CASE WHEN t.transaction_type = 'Profit' THEN t.transaction_amount ELSE 0 END) / \n SUM(CASE WHEN t.transaction_type = 'Loss' THEN t.transaction_amount ELSE 0 END) > 2\n ),\n \n -- Get traders who have executed trades in more than 5 different currencies\n diverse_traders AS (\n SELECT th.trader_id\n FROM TradeHistories th\n GROUP BY th.trader_id\n HAVING COUNT(DISTINCT th.base_currency_id) > 5\n ),\n \n -- Get traders with total trade value of more than $100,000\n high_value_traders AS (\n SELECT th.trader_id\n FROM TradeHistories th\n GROUP BY th.trader_id\n HAVING SUM(th.trade_value) > 100000\n ),\n \n -- Get traders with average trade amount of more than $10,000\n high_amount_traders AS (\n SELECT th.trader_id\n FROM TradeHistories th\n GROUP BY th.trader_id\n HAVING AVG(th.trade_amount) > 10000\n )\n\nSELECT SUM(th.trade_value)\nFROM TradeHistories th\nWHERE th.trader_id IN (\n SELECT at.trader_id\n FROM active_traders at\n JOIN profitable_traders pt ON at.trader_id = pt.trader_id\n JOIN diverse_traders dt ON at.trader_id = dt.trader_id\n JOIN high_value_traders hvt ON at.trader_id = hvt.trader_id\n JOIN high_amount_traders hat ON at.trader_id = hat.trader_id\n)"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average trade amount of all trades executed by traders who have a trade history of more than 5 trades, and have a profit to loss ratio of more than 1.5:1, and have executed trades in more than 3 different currencies, and have a total trade value of more than $50,000, and have a average trade amount of more than $5,000?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "WITH \n -- Define a CTE to calculate trade statistics for each trader\n TradeStats AS (\n SELECT \n th.trader_id,\n COUNT(DISTINCT th.trade_id) AS num_trades,\n COUNT(DISTINCT th.base_currency_id) AS num_currencies,\n SUM(th.trade_value) AS total_trade_value,\n AVG(th.trade_amount) AS avg_trade_amount\n FROM \n TradeHistories th\n GROUP BY \n th.trader_id\n ),\n \n -- Define a CTE to calculate profit/loss ratio for each trader\n ProfitLossRatio AS (\n SELECT \n th.trader_id,\n SUM(th.trade_value) / SUM(tr.transaction_amount) AS profit_loss_ratio\n FROM \n TradeHistories th\n JOIN \n TradingAccounts ta ON th.trader_id = ta.trader_id\n JOIN \n Transactions tr ON ta.account_id = tr.account_id\n WHERE \n tr.transaction_type = 'Loss'\n GROUP BY \n th.trader_id\n )\n\n-- Main query to filter traders based on conditions\nSELECT \n AVG(ts.avg_trade_amount) AS avg_trade_amount\nFROM \n TradeStats ts\nJOIN \n ProfitLossRatio plr ON ts.trader_id = plr.trader_id\nWHERE \n ts.num_trades > 5\n AND ts.num_currencies > 3\n AND ts.total_trade_value > 50000\n AND ts.avg_trade_amount > 5000\n AND plr.profit_loss_ratio > 1.5;"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total amount of deposits made by traders who have a bank account with a currency of USD and have made at least one withdrawal in the last 30 days?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n SUM(T.transaction_amount) \nFROM \n Transactions T \nJOIN \n TradingAccounts TA ON T.account_id = TA.account_id \nJOIN \n BankAccounts BA ON TA.trader_id = BA.trader_id \nWHERE \n T.transaction_type = 'Deposit' \n AND BA.currency_id = (SELECT currency_id FROM Currencies WHERE currency_code = 'USD') \n AND TA.trader_id IN (SELECT trader_id FROM Withdrawals WHERE withdrawal_date >= DATE('now', '-30 days'))"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: Which traders have made a profit in the last 7 days and have a trading account with a balance greater than 1000 USD?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "-- Retrieve traders with profitable transactions in the last 7 days\nSELECT DISTINCT T.first_name, T.last_name \nFROM Traders T \nJOIN TradingAccounts TA ON T.trader_id = TA.trader_id \nJOIN Transactions TRANS ON TA.account_id = TRANS.account_id \nJOIN Currencies C ON TA.currency_id = C.currency_id \nWHERE TRANS.transaction_type = 'Profit' \nAND TRANS.transaction_date >= DATE('now', '-7 days') \nAND TA.account_balance > 1000 \nAND C.currency_code = 'USD';"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average exchange rate for trades made in the last 14 days where the base currency is EUR and the quote currency is USD?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT AVG(T.executed_rate) \nFROM Trades T \nJOIN Orders O ON T.order_id = O.order_id \nWHERE O.base_currency_id = (SELECT C.currency_id FROM Currencies C WHERE C.currency_code = 'EUR') \nAND O.quote_currency_id = (SELECT C.currency_id FROM Currencies C WHERE C.currency_code = 'USD') \nAND T.executed_at >= CURRENT_DATE - INTERVAL 14 DAY"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total value of trades made by traders who have a bank account with a currency of GBP and have made at least one deposit in the last 30 days?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n SUM(T.trade_value) \nFROM \n Trades T \nJOIN \n Orders O ON T.order_id = O.order_id \nJOIN \n TradingAccounts TA ON O.account_id = TA.account_id \nJOIN \n BankAccounts BA ON TA.trader_id = BA.trader_id \nWHERE \n BA.currency_id = (SELECT currency_id FROM Currencies WHERE currency_code = 'GBP') \n AND TA.trader_id IN (SELECT trader_id FROM Transactions WHERE transaction_type = 'Deposit' AND transaction_date >= DATE('now', '-30 days'))"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: Which traders have made a loss in the last 7 days and have a trading account with a balance less than 500 USD?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT DISTINCT T.first_name, T.last_name \nFROM Traders T \nJOIN TradingAccounts TA ON T.trader_id = TA.trader_id \nJOIN Transactions TR ON TA.account_id = TR.account_id \nWHERE TR.transaction_type = 'Loss' \nAND TR.transaction_date >= DATE('now', '-7 days') \nAND TA.account_balance < 500 \nAND TA.currency_id = (SELECT currency_id FROM Currencies WHERE currency_code = 'USD');"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total amount of withdrawals made by traders who have a bank account with a currency of JPY and have made at least one trade in the last 14 days?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n SUM(W.withdrawal_amount) \nFROM \n Withdrawals W \nJOIN \n BankAccounts BA ON W.bank_account_id = BA.bank_account_id \nJOIN \n TradingAccounts TA ON BA.trader_id = TA.trader_id \nWHERE \n BA.currency_id = (SELECT currency_id FROM Currencies WHERE currency_code = 'JPY') \n AND TA.trader_id IN (SELECT trader_id FROM TradeHistories WHERE executed_at >= DATE('now', '-14 days'))"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average exchange rate for trades made in the last 30 days where the base currency is USD and the quote currency is EUR?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT AVG(T.executed_rate) \nFROM Trades T \nJOIN Orders O ON T.order_id = O.order_id \nWHERE O.base_currency_id = (SELECT currency_id FROM Currencies WHERE currency_code = 'USD') \nAND O.quote_currency_id = (SELECT currency_id FROM Currencies WHERE currency_code = 'EUR') \nAND T.executed_at >= DATE('now', '-30 days')"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: Which traders have made a profit in the last 14 days and have a trading account with a balance greater than 2000 USD?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT DISTINCT T.first_name, T.last_name \nFROM Traders T \nJOIN TradingAccounts TA ON T.trader_id = TA.trader_id \nJOIN Transactions TR ON TA.account_id = TR.account_id \nWHERE TR.transaction_type = 'Profit' \nAND TR.transaction_date >= DATE('now', '-14 days') \nAND TA.account_balance > 2000 \nAND TA.currency_id = (SELECT currency_id FROM Currencies WHERE currency_code = 'USD');"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total value of trades made by traders who have a bank account with a currency of AUD and have made at least one withdrawal in the last 30 days?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n SUM(T.trade_value) \nFROM \n Trades T \nJOIN \n Orders O ON T.order_id = O.order_id \nJOIN \n TradingAccounts TA ON O.account_id = TA.account_id \nJOIN \n BankAccounts BA ON TA.trader_id = BA.trader_id \nWHERE \n BA.currency_id = (SELECT currency_id FROM Currencies WHERE currency_code = 'AUD') \n AND TA.trader_id IN (SELECT trader_id FROM Withdrawals WHERE withdrawal_date >= DATE('now', '-30 days'))"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total amount of deposits made by traders who have a bank account with a currency of CAD and have made at least one trade in the last 14 days?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n SUM(T.transaction_amount) \nFROM \n Transactions T \nJOIN \n TradingAccounts TA ON T.account_id = TA.account_id \nJOIN \n BankAccounts BA ON TA.trader_id = BA.trader_id \nWHERE \n T.transaction_type = 'Deposit' \n AND BA.currency_id = (SELECT currency_id FROM Currencies WHERE currency_code = 'CAD') \n AND TA.trader_id IN (SELECT trader_id FROM TradeHistories WHERE executed_at >= DATE('now', '-14 days'))"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: Which traders have made a loss in the last 14 days and have a trading account with a balance less than 1000 USD?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "-- Retrieve traders with losses in the last 14 days and account balance < 1000 USD\nSELECT DISTINCT T.first_name, T.last_name \nFROM Traders T \nJOIN TradingAccounts TA ON T.trader_id = TA.trader_id \nJOIN Transactions TR ON TA.account_id = TR.account_id \nWHERE TR.transaction_type = 'Loss' \nAND TR.transaction_date >= DATE('now', '-14 days') \nAND TA.account_balance < 1000 \nAND TA.currency_id = ( -- Subquery to get the currency_id for USD\n SELECT currency_id \n FROM Currencies \n WHERE currency_code = 'USD'\n);"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average exchange rate for trades made in the last 7 days where the base currency is GBP and the quote currency is USD?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT AVG(T.executed_rate) \nFROM Trades T \nJOIN Orders O ON T.order_id = O.order_id \nWHERE O.base_currency_id = (SELECT currency_id FROM Currencies WHERE currency_code = 'GBP') \nAND O.quote_currency_id = (SELECT currency_id FROM Currencies WHERE currency_code = 'USD') \nAND T.executed_at >= DATE('now', '-7 days')"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total value of trades made by traders who have a bank account with a currency of CHF and have made at least one deposit in the last 30 days?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n SUM(T.trade_value) \nFROM \n Trades T \nJOIN \n Orders O ON T.order_id = O.order_id \nJOIN \n TradingAccounts TA ON O.account_id = TA.account_id \nJOIN \n BankAccounts BA ON TA.trader_id = BA.trader_id \nWHERE \n BA.currency_id = (SELECT currency_id FROM Currencies WHERE currency_code = 'CHF') \n AND TA.trader_id IN (SELECT trader_id FROM Transactions WHERE transaction_type = 'Deposit' AND transaction_date >= DATE('now', '-30 days'))"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: Which traders have made a profit in the last 30 days and have a trading account with a balance greater than 5000 USD?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT DISTINCT T.first_name, T.last_name \nFROM Traders T \nJOIN TradingAccounts TA ON T.trader_id = TA.trader_id \nJOIN Transactions TR ON TA.account_id = TR.account_id \nWHERE TR.transaction_type = 'Profit' \nAND TR.transaction_date >= DATE('now', '-30 days') \nAND TA.account_balance > 5000 \nAND TA.currency_id = (SELECT currency_id FROM Currencies WHERE currency_code = 'USD');"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total amount of withdrawals made by traders who have a bank account with a currency of NZD and have made at least one trade in the last 14 days?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n SUM(W.withdrawal_amount) \nFROM \n Withdrawals W \nJOIN \n BankAccounts BA ON W.bank_account_id = BA.bank_account_id \nJOIN \n TradingAccounts TA ON BA.trader_id = TA.trader_id \nWHERE \n BA.currency_id = (SELECT currency_id FROM Currencies WHERE currency_code = 'NZD') \n AND TA.trader_id IN (SELECT trader_id FROM TradeHistories WHERE executed_at >= DATE('now', '-14 days'))"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average exchange rate for trades made in the last 30 days where the base currency is AUD and the quote currency is USD?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT AVG(T.executed_rate) \nFROM Trades T \nJOIN Orders O ON T.order_id = O.order_id \nWHERE O.base_currency_id = (SELECT currency_id FROM Currencies WHERE currency_code = 'AUD') \nAND O.quote_currency_id = (SELECT currency_id FROM Currencies WHERE currency_code = 'USD') \nAND T.executed_at >= DATE('now', '-30 days')"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: Which traders have made a loss in the last 30 days and have a trading account with a balance less than 2000 USD?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "-- Get traders with a loss transaction in the last 30 days and low account balance\nSELECT DISTINCT T.first_name, T.last_name \nFROM Traders T \nJOIN TradingAccounts TA ON T.trader_id = TA.trader_id \nJOIN Transactions TX ON TA.account_id = TX.account_id \nJOIN Currencies C ON TA.currency_id = C.currency_id \nWHERE TX.transaction_type = 'Loss' \nAND TX.transaction_date >= DATE('now', '-30 days') \nAND TA.account_balance < 2000 \nAND C.currency_code = 'USD';"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total amount of withdrawals made by traders who have more than one bank account, and have made at least one withdrawal in the last 30 days, in the currency of the bank account?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n SUM(w.withdrawal_amount) AS total_withdrawal_amount, \n c.currency_code\nFROM \n Withdrawals w\nJOIN \n BankAccounts b ON w.bank_account_id = b.bank_account_id\nJOIN \n Currencies c ON b.currency_id = c.currency_id\nWHERE \n b.trader_id IN (\n SELECT \n trader_id\n FROM \n BankAccounts\n GROUP BY \n trader_id\n HAVING \n COUNT(bank_account_id) > 1\n )\nAND \n w.withdrawal_date >= DATE('now', '-30 days')\nGROUP BY \n c.currency_code"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average withdrawal amount for each trader, for traders who have made at least 5 withdrawals in the last 60 days, and have a bank account with a balance greater than 1000?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n AVG(w.withdrawal_amount) AS average_withdrawal_amount, \n t.trader_id\nFROM \n Withdrawals w\nJOIN \n BankAccounts b ON w.bank_account_id = b.bank_account_id\nJOIN \n Traders t ON b.trader_id = t.trader_id\nWHERE \n w.withdrawal_date >= DATE('now', '-60 days')\nAND \n b.bank_account_id IN (\n SELECT bank_account_id \n FROM BankAccounts \n JOIN TradingAccounts ta ON BankAccounts.trader_id = ta.trader_id \n WHERE ta.account_balance > 1000\n )\nGROUP BY \n t.trader_id\nHAVING \n COUNT(w.withdrawal_id) >= 5"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total amount of withdrawals made by traders who have a bank account with a currency that is not the same as the currency of their trading account?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n SUM(w.withdrawal_amount) AS total_withdrawal_amount\nFROM \n Withdrawals w\nJOIN \n BankAccounts b ON w.bank_account_id = b.bank_account_id\nJOIN \n TradingAccounts ta ON w.account_id = ta.account_id\nWHERE \n b.currency_id != ta.currency_id"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the maximum withdrawal amount made by a trader in a single day, for traders who have made at least 3 withdrawals in the last 90 days?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n MAX(w.withdrawal_amount) AS max_withdrawal_amount, \n b.trader_id\nFROM \n Withdrawals w\nJOIN \n BankAccounts b ON w.bank_account_id = b.bank_account_id\nWHERE \n w.withdrawal_date >= DATE('now') - 90\nGROUP BY \n b.trader_id\nHAVING \n COUNT(w.withdrawal_id) >= 3\nORDER BY \n max_withdrawal_amount DESC\nLIMIT 1;"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total amount of withdrawals made by traders who have a bank account with a balance less than 500, and have made at least one withdrawal in the last 30 days?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n SUM(wd.withdrawal_amount) AS total_withdrawal_amount\nFROM \n Withdrawals wd\nJOIN \n TradingAccounts ta ON wd.account_id = ta.account_id\nJOIN \n BankAccounts ba ON wd.bank_account_id = ba.bank_account_id\nWHERE \n ta.account_balance < 500\nAND \n wd.withdrawal_date >= DATE('now', '-30 days', 'localtime')\nAND \n wd.status = 'Completed'"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average withdrawal amount for each currency, for withdrawals made in the last 60 days?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n AVG(w.withdrawal_amount) AS average_withdrawal_amount, \n c.currency_code\nFROM \n Withdrawals w\nJOIN \n BankAccounts b ON w.bank_account_id = b.bank_account_id\nJOIN \n Currencies c ON b.currency_id = c.currency_id\nWHERE \n w.withdrawal_date >= DATE('now', '-60 days')\nGROUP BY \n c.currency_code"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total amount of withdrawals made by traders who have made at least one deposit in the last 30 days, and have a bank account with a balance greater than 1000?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n SUM(w.withdrawal_amount) AS total_withdrawal_amount\nFROM \n Withdrawals w\nJOIN \n BankAccounts b ON w.bank_account_id = b.bank_account_id\nJOIN \n TradingAccounts ta ON w.account_id = ta.account_id\nWHERE \n b.account_balance > 1000\nAND \n w.withdrawal_date >= CURRENT_DATE - INTERVAL 30 DAY"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the maximum withdrawal amount made by a trader in a single day, for traders who have made at least 2 withdrawals in the last 60 days?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n MAX(w.withdrawal_amount) AS max_withdrawal_amount, \n w.trader_id\nFROM \n Withdrawals w\nJOIN \n Traders t ON w.trader_id = t.trader_id\nWHERE \n w.withdrawal_date >= CURRENT_DATE - INTERVAL 60 DAY\nGROUP BY \n w.trader_id\nHAVING \n COUNT(w.withdrawal_id) >= 2\nORDER BY \n max_withdrawal_amount DESC\nLIMIT 1;"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average withdrawal amount for each trader, for traders who have made at least 3 withdrawals in the last 90 days, and have a bank account with a balance less than 500?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n AVG(w.withdrawal_amount) AS average_withdrawal_amount, \n t.trader_id\nFROM \n Withdrawals w\nJOIN \n TradingAccounts ta ON w.account_id = ta.account_id\nJOIN \n BankAccounts b ON w.bank_account_id = b.bank_account_id\nJOIN \n Traders t ON b.trader_id = t.trader_id\nWHERE \n w.withdrawal_date >= DATE('now', '-90 day')\nAND \n ta.account_balance < 500\nGROUP BY \n t.trader_id\nHAVING \n (SELECT COUNT(*) FROM Withdrawals WHERE account_id = ta.account_id) >= 3"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total amount of withdrawals made by traders who have a bank account with a currency that is not the same as the currency of their trading account, and have made at least one withdrawal in the last 30 days?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n SUM(w.withdrawal_amount) AS total_withdrawal_amount\nFROM \n Withdrawals w\nJOIN \n BankAccounts b ON w.bank_account_id = b.bank_account_id\nJOIN \n TradingAccounts ta ON w.account_id = ta.account_id\nWHERE \n b.currency_id != ta.currency_id\nAND \n w.withdrawal_date >= DATE('now', '-30 days')"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the maximum withdrawal amount made by a trader in a single day, for traders who have made at least 2 withdrawals in the last 60 days, and have a bank account with a balance greater than 1000?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n MAX(w.withdrawal_amount) AS max_withdrawal_amount, \n t.trader_id\nFROM \n Withdrawals w\nJOIN \n BankAccounts ba ON w.bank_account_id = ba.bank_account_id\nJOIN \n Traders t ON ba.trader_id = t.trader_id\nWHERE \n w.withdrawal_date >= DATE('now', '-60 days')\nAND \n ba.trader_id IN (SELECT trader_id FROM TradingAccounts WHERE account_balance > 1000)\nGROUP BY \n t.trader_id\nHAVING \n COUNT(w.withdrawal_id) >= 2\nLIMIT 1;"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average withdrawal amount for each currency, for withdrawals made in the last 90 days?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n AVG(w.withdrawal_amount) AS average_withdrawal_amount, \n c.currency_code\nFROM \n Withdrawals w\nJOIN \n BankAccounts b ON w.bank_account_id = b.bank_account_id\nJOIN \n Currencies c ON b.currency_id = c.currency_id\nWHERE \n w.withdrawal_date >= DATE('now', '-90 days')\nGROUP BY \n c.currency_code"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total amount of withdrawals made by traders who have made at least one deposit in the last 30 days, and have a bank account with a balance less than 500?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n SUM(w.withdrawal_amount) AS total_withdrawal_amount\nFROM \n Withdrawals w\nJOIN \n TradingAccounts ta ON w.account_id = ta.account_id\nJOIN \n BankAccounts b ON w.bank_account_id = b.bank_account_id\nWHERE \n ta.account_balance < 500\nAND \n w.withdrawal_date >= DATE('now', '-30 days')"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the maximum withdrawal amount made by a trader in a single day, for traders who have made at least 3 withdrawals in the last 90 days, and have a bank account with a balance greater than 1000?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "-- Get the maximum withdrawal amount made by a trader in a single day, \n-- for traders who have made at least 3 withdrawals in the last 90 days, \n-- and have a bank account with a balance greater than 1000.\n\nSELECT \n MAX(w.withdrawal_amount) AS max_withdrawal_amount, \n t.trader_id\nFROM \n Withdrawals w\nJOIN \n BankAccounts b ON w.bank_account_id = b.bank_account_id\nJOIN \n TradingAccounts ta ON w.account_id = ta.account_id\nJOIN \n Traders t ON b.trader_id = t.trader_id\nWHERE \n w.withdrawal_date >= DATE('now', '-90 days')\nAND \n ta.account_balance > 1000\nGROUP BY \n t.trader_id, w.withdrawal_date::date\nHAVING \n COUNT(w.withdrawal_id) >= 3\nORDER BY \n max_withdrawal_amount DESC, t.trader_id\nLIMIT 1;"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the average withdrawal amount for each trader, for traders who have made at least 2 withdrawals in the last 60 days, and have a bank account with a balance less than 500?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n AVG(w.withdrawal_amount) AS average_withdrawal_amount, \n t.trader_id\nFROM \n Withdrawals w\nJOIN \n BankAccounts b ON w.bank_account_id = b.bank_account_id\nJOIN \n TradingAccounts ta ON w.account_id = ta.account_id\nJOIN \n Traders t ON b.trader_id = t.trader_id\nWHERE \n w.withdrawal_date >= DATE('now', '-60 days')\nAND \n ta.account_balance < 500\nGROUP BY \n t.trader_id\nHAVING \n COUNT(w.withdrawal_id) >= 2"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total amount of withdrawals made by traders who have a bank account with a currency that is not the same as the currency of their trading account, and have made at least one withdrawal in the last 60 days?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n SUM(w.withdrawal_amount) AS total_withdrawal_amount\nFROM \n Withdrawals w\nJOIN \n BankAccounts b ON w.bank_account_id = b.bank_account_id\nJOIN \n TradingAccounts ta ON w.account_id = ta.account_id\nWHERE \n b.currency_id != ta.currency_id\nAND \n w.withdrawal_date >= DATE('now', '-60 days')\nAND \n w.status = 'Completed'"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the maximum withdrawal amount made by a trader in a single day, for traders who have made at least 3 withdrawals in the last 90 days?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n MAX(w.withdrawal_amount) AS max_withdrawal_amount, \n b.trader_id\nFROM \n Withdrawals w\nJOIN \n BankAccounts b ON w.bank_account_id = b.bank_account_id\nWHERE \n w.withdrawal_date >= DATE('now') - 90\nGROUP BY \n b.trader_id\nHAVING \n COUNT(w.withdrawal_id) >= 3\nORDER BY \n max_withdrawal_amount DESC\nLIMIT 1;"
}
] |
[
{
"role": "system",
"content": "Below are SQL table schemas paired with an instruction describing a task. Using valid SQLite, generate an optimized and syntactically correct SQL query that satisfies the given instruction. Ensure that the query strictly uses only the tables and columns provided in the schema. Do not invent or hallucinate additional tables or columns. The query should also handle edge cases, use appropriate SQL functions, and adhere to schema constraints. Be mindful of using joins, aggregations, subqueries, and filtering conditions where appropriate."
},
{
"role": "user",
"content": "Instruction: What is the total trading volume in USD for the EUR/USD currency pair for the month of January 2022, considering only trades executed at a rate above 1.15?, Input schema: CREATE TABLE Currencies (\n currency_id INTEGER PRIMARY KEY AUTOINCREMENT,\n currency_code TEXT NOT NULL UNIQUE, -- ISO 4217 Currency Code, e.g., 'USD', 'EUR'\n currency_name TEXT NOT NULL,\n country TEXT NOT NULL\n);\n\nCREATE TABLE Traders (\n trader_id INTEGER PRIMARY KEY AUTOINCREMENT,\n first_name TEXT NOT NULL,\n last_name TEXT NOT NULL,\n email TEXT NOT NULL UNIQUE,\n phone TEXT,\n registration_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\nCREATE TABLE TradingAccounts (\n account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n account_balance REAL DEFAULT 0.0,\n currency_id INTEGER NOT NULL, -- Account base currency\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE ForexRates (\n rate_id INTEGER PRIMARY KEY AUTOINCREMENT,\n base_currency_id INTEGER NOT NULL, -- E.g., 'USD'\n quote_currency_id INTEGER NOT NULL, -- E.g., 'EUR'\n exchange_rate REAL NOT NULL, -- E.g., 1.13 USD = 1 EUR\n rate_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Orders (\n order_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n order_type TEXT CHECK(order_type IN ('Buy', 'Sell')) NOT NULL, -- Buy or Sell\n base_currency_id INTEGER NOT NULL, -- Currency being bought or sold\n quote_currency_id INTEGER NOT NULL, -- Currency used to execute the trade\n order_amount REAL NOT NULL, -- Amount of base currency being bought or sold\n executed_rate REAL, -- Rate at which the order was executed\n order_status TEXT CHECK(order_status IN ('Pending', 'Executed', 'Canceled')) DEFAULT 'Pending',\n placed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n executed_at TIMESTAMP, -- Time the order was executed\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Trades (\n trade_id INTEGER PRIMARY KEY AUTOINCREMENT,\n order_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL, -- Rate at which the trade was executed\n trade_amount REAL NOT NULL, -- Amount of base currency that was traded\n trade_value REAL NOT NULL, -- Value of the trade in quote currency\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (order_id) REFERENCES Orders(order_id)\n);\n\nCREATE TABLE TradeHistories (\n history_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trade_id INTEGER NOT NULL,\n trader_id INTEGER NOT NULL,\n trade_amount REAL NOT NULL,\n trade_value REAL NOT NULL,\n base_currency_id INTEGER NOT NULL,\n quote_currency_id INTEGER NOT NULL,\n executed_rate REAL NOT NULL,\n executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trade_id) REFERENCES Trades(trade_id),\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (base_currency_id) REFERENCES Currencies(currency_id),\n FOREIGN KEY (quote_currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Transactions (\n transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,\n account_id INTEGER NOT NULL,\n transaction_type TEXT CHECK(transaction_type IN ('Deposit', 'Withdrawal', 'Profit', 'Loss')) NOT NULL,\n transaction_amount REAL NOT NULL,\n transaction_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n description TEXT,\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);\n\nCREATE TABLE BankAccounts (\n bank_account_id INTEGER PRIMARY KEY AUTOINCREMENT,\n trader_id INTEGER NOT NULL,\n bank_name TEXT NOT NULL,\n account_number TEXT NOT NULL UNIQUE,\n currency_id INTEGER NOT NULL,\n created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n FOREIGN KEY (trader_id) REFERENCES Traders(trader_id),\n FOREIGN KEY (currency_id) REFERENCES Currencies(currency_id)\n);\n\nCREATE TABLE Withdrawals (\n withdrawal_id INTEGER PRIMARY KEY AUTOINCREMENT,\n bank_account_id INTEGER NOT NULL,\n account_id INTEGER NOT NULL,\n withdrawal_amount REAL NOT NULL,\n withdrawal_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,\n status TEXT CHECK(status IN ('Pending', 'Completed', 'Rejected')) DEFAULT 'Pending',\n FOREIGN KEY (bank_account_id) REFERENCES BankAccounts(bank_account_id),\n FOREIGN KEY (account_id) REFERENCES TradingAccounts(account_id)\n);"
},
{
"role": "assistant",
"content": "SELECT \n SUM(trades.trade_amount * trades.executed_rate) AS total_trading_volume,\n COUNT(trades.trade_id) AS number_of_trades,\n AVG(trades.executed_rate) AS average_executed_rate\nFROM Trades trades\nJOIN Orders orders ON trades.order_id = orders.order_id\nJOIN Currencies base_currency ON orders.base_currency_id = base_currency.currency_id\nJOIN Currencies quote_currency ON orders.quote_currency_id = quote_currency.currency_id\nWHERE base_currency.currency_code = 'EUR' AND quote_currency.currency_code = 'USD'\nAND trades.executed_at >= '2022-01-01' AND trades.executed_at < '2022-02-01'\nAND trades.executed_rate > 1.15"
}
] |