spider_version
int64
1
2
db_id
stringclasses
204 values
schema
stringclasses
187 values
question
stringlengths
3
328
query
stringlengths
20
3.81k
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
Which type of policy is most frequently used? Give me the policy type code.
SELECT policy_type_code FROM policies GROUP BY policy_type_code ORDER BY count(*) DESC LIMIT 1
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
Find the type code of the most frequently used policy.
SELECT policy_type_code FROM policies GROUP BY policy_type_code ORDER BY count(*) DESC LIMIT 1
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
Find all the policy types that are used by more than 2 customers.
SELECT policy_type_code FROM policies GROUP BY policy_type_code HAVING count(*) > 2
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
Which types of policy are chosen by more than 2 customers? Give me the policy type codes.
SELECT policy_type_code FROM policies GROUP BY policy_type_code HAVING count(*) > 2
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
Find the total and average amount paid in claim headers.
SELECT sum(amount_piad) , avg(amount_piad) FROM claim_headers
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
What are the total amount and average amount paid in claim headers?
SELECT sum(amount_piad) , avg(amount_piad) FROM claim_headers
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
Find the total amount claimed in the most recently created document.
SELECT sum(t1.amount_claimed) FROM claim_headers AS t1 JOIN claims_documents AS t2 ON t1.claim_header_id = t2.claim_id WHERE t2.created_date = (SELECT created_date FROM claims_documents ORDER BY created_date LIMIT 1)
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
How much amount in total were claimed in the most recently created document?
SELECT sum(t1.amount_claimed) FROM claim_headers AS t1 JOIN claims_documents AS t2 ON t1.claim_header_id = t2.claim_id WHERE t2.created_date = (SELECT created_date FROM claims_documents ORDER BY created_date LIMIT 1)
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
What is the name of the customer who has made the largest amount of claim in a single claim?
SELECT t3.customer_details FROM claim_headers AS t1 JOIN policies AS t2 ON t1.policy_id = t2.policy_id JOIN customers AS t3 ON t2.customer_id = t3.customer_id WHERE t1.amount_claimed = (SELECT max(amount_claimed) FROM claim_headers)
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
Which customer made the largest amount of claim in a single claim? Return the customer details.
SELECT t3.customer_details FROM claim_headers AS t1 JOIN policies AS t2 ON t1.policy_id = t2.policy_id JOIN customers AS t3 ON t2.customer_id = t3.customer_id WHERE t1.amount_claimed = (SELECT max(amount_claimed) FROM claim_headers)
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
What is the name of the customer who has made the minimum amount of payment in one claim?
SELECT t3.customer_details FROM claim_headers AS t1 JOIN policies AS t2 ON t1.policy_id = t2.policy_id JOIN customers AS t3 ON t2.customer_id = t3.customer_id WHERE t1.amount_piad = (SELECT min(amount_piad) FROM claim_headers)
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
Which customer made the smallest amount of claim in one claim? Return the customer details.
SELECT t3.customer_details FROM claim_headers AS t1 JOIN policies AS t2 ON t1.policy_id = t2.policy_id JOIN customers AS t3 ON t2.customer_id = t3.customer_id WHERE t1.amount_piad = (SELECT min(amount_piad) FROM claim_headers)
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
Find the names of customers who have no policies associated.
SELECT customer_details FROM customers EXCEPT SELECT t2.customer_details FROM policies AS t1 JOIN customers AS t2 ON t1.customer_id = t2.customer_id
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
What are the names of customers who do not have any policies?
SELECT customer_details FROM customers EXCEPT SELECT t2.customer_details FROM policies AS t1 JOIN customers AS t2 ON t1.customer_id = t2.customer_id
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
How many claim processing stages are there in total?
SELECT count(*) FROM claims_processing_stages
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
Find the number of distinct stages in claim processing.
SELECT count(*) FROM claims_processing_stages
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
What is the name of the claim processing stage that most of the claims are on?
SELECT t2.claim_status_name FROM claims_processing AS t1 JOIN claims_processing_stages AS t2 ON t1.claim_stage_id = t2.claim_stage_id GROUP BY t1.claim_stage_id ORDER BY count(*) DESC LIMIT 1
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
Which claim processing stage has the most claims? Show the claim status name.
SELECT t2.claim_status_name FROM claims_processing AS t1 JOIN claims_processing_stages AS t2 ON t1.claim_stage_id = t2.claim_stage_id GROUP BY t1.claim_stage_id ORDER BY count(*) DESC LIMIT 1
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
Find the names of customers whose name contains "Diana".
SELECT customer_details FROM customers WHERE customer_details LIKE "%Diana%"
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
Which customers have the substring "Diana" in their names? Return the customer details.
SELECT customer_details FROM customers WHERE customer_details LIKE "%Diana%"
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
Find the names of the customers who have an deputy policy.
SELECT DISTINCT t2.customer_details FROM policies AS t1 JOIN customers AS t2 ON t1.customer_id = t2.customer_id WHERE t1.policy_type_code = "Deputy"
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
Which customers have an insurance policy with the type code "Deputy"? Give me the customer details.
SELECT DISTINCT t2.customer_details FROM policies AS t1 JOIN customers AS t2 ON t1.customer_id = t2.customer_id WHERE t1.policy_type_code = "Deputy"
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
Find the names of customers who either have an deputy policy or uniformed policy.
SELECT DISTINCT t2.customer_details FROM policies AS t1 JOIN customers AS t2 ON t1.customer_id = t2.customer_id WHERE t1.policy_type_code = "Deputy" OR t1.policy_type_code = "Uniform"
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
Which customers have an insurance policy with the type code "Deputy" or "Uniform"? Return the customer details.
SELECT DISTINCT t2.customer_details FROM policies AS t1 JOIN customers AS t2 ON t1.customer_id = t2.customer_id WHERE t1.policy_type_code = "Deputy" OR t1.policy_type_code = "Uniform"
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
Find the names of all the customers and staff members.
SELECT customer_details FROM customers UNION SELECT staff_details FROM staff
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
What are the names of the customers and staff members?
SELECT customer_details FROM customers UNION SELECT staff_details FROM staff
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
Find the number of records of each policy type and its type code.
SELECT policy_type_code , count(*) FROM policies GROUP BY policy_type_code
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
For each policy type, return its type code and its count in the record.
SELECT policy_type_code , count(*) FROM policies GROUP BY policy_type_code
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
Find the name of the customer that has been involved in the most policies.
SELECT t2.customer_details FROM policies AS t1 JOIN customers AS t2 ON t1.customer_id = t2.customer_id GROUP BY t2.customer_details ORDER BY count(*) DESC LIMIT 1
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
Which customer have the most policies? Give me the customer details.
SELECT t2.customer_details FROM policies AS t1 JOIN customers AS t2 ON t1.customer_id = t2.customer_id GROUP BY t2.customer_details ORDER BY count(*) DESC LIMIT 1
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
What is the description of the claim status "Open"?
SELECT claim_status_description FROM claims_processing_stages WHERE claim_status_name = "Open"
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
Find the description of the claim status "Open".
SELECT claim_status_description FROM claims_processing_stages WHERE claim_status_name = "Open"
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
How many distinct claim outcome codes are there?
SELECT count(DISTINCT claim_outcome_code) FROM claims_processing
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
Count the number of distinct claim outcome codes.
SELECT count(DISTINCT claim_outcome_code) FROM claims_processing
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
Which customer is associated with the latest policy?
SELECT t2.customer_details FROM policies AS t1 JOIN customers AS t2 ON t1.customer_id = t2.customer_id WHERE t1.start_date = (SELECT max(start_date) FROM policies)
1
insurance_and_eClaims
CREATE TABLE Customers ( Customer_ID INTEGER NOT NULL, Customer_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Customer_ID) ); CREATE TABLE Staff ( Staff_ID INTEGER NOT NULL, Staff_Details VARCHAR(255) NOT NULL, PRIMARY KEY (Staff_ID) ); CREATE TABLE Policies ( Policy_ID INTEGER NOT NULL, Customer_ID INTEGER NOT NULL, Policy_Type_Code CHAR(15) NOT NULL, Start_Date DATETIME, End_Date DATETIME, PRIMARY KEY (Policy_ID), FOREIGN KEY (Customer_ID) REFERENCES Customers (Customer_ID) ); CREATE TABLE Claim_Headers ( Claim_Header_ID INTEGER NOT NULL, Claim_Status_Code CHAR(15) NOT NULL, Claim_Type_Code CHAR(15) NOT NULL, Policy_ID INTEGER NOT NULL, Date_of_Claim DATETIME, Date_of_Settlement DATETIME, Amount_Claimed DECIMAL(20,4), Amount_Piad DECIMAL(20,4), PRIMARY KEY (Claim_Header_ID), FOREIGN KEY (Policy_ID) REFERENCES Policies (Policy_ID) ); CREATE TABLE Claims_Documents ( Claim_ID INTEGER NOT NULL, Document_Type_Code CHAR(15) NOT NULL, Created_by_Staff_ID INTEGER, Created_Date INTEGER, PRIMARY KEY (Claim_ID, Document_Type_Code), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Created_by_Staff_ID) REFERENCES Staff (Staff_ID) ); CREATE TABLE Claims_Processing_Stages ( Claim_Stage_ID INTEGER NOT NULL, Next_Claim_Stage_ID INTEGER, Claim_Status_Name VARCHAR(255) NOT NULL, Claim_Status_Description VARCHAR(255) NOT NULL, PRIMARY KEY (Claim_Stage_ID) ); CREATE TABLE Claims_Processing ( Claim_Processing_ID INTEGER NOT NULL, Claim_ID INTEGER NOT NULL, Claim_Outcome_Code CHAR(15) NOT NULL, Claim_Stage_ID INTEGER NOT NULL, Staff_ID INTEGER, PRIMARY KEY (Claim_Processing_ID), FOREIGN KEY (Claim_ID) REFERENCES Claim_Headers (Claim_Header_ID), FOREIGN KEY (Staff_ID) REFERENCES Staff (Staff_ID) )
Find the customer who started a policy most recently.
SELECT t2.customer_details FROM policies AS t1 JOIN customers AS t2 ON t1.customer_id = t2.customer_id WHERE t1.start_date = (SELECT max(start_date) FROM policies)
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Show the number of accounts.
SELECT count(*) FROM Accounts
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
How many accounts are there?
SELECT count(*) FROM Accounts
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
How many customers have opened an account?
SELECT count(DISTINCT customer_id) FROM Accounts
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Count the number of customers who have an account.
SELECT count(DISTINCT customer_id) FROM Accounts
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Show the id, the date of account opened, the account name, and other account detail for all accounts.
SELECT account_id , date_account_opened , account_name , other_account_details FROM Accounts
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
What are the ids, date opened, name, and other details for all accounts?
SELECT account_id , date_account_opened , account_name , other_account_details FROM Accounts
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Show the id, the account name, and other account details for all accounts by the customer with first name 'Meaghan'.
SELECT T1.account_id , T1.date_account_opened , T1.account_name , T1.other_account_details FROM Accounts AS T1 JOIN Customers AS T2 ON T1.customer_id = T2.customer_id WHERE T2.customer_first_name = 'Meaghan'
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
What are the ids, names, dates of opening, and other details for accounts corresponding to the customer with the first name "Meaghan"?
SELECT T1.account_id , T1.date_account_opened , T1.account_name , T1.other_account_details FROM Accounts AS T1 JOIN Customers AS T2 ON T1.customer_id = T2.customer_id WHERE T2.customer_first_name = 'Meaghan'
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Show the account name and other account detail for all accounts by the customer with first name Meaghan and last name Keeling.
SELECT T1.account_name , T1.other_account_details FROM Accounts AS T1 JOIN Customers AS T2 ON T1.customer_id = T2.customer_id WHERE T2.customer_first_name = "Meaghan" AND T2.customer_last_name = "Keeling"
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
What are the names and other details for accounts corresponding to the customer named Meaghan Keeling?
SELECT T1.account_name , T1.other_account_details FROM Accounts AS T1 JOIN Customers AS T2 ON T1.customer_id = T2.customer_id WHERE T2.customer_first_name = "Meaghan" AND T2.customer_last_name = "Keeling"
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Show the first name and last name for the customer with account name 900.
SELECT T2.customer_first_name , T2.customer_last_name FROM Accounts AS T1 JOIN Customers AS T2 ON T1.customer_id = T2.customer_id WHERE T1.account_name = "900"
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
What are the full names of customers with the account name 900?
SELECT T2.customer_first_name , T2.customer_last_name FROM Accounts AS T1 JOIN Customers AS T2 ON T1.customer_id = T2.customer_id WHERE T1.account_name = "900"
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
How many customers don't have an account?
SELECT count(*) FROM Customers WHERE customer_id NOT IN (SELECT customer_id FROM Accounts)
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Count the number of customers who do not have an account.
SELECT count(*) FROM Customers WHERE customer_id NOT IN (SELECT customer_id FROM Accounts)
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Show the unique first names, last names, and phone numbers for all customers with any account.
SELECT DISTINCT T1.customer_first_name , T1.customer_last_name , T1.phone_number FROM Customers AS T1 JOIN Accounts AS T2 ON T1.customer_id = T2.customer_id
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
What are the distinct first names, last names, and phone numbers for customers with accounts?
SELECT DISTINCT T1.customer_first_name , T1.customer_last_name , T1.phone_number FROM Customers AS T1 JOIN Accounts AS T2 ON T1.customer_id = T2.customer_id
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Show customer ids who don't have an account.
SELECT customer_id FROM Customers EXCEPT SELECT customer_id FROM Accounts
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
What are the customer ids for customers who do not have an account?
SELECT customer_id FROM Customers EXCEPT SELECT customer_id FROM Accounts
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
How many accounts does each customer have? List the number and customer id.
SELECT count(*) , customer_id FROM Accounts GROUP BY customer_id
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Count the number of accounts corresponding to each customer id.
SELECT count(*) , customer_id FROM Accounts GROUP BY customer_id
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
What is the customer id, first and last name with most number of accounts.
SELECT T1.customer_id , T2.customer_first_name , T2.customer_last_name FROM Accounts AS T1 JOIN Customers AS T2 ON T1.customer_id = T2.customer_id GROUP BY T1.customer_id ORDER BY count(*) DESC LIMIT 1
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Return the id and full name of the customer with the most accounts.
SELECT T1.customer_id , T2.customer_first_name , T2.customer_last_name FROM Accounts AS T1 JOIN Customers AS T2 ON T1.customer_id = T2.customer_id GROUP BY T1.customer_id ORDER BY count(*) DESC LIMIT 1
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Show id, first name and last name for all customers and the number of accounts.
SELECT T1.customer_id , T2.customer_first_name , T2.customer_last_name , count(*) FROM Accounts AS T1 JOIN Customers AS T2 ON T1.customer_id = T2.customer_id GROUP BY T1.customer_id
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
What are the the full names and ids for all customers, and how many accounts does each have?
SELECT T1.customer_id , T2.customer_first_name , T2.customer_last_name , count(*) FROM Accounts AS T1 JOIN Customers AS T2 ON T1.customer_id = T2.customer_id GROUP BY T1.customer_id
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Show first name and id for all customers with at least 2 accounts.
SELECT T2.customer_first_name , T1.customer_id FROM Accounts AS T1 JOIN Customers AS T2 ON T1.customer_id = T2.customer_id GROUP BY T1.customer_id HAVING count(*) >= 2
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
What are the first names and ids for customers who have two or more accounts?
SELECT T2.customer_first_name , T1.customer_id FROM Accounts AS T1 JOIN Customers AS T2 ON T1.customer_id = T2.customer_id GROUP BY T1.customer_id HAVING count(*) >= 2
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Show the number of customers.
SELECT count(*) FROM Customers
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Count the number of customers.
SELECT count(*) FROM Customers
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Show the number of customers for each gender.
SELECT gender , count(*) FROM Customers GROUP BY gender
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
How many customers are there of each gender?
SELECT gender , count(*) FROM Customers GROUP BY gender
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
How many transactions do we have?
SELECT count(*) FROM Financial_transactions
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Count the number of transactions.
SELECT count(*) FROM Financial_transactions
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
How many transaction does each account have? Show the number and account id.
SELECT count(*) , account_id FROM Financial_transactions
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Count the number of financial transactions that correspond to each account id.
SELECT count(*) , account_id FROM Financial_transactions
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
How many transaction does account with name 337 have?
SELECT count(*) FROM Financial_transactions AS T1 JOIN Accounts AS T2 ON T1.account_id = T2.account_id WHERE T2.account_name = "337"
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Count the number of financial transactions that the account with the name 337 has.
SELECT count(*) FROM Financial_transactions AS T1 JOIN Accounts AS T2 ON T1.account_id = T2.account_id WHERE T2.account_name = "337"
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
What is the average, minimum, maximum, and total transaction amount?
SELECT avg(transaction_amount) , min(transaction_amount) , max(transaction_amount) , sum(transaction_amount) FROM Financial_transactions
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Return the average, minimum, maximum, and total transaction amounts.
SELECT avg(transaction_amount) , min(transaction_amount) , max(transaction_amount) , sum(transaction_amount) FROM Financial_transactions
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Show ids for all transactions whose amounts are greater than the average.
SELECT transaction_id FROM Financial_transactions WHERE transaction_amount > (SELECT avg(transaction_amount) FROM Financial_transactions)
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
What are the ids for transactions that have an amount greater than the average amount of a transaction?
SELECT transaction_id FROM Financial_transactions WHERE transaction_amount > (SELECT avg(transaction_amount) FROM Financial_transactions)
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Show the transaction types and the total amount of transactions.
SELECT transaction_type , sum(transaction_amount) FROM Financial_transactions GROUP BY transaction_type
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
What are total transaction amounts for each transaction type?
SELECT transaction_type , sum(transaction_amount) FROM Financial_transactions GROUP BY transaction_type
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Show the account name, id and the number of transactions for each account.
SELECT T2.account_name , T1.account_id , count(*) FROM Financial_transactions AS T1 JOIN Accounts AS T2 ON T1.account_id = T2.account_id GROUP BY T1.account_id
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Return the names and ids of each account, as well as the number of transactions.
SELECT T2.account_name , T1.account_id , count(*) FROM Financial_transactions AS T1 JOIN Accounts AS T2 ON T1.account_id = T2.account_id GROUP BY T1.account_id
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Show the account id with most number of transactions.
SELECT account_id FROM Financial_transactions GROUP BY account_id ORDER BY count(*) DESC LIMIT 1
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
What is the id of the account with the most transactions?
SELECT account_id FROM Financial_transactions GROUP BY account_id ORDER BY count(*) DESC LIMIT 1
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Show the account id and name with at least 4 transactions.
SELECT T1.account_id , T2.account_name FROM Financial_transactions AS T1 JOIN Accounts AS T2 ON T1.account_id = T2.account_id GROUP BY T1.account_id HAVING count(*) >= 4
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
What are the ids and names of accounts with 4 or more transactions?
SELECT T1.account_id , T2.account_name FROM Financial_transactions AS T1 JOIN Accounts AS T2 ON T1.account_id = T2.account_id GROUP BY T1.account_id HAVING count(*) >= 4
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Show all product sizes.
SELECT DISTINCT product_size FROM Products
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
What are the different product sizes?
SELECT DISTINCT product_size FROM Products
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Show all product colors.
SELECT DISTINCT product_color FROM Products
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
What are the different product colors?
SELECT DISTINCT product_color FROM Products
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Show the invoice number and the number of transactions for each invoice.
SELECT invoice_number , count(*) FROM Financial_transactions GROUP BY invoice_number
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
How many transactions correspond to each invoice number?
SELECT invoice_number , count(*) FROM Financial_transactions GROUP BY invoice_number
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
What is the invoice number and invoice date for the invoice with most number of transactions?
SELECT T2.invoice_number , T2.invoice_date FROM Financial_transactions AS T1 JOIN Invoices AS T2 ON T1.invoice_number = T2.invoice_number GROUP BY T1.invoice_number ORDER BY count(*) DESC LIMIT 1
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
What is the invoice number and invoice date corresponding to the invoice with the greatest number of transactions?
SELECT T2.invoice_number , T2.invoice_date FROM Financial_transactions AS T1 JOIN Invoices AS T2 ON T1.invoice_number = T2.invoice_number GROUP BY T1.invoice_number ORDER BY count(*) DESC LIMIT 1
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
How many invoices do we have?
SELECT count(*) FROM Invoices
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Count the number of invoices.
SELECT count(*) FROM Invoices
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Show invoice dates and order id and details for all invoices.
SELECT T1.invoice_date , T1.order_id , T2.order_details FROM Invoices AS T1 JOIN Orders AS T2 ON T1.order_id = T2.order_id
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
What are the invoice dates, order ids, and order details for all invoices?
SELECT T1.invoice_date , T1.order_id , T2.order_details FROM Invoices AS T1 JOIN Orders AS T2 ON T1.order_id = T2.order_id
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Show the order ids and the number of invoices for each order.
SELECT order_id , count(*) FROM Invoices GROUP BY order_id
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
How many invoices correspond to each order id?
SELECT order_id , count(*) FROM Invoices GROUP BY order_id
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
What is the order id and order details for the order more than two invoices.
SELECT T2.order_id , T2.order_details FROM Invoices AS T1 JOIN Orders AS T2 ON T1.order_id = T2.order_id GROUP BY T2.order_id HAVING count(*) > 2
1
customers_and_invoices
CREATE TABLE `Customers` ( `customer_id` INTEGER PRIMARY KEY, `customer_first_name` VARCHAR(50), `customer_middle_initial` VARCHAR(1), `customer_last_name` VARCHAR(50), `gender` VARCHAR(1), `email_address` VARCHAR(255), `login_name` VARCHAR(80), `login_password` VARCHAR(20), `phone_number` VARCHAR(255), `town_city` VARCHAR(50), `state_county_province` VARCHAR(50), `country` VARCHAR(50) ); CREATE TABLE `Orders` ( `order_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_order_placed` DATETIME NOT NULL, `order_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Invoices` ( `invoice_number` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `invoice_date` DATETIME, FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Accounts` ( `account_id` INTEGER PRIMARY KEY, `customer_id` INTEGER NOT NULL, `date_account_opened` DATETIME, `account_name` VARCHAR(50), `other_account_details` VARCHAR(255), FOREIGN KEY (`customer_id` ) REFERENCES `Customers`(`customer_id` ) ); CREATE TABLE `Product_Categories` ( `production_type_code` VARCHAR(15) PRIMARY KEY, `product_type_description` VARCHAR(80), `vat_rating` DECIMAL(19,4) ); CREATE TABLE `Products` ( `product_id` INTEGER PRIMARY KEY, `parent_product_id` INTEGER, `production_type_code` VARCHAR(15) NOT NULL, `unit_price` DECIMAL(19,4), `product_name` VARCHAR(80), `product_color` VARCHAR(20), `product_size` VARCHAR(20), FOREIGN KEY (`production_type_code` ) REFERENCES `Product_Categories`(`production_type_code` ) ); CREATE TABLE `Financial_Transactions` ( `transaction_id` INTEGER NOT NULL , `account_id` INTEGER NOT NULL, `invoice_number` INTEGER, `transaction_type` VARCHAR(15) NOT NULL, `transaction_date` DATETIME, `transaction_amount` DECIMAL(19,4), `transaction_comment` VARCHAR(255), `other_transaction_details` VARCHAR(255), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`account_id` ) REFERENCES `Accounts`(`account_id` ) ); CREATE TABLE `Order_Items` ( `order_item_id` INTEGER PRIMARY KEY, `order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_quantity` VARCHAR(50), `other_order_item_details` VARCHAR(255), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ), FOREIGN KEY (`order_id` ) REFERENCES `Orders`(`order_id` ) ); CREATE TABLE `Invoice_Line_Items` ( `order_item_id` INTEGER NOT NULL, `invoice_number` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `product_title` VARCHAR(80), `product_quantity` VARCHAR(50), `product_price` DECIMAL(19,4), `derived_product_cost` DECIMAL(19,4), `derived_vat_payable` DECIMAL(19,4), `derived_total_cost` DECIMAL(19,4), FOREIGN KEY (`order_item_id` ) REFERENCES `Order_Items`(`order_item_id` ), FOREIGN KEY (`invoice_number` ) REFERENCES `Invoices`(`invoice_number` ), FOREIGN KEY (`product_id` ) REFERENCES `Products`(`product_id` ) )
Return the order ids and details for orderes with two or more invoices.
SELECT T2.order_id , T2.order_details FROM Invoices AS T1 JOIN Orders AS T2 ON T1.order_id = T2.order_id GROUP BY T2.order_id HAVING count(*) > 2