Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
41 views

Lab - SQL Queries Worksheet-2

Uploaded by

egorboy2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Lab - SQL Queries Worksheet-2

Uploaded by

egorboy2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

4005CEM Database Systems

SQL Queries

Lab – Test Your Understanding Worksheet


Complete the exercises below in SQLite. You should write the SQL code for these exercises using the SQL editor (not
using in-built functionality of the SQLite interface)

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”.

Table Name Description


ACCOUNT The table holds information regarding a customer’s bank account. Each customer can register multiple accounts
each of which corresponds to a product type provided by the bank. (See more: PRODUCT)
ACC_TRANSACTION The table holds the transaction history between the bank and a certain account.
BRANCH Branch details for the bank
BUSINESS Business customers
CUSTOMER The banks customers
DEPARTMENT The departments within the bank. Each employee can only work in one department
EMPLOYEE Holds details of all employees that work at the bank.
OFFICER Stores information about the president of companies that have business accounts
PRODUCT The products and services of bank, such as:
• Savings accounts
• Mortgages on homes
• Personal loans
• Loans to small business
PRODUCT_TYPE The types of products and services, such as:
1. Account – for personal and business customer
2. Loans - for personal and business customer
3. Insurance
The database schema has been adapted from https://o7planning.org/en/10231/sql-tutorial-for-beginners-with-oracle

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]

Amanda Brooks 20192020-JANMAY


4005CEM Database Systems
SQL Queries
c. Enter a suitable path and filename for your database. This is where your database will be saved so don’t store it locally.

d. Right click on the name of your database, shown in the left panel underneath ‘Databases’. Choose [Connect to Database]

e. Click [Tools] and select [Open SQL Editor]

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].

Amanda Brooks 20192020-JANMAY


4005CEM Database Systems
SQL Queries
k. You can view the structure of each of the tables by double-clicking ‘Tables’ in the ‘Databases’ panel and then double-clicking
on whichever table you wish to view. A number of tabs open on the right that relate to the table: Structure, Data, Constraints,
Indexes, Triggers, DDL. Click on each of these tabs to learn more. If you double-click on another table, you can switch between
their windows by selecting the relevant tab at the bottom of the window. You can also switch to the SQL editor here.

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:

Amanda Brooks 20192020-JANMAY


4005CEM Database Systems
SQL Queries

Exercises: Write SQL queries to answer the following questions.

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%.

3. List the first and last name of all Employees.

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.

5. List the different types of Loan that are offered.

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.

18. List all customers that were born before 1965.

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.

Amanda Brooks 20192020-JANMAY

You might also like