SQL Exercises and Solutions in MySQL
SQL Exercises and Solutions in MySQL
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);
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;