SQL

 

Nested Queries Between Multiple Tables

Data was imported using PostgreSQL from multiple CSV files. Relationship between tables was designed in order to identify retiring employees for a company and identifying opportunities for mentorships and recruiting.

Tables With Established Relationships

Multiple visualization were created in order to meet Human Resources need for information.

-- Creating tables for PH-EmployeeDB

CREATE TABLE departments (

dept_no VARCHAR(4) NOT NULL,

dept_name VARCHAR(40) NOT NULL,

PRIMARY KEY (dept_no),

UNIQUE (dept_name)

);

CREATE TABLE employees (

emp_no INT NOT NULL,

birth_date DATE NOT NULL,

first_name VARCHAR NOT NULL,

last_name VARCHAR NOT NULL,

gender VARCHAR NOT NULL,

hire_date DATE NOT NULL,

PRIMARY KEY (emp_no)

);

CREATE TABLE dept_manager (

dept_no VARCHAR(4) NOT NULL,

emp_no INT NOT NULL,

from_date DATE NOT NULL,

to_date DATE NOT NULL,

FOREIGN KEY (emp_no) REFERENCES employees (emp_no),

FOREIGN KEY (dept_no) REFERENCES departments (dept_no),

PRIMARY KEY (emp_no, dept_no)

);

CREATE TABLE salaries (

emp_no INT NOT NULL,

salary INT NOT NULL,

from_date DATE NOT NULL,

to_date DATE NOT NULL,

FOREIGN KEY (emp_no) REFERENCES employees (emp_no),

PRIMARY KEY (emp_no)

);

CREATE TABLE titles (

emp_no INT NOT NULL,

title VARCHAR NOT NULL,

from_date DATE NOT NULL,

to_date DATE NOT NULL

-- PRIMARY KEY (emp_no)

);

CREATE TABLE dept_emp (

emp_no INT NOT NULL,

dept_no VARCHAR NOT NULL,

from_date DATE NOT NULL,

to_date DATE NOT NULL

-- PRIMARY KEY (emp_no)

);

SELECT * FROM dept_emp

DROP TABLE dept_emp

SELECT first_name, last_name

FROM employees

WHERE birth_date BETWEEN '1952-01-01' AND '1955-12-31';

SELECT first_name, last_name

FROM employees

WHERE birth_date BETWEEN '1952-01-01' AND '1952-12-31';

-- Retirement eligibility

SELECT first_name, last_name

FROM employees

WHERE (birth_date BETWEEN '1952-01-01' AND '1955-12-31')

AND (hire_date BETWEEN '1985-01-01' AND '1988-12-31');

-- Number of employees retiring

SELECT COUNT(first_name)

FROM employees

WHERE (birth_date BETWEEN '1952-01-01' AND '1955-12-31')

AND (hire_date BETWEEN '1985-01-01' AND '1988-12-31');

SELECT first_name, last_name

INTO retirement_info

FROM employees

WHERE (birth_date BETWEEN '1952-01-01' AND '1955-12-31')

AND (hire_date BETWEEN '1985-01-01' AND '1988-12-31');

SELECT * FROM retirement_info;

-- Call tables for visualization

SELECT * FROM employees

SELECT * FROM titles

-- Create retiring_by_title table by merging employees and titles tables

SELECT e.emp_no,

e.first_name,

e.last_name,

t.title,

t.from_date,

t.to_date

INTO retirement_titles

FROM employees AS e

LEFT JOIN titles AS t

ON e.emp_no = t.emp_no

WHERE e.birth_date BETWEEN '1952-01-01' AND '1955-12-31'

ORDER BY e.emp_no ASC

-- Verify table

SELECT * FROM retirement_titles

-- Create unique_titles table by using previous table, drop from_date and to_date,

-- filter by the last title of employees

SELECT DISTINCT ON (r.emp_no) r.emp_no,

r.first_name,

r.last_name,

r.title

INTO unique_titles

FROM retirement_titles AS r

ORDER BY r.emp_no ASC, r.to_date DESC;

-- Verify table

SELECT * FROM unique_titles

-- Export unique_titles as csv and retrieve number of employees by most recent job title who are about to retire

-- Create a retiring_titles table

SELECT COUNT(u.title), u.title

INTO retiring_titles

FROM unique_titles AS u

GROUP BY u.title

ORDER BY COUNT(u.title) DESC

-- Verify table

SELECT * FROM retiring_titles

-- Call tables for visualization

SELECT * FROM employees

SELECT * FROM dept_emp

SELECT * FROM titles

-- Create mentorship_eligibilty table with employees who are eligible to participate in a mentorship program.

SELECT DISTINCT ON(e.emp_no) e.emp_no,

e.first_name,

e.last_name,

e.birth_date,

d.from_date,

d.to_date,

t.title

FROM employees AS e

JOIN dept_emp AS d

ON e.emp_no = d.emp_no

JOIN titles AS t

ON e.emp_no = t.emp_no

WHERE (d.to_date = ('9999-01-01'))

AND (e.birth_date BETWEEN '1965-01-01' AND '1965-12-31')

ORDER BY e.emp_no ASC, t.to_date DESC

-- Verify table

SELECT * FROM mentorship_eligibilty