SQL
1. Table Schema (DDL)
-- USER table
CREATE TABLE [User] (
userid INT IDENTITY(1,1) PRIMARY KEY,
username VARCHAR(100) NOT NULL,
city VARCHAR(100) NOT NULL
);
-- PRODUCT table
CREATE TABLE Product (
productid INT IDENTITY(1,1) PRIMARY KEY,
title VARCHAR(200) NOT NULL,
price FLOAT NOT NULL
);
-- USER_PURCHASE table
CREATE TABLE User_Purchase (
purchaseid INT IDENTITY(1,1) PRIMARY KEY,
product_id INT NOT NULL,
user_id INT NOT NULL,
purchase_date DATETIME NOT NULL DEFAULT GETDATE(),
quantity INT NOT NULL,
total_price FLOAT NOT NULL,
CONSTRAINT FK_UserPurchase_Product
FOREIGN KEY (product_id) REFERENCES Product(productid),
CONSTRAINT FK_UserPurchase_User
FOREIGN KEY (user_id) REFERENCES [User](userid)
);2. Insert Dummy Data (DML)
3. Optional Validation Query
username
title
quantity
total_price
purchase_date
Sample SQL
Results
purchaseid
username
city
product_name
quantity
unit_price
total_price
purchase_date
2. Total Spending per User
Results
userid
username
city
total_spent
3. Product Sales Summary (Quantity + Revenue)
Results
productid
title
total_units_sold
total_revenue
4. Users Who Purchased More Than One Item in a Single Order
username
title
quantity
total_price
5. Daily Revenue Trend
purchase_day
daily_revenue
Assignment 1: Basic Data Retrieval
Results
username
city
Assignment 2: Product Catalog View
productid
title
price
Assignment 3: Purchase History for a Specific User
product_name
quantity
total_price
purchase_date
Assignment 4: Total Revenue Generated
Results
total_revenue
Assignment 5: Total Spending Per User
Results
username
city
total_amount_spent
Assignment 6: Best-Selling Product
Results
productid
title
total_quantity_sold
Assignment 7: Users with Multiple-Item Purchases
Results
username
city
Assignment 8: Products Never Purchased
Results
productid
title
price
Assignment 9: Daily Purchase Summary
Results
purchase_date
number_of_purchases
total_revenue
Assignment 10: Top Spending User
Results
username
city
total_amount_spent
Assignment 11: Users Without Any Purchases
Results
Assignment 12: Average Order Value
Results
average_order_value
Assignment 13: City-Wise Revenue
Results
city
total_revenue
Assignment 14: Most Recent Purchase Per User
Results
username
city
latest_purchase_date
Assignment 15: Products with Revenue Above Average
Results
productid
title
total_revenue
Assignment 16: Users Who Purchased the Same Product More Than Once
Results
Assignment 17: Purchase Count Per Product
Results
productid
title
purchase_count
Assignment 18: High-Value Purchases
Results
purchaseid
username
product_name
quantity
total_price
purchase_date
Assignment 19: First Purchase Date of Each User
Results
username
city
first_purchase_date
Assignment 20: Users Who Bought More Than One Product Type
Results
username
city
distinct_products
Assignment 21: Highest Single Purchase Value
Results
highest_single_purchase_value
Assignment 22: User Contribution Percentage
Results
username
city
total_spent
revenue_pct
Assignment 23: Product Popularity Rank
Results
productid
title
total_quantity_sold
popularity_rank
Assignment 24: Purchases Made in the Last 7 Days
Results
purchaseid
username
product_name
quantity
total_price
purchase_date
Assignment 25: Average Quantity Per Product
Results
productid
title
avg_quantity_per_txn
Assignment 26: Users with Above-Average Spending
Results
username
city
total_spent
Assignment 27: Most Purchased Product Per User
Results
username
city
most_purchased_product
total_quantity
Assignment 28: Revenue Gap Between Top 2 Products
Results
revenue_gap_top2_products
Assignment 29: Purchase Frequency Per User
Results
username
city
total_purchases
first_purchase_date
last_purchase_date
Assignment 30: Products Purchased by Users from Multiple Cities
Results
productid
title
cities_count
Assignment 31: Running Total Revenue Over Time
Results
purchase_date
purchaseid
cumulative_revenue
Assignment 32: Revenue Share per Product (%)
Results
productid
title
total_revenue
revenue_share_pct
Assignment 33: Users with Identical Total Spending
Results
Assignment 34: Most Active Purchase Day
Results
purchase_date
number_of_purchases
Assignment 35: Products with Declining Sales
Results
purchaseid
productid
title
purchase_date
previous_quantity
current_quantity
Assignment 36: Median Purchase Value
Results
median_purchase_value
Assignment 37: First-Time Buyers
Results
purchaseid
username
product_name
quantity
total_price
purchase_date
Assignment 38: User–Product Purchase Matrix
Results
username
product_name
total_quantity
Assignment 39: Products Purchased on Consecutive Days
Results
productid
title
Assignment 40: Top Product Per City
Results
city
productid
top_product
top_product_revenue
Assignment 41: Revenue Growth Between Purchases
Results
purchase_date
purchaseid
total_price
prev_total_price
revenue_diff
Assignment 42: Longest Gap Between Purchases per User
Results
username
city
max_gap_days
Assignment 43: Products Purchased by Every User
Results
Assignment 44: User Purchase Streak
Results
username
city
longest_streak_days
Assignment 45: Revenue Percentile Ranking
Results
purchaseid
total_price
percent_rank
Assignment 46: City Revenue Ranking
Results
city
total_revenue
city_rank
Assignment 47: Products with Consistent Pricing
Results
productid
title
Assignment 48: Users Whose Average Purchase Value Increased Over Time
Results
username
city
Assignment 49: Purchase Distribution Buckets
Results
bucket
purchase_count
Assignment 50: Revenue Contribution Stability
Results
username
city
stdev_purchase_value
Last updated