SQL Exercises and Solutions in MySQL Practice
SQL Exercises and Solutions in MySQL Practice
Prepare Schema
Employees and Departments is a very common example to solve and practice many data problems. Maybe it is
too easy to create a table and insert records, but it is a tiresome job. So, here I am providing a SQL script to
create tables and insert records that will work in dB-fiddle MySQL version 5.7.
Create a Table with a primary key column
CREATE TABLE departments
( department_id INTEGER PRIMARY KEY
, department_name VARCHAR(30)
, location_id INTEGER
);
Create a Table with a Foreign Key
CREATE TABLE employees
( employee_id INTEGER
, first_name VARCHAR(20)
, last_name VARCHAR(25)
, email VARCHAR(25)
, phone_number VARCHAR(20)
, hire_date DATE
, job_id VARCHAR(10)
, salary INTEGER
, commission_pct INTEGER
, manager_id INTEGER
, department_id INTEGER
, constraint pk_emp primary key (employee_id)
, constraint fk_deptno foreign key (department_id) references departments(department_id)
);
Insert Records into Tables
## Insert insto Departments table
INSERT INTO departments VALUES ( 20,'Marketing', 180);
INSERT INTO departments VALUES ( 30,'Purchasing', 1700);
INSERT INTO departments VALUES ( 40, 'Human Resources', 2400);
INSERT INTO departments VALUES ( 50, 'Shipping', 1500);
INSERT INTO departments VALUES ( 60 , 'IT', 1400);
INSERT INTO departments VALUES ( 70, 'Public Relations', 2700);
INSERT INTO departments VALUES ( 80 , 'Sales', 2500 );
INSERT INTO departments VALUES ( 90 , 'Executive', 1700);
INSERT INTO departments VALUES ( 100 , 'Finance', 1700);
INSERT INTO departments VALUES ( 110 , 'Accounting', 1700);
INSERT INTO departments VALUES ( 120 , 'Treasury' , 1700);
INSERT INTO departments VALUES ( 130 , 'Corporate Tax' , 1700 );
INSERT INTO departments VALUES ( 140, 'Control And Credit' , 1700);
INSERT INTO departments VALUES ( 150 , 'Shareholder Services', 1700);
INSERT INTO departments VALUES ( 160 , 'Benefits', 1700);
INSERT INTO departments VALUES ( 170 , 'Payroll' , 1700);
NFLX 176.49
MSFT 153.00
META 94.33
AMZN 85.46
AAPL 61.63
GOOG 56.10
Chrome 13
Safari 11
Edge 3
GROUP BY Practice Exercise #2
Suppose you are given a table of Data Science candidates, and their technical skills:
Sample Input:
candidate_id skill
123 Python
234 R
234 Python
234 SQL Server
345 Python
... ...
ORDER BY helps you output your rows in a specific order, such as alphabetically on some text column, or from
smallest to biggest, for some text column.
COUNT DISTINCT Example
You can use with aggregate functions – the most common one being . Here's an example that finds the number
of unique user's who made trades:
Notice that goes inside the COUNT() aggregate function, rather at the beginning of the SELECT statement.
While you could use DISTINCT with SUM or AVG, in practice it's rare to want to just sum or average just the
unique values. When it comes to MAX and MIN, they aren't affected by DISTINCT – whether there are duplicates
or not, the lowest/highest value in the dataset will be the same.
SQL COUNT DISTINCT Practice Exercise
Imagine you're given a table containing data on Amazon customers and their spending on products in different
category. Write a query using to identify the number of unique products within each product category.
Example Sample Input:
category product user_id spend transaction_date
appliance refrigerator 165 246.00 12/26/2021 12:00:00
appliance refrigerator 123 299.99 03/02/2022 12:00:00
appliance washing machine 123 219.80 03/02/2022 12:00:00
electronics vacuum 178 152.00 04/05/2022 12:00:00
electronics wireless headset 156 249.90 07/08/2022 12:00:00
Example Sample Output:
category count
appliance 2
electronics 2
MySQL practice problems using the Employees Sample Database along with my solutions. See here for database
installation details.
DROP DATABASE IF EXISTS employees;
CREATE DATABASE IF NOT EXISTS employees;
USE employees;
Introduction
Exercises involve creating SQL statements in a query window.
Open a new query tab in your SQL editor [MySQL Workbench]
Create a comment at the top of the query window with your name and the exercise title.
Hint:
See Create Databases and Tables,
Page 1
At the end of these exercises you should have 3 tables with data inserted into the tables
Required setup:
This section of exercises assumes you have already accessed the sales script
at the top of the course page and created the JTS sales database in MySQL.
Exercises:
Install MySQL onto your Windows Server Virtual Machine.
Use MySQL Workbench for these questions below.
Use the SQL queries to complete the questions below and not the Workbench GUI.
1 Add your own details to the staff table
2 Add the following data to the customers table: