This project contains SQL scripts for creating and populating a sample database, along with a series of SQL queries to practice database operations such as joins, aggregations, subqueries, and date functions.
- pt_m9.sql: Main SQL script containing:
- User and database creation
- Table definitions for
employees
,departments
, andorders
- Sample data insertion
- Practice SQL queries for various tasks
-
departments
department_id
(SERIAL, Primary Key)department_name
(VARCHAR)
-
employees
employee_id
(SERIAL, Primary Key)employee_name
(VARCHAR)department_id
(INT, Foreign Key)salary
(DECIMAL)hire_date
(DATE)
-
orders
order_id
(SERIAL, Primary Key)customer_id
(INT)order_date
(DATE)total_amount
(DECIMAL)
- Inner Join: Retrieve employees with salary above 70,000 and their department names.
- Left Join: List all employees and their departments.
- Group By: Show average salary per department.
- Aggregate: Count employees in each department.
- Subquery: Find employees with salary above the company average.
- Top Departments: Departments with the highest average salary.
- Recent Hires: Employees hired in the last year.
- Yearly Hires: Count of employees hired each year.
- Frequent Customers: Customers with more than 2 orders and their total spend.
- Top Spender: Customer who spent the most.
- Average Order: Average order amount per customer.
- Monthly Orders: Number of orders per month.
- Monthly Sales (2022): Total order amount per month in 2022.
-
Setup Database
- Run the script in pt_m9.sql using your PostgreSQL client.
- This will create the user, database, tables, and insert sample data.
-
Run Queries
- Each task is separated by comments in the SQL file.
- Copy and execute the queries as needed to practice or analyze results.
- PostgreSQL
This project is for educational purposes.