CMPSC 431W Database Management System Feb. 17, 2021 Assignment 2 SQL Query Basics Assigned: Feb 17, 2021 Due: Mar 3, 2021. 11:59:59PM Objectives The purpose of this assignment is go through some exercise in querying a database. It will focus on the following things: • • • • • • How to select certain rows or columns from a table How to conduct queries on multiple tables How to use expressions How to use DISTINCT, ORDER BY, and LIMIT. How to use set operations to union/intersect multiple tables How to use Aggregations and Group By to aggregate data In this particular homework, we are allow to write SQL in SQL Standard, which means you are allowed to use a few operators (e.g., EXCEPT, INTERSECT) that is not supported by MySQL. Even though it is not required, you are encouraged to use a real-world DBMS to cross check your results. If you decide to do so, I would recommend MariaDB, SQLite or MySQL. Please submit your solution to GradeScope before due date above. If you have further questions, please contact the instructor or any TA. Background Suppose you work at a bank as a data analyst. Your main job is to analyze the data stored in their database. The database has five tables, whose schema is shown as below. Primary keys attributes are underlined and foreign keys are noted in the superscript. • • • • • • Customer = {customerID, firstName, lastName, income, birthDate} Account = {accNumber, type, balance, branchNumberFK-Branch } Owns = {customerIDFK-Customer , accNumberFK-Account } Transaction = {transNumber, accNumberFK-Account , amount} Employee = {ssn, firstName, lastName, salary, branchNumberFK-Branch } Branch = {branchNumber, branchName, managerSSNFK-Employee , budget} Notes. • The customerID attribute (Customer) is a unique number that represents a customer, it is not a customer’s SSN • The accNumber attribute (Account) represents the account number • The balance (Account) attribute represents the total amount in an account • The type (Account) attribute represents the type an account: checking, saving, or business • The transNumber attribute (Transactions) represents a transaction number, combined with account number it uniquely identify a transaction 1 CMPSC 431W Database Management System Feb. 17, 2021 • The branchNumber attribute (Branch) uniquely identifies a branch • The managerSSN attribute (Branch) represents the SSN of the branch manager Questions Write SQL queries to return data specified in questions 1 to 20 (each is worth 5pts), which has to satisfy the following requirements: • The answer to each question should be a single SQL query. • You must order each query as described in the question, order is always ascending unless specified otherwise • Every column in the result should be named. So if the query ask you to return something like income times 10, make sure you include an AS statement to name the column. • While your question will not be assessed on their efficiency, marks may be deducted if unnecessary tables are included in the query (e.g., including both Owns and Customer when you only require the customerID of customers who own accounts). • You may use some date functions in the following questions. In this homework, you should refer to the date and time functions of MySQL (which is also compatible with MariaDB). For more details, please refer to this page. • In this homework, you are NOT allow to use JOIN keyword or subqueries. 1. Return rows in the Customers table representing customers who were born after January 1, 1995 and has an income less then $25,000. 2. Return first name, last name, and income of customers who earns the 20 highest income among all customers who own an account in New York branch, order by income (descending). 3. Return first name, last name, and income of customers whose income is within [60000, 70000] ordered by income (descending), lastName, firstName. 4. Return SSN, branch name, salary, and (manager’s salary - salary) (that is, the salary of a employee’s manager minus salary of that employee) of all employees in New York, London or Berlin, order by ascending (manager’s salary - salary). 5. Count the number of joint accounts (i.e., accounts owned by at least two different customers) in New York branch. 6. Return customerID, first name, last name, and income of customers who owns an account with balance greater than $5,000 in either Philadelphia branch or New York branch (not in both branches). 7. Return the customer IDs of customers whose accounts have no transactions with amounts of which the absolute value is less than $3,000 (i.e. all their transactions are either greater than or equal to $3,000 or less than or equal to -$3,000). 8. Return branch name of branches whose budget is less than the sum of salaries for all its employees. 9. Return first name, last name, and income of customers whose income are at least twice the that of any customer whose lastName is Smith, order by last name then first name. Note that no duplicate customer information should be provided in the final results. 2 CMPSC 431W Database Management System Feb. 17, 2021 10. Return customer ID, income, account numbers and branch numbers of customers with income greater than $90,000 who own an account at both London and New York branches, order by customer ID then account number. The result should contain ALL the account numbers of customers who meet the criteria, even if the account itself is not held at London or New York. 11. Return customer ID, types, account numbers, and balances of all business and savings accounts owned by customers who own at least one business account or at least one savings account, order by customer ID, then type, then account number. 12. Return branch name, account number and balance of accounts with balances greater than $110,000 held at the branch managed by Phillip Edwards, order by account number. 13. Return SSN, first name, last name and salary of the lowest paid employee (or employees) of the London branch. 14. Return branch name, and the difference between maximum and minimum salary (salary gap) and average salary of the employees at each branch, order by branch name. 15. Count of the number of different last names of employees working at the New York branch. 16. List 10 different last names of employees such that the average salary of employees in London branch sharing one of them is above $10,000. 17. Return the average income of customers from different age group (where age group is formed in a unit of 10 years, i.e., [0, 10), [10, 20), · · · ). Note that you can calculate a customer’s age by subtracting his/her birth year from the current year. 18. Return customer ID, first name, last name, income and the average account balance of customers who have at least three accounts, and whose last names begin with ‘s’ and contain an ‘e’ (e.g. Steve) OR whose first names begin with ‘a’ and have the letter ‘n’ just before the last 2 letters (e.g. Anne). Note that to appear in the result customers must have at least 3 accounts and satisfy one (or both) of the name conditions. 19. Return account number, balance, sum of transaction amounts, and (balance - transaction sum) for accounts in the London branch that have at least 15 transactions, order by transaction sum. 20. Return customer ID, first name, last name of the customers who have a total account balance in all branches greater than $300,000 but has less than $100,000 in the total balance of their accounts at the New York branch. 3