0% found this document useful (0 votes)
2 views

SQL User Stories

Uploaded by

melvanogondra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL User Stories

Uploaded by

melvanogondra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

SQL User Stories

User Story 1:-


As a developer, I like to create tables and insert data into the following
tables.

1.Employee Table
Create a table to store employee details with the following structure and
insert data:
- `employee_id`: Serial, Primary Key
- `first_name`: VARCHAR(50), Not Null
- `last_name`: VARCHAR(50), Not Null
- `email`: VARCHAR(100), Unique, Not Null
- `hire_date`: DATE, Not Null
- `salary`: DECIMAL(10, 2), Not Null
- `department_id`: INT, Foreign Key (References `departments`)
- `manager_id`: INT, Foreign Key (References `employees`)

2.Department Table
Create a table to store department information and insert data:
- `department_id`: Serial, Primary Key
- `department_name`: VARCHAR(100), Not Null

3.Project Table
Create a table to store project details and insert data:
- `project_id`: Serial, Primary Key
- `project_name`: VARCHAR(100), Not Null
- `start_date`: DATE, Not Null
- `end_date`: DATE (nullable)

4.Employee_Project Table
Create a table that links employees to projects and insert data:
- `employee_id`: INT, Foreign Key (References `employees`)
- `project_id`: INT, Foreign Key (References `projects`)
- `role`: VARCHAR(50), Primary Key (Combination of `employee_id` and
`project_id`)

5.Payroll Table
Create a table to store employee payroll information and insert data:
- `payroll_id`: Serial, Primary Key
- `employee_id`: INT, Foreign Key (References `employees`)
- `base_salary`: DECIMAL(10, 2)
- `bonus`: DECIMAL(10, 2)
- `total_salary`: DECIMAL(10, 2)
- `payment_date`: DATE

6. Attendance Table
Create a table to track employee attendance and insert data:
- `employee_id`: INT, Foreign Key (References `employees`)
- `check_in`: TIMESTAMP, Primary Key (Combination of `employee_id` and
`check_in`)
- `check_out`: TIMESTAMP

7. Training Programs Table

Create a table to track training programs:

program_id: Serial, Primary Key

program_name: VARCHAR(100), Not Null

start_date: DATE

end_date: DATE

8. Employee Training Enrollment Table

Create a table to link employees to training programs:

employee_id: INT, Foreign Key (References employees)

program_id: INT, Foreign Key (References training_programs)

enrollment_date: DATE, Primary Key (Combination of employee_id and


program_id)

Note:- While inserting data into the table go through all the User stories and
enter data based on them, this data will be reused in all the user stories.

Acceptance Criteria:
 Create table and Insert data into those tables.
 Add Screen shots of tables in tasks.

User story 2:
As a developer, I need to write the queries for the questions using joins.

1. Get Employee Details with Department Name - Write a query


to fetch the first name, last name, and department name of all employees.
2. List All Projects an Employee is Assigned To - Write a query to
retrieve the employee's first name, last name, and the names of the projects
they are assigned to.

3. Find Employees and Their Managers - Write a query to get the


first name, last name of employees, along with their manager's first and last
name.

4. Get Total Salary for Each Employee - Write a query to fetch the
first name, last name, and total salary (base salary + bonus) for all
employees from the `payroll` table.

5. List Employees Enrolled in Training Programs - Write a query


to list the first name, last name of employees, and the training program they
are enrolled in.

Acceptance Criteria:
 Write the queries for above question.
 Add Screen shots of Queries and Results in tasks.

User Story 3:

As a developer, I need to write queries for the questions below using CTE.

1.Employees with Salary Above Average - Write a query using a CTE to


find all employees whose salary is above the average salary. Display their
first name, last name, and salary.

Hint: First, use the CTE to calculate the average salary, and then use that
CTE in the main query to filter employees.

2. Projects with No Employees Assigned - Write a query using a CTE to


find all projects that have no employees assigned. Display the project name.

Hint: Use a CTE to find projects with employees from the


`employee_project` table, and then in the main query, use an ANTI JOIN or
NOT IN to find projects that are not in that CTE.
3. Count of Employees in Each Department - Write a query using a CTE
to get the count of employees in each department. Display the department
name and the count of employees.

Hint: Use a CTE to first group by department in the `employees` table and
then count the employees.

4. Employees Enrolled in Training Programs in 2024 - Write a query


using a CTE to get the list of employees who are enrolled in training
programs starting in 2024. Display the employee's first name, last name, and
the program name.

Hint: Use a CTE to filter training programs by `start_date` and then join with
`employee_training_enrollment` and `employees`.

5. Employees Working on More Than 1 Project - Write a query using a


CTE to find employees who are working on more than one project. Display
the employee’s first name, last name, and the number of projects they are
working on.

Hint: Use a CTE to count the number of projects each employee is assigned
to from the `employee_project` table, and then filter for those with more
than 1 project in the main query.

Acceptance Criteria:
 Write the queries for above question.
 Add Screen shots of Queries and Results in tasks.

User Story 4

As a developer, I need to write the queries for the questions below.

1. Retrieve All Employees Hired After 2020: - Write a query to retrieve


the first name, last name, and hire date of employees who were hired after
January 1, 2020.

2. Find Departments with More Than 5 Employees: - Write a query to


list department names where the number of employees is greater than 5.

3. List Employees by Salary in Descending Order: - Write a query to


display the first name, last name, and salary of all employees, ordered by
salary in descending order.
4. Find Employees Earning More Than the Average Salary: - Write a
query to retrieve employees who earn more than the average salary of all
employees. Display their first name, last name, and salary. (Use Subquery)

5. Find Projects Without Employees: - Write a query to find projects that


have no employees assigned. Display the project name. (Use Sub query)

6. Get Departments Where Total Salary Exceeds $500,000: - Write a


query to retrieve department names where the total salary of employees in
that department exceeds $500,000.

Acceptance Criteria:
 Write the queries for above question.
 Add Screen shots of Queries and Results in tasks.

User Story 5

As a developer, I need to write the queries for the questions below.

1. Calculate Total Salary (Base Salary + Bonus) for Each


Employee - Write a query to display the first name, last name, and
total salary (base salary + bonus) for each employee.
2. Find Employees with the Longest Last Name - Write a query to
retrieve the first name, last name, and length of the last name for
employees with the longest last name.
3. Get the Current Date and Time - Write a query to retrieve the
current date and time.
4. Create a View for High-Paid Employees - Create a view called
`high_paid_employees` that displays employee first name, last name,
and salary for employees earning more than $100,000.
5. Query the High-Paid Employees View - Write a query to retrieve all
data from the `high_paid_employees` view.
6. Update a View to Include Department Name - Alter the
`high_paid_employees` view to also include the department name.
7. Find Employees Enrolled in More Than 2 Training Programs -
Write a query to retrieve employees who are enrolled in more than two
training programs. Display their first name, last name, and the number
of programs they are enrolled in. (Use Sub query)

Acceptance Criteria:
 Write the queries for above question.
 Add Screen shots of Queries and Results in tasks.

You might also like