The SQL script contains a set of 50 queries aimed at retrieving insights from a sample database, which appears to be related to employee management, covering data like employee names, salaries, departments, job titles, and more. The dataset includes tables such as countries
, departments
, employees
, jobs
, locations
, and regions
. The goal of these queries is to address various questions about employee details, job history, salary ranges, and other employee-related data.
To incorporate the SQL file link into your analysis, you can add a section at the beginning or end of the document that references the file. Here’s how you can integrate it into your existing outline:
You can access the SQL script containing the queries here.
Below is the process for each question, breaking down the logic and methodology used to derive the results:
Objective: Display names and salaries of employees whose salaries are not between $10,000 and $15,000.
Query:
SELECT FIRST_NAME, LAST_NAME, SALARY
FROM employees
WHERE SALARY < 10000 OR SALARY > 15000;
Explanation:
- The query fetches employee names and salaries where the salary falls outside the range $10,000 to $15,000.
- It uses the condition
SALARY < 10000 OR SALARY > 15000
to exclude salaries in that range.
Objective: Display the name and department ID of all employees in departments 30 or 100, sorted by department ID.
Query:
SELECT FIRST_NAME, LAST_NAME, DEPARTMENT_ID
FROM employees
WHERE DEPARTMENT_ID IN (30, 100)
ORDER BY DEPARTMENT_ID ASC;
Explanation:
- The query filters employees who belong to department IDs 30 or 100 and orders them by department in ascending order.
- The
IN
clause simplifies filtering for multiple values.
Objective: Display names and salaries of employees in departments 30 or 100 whose salaries are outside the $10,000–$15,000 range.
Query:
SELECT FIRST_NAME, LAST_NAME, SALARY
FROM employees
WHERE DEPARTMENT_ID IN (30, 100)
AND SALARY NOT BETWEEN 10000 AND 15000;
Explanation:
- This combines two conditions: employees must belong to departments 30 or 100 and have a salary outside the specified range.
- The
NOT BETWEEN
condition is used to exclude the salary range.
Objective: Retrieve the names and hire dates of all employees hired in 1987.
Query:
SELECT FIRST_NAME, LAST_NAME, HIRE_DATE
FROM employees
WHERE YEAR(HIRE_DATE) = 1987;
Explanation:
- The
YEAR()
function extracts the year from theHIRE_DATE
column, and the query filters only those employees hired in 1987.
Objective: Display the first names of employees who have both "b" and "c" in their names.
Query:
SELECT FIRST_NAME
FROM employees
WHERE FIRST_NAME LIKE '%b%' AND FIRST_NAME LIKE '%c%';
Explanation:
- The
LIKE
operator searches for patterns in theFIRST_NAME
column. The%
wildcard is used to match any number of characters. - Both conditions ensure the name contains both letters "b" and "c."
Objective: Display the last name, job, and salary of employees whose job is Programmer or Shipping Clerk and whose salary is not $4,500, $10,000, or $15,000.
Query:
SELECT c.LAST_NAME, j.JOB_TITLE, c.SALARY
FROM employees AS c
LEFT JOIN jobs AS j ON c.JOB_ID = j.JOB_ID
WHERE j.JOB_TITLE IN ('Programmer', 'Shipping Clerk')
AND c.SALARY NOT IN (4500, 10000, 15000);
Explanation:
- The
LEFT JOIN
combines theemployees
andjobs
tables to fetch job titles. - The query filters employees with specific job titles and excludes the specified salaries using
NOT IN
.
Objective: Retrieve the last names of employees whose names have exactly 6 characters.
Query:
SELECT LAST_NAME
FROM employees
WHERE LENGTH(LAST_NAME) = 6;
Explanation:
- The
LENGTH()
function is used to count the number of characters in theLAST_NAME
field, and the condition filters for exactly 6 characters.
Some queries may benefit from indexing the employees
table on frequently queried columns like SALARY
, DEPARTMENT_ID
, and HIRE_DATE
to improve performance. For complex joins, indexing on foreign keys in both the employees
and jobs
tables can also optimize the query execution.
This SQL script addresses a range of business questions involving employee data, with particular focus on salary ranges, department assignments, and hiring periods. The use of joins, filtering conditions, and SQL functions such as YEAR()
and LENGTH()
allows the script to extract meaningful insights from the dataset.
- Indexing: Add indexes to columns frequently used in
WHERE
conditions (e.g.,SALARY
,DEPARTMENT_ID
,HIRE_DATE
) to enhance query performance. - Data Validation: Ensure that all columns (such as
SALARY
andDEPARTMENT_ID
) are complete and correctly populated, as missing data could skew the results. - Further Analysis: Additional questions could focus on employee retention, performance metrics, and salary trends over time.
Objective: Retrieve names and salaries of employees whose salaries exceed $100,000.
Query:
SELECT FIRST_NAME, LAST_NAME, SALARY
FROM employees
WHERE SALARY > 100000;
Explanation:
- The query filters for employees with a salary greater than $100,000, allowing the organization to identify high earners.
Objective: Get the total number of employees in each department.
Query:
SELECT DEPARTMENT_ID, COUNT(*) AS Employee_Count
FROM employees
GROUP BY DEPARTMENT_ID;
Explanation:
- The
GROUP BY
clause aggregates the employee count by department ID, providing insight into department sizes.
Objective: Retrieve names and hire dates of employees hired after January 1, 2020.
Query:
SELECT FIRST_NAME, LAST_NAME, HIRE_DATE
FROM employees
WHERE HIRE_DATE > '2020-01-01';
Explanation:
- This query filters employees hired after a specified date, useful for understanding recent hires.
Objective: Find the highest salary within each department.
Query:
SELECT DEPARTMENT_ID, MAX(SALARY) AS Highest_Salary
FROM employees
GROUP BY DEPARTMENT_ID;
Explanation:
- The
MAX()
function retrieves the highest salary for each department, helping to identify top earners by department.
Objective: List names and salaries of employees with job titles of "Manager" or "Senior Developer" and salaries over $75,000.
Query:
SELECT FIRST_NAME, LAST_NAME, SALARY
FROM employees
WHERE JOB_ID IN (SELECT JOB_ID FROM jobs WHERE JOB_TITLE IN ('Manager', 'Senior Developer'))
AND SALARY > 75000;
Explanation:
- The query uses a subquery to filter for specific job titles, combined with a salary threshold, providing insights into high-paying managerial and senior developer roles.
Objective: Calculate the average salary for each job title.
Query:
SELECT j.JOB_TITLE, AVG(e.SALARY) AS Average_Salary
FROM employees e
JOIN jobs j ON e.JOB_ID = j.JOB_ID
GROUP BY j.JOB_TITLE;
Explanation:
- This query joins the
employees
andjobs
tables to calculate the average salary for each job title, giving insight into pay scales across different roles.
Objective: Retrieve names of employees who do not have a manager assigned.
Query:
SELECT FIRST_NAME, LAST_NAME
FROM employees
WHERE MANAGER_ID IS NULL;
Explanation:
- This query identifies employees without a manager, potentially flagging them for further review or action regarding team structure.
Objective: List employees with last names that start with the same letter.
Query:
SELECT LAST_NAME, COUNT(*) AS Name_Count
FROM employees
GROUP BY LAST_NAME
HAVING COUNT(*) > 1;
Explanation:
- The
HAVING
clause filters for last names that appear more than once, identifying employees with common last names.
Objective: Analyze the distribution of employee salaries by creating salary ranges.
Query:
SELECT CASE
WHEN SALARY < 30000 THEN 'Low'
WHEN SALARY BETWEEN 30000 AND 60000 THEN 'Medium'
WHEN SALARY > 60000 THEN 'High'
END AS Salary_Range,
COUNT(*) AS Employee_Count
FROM employees
GROUP BY Salary_Range;
Explanation:
- This query categorizes employees into salary ranges (Low, Medium, High) and counts the number of employees in each range, providing a distribution overview.
Objective: Count employees by their respective locations.
Query:
SELECT l.CITY, COUNT(e.EMPLOYEE_ID) AS Employee_Count
FROM employees e
JOIN locations l ON e.LOCATION_ID = l.LOCATION_ID
GROUP BY l.CITY;
Explanation:
- This query counts employees in each city by joining the
employees
andlocations
tables, highlighting workforce distribution across locations.
Objective: Retrieve names of employees who receive commissions.
Query:
SELECT FIRST_NAME, LAST_NAME
FROM employees
WHERE COMMISSION_PCT IS NOT NULL;
Explanation:
- The query filters for employees with a non-null
COMMISSION_PCT
, identifying those who earn commissions.
Objective: List employees whose first names begin with a vowel.
Query:
SELECT FIRST_NAME, LAST_NAME
FROM employees
WHERE FIRST_NAME REGEXP '^[AEIOUaeiou]';
Explanation:
- This query uses a regular expression to filter first names starting with a vowel, providing insights into naming patterns.
Objective: Identify job titles that currently have no employees assigned.
Query:
SELECT j.JOB_TITLE
FROM jobs j
LEFT JOIN employees e ON j.JOB_ID = e.JOB_ID
WHERE e.EMPLOYEE_ID IS NULL;
Explanation:
- A
LEFT JOIN
is used to find job titles without employees, helping the organization identify roles that need filling.
This section of the SQL script further demonstrates the capability to extract valuable insights regarding employee data, salary distributions, hiring trends, and job roles. The use of various SQL functions and techniques, such as GROUP BY
, HAVING
, and JOIN
, enhances the depth of the analysis.