Lab - SQL Queries Worksheet-2
Lab - SQL Queries Worksheet-2
SQL Queries
The aim of the exercises are to test your understanding of the knowledge introduced in this week’s lectures. Please
also use the additional documentation on Aula under this week (lectures page). They should be completed using the
examples in the worksheet along with your own research. The exercises in this worksheet are based on a small
database simulating a bank. An overview of the tables in the database are shown below. A full definition of each of
the tables (including constraints) and the records in each can be found in the excel spreadsheet under Week 4 >
Additional Task titled “Bank Database - Table Definitions and Data.xls”.
The first thing to do is to create the tables and insert all records in SqlLiteStudio. To do this, complete the following steps:
a. Open SqlLiteStudio
b. Click [Database] select [Add a Database]
d. Right click on the name of your database, shown in the left panel underneath ‘Databases’. Choose [Connect to Database]
f. Open the file ‘Bank Database Creation Script.sql’ which can be found under Week 8 on Moodle.
g. This will open in Notepad. Select all the text in this file and paste it into the SQL Editor in SqlLiteStudio.
h. Highlight all of the text in the ‘Query’ window and then click the blue triangle icon ‘Execute Query’.
i. If the script ran successfully then you should have a ‘successful’ message in the ‘status’ window. For example, “[16:38:41]
Query finished in 1.200 second(s).”
j. To see the changes to the database schema, right-click on ‘Tables’ in the ‘Database’ panel on the left and click [Refresh all
database schemas].
l. Each of the questions below require you to write and execute SQL statements. Click [Tools] and select [Open SQL Editor].
You write queries in the ‘Query’ window, execute them by clicking the blue triangle. The results of the query are returned in
the window below the query editor. Any status messages, errors etc are displayed in the status window. Here is an example
of a simple query and its results:
1. Write an SQL statement to display this string “Keep Calm and Carry On writing SQL”.
2. Write a suitable query to display the result of increasing a value of 28,964 by 18.5%.
4. The PRODUCTS table lists products currently offered by the bank. There can be three types; Account, Loan or Insurance. List
the types currently offered by the bank. Your results should not show duplicate records.
6. List all employees whose first name starts with the letter ‘S’.
7. List all employees whose first name starts with the letter ‘S’ or ‘T’ and that also works in the ‘Operations’ department.
8. Find all employees whose first name is either Susan, Helen or Paula. Your results should show their employee ID, first name
and surname.
9. Find all employees with a start date after 1st January 2001 and before 31st December 2002.
10. The FED_ID column in the CUSTOMER table can be formatted in two ways (where n is a number):
a. nnn-nn-nnnn
b. nn-nnnnnn
List all customers that have a fed_id formatted the same as shown in (a) above.
11. Using the PRODUCTS table, list all product types and their names. The product type should be in ascending alphabetical order,
and the names in descending alphabetical order.
12. List all employees whose position is a ‘Teller’. Sort them based on their start date, displaying the longest employed person
first.
13. Select the account ID, product code, available and pending balances from the ACCOUNTS table for cust_ID=1. Write a
statement to increase all balances in this customers account by 2%. Display the updated data, listing the fields as before.
14. There has been an error and all transactions that were made on the 30th July 2003 need to be removed. Write a suitable
statement to remove the relevant records from the ACCOUNT_TRANSACTIONS table. How many records were affected?
15. List the account number, customer number and available balance for all accounts with an available balance bigger than
£10,000. Display the records in decreasing balance order.
16. List all city’s based in the state of “NH”. The results should not contain multiple rows and should be sorted alphabetically.
17. The customer Susan Tingley has notified the bank of a name change. Her surname is now ‘Brown’. Identify which table and
column need updating and write the appropriate statement to amend the data.
19. Thomas Ziegler left his job at the bank on 1st November 2019. Write an appropriate statement to amend his Employee record.
20. List all customers with a product code of ‘SAV’. Sort them by decreasing available balance.