SQL Query Basics
Write SQL queries to return data specified in questions 1 to 20 (each is worth 5pts), which has to satisfy the
• 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
• 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
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.
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
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
本网站支持淘宝 支付宝 微信支付 paypal等等交易。如果不放心可以用淘宝交易！
E-mail: firstname.lastname@example.org 微信:itcsdx