Database: loan_1

Question: List the name of all different customers who have an loan sorted by their total loan amount.
SQL: SELECT T1.cust_name FROM customer AS T1 JOIN loan AS T2 ON T1.cust_id  =  T2.cust_id GROUP BY T1.cust_name ORDER BY sum(T2.amount)

Question: Find the name and account balance of the customer whose name includes the letter ‘a’.
SQL: SELECT cust_name ,  acc_bal FROM customer WHERE cust_name LIKE '%a%'

Question: Find the total account balance of each customer from Utah or Texas.
SQL: SELECT sum(acc_bal) FROM customer WHERE state  =  'Utah' OR state  =  'Texas'

Question: Find the name of customers who have both saving and checking account types.
SQL: SELECT cust_name FROM customer WHERE acc_type  =  'saving' INTERSECT SELECT cust_name FROM customer WHERE acc_type  =  'checking'

Question: Find the name of customers who do not have a saving account.
SQL: SELECT cust_name FROM customer EXCEPT SELECT cust_name FROM customer WHERE acc_type  =  'saving'

Question: Find the name of customers who do not have a loan with a type of Mortgages.
SQL: SELECT cust_name FROM customer EXCEPT SELECT T1.cust_name FROM customer AS T1 JOIN loan AS T2 ON T1.cust_id  =  T2.cust_id WHERE T2.loan_type  =  'Mortgages'

Question: Find the name of customers who have loans of both Mortgages and Auto.
SQL: SELECT T1.cust_name FROM customer AS T1 JOIN loan AS T2 ON T1.cust_id  =  T2.cust_id WHERE loan_type  =  'Mortgages' INTERSECT SELECT T1.cust_name FROM customer AS T1 JOIN loan AS T2 ON T1.cust_id  =  T2.cust_id WHERE loan_type  =  'Auto'

Question: Find the name of customers whose credit score is below the average credit scores of all customers.
SQL: SELECT cust_name FROM customer WHERE credit_score  <  (SELECT avg(credit_score) FROM customer)

Question: Find the branch name of the bank that has the most number of customers.
SQL: SELECT bname FROM bank ORDER BY no_of_customers DESC LIMIT 1

Question: Find the name of customer who has the lowest credit score.
SQL: SELECT cust_name FROM customer ORDER BY credit_score LIMIT 1

Question: Find the name, account type, and account balance of the customer who has the highest credit score.
SQL: SELECT cust_name ,  acc_type ,  acc_bal FROM customer ORDER BY credit_score DESC LIMIT 1

Question: Find the name of customer who has the highest amount of loans.
SQL: SELECT T1.cust_name FROM customer AS T1 JOIN loan AS T2 ON T1.cust_id  =  T2.cust_id GROUP BY T1.cust_name ORDER BY sum(T2.amount) DESC LIMIT 1

Question: Find the state which has the most number of customers.
SQL: SELECT state FROM bank GROUP BY state ORDER BY sum(no_of_customers) DESC LIMIT 1

Question: For each account type, find the average account balance of customers with credit score lower than 50.
SQL: SELECT avg(acc_bal) ,  acc_type FROM customer WHERE credit_score  <  50 GROUP BY acc_type

Question: For each state, find the total account balance of customers whose credit score is above 100.
SQL: SELECT sum(acc_bal) ,  state FROM customer WHERE credit_score  >  100 GROUP BY state