Hone logo
Hone
Problems

Crafting a Consolidated Sales Report View

Creating views in SQL allows you to encapsulate complex queries and present simplified data representations. This challenge asks you to design and implement a view that combines sales data from two tables – Orders and Customers – to provide a consolidated sales report including customer information alongside order details. This is a common task in data warehousing and reporting, simplifying access to frequently used data combinations.

Problem Description

You are tasked with creating a SQL view named CustomerSalesReport. This view should combine data from the Orders and Customers tables. The view should include the following columns:

  • OrderID: The ID of the order (from the Orders table).
  • CustomerID: The ID of the customer who placed the order (from the Customers table).
  • CustomerName: The name of the customer (from the Customers table).
  • OrderDate: The date the order was placed (from the Orders table).
  • TotalAmount: The total amount of the order (from the Orders table).

The view should be created using a CREATE VIEW statement and should join the Orders and Customers tables on the CustomerID column. The view should return all rows where a matching CustomerID exists in both tables.

Key Requirements:

  • The view must be named CustomerSalesReport.
  • The view must include all the specified columns.
  • The view must join the Orders and Customers tables based on CustomerID.
  • The view must return all matching rows.

Expected Behavior:

When queried, the CustomerSalesReport view should return a result set containing the combined data as described above. The query against the view should behave as if it were a single table.

Edge Cases to Consider:

  • What happens if a CustomerID exists in the Orders table but not in the Customers table (or vice versa)? The view should only return rows where a match exists in both tables.
  • Consider potential data types of the columns involved. Ensure the view correctly handles these types.

Examples

Example 1:

-- Assume the following data exists in the tables:

-- Customers Table:
-- CustomerID | CustomerName
-- -----------|--------------
-- 1          | Alice Smith
-- 2          | Bob Johnson

-- Orders Table:
-- OrderID | CustomerID | OrderDate    | TotalAmount
-- --------|------------|--------------|-------------
-- 101     | 1          | 2023-10-26   | 100.00
-- 102     | 2          | 2023-10-27   | 250.00
-- 103     | 1          | 2023-10-28   | 75.00

-- Expected Output from SELECT * FROM CustomerSalesReport;
-- OrderID | CustomerID | CustomerName | OrderDate    | TotalAmount
-- --------|------------|--------------|--------------|-------------
-- 101     | 1          | Alice Smith  | 2023-10-26   | 100.00
-- 102     | 2          | Bob Johnson  | 2023-10-27   | 250.00
-- 103     | 1          | Alice Smith  | 2023-10-28   | 75.00

Example 2:

-- Assume the following data exists in the tables:

-- Customers Table:
-- CustomerID | CustomerName
-- -----------|--------------
-- 1          | Alice Smith
-- 2          | Bob Johnson
-- 3          | Charlie Brown

-- Orders Table:
-- OrderID | CustomerID | OrderDate    | TotalAmount
-- --------|------------|--------------|-------------
-- 101     | 1          | 2023-10-26   | 100.00
-- 102     | 2          | 2023-10-27   | 250.00
-- 104     | 4          | 2023-10-29   | 50.00  -- CustomerID 4 doesn't exist in Customers

-- Expected Output from SELECT * FROM CustomerSalesReport;
-- OrderID | CustomerID | CustomerName | OrderDate    | TotalAmount
-- --------|------------|--------------|--------------|-------------
-- 101     | 1          | Alice Smith  | 2023-10-26   | 100.00
-- 102     | 2          | Bob Johnson  | 2023-10-27   | 250.00

Constraints

  • The SQL dialect should be compatible with standard SQL (e.g., PostgreSQL, MySQL, SQL Server).
  • The Orders table has columns: OrderID (INT, Primary Key), CustomerID (INT, Foreign Key referencing Customers), OrderDate (DATE), TotalAmount (DECIMAL).
  • The Customers table has columns: CustomerID (INT, Primary Key), CustomerName (VARCHAR).
  • The view creation statement must be valid SQL.
  • The view should be efficient enough to handle a reasonable number of rows in both tables (e.g., up to 10,000 rows in each table).

Notes

  • Focus on creating the CREATE VIEW statement. You don't need to create the tables or populate them with data.
  • Consider using an INNER JOIN to ensure that only matching CustomerID values are included in the view.
  • The order of columns in the view's definition doesn't matter, as long as all required columns are present.
  • Think about the purpose of a view – to simplify queries and provide a logical representation of data.
Loading editor...
plaintext