Hone logo
Hone
Problems

Designing an Efficient Sales Data Warehouse in SQL

Data warehouses are crucial for business intelligence, enabling organizations to analyze historical data and make informed decisions. This challenge asks you to design a SQL schema and populate it with sample data to efficiently support common sales reporting queries. The goal is to optimize for query performance and data integrity, demonstrating your understanding of data warehousing principles.

Problem Description

You are tasked with designing a data warehouse to store sales data for a retail company. The data warehouse should be structured to efficiently answer common business questions, such as: "What were the total sales for a specific product category in a given month?", "Which region had the highest sales growth compared to the previous year?", and "What are the top 10 best-selling products overall?".

The data warehouse will be populated with data from two source tables: Staging.Sales and Staging.Products. You need to design a star schema (or a similar dimensional model) that includes fact and dimension tables. The schema should be optimized for analytical queries, minimizing joins and maximizing performance. You must also consider data integrity and consistency.

Key Requirements:

  • Star Schema: Implement a star schema with a central fact table and multiple dimension tables.
  • Fact Table: The fact table should contain transactional data, including sales amount, quantity sold, date of sale, product ID, customer ID, and store ID.
  • Dimension Tables: Dimension tables should contain descriptive attributes related to products, customers, stores, and dates.
  • Data Types: Choose appropriate data types for each column to optimize storage and performance.
  • Indexing: Strategically create indexes to speed up query execution.
  • Data Loading: Provide pseudocode for loading data from the staging tables into the data warehouse tables.
  • Query Performance: The schema should be designed to support efficient querying, even with large datasets.

Expected Behavior:

The data warehouse should be able to efficiently answer the example queries mentioned above. Queries should execute within acceptable timeframes (e.g., under 5 seconds for common reports). The data should be accurate and consistent, reflecting the data in the staging tables.

Edge Cases to Consider:

  • Null Values: Handle null values appropriately in both the staging and data warehouse tables.
  • Data Types: Ensure data types are compatible between staging and data warehouse tables.
  • Large Datasets: The design should scale well to handle large volumes of data.
  • Changing Requirements: Consider how the schema could be adapted to accommodate future reporting needs.
  • Duplicate Records: Handle potential duplicate records in the staging tables during the loading process.

Examples

Example 1:

Input: Staging.Sales: [SaleID: 1, ProductID: 101, CustomerID: 201, StoreID: 301, SaleDate: '2023-01-15', Quantity: 2, SaleAmount: 50.00], Staging.Products: [ProductID: 101, ProductName: 'Laptop', CategoryID: 401, Price: 1000.00]
Output: Data Warehouse Fact Table: [SaleID: 1, ProductKey: 101, CustomerKey: 201, StoreKey: 301, DateKey: 20230115, Quantity: 2, SaleAmount: 50.00]
Dimension Tables populated with relevant product, customer, store, and date information.
Explanation: The staging data is transformed and loaded into the appropriate keys in the fact table and dimension tables.  The SaleDate is converted to a DateKey (YYYYMMDD).

Example 2:

Input: Staging.Sales: [SaleID: 2, ProductID: 102, CustomerID: 202, StoreID: 302, SaleDate: '2023-02-20', Quantity: 1, SaleAmount: 200.00], Staging.Products: [ProductID: 102, ProductName: 'Mouse', CategoryID: 402, Price: 25.00]
Output: Data Warehouse Fact Table: [SaleID: 2, ProductKey: 102, CustomerKey: 202, StoreKey: 302, DateKey: 20230220, Quantity: 1, SaleAmount: 200.00]
Dimension Tables populated with relevant product, customer, store, and date information.
Explanation: Similar to Example 1, the data is transformed and loaded into the data warehouse.

Example 3: (Edge Case - Duplicate Sales Record)

Input: Staging.Sales: [SaleID: 3, ProductID: 101, CustomerID: 201, StoreID: 301, SaleDate: '2023-01-15', Quantity: 2, SaleAmount: 50.00], Staging.Sales: [SaleID: 4, ProductID: 101, CustomerID: 201, StoreID: 301, SaleDate: '2023-01-15', Quantity: 2, SaleAmount: 50.00]
Output: Data Warehouse Fact Table: [SaleID: 3, ProductKey: 101, CustomerKey: 201, StoreKey: 301, DateKey: 20230115, Quantity: 2, SaleAmount: 50.00]
Explanation: The duplicate record (SaleID 4) is ignored or handled appropriately (e.g., by using a unique constraint or a deduplication process) to ensure data integrity.

Constraints

  • Database System: Assume a standard SQL database system (e.g., PostgreSQL, MySQL, SQL Server). The specific syntax may need to be adjusted based on the chosen system.
  • Data Volume: The data warehouse is expected to handle at least 1 million sales records.
  • Query Performance: Queries should execute in under 5 seconds for common reporting scenarios.
  • Staging Tables: The Staging.Sales table has columns: SaleID (INT, Primary Key), ProductID (INT, Foreign Key), CustomerID (INT, Foreign Key), StoreID (INT, Foreign Key), SaleDate (DATE), Quantity (INT), SaleAmount (DECIMAL).
  • Staging Tables: The Staging.Products table has columns: ProductID (INT, Primary Key), ProductName (VARCHAR), CategoryID (INT, Foreign Key), Price (DECIMAL).

Notes

  • Focus on the schema design and indexing strategy. Detailed implementation of data loading scripts is not required, but pseudocode is.
  • Consider using surrogate keys for dimension tables to improve performance and flexibility.
  • Think about how to handle slowly changing dimensions (SCDs) if the requirements evolve. For this challenge, assume a simple SCD Type 0 (overwrite existing data) for simplicity.
  • The goal is to demonstrate a solid understanding of data warehousing principles and best practices.
  • Provide SQL DDL statements for creating the tables and pseudocode for the data loading process.
  • Clearly explain your design choices and how they contribute to efficient query performance.
  • Assume that the staging tables are already populated with data.
Loading editor...
plaintext