Exercises

In fact, there are many ways that queries can be constructed.

The best way to learn is by doing, and taking notes as you go.

Work with a partner, and refer to this summary sheet:

… to complete the questions listed a bit further below.

TIP

DB Browser is a macOS desktop application. Your work to answer these questions will exist in a document which you must create and save changes to manually (as opposed to environments like Google Docs where your work is automatically saved).

You are strongly recommended to take five minutes and watch this video that demonstrates how to work with multiple queries in DB Browser – this also shows how to save your work:

For reference, here is the diagram showing the database schema once again:

Now, with your partner, try to write queries to answer these questions.

Try each question on your own first. Then expand the example solution provided to check your work.

Question 1

Find a list of all the departments in the company.

Question 2

Write a query to get the details for the first 25 employees in the employee table ordered by first name, descending.

HINT

Append LIMIT x to your query, where x is an integer.

Question 3

Write a query to display the first 10 employee names (first_name, last_name) using the alias name “First Name”, “Last Name”.

Question 4

How many male employees are there? How many female employees?

Question 5

Write a query that lists all the salaries for employees in the company, in ascending order.

NOTE

Depending on the speed of your computer, this query might take a few extra seconds to run. Be patient. 🙂

Question 6

Write a query that lists all the salaries for employees in the company, in descending order.

NOTE

Depending on the speed of your computer, this query might take a few extra seconds to run. Be patient. 🙂

Question 7

Write a query to get the total salaries payable to employees.

Question 8

What is the average salary for all employees?

Question 9

How many employees work for the company?

Question 10

Find a list of employee IDs where the salary paid is greater than $60000.

Question 11

How many employee IDs have been tied to a salary that is greater than $60000?

Question 12

Find a list of employee IDs where the salary paid is in the range $60000 to $70000.

Question 13

How many employee IDs have been tied to a salary in the range $60000 to $70000?

Question 14

Find a list of employee IDs whose salary is not in the range $60000 to $70000.

Question 15

Find only the maximum and minimum salary paid to employee number 10012.

Question 16

Find the maximum and minimum salary paid to all employees.

Question 17

Write a query to display the first name and last name of all employees who have both “b” and “c” in their first name. Order the results by first name, then by last name.

Question 18

Find a list of all the job titles held by employee number 499998. List the job titles in alphabetical order.