String Pattern SQL
String Pattern SQL
In this lab, you will go through some SQL practice problems that will provide hands-on experience with
string patterns, sorting result sets and grouping result sets.
To complete this lab you will utilize a Db2 database service on IBM Cloud. If you did not already complete
this lab task earlier in this module, you will not yet have access to Db2 on IBM Cloud, and you will need to
follow the lab below first:
Hands-on Lab : Sign up for IBM Cloud, Create Db2 service instance and Get started with the Db2
console
about:blank 1/5
21/03/2024, 21:59 about:blank
NOTE: This lab requires you to have all 5 of these tables of the HR database populated with sample data on
Db2. If you didn’t complete the earlier lab in this module, you won’t have the tables above populated with
sample data on Db2, so you will need to go through the lab below first:
Hands-on Lab : Create tables using SQL scripts and Load data into tables
Objectives
After completing this lab, you will be able to:
NOTE : Make sure that you are using the CSV file and datasets from the same
instruction file.
Instructions
When you approach the exercises in this lab, follow the instructions to run the queries on Db2:
Go to the Resource List of IBM Cloud by logging in where you can find the Db2 service instance that
you created in a previous lab under Services section. Click on the Db2-xx service. Next, open the Db2
Console by clicking on Open Console button. Click on the 3-bar menu icon in the top left corner and
go to the Run SQL page. The Run SQL tool enables you to run SQL statements.
If needed, follow Hands-on Lab : Sign up for IBM Cloud, Create Db2 service instance and Get
started with the Db2 console
In this exercise, you will go through some SQL problems on String Patterns.
about:blank 2/5
21/03/2024, 21:59 about:blank
1. Problem:
Hint
Solution
1. 1
2. 2
3. 3
1. SELECT F_NAME , L_NAME
2. FROM EMPLOYEES
3. WHERE ADDRESS LIKE '%Elgin,IL%';
Copied!
Output
2. Problem:
Hint
Solution
Output
3. Problem:
Retrieve all employees in department 5 whose salary is between 60000 and 70000.
Hint
Solution
Output
Exercise 2: Sorting
1. Problem:
Hint
Solution
Output
2. Problem:
about:blank 3/5
21/03/2024, 21:59 about:blank
Hint
Solution
Output
3. (Optional) Problem:
In SQL problem 2 (Exercise 2 Problem 2), use department name instead of department ID.
Retrieve a list of employees ordered by department name, and within each department
ordered alphabetically in descending order by last name.
Hint
Solution
Output
Exercise 3: Grouping
NOTE: The SQL problems in this exercise involve usage of SQL Aggregate functions AVG and COUNT.
COUNT has been covered earlier. AVG is a function that can be used to calculate the Average or Mean of all
values of a specified column in the result set. For example, to retrieve the average salary for all employees in
the EMPLOYEES table, issue the query: SELECT AVG(SALARY) FROM EMPLOYEES;. You will learn more
about AVG and other aggregate functions later in the lecture Built-in Database Functions.
1. Problem:
Hint
Solution
Output
2. Problem:
For each department retrieve the number of employees in the department, and the average
employee salary in the department..
Hint
Solution
Output
3. Problem:
Label the computed columns in the result set of SQL problem 2 (Exercise 3 Problem 2) as
NUM_EMPLOYEES and AVG_SALARY.
Hint
Solution
about:blank 4/5
21/03/2024, 21:59 about:blank
Output
4. Problem:
In SQL problem 3 (Exercise 3 Problem 3), order the result set by Average Salary..
Hint
Solution
Output
5. Problem:
In SQL problem 4 (Exercise 3 Problem 4), limit the result to departments with fewer than 4
employees.
Hint
Solution
Output
Solution Script
If you would like to run all the solution queries of the SQL problems of this lab with a script, download the
script below. Upload the script to the Db2 console and run. Follow Hands-on Lab : Create tables using SQL
scripts and Load data into tables on how to upload a script to Db2 console and run it.
StringPattern-Sorting-Grouping_Solution_Script.sql
Congratulations! You have completed this lab, and you are ready for the next topic.
Author(s)
Rav Ahuja
Sandip Saha Joy
Changelog
about:blank 5/5