The following database tables represent the structure and relationships of a company's…
- User
The following database tables represent the structure and relationships of a company's employees, departments, and projects. The database is designed to store information about employees, their roles, salaries, and the departments they belong to. It also captures details about various projects undertaken by the company, including the project's name, associated department, start date, and end date (if applicable). The "Employees" table contains information about individual employees, such as their unique ID, name, department, salary, and the ID of their manager (if any). This table allows for the representation of hierarchical relationships between employees. The "Departments" table stores information about the different departments within the company, including their unique ID, name, and allocated budget. This table helps in understanding the structure and financial resources of each department. The "Projects" table holds details about the projects the company is working on. Each project has a unique ID, name, associated department, start date, and end date (if the project has been completed). This table provides insights into the ongoing and completed projects within the company. Lastly, the "EmployeeProjects" table represents the relationship between employees and projects. It captures the employee ID, project ID, and the number of hours the employee has worked on that particular project. This table allows for tracking the involvement and effort of employees across different projects. Employees Table: ID Name Department Salary ManagerID 1 John Sales 50,000 NULL 2 Mary Engineering 75,000 5 3 Peter Sales 60,000 1 4 Emma Marketing 55,000 7 5 Michael Engineering 80,000 NULL 6 Sarah HR 62,000 7 7 Robert Executive 120,000 NULL Departments Table: ID Name Budget 1 Sales 500,000 2 Engineering 750,000 3 Marketing 400,000 4 HR 250,000 5 Executive 1,000,000
Projects Table: ID Name Department StartDate EndDate 1 Project A Sales 2023-01-01 NULL 2 Project B Engineering 2023-02-15 NULL 3 Project C Marketing 2023-03-10 NULL 4 Project D Sales 2023-04-01 NULL 5 Project E Engineering 2022-10-01 2023-03-31 EmployeeProjects Table: EmployeeID ProjectID Hours 1 1 40 1 4 25 2 2 35 2 5 20 3 1 30 4 3 40 5 2 30 5 5 15
- Calculate the total number of hours worked on each project and display the project name, department, and total hours. SORUDA SQL SATIRI NASIL YAZILIR BU SORULUYOR
- Assistant
To calculate the total number of hours worked on each project and display the project name, department, and total hours, you can use the following SQL query. This query joins the
ProjectsandEmployeeProjectstables and then aggregates the hours worked by project.sqlSELECT p.Name AS ProjectName, p.Department, SUM(ep.Hours) AS TotalHours FROM Projects p JOIN EmployeeProjects ep ON p.ID = ep.ProjectID GROUP BY p.Name, p.Department;Explanation of the query:
SELECTclause: Retrieves the project name (p.Name), department (p.Department), and the total hours worked on each project (SUM(ep.Hours)).FROMclause: Specifies theProjectstable (p) as the primary table.JOINclause: Joins theProjectstable with the table () on the matching project IDs ( = ).
- User
Find the names of employees who have worked on projects in multiple departments
- Assistant
To find the names of employees who have worked on projects in multiple departments, you can use the following SQL query. This query joins the necessary tables, groups the results by employee, and counts the distinct departments they have worked in to identify those who have worked in multiple departments.
sqlSELECT e.Name FROM Employees e JOIN EmployeeProjects ep ON e.ID = ep.EmployeeID JOIN Projects p ON ep.ProjectID = p.ID GROUP BY e.ID, e.Name HAVING COUNT(DISTINCT p.Department) > 1;Explanation of the query:
SELECTclause: Retrieves the employee name (e.Name).FROMclause: Specifies theEmployeestable (e) as the primary table.JOINclause: Joins the table with the table () using the employee IDs (), and the table with the table () using the project IDs ().