SQL Sample Database
A sample database is a simplified dataset used to demonstrate SQL concepts like creating tables, querying data, applying constraints, and joining tables. Below is an example of a sample database for a company that manages employees, departments, and projects.
Database Structure
1. Tables Overview
- Departments: Stores department information.
- Employees: Contains details about employees.
- Projects: Information about ongoing projects.
- Assignments: Tracks which employees are assigned to which projects.
SQL Script to Create the Sample Database
Inserting Sample Data
Sample Queries
1. List All Employees
2. List Employees in the IT Department
3. List All Projects and Their Budgets
4. Total Hours Worked on Each Project
5. Employees Assigned to a Specific Project
6. The department with the Highest Total Salaries
7. Projects That Have Not Ended
8. Average Salary by Department
Visualization of Data
Departments Table
department_id | department_name |
---|
1 | HR |
2 | IT |
3 | Finance |
Employees Table
employee_id | first_name | last_name | hire_date | salary | department_id |
---|
101 | John | Doe | 2015-01-10 | 60000 | 1 |
102 | Jane | Smith | 2018-03-15 | 75000 | 2 |
103 | Bob | Brown | 2020-07-23 | 50000 | 3 |
104 | Alice | White | 2019-11-12 | 80000 | 2 |
Projects Table
project_id | project_name | start_date | end_date | budget |
---|
201 | Website Redesign | 2023-01-01 | NULL | 50000 |
202 | Cloud Migration | 2023-06-15 | NULL | 150000 |
203 | Payroll System Upgrade | 2023-04-01 | NULL | 30000 |
Assignments Table
assignment_id | employee_id | project_id | assigned_date | hours_worked |
---|
301 | 102 | 201 | 2023-01-15 | 120 |
302 | 104 | 202 | 2023-06-20 | 200 |
303 | 103 | 203 | 2023-04-05 | 80 |
304 | 101 | 201 | 2023-02-01 | 60 |
Conclusion
This sample database provides a solid foundation for practicing SQL queries. It includes relationships, constraints, and real-world use cases. You can expand it by adding more tables or data as needed for more complex queries.