This repo constitues the SQL challenge homework for MSU Data bootcamp.
I first generated an ERD diagram using quick data base diagrams app.
The diagram can be seen at the following link.
https://app.quickdatabasediagrams.com/#/d/xMgkZu
I exported the postgre SQL file generated by this app and madified it to geenrate the various queries needed for this assignment.
The code for generating the emploees Data base is here: (And saved as a file in this repo)
-- Exported from QuickDBD: https://www.quickdatabasediagrams.com/ -- Link to schema: https://app.quickdatabasediagrams.com/#/d/xMgkZu
CREATE TABLE "departments" ( "dept_no" varchar NOT NULL, "dept_name" varchar NOT NULL, CONSTRAINT "pk_departments" PRIMARY KEY ( "dept_no" ) );
CREATE TABLE "dept_emp" ( "emp_no" int NOT NULL, "dept_no" varchar NOT NULL, CONSTRAINT "pk_dept_emp" PRIMARY KEY ( "emp_no","dept_no" ) );
CREATE TABLE "dept_manager" ( "dept_no" varchar NOT NULL, "emp_no" int NOT NULL, CONSTRAINT "pk_dept_manager" PRIMARY KEY ( "dept_no","emp_no" ) );
CREATE TABLE "employees" ( "emp_no" int NOT NULL, "emp_title" varchar NOT NULL, "birth_date" date NOT NULL, "first_name" varchar NOT NULL, "last_name" varchar NOT NULL, "sex" varchar NOT NULL, "hired_date" date NOT NULL, CONSTRAINT "pk_employees" PRIMARY KEY ( "emp_no" ) );
CREATE TABLE "salaries" ( "emp_no" int NOT NULL, "salary" int NOT NULL, CONSTRAINT "pk_salaries" PRIMARY KEY ( "emp_no","salary" ) );
CREATE TABLE "titles" ( "title_id" varchar NOT NULL, "title" varchar NOT NULL, CONSTRAINT "pk_titles" PRIMARY KEY ( "title_id" ) );
ALTER TABLE "dept_emp" ADD CONSTRAINT "fk_dept_emp_emp_no" FOREIGN KEY("emp_no") REFERENCES "employees" ("emp_no");
ALTER TABLE "dept_emp" ADD CONSTRAINT "fk_dept_emp_dept_no" FOREIGN KEY("dept_no") REFERENCES "departments" ("dept_no");
ALTER TABLE "dept_manager" ADD CONSTRAINT "fk_dept_manager_dept_no" FOREIGN KEY("dept_no") REFERENCES "departments" ("dept_no");
ALTER TABLE "dept_manager" ADD CONSTRAINT "fk_dept_manager_emp_no" FOREIGN KEY("emp_no") REFERENCES "employees" ("emp_no");
ALTER TABLE "employees" ADD CONSTRAINT "fk_employees_emp_no" FOREIGN KEY("emp_no") REFERENCES "salaries" ("emp_no");
ALTER TABLE "employees" ADD CONSTRAINT "fk_employees_emp_title" FOREIGN KEY("emp_title") REFERENCES "titles" ("title_id"); Employees_DBD
The final SQL query code is below.
--check out each table select * from departments select * from dept_emp select * from dept_manager select * from employees select * from salaries select * from titles
-- mislableded hire_date as hired_date. Will change that here ALTER TABLE employees RENAME COLUMN hired_date TO hire_date;
--1. List the following details of each employee: -- employee number, last name, first name, sex, and salary.
SELECT employees.emp_no, last_name, first_name, sex, salary FROM employees INNER JOIN salaries ON employees.emp_no = salaries.emp_no;
--2. List first name, last name, and hire date for employees who were hired in 1986.
SELECT first_name, last_name, hire_date FROM employees where hire_date between '1986-01-01' and '1986-12-31' ;
--3. List the manager of each department with the following information: --department number, department name, the manager's employee number, --last name, first name. (Need to connect departments, dept_manager, employees)
SELECT d.dept_no, d.dept_name, dm.emp_no, e.last_name, e.first_name FROM departments d INNER JOIN dept_manager dm ON d.dept_no = dm.dept_no INNER JOIN employees e ON dm.emp_no = e.emp_no ;
--4. List the department of each employee with the following information: --employee number, last name, first name, and department name.
SELECT d.dept_no, d.dept_name, e.emp_no, e.last_name, e.first_name FROM departments d INNER JOIN dept_emp de ON d.dept_no = de.dept_no INNER JOIN employees e ON de.emp_no = e.emp_no ;
--5. List first name, last name, and sex for employees whose --first name is "Hercules" and last names begin with "B."
Select first_name, last_name, sex FROM employees WHERE first_name = 'Hercules' and last_name LIKE 'B%' ;
--6. List all employees in the Sales department, --including their employee number, last name, first name, and department name. SELECT e.emp_no, e.last_name, e.first_name, d.dept_name FROM departments d INNER JOIN dept_emp de ON d.dept_no = de.dept_no INNER JOIN employees e ON de.emp_no = e.emp_no WHERE d.dept_name = 'Sales' ;
--7. List all employees in the Sales and Development departments, --including their employee number, last name, first name, and department name. SELECT e.emp_no, e.last_name, e.first_name, d.dept_name FROM departments d INNER JOIN dept_emp de ON d.dept_no = de.dept_no INNER JOIN employees e ON de.emp_no = e.emp_no WHERE d.dept_name = 'Sales' or d.dept_name = 'Development' ;
--8. In descending order, list the frequency count of employee last names, --i.e., how many employees share each last name. SELECT last_name, COUNT (last_name) FROM employees GROUP BY last_name ORDER BY COUNT(last_name) DESC;