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)
);

Note:

  • [User] is a reserved keyword in SQL Server, so it is wrapped in square brackets.

  • IDENTITY(1,1) implements auto-increment behavior.


2. Insert Dummy Data (DML)

Users (3 records)


Products (5 records)


User Purchases (Sample transactions)

Add 5 More Users


Add 5 More Products


Add 20 More Transactions

Assuming:

  • Existing users: userid = 1–3

  • New users: userid = 4–8

  • Existing products: productid = 1–5

  • New products: productid = 6–10

3. Optional Validation Query

Results

24 rows returned

username
title
quantity
total_price
purchase_date

Arjun

Laptop

1

75000.0

2025-01-10T00:00:00

Arjun

Wireless Mouse

2

2400.0

2025-01-11T00:00:00

Meera

Mechanical Keyboard

1

4500.0

2025-01-12T00:00:00

Rahul

27-inch Monitor

1

18000.0

2025-01-13T00:00:00

Meera

USB-C Hub

3

7500.0

2025-01-14T00:00:00

Suresh

Laptop

1

75000.0

2025-01-15T00:00:00

Suresh

External SSD 1TB

1

9500.0

2025-01-16T00:00:00

Suresh

Wireless Mouse

2

2400.0

2025-01-17T00:00:00

Ananya

Mechanical Keyboard

1

4500.0

2025-01-15T00:00:00

Ananya

Bluetooth Headphones

1

6500.0

2025-01-16T00:00:00

Ananya

27-inch Monitor

1

18000.0

2025-01-18T00:00:00

Ananya

Gaming Chair

1

22000.0

2025-01-19T00:00:00

Vikram

USB-C Hub

2

5000.0

2025-01-15T00:00:00

Vikram

Webcam HD

1

3200.0

2025-01-16T00:00:00

Vikram

Laptop Stand

1

2800.0

2025-01-17T00:00:00

Vikram

External SSD 1TB

1

9500.0

2025-01-18T00:00:00

Neha

Wireless Mouse

1

1200.0

2025-01-16T00:00:00

Neha

Mechanical Keyboard

2

9000.0

2025-01-17T00:00:00

Neha

Bluetooth Headphones

1

6500.0

2025-01-18T00:00:00

Neha

27-inch Monitor

1

18000.0

2025-01-19T00:00:00

Rohit

Laptop

1

75000.0

2025-01-16T00:00:00

Rohit

Gaming Chair

1

22000.0

2025-01-17T00:00:00

Rohit

External SSD 1TB

2

19000.0

2025-01-18T00:00:00

Rohit

Laptop Stand

1

2800.0

2025-01-19T00:00:00

Sample SQL

Below are five practical SELECT queries you can use to explore and “play” with the data. These are written for Microsoft SQL Server (T-SQL) and cover common analytical patterns. 1. List All Purchases with User and Product Details

Shows who bought what, when, and for how much.

Results

24 rows returned

purchaseid
username
city
product_name
quantity
unit_price
total_price
purchase_date

12

Ananya

Delhi

Gaming Chair

1

22000.0

22000.0

2025-01-19T00:00:00

20

Neha

Jaipur

27-inch Monitor

1

18000.0

18000.0

2025-01-19T00:00:00

24

Rohit

Kolkata

Laptop Stand

1

2800.0

2800.0

2025-01-19T00:00:00

23

Rohit

Kolkata

External SSD 1TB

2

9500.0

19000.0

2025-01-18T00:00:00

19

Neha

Jaipur

Bluetooth Headphones

1

6500.0

6500.0

2025-01-18T00:00:00

11

Ananya

Delhi

27-inch Monitor

1

18000.0

18000.0

2025-01-18T00:00:00

16

Vikram

Pune

External SSD 1TB

1

9500.0

9500.0

2025-01-18T00:00:00

8

Suresh

Hyderabad

Wireless Mouse

2

1200.0

2400.0

2025-01-17T00:00:00

22

Rohit

Kolkata

Gaming Chair

1

22000.0

22000.0

2025-01-17T00:00:00

15

Vikram

Pune

Laptop Stand

1

2800.0

2800.0

2025-01-17T00:00:00

18

Neha

Jaipur

Mechanical Keyboard

2

4500.0

9000.0

2025-01-17T00:00:00

21

Rohit

Kolkata

Laptop

1

75000.0

75000.0

2025-01-16T00:00:00

17

Neha

Jaipur

Wireless Mouse

1

1200.0

1200.0

2025-01-16T00:00:00

14

Vikram

Pune

Webcam HD

1

3200.0

3200.0

2025-01-16T00:00:00

7

Suresh

Hyderabad

External SSD 1TB

1

9500.0

9500.0

2025-01-16T00:00:00

10

Ananya

Delhi

Bluetooth Headphones

1

6500.0

6500.0

2025-01-16T00:00:00

13

Vikram

Pune

USB-C Hub

2

2500.0

5000.0

2025-01-15T00:00:00

9

Ananya

Delhi

Mechanical Keyboard

1

4500.0

4500.0

2025-01-15T00:00:00

6

Suresh

Hyderabad

Laptop

1

75000.0

75000.0

2025-01-15T00:00:00

5

Meera

Bangalore

USB-C Hub

3

2500.0

7500.0

2025-01-14T00:00:00

4

Rahul

Mumbai

27-inch Monitor

1

18000.0

18000.0

2025-01-13T00:00:00

3

Meera

Bangalore

Mechanical Keyboard

1

4500.0

4500.0

2025-01-12T00:00:00

2

Arjun

Chennai

Wireless Mouse

2

1200.0

2400.0

2025-01-11T00:00:00

1

Arjun

Chennai

Laptop

1

75000.0

75000.0

2025-01-10T00:00:00


2. Total Spending per User

Useful for customer value or ranking users by spend.

Results

8 rows returned

userid
username
city
total_spent

8

Rohit

Kolkata

118800.0

4

Suresh

Hyderabad

86900.0

1

Arjun

Chennai

77400.0

5

Ananya

Delhi

51000.0

7

Neha

Jaipur

34700.0

6

Vikram

Pune

20500.0

3

Rahul

Mumbai

18000.0

2

Meera

Bangalore

12000.0


3. Product Sales Summary (Quantity + Revenue)

Helps identify best-selling products.

Results

10 rows returned

productid
title
total_units_sold
total_revenue

1

Laptop

3

225000.0

4

27-inch Monitor

3

54000.0

10

Gaming Chair

2

44000.0

6

External SSD 1TB

4

38000.0

3

Mechanical Keyboard

4

18000.0

7

Bluetooth Headphones

2

13000.0

5

USB-C Hub

5

12500.0

2

Wireless Mouse

5

6000.0

9

Laptop Stand

2

5600.0

8

Webcam HD

1

3200.0


4. Users Who Purchased More Than One Item in a Single Order

Demonstrates filtering with business logic.

Results

6 rows returned

username
title
quantity
total_price

Arjun

Wireless Mouse

2

2400.0

Meera

USB-C Hub

3

7500.0

Suresh

Wireless Mouse

2

2400.0

Vikram

USB-C Hub

2

5000.0

Neha

Mechanical Keyboard

2

9000.0

Rohit

External SSD 1TB

2

19000.0


5. Daily Revenue Trend

Good for simple time-series analysis.

Results 10 rows returned

purchase_day
daily_revenue

2025-01-10

75000.0

2025-01-11

2400.0

2025-01-12

4500.0

2025-01-13

18000.0

2025-01-14

7500.0

2025-01-15

84500.0

2025-01-16

95400.0

2025-01-17

36200.0

2025-01-18

53000.0

2025-01-19

42800.0


Assignments

Below are 10 well-structured SQL assignments focused only on SELECT statements, designed to progressively improve querying, filtering, aggregation, and analytical thinking using your existing schema.

These are suitable for practice, interviews, or classroom exercises.


Assignment 1: Basic Data Retrieval

Task: Retrieve all users along with their city.

Results

8 rows returned

username
city

Arjun

Chennai

Meera

Bangalore

Rahul

Mumbai

Suresh

Hyderabad

Ananya

Delhi

Vikram

Pune

Neha

Jaipur

Rohit

Kolkata


Assignment 2: Product Catalog View

Task: List all products showing productid, title, and price. Sort the results by price in descending order.

10 rows returned

productid
title
price

1

Laptop

75000.0

2

Wireless Mouse

1200.0

3

Mechanical Keyboard

4500.0

4

27-inch Monitor

18000.0

5

USB-C Hub

2500.0

6

External SSD 1TB

9500.0

7

Bluetooth Headphones

6500.0

8

Webcam HD

3200.0

9

Laptop Stand

2800.0

10

Gaming Chair

22000.0


Assignment 3: Purchase History for a Specific User

Task: Display all purchases made by the user named "Arjun", including:

  • product name

  • quantity

  • total price

  • purchase date

2 rows returned

product_name
quantity
total_price
purchase_date

Laptop

1

75000.0

2025-01-10T00:00:00

Wireless Mouse

2

2400.0

2025-01-11T00:00:00


Assignment 4: Total Revenue Generated

Task: Calculate the total revenue generated from all purchases.

Results

1 rows returned

total_revenue

419300.0


Assignment 5: Total Spending Per User

Task: For each user, show:

  • username

  • city

  • total amount spent

Results

8 rows returned

username
city
total_amount_spent

Rohit

Kolkata

118800.0

Suresh

Hyderabad

86900.0

Arjun

Chennai

77400.0

Ananya

Delhi

51000.0

Neha

Jaipur

34700.0

Vikram

Pune

20500.0

Rahul

Mumbai

18000.0

Meera

Bangalore

12000.0


Assignment 6: Best-Selling Product

Task: Identify the product with the highest total quantity sold.

Results

1 rows returned

productid
title
total_quantity_sold

2

Wireless Mouse

5


Assignment 7: Users with Multiple-Item Purchases

Task: Find users who have purchased more than one quantity in any single purchase.

Results

X rows returned

username
city

Arjun

Chennai

Meera

Bangalore

Neha

Jaipur

Suresh

Hyderabad

(Result depends on data)


Assignment 8: Products Never Purchased

Task: List all products that have never been purchased by any user.

Results

X rows returned

productid
title
price


Assignment 9: Daily Purchase Summary

Task: For each purchase date, show:

  • number of purchases

  • total revenue for that day

Results

X rows returned

purchase_date
number_of_purchases
total_revenue

2025-01-10

1

75000.0

2025-01-11

1

2400.0


Assignment 10: Top Spending User

Task: Find the user who has spent the maximum total amount across all purchases.

Results

1 row returned

username
city
total_amount_spent

Rohit

Kolkata

118800.0


Assignment 11: Users Without Any Purchases

Task: List all users who have never made a purchase.

Results

0 rows returned

username

city


Assignment 12: Average Order Value

Task: Calculate the average total_price per purchase.

Results

1 rows returned

average_order_value

17470.833333333332


Assignment 13: City-Wise Revenue

Task: Show total revenue generated from each city.

Results

8 rows returned

city
total_revenue

Kolkata

118800

Hyderabad

86900

Chennai

77400

Delhi

51000

Jaipur

34700

Pune

20500

Mumbai

18000

Bangalore

12000


Assignment 14: Most Recent Purchase Per User

Task: For each user, display their latest purchase date.

Results

8 rows returned

username
city
latest_purchase_date

Ananya

Delhi

2025-01-19 00:00:00

Arjun

Chennai

2025-01-11 00:00:00

Meera

Bangalore

2025-01-14 00:00:00

Neha

Jaipur

2025-01-19 00:00:00

Rahul

Mumbai

2025-01-13 00:00:00

Rohit

Kolkata

2025-01-19 00:00:00

Suresh

Hyderabad

2025-01-17 00:00:00

Vikram

Pune

2025-01-18 00:00:00


Assignment 15: Products with Revenue Above Average

Task: List products whose total revenue is greater than the average product revenue.

Results

3 rows returned

productid
title
total_revenue

1

Laptop

225000

4

27-inch Monitor

54000

10

Gaming Chair

44000


Assignment 16: Users Who Purchased the Same Product More Than Once

Task: Identify users who have purchased the same product multiple times.

Results

0 rows returned

username

product

purchase_count


Assignment 17: Purchase Count Per Product

Task: Show how many times each product was purchased (number of transactions).

Results

10 rows returned

productid
title
purchase_count

1

Laptop

3

2

Wireless Mouse

3

3

Mechanical Keyboard

3

4

27-inch Monitor

3

6

External SSD 1TB

3

5

USB-C Hub

2

7

Bluetooth Headphones

2

9

Laptop Stand

2

10

Gaming Chair

2

8

Webcam HD

1


Assignment 18: High-Value Purchases

Task: List all purchases where total_price is greater than ₹10,000.

Results

9 rows returned

purchaseid
username
product_name
quantity
total_price
purchase_date

1

Arjun

Laptop

1

75000

2025-01-10

6

Suresh

Laptop

1

75000

2025-01-15

21

Rohit

Laptop

1

75000

2025-01-16

12

Ananya

Gaming Chair

1

22000

2025-01-19

22

Rohit

Gaming Chair

1

22000

2025-01-17

4

Rahul

27-inch Monitor

1

18000

2025-01-13

11

Ananya

27-inch Monitor

1

18000

2025-01-18

20

Neha

27-inch Monitor

1

18000

2025-01-19

23

Rohit

External SSD 1TB

2

19000

2025-01-18


Assignment 19: First Purchase Date of Each User

Task: For each user, find their first purchase date.

Results

8 rows returned

username
city
first_purchase_date

Ananya

Delhi

2025-01-15 00:00:00

Arjun

Chennai

2025-01-10 00:00:00

Meera

Bangalore

2025-01-12 00:00:00

Neha

Jaipur

2025-01-16 00:00:00

Rahul

Mumbai

2025-01-13 00:00:00

Rohit

Kolkata

2025-01-16 00:00:00

Suresh

Hyderabad

2025-01-15 00:00:00

Vikram

Pune

2025-01-15 00:00:00


Assignment 20: Users Who Bought More Than One Product Type

Task: Find users who have purchased more than one distinct product.

Results

7 rows returned

username
city
distinct_products

Ananya

Delhi

4

Vikram

Pune

4

Neha

Jaipur

4

Rohit

Kolkata

4

Suresh

Hyderabad

3

Arjun

Chennai

2

Meera

Bangalore

2


Below are 10 more SELECT-only SQL assignments (Assignments 21–30). These focus on deeper analysis, edge cases, and interview-grade reasoning, while still staying strictly within your schema.


Assignment 21: Highest Single Purchase Value

Task: Find the maximum total_price recorded in a single purchase.

Results

1 rows returned

highest_single_purchase_value

75000


Assignment 22: User Contribution Percentage

Task: For each user, calculate their percentage contribution to the overall revenue.

Results

8 rows returned

username
city
total_spent
revenue_pct

Rohit

Kolkata

118800

28.33

Suresh

Hyderabad

86900

20.73

Arjun

Chennai

77400

18.46

Ananya

Delhi

51000

12.16

Neha

Jaipur

34700

8.28

Vikram

Pune

20500

4.89

Rahul

Mumbai

18000

4.29

Meera

Bangalore

12000

2.86


Assignment 23: Product Popularity Rank

Task: Rank products based on total quantity sold (highest first).

Results

10 rows returned

productid
title
total_quantity_sold
popularity_rank

2

Wireless Mouse

5

1

5

USB-C Hub

5

1

3

Mechanical Keyboard

4

2

6

External SSD 1TB

4

2

1

Laptop

3

3

4

27-inch Monitor

3

3

7

Bluetooth Headphones

2

4

9

Laptop Stand

2

4

10

Gaming Chair

2

4

8

Webcam HD

1

5


Assignment 24: Purchases Made in the Last 7 Days

Task: List all purchases made in the last 7 days (relative to the latest purchase date in the table).

Results

22 rows returned

purchaseid
username
product_name
quantity
total_price
purchase_date

3

Meera

Mechanical Keyboard

1

4500

2025-01-12

4

Rahul

27-inch Monitor

1

18000

2025-01-13

5

Meera

USB-C Hub

3

7500

2025-01-14

6

Suresh

Laptop

1

75000

2025-01-15

9

Ananya

Mechanical Keyboard

1

4500

2025-01-15

13

Vikram

USB-C Hub

2

5000

2025-01-15

7

Suresh

External SSD 1TB

1

9500

2025-01-16

10

Ananya

Bluetooth Headphones

1

6500

2025-01-16

14

Vikram

Webcam HD

1

3200

2025-01-16

17

Neha

Wireless Mouse

1

1200

2025-01-16

21

Rohit

Laptop

1

75000

2025-01-16

8

Suresh

Wireless Mouse

2

2400

2025-01-17

15

Vikram

Laptop Stand

1

2800

2025-01-17

18

Neha

Mechanical Keyboard

2

9000

2025-01-17

22

Rohit

Gaming Chair

1

22000

2025-01-17

11

Ananya

27-inch Monitor

1

18000

2025-01-18

16

Vikram

External SSD 1TB

1

9500

2025-01-18

19

Neha

Bluetooth Headphones

1

6500

2025-01-18

23

Rohit

External SSD 1TB

2

19000

2025-01-18

12

Ananya

Gaming Chair

1

22000

2025-01-19

20

Neha

27-inch Monitor

1

18000

2025-01-19

24

Rohit

Laptop Stand

1

2800

2025-01-19


Assignment 25: Average Quantity Per Product

Task: For each product, calculate the average quantity purchased per transaction.

Results

10 rows returned

productid
title
avg_quantity_per_txn

5

USB-C Hub

2.50

2

Wireless Mouse

1.67

3

Mechanical Keyboard

1.33

6

External SSD 1TB

1.33

1

Laptop

1.00

4

27-inch Monitor

1.00

7

Bluetooth Headphones

1.00

8

Webcam HD

1.00

9

Laptop Stand

1.00

10

Gaming Chair

1.00


Assignment 26: Users with Above-Average Spending

Task: List users whose total spending is above the average user spending.

Results

3 rows returned

username
city
total_spent

Rohit

Kolkata

118800

Suresh

Hyderabad

86900

Arjun

Chennai

77400


Assignment 27: Most Purchased Product Per User

Task: For each user, identify the product they purchased the most (by quantity).

Results

11 rows returned

username
city
most_purchased_product
total_quantity

Ananya

Delhi

27-inch Monitor

1

Ananya

Delhi

Bluetooth Headphones

1

Ananya

Delhi

Gaming Chair

1

Ananya

Delhi

Mechanical Keyboard

1

Arjun

Chennai

Wireless Mouse

2

Meera

Bangalore

USB-C Hub

3

Neha

Jaipur

Mechanical Keyboard

2

Rahul

Mumbai

27-inch Monitor

1

Rohit

Kolkata

External SSD 1TB

2

Suresh

Hyderabad

Wireless Mouse

2

Vikram

Pune

USB-C Hub

2


Assignment 28: Revenue Gap Between Top 2 Products

Task: Calculate the difference in total revenue between the highest-revenue product and the second highest.

Results

1 rows returned

revenue_gap_top2_products

171000


Assignment 29: Purchase Frequency Per User

Task: For each user, show total number of purchases, first purchase date, last purchase date.

Results

8 rows returned

username
city
total_purchases
first_purchase_date
last_purchase_date

Ananya

Delhi

4

2025-01-15 00:00:00

2025-01-19 00:00:00

Vikram

Pune

4

2025-01-15 00:00:00

2025-01-18 00:00:00

Neha

Jaipur

4

2025-01-16 00:00:00

2025-01-19 00:00:00

Rohit

Kolkata

4

2025-01-16 00:00:00

2025-01-19 00:00:00

Suresh

Hyderabad

3

2025-01-15 00:00:00

2025-01-17 00:00:00

Arjun

Chennai

2

2025-01-10 00:00:00

2025-01-11 00:00:00

Meera

Bangalore

2

2025-01-12 00:00:00

2025-01-14 00:00:00

Rahul

Mumbai

1

2025-01-13 00:00:00

2025-01-13 00:00:00


Assignment 30: Products Purchased by Users from Multiple Cities

Task: Find products that have been purchased by users from more than one city.

Results

9 rows returned

productid
title
cities_count

1

Laptop

3

2

Wireless Mouse

3

3

Mechanical Keyboard

3

4

27-inch Monitor

3

6

External SSD 1TB

3

5

USB-C Hub

2

7

Bluetooth Headphones

2

9

Laptop Stand

2

10

Gaming Chair

2


Below are 10 additional SELECT-only SQL assignments (Assignments 31–40). These are more advanced and closer to real-world analytics and interview scenarios, while remaining strictly within your schema.


Assignment 31: Running Total Revenue Over Time

Task: Show purchase date, purchase id, and cumulative revenue ordered by purchase date.

Results

24 rows returned

purchase_date
purchaseid
cumulative_revenue

2025-01-10

1

75000

2025-01-11

2

77400

2025-01-12

3

81900

2025-01-13

4

99900

2025-01-14

5

107400

2025-01-15

6

182400

2025-01-15

9

186900

2025-01-15

13

191900

2025-01-16

7

201400

2025-01-16

10

207900

2025-01-16

14

211100

2025-01-16

17

212300

2025-01-16

21

287300

2025-01-17

8

289700

2025-01-17

15

292500

2025-01-17

18

301500

2025-01-17

22

323500

2025-01-18

11

341500

2025-01-18

16

351000

2025-01-18

19

357500

2025-01-18

23

376500

2025-01-19

12

398500

2025-01-19

20

416500

2025-01-19

24

419300


Assignment 32: Revenue Share per Product (%)

Task: For each product, calculate its percentage share of total revenue.

Results

10 rows returned

productid
title
total_revenue
revenue_share_pct

1

Laptop

225000

53.66

4

27-inch Monitor

54000

12.88

10

Gaming Chair

44000

10.49

6

External SSD 1TB

38000

9.06

3

Mechanical Keyboard

18000

4.29

7

Bluetooth Headphones

13000

3.10

5

USB-C Hub

12500

2.98

2

Wireless Mouse

6000

1.43

9

Laptop Stand

5600

1.34

8

Webcam HD

3200

0.76


Assignment 33: Users with Identical Total Spending

Task: Find users who have exactly the same total spending as at least one other user.

Results

0 rows returned

username

city

total_spent


Assignment 34: Most Active Purchase Day

Task: Identify the day with the highest number of purchases.

Results

1 rows returned

purchase_date
number_of_purchases

2025-01-16

5


Assignment 35: Products with Declining Sales

Task: Detect product purchases where quantity decreased compared to the previous purchase (chronologically, per product).

Results

2 rows returned

purchaseid
productid
title
purchase_date
previous_quantity
current_quantity

17

2

Wireless Mouse

2025-01-16

2

1

13

5

USB-C Hub

2025-01-15

3

2


Assignment 36: Median Purchase Value

Task: Calculate the median total_price across all purchases.

Results

1 rows returned

median_purchase_value

8250


Assignment 37: First-Time Buyers

Task: List purchases that represent a user’s first-ever purchase.

Results

8 rows returned

purchaseid
username
product_name
quantity
total_price
purchase_date

9

Ananya

Mechanical Keyboard

1

4500

2025-01-15

1

Arjun

Laptop

1

75000

2025-01-10

3

Meera

Mechanical Keyboard

1

4500

2025-01-12

17

Neha

Wireless Mouse

1

1200

2025-01-16

4

Rahul

27-inch Monitor

1

18000

2025-01-13

21

Rohit

Laptop

1

75000

2025-01-16

6

Suresh

Laptop

1

75000

2025-01-15

13

Vikram

USB-C Hub

2

5000

2025-01-15


Assignment 38: User–Product Purchase Matrix

Task: Show username, product title, and total quantity purchased by that user for that product.

Results

24 rows returned

username
product_name
total_quantity

Ananya

27-inch Monitor

1

Ananya

Bluetooth Headphones

1

Ananya

Gaming Chair

1

Ananya

Mechanical Keyboard

1

Arjun

Laptop

1

Arjun

Wireless Mouse

2

Meera

Mechanical Keyboard

1

Meera

USB-C Hub

3

Neha

27-inch Monitor

1

Neha

Bluetooth Headphones

1

Neha

Mechanical Keyboard

2

Neha

Wireless Mouse

1

Rahul

27-inch Monitor

1

Rohit

External SSD 1TB

2

Rohit

Gaming Chair

1

Rohit

Laptop

1

Rohit

Laptop Stand

1

Suresh

External SSD 1TB

1

Suresh

Laptop

1

Suresh

Wireless Mouse

2

Vikram

External SSD 1TB

1

Vikram

Laptop Stand

1

Vikram

USB-C Hub

2

Vikram

Webcam HD

1


Assignment 39: Products Purchased on Consecutive Days

Task: Identify products purchased on two or more consecutive days.

Results

4 rows returned

productid
title

1

Laptop

2

Wireless Mouse

4

27-inch Monitor

5

USB-C Hub


Assignment 40: Top Product Per City

Task: For each city, find the highest revenue-generating product.

Results

8 rows returned

city
productid
top_product
top_product_revenue

Bangalore

5

USB-C Hub

7500

Chennai

1

Laptop

75000

Delhi

10

Gaming Chair

22000

Hyderabad

1

Laptop

75000

Jaipur

4

27-inch Monitor

18000

Kolkata

1

Laptop

75000

Mumbai

4

27-inch Monitor

18000

Pune

6

External SSD 1TB

9500


Below are 10 more SELECT-only SQL assignments (Assignments 41–50). These are expert-level, emphasizing analytical depth, edge cases, and advanced SQL reasoning in Microsoft SQL Server.


Assignment 41: Revenue Growth Between Purchases

Task: For each purchase, calculate difference in total_price compared to previous purchase (ordered by date).

Results

24 rows returned

purchase_date
purchaseid
total_price
prev_total_price
revenue_diff

2025-01-10

1

75000

NaN

NaN

2025-01-11

2

2400

75000

-72600

2025-01-12

3

4500

2400

2100

2025-01-13

4

18000

4500

13500

2025-01-14

5

7500

18000

-10500

2025-01-15

6

75000

7500

67500

2025-01-15

9

4500

75000

-70500

2025-01-15

13

5000

4500

500

2025-01-16

7

9500

5000

4500

2025-01-16

10

6500

9500

-3000

2025-01-16

14

3200

6500

-3300

2025-01-16

17

1200

3200

-2000

2025-01-16

21

75000

1200

73800

2025-01-17

8

2400

75000

-72600

2025-01-17

15

2800

2400

400

2025-01-17

18

9000

2800

6200

2025-01-17

22

22000

9000

13000

2025-01-18

11

18000

22000

-4000

2025-01-18

16

9500

18000

-8500

2025-01-18

19

6500

9500

-3000

2025-01-18

23

19000

6500

12500

2025-01-19

12

22000

19000

3000

2025-01-19

20

18000

22000

-4000

2025-01-19

24

2800

18000

-15200


Assignment 42: Longest Gap Between Purchases per User

Task: For each user, find the maximum number of days between any two consecutive purchase days.

Results

8 rows returned

username
city
max_gap_days

Ananya

Delhi

2

Meera

Bangalore

2

Arjun

Chennai

1

Neha

Jaipur

1

Rohit

Kolkata

1

Suresh

Hyderabad

1

Vikram

Pune

1

Rahul

Mumbai

0


Assignment 43: Products Purchased by Every User

Task: Identify products purchased by all users in the system.

Results

0 rows returned

productid

title


Assignment 44: User Purchase Streak

Task: For each user, calculate the longest consecutive-day purchase streak.

Results

8 rows returned

username
city
longest_streak_days

Neha

Jaipur

4

Rohit

Kolkata

4

Vikram

Pune

4

Suresh

Hyderabad

3

Ananya

Delhi

2

Arjun

Chennai

2

Meera

Bangalore

1

Rahul

Mumbai

1


Assignment 45: Revenue Percentile Ranking

Task: Rank purchases into percentiles based on total_price (PERCENT_RANK).

Results

24 rows returned

purchaseid
total_price
percent_rank

1

75000

91.30

6

75000

91.30

21

75000

91.30

12

22000

82.61

22

22000

82.61

23

19000

78.26

4

18000

65.22

11

18000

65.22

20

18000

65.22

7

9500

56.52

16

9500

56.52

5

7500

47.83

10

6500

39.13

19

6500

39.13

13

5000

30.43

3

4500

21.74

9

4500

21.74

18

9000

52.17

14

3200

13.04

15

2800

8.70

24

2800

8.70

2

2400

4.35

8

2400

4.35

17

1200

0.00


Assignment 46: City Revenue Ranking

Task: Rank cities based on total revenue generated.

Results

8 rows returned

city
total_revenue
city_rank

Kolkata

118800

1

Hyderabad

86900

2

Chennai

77400

3

Delhi

51000

4

Jaipur

34700

5

Pune

20500

6

Mumbai

18000

7

Bangalore

12000

8


Assignment 47: Products with Consistent Pricing

Task: List products whose unit price never changed across all purchases.

Results

10 rows returned

productid
title

1

Laptop

2

Wireless Mouse

3

Mechanical Keyboard

4

27-inch Monitor

5

USB-C Hub

6

External SSD 1TB

7

Bluetooth Headphones

8

Webcam HD

9

Laptop Stand

10

Gaming Chair


Assignment 48: Users Whose Average Purchase Value Increased Over Time

Task: Identify users whose last purchase value is greater than their first purchase value.

Results

4 rows returned

username
city

Ananya

Delhi

Meera

Bangalore

Neha

Jaipur

Vikram

Pune


Assignment 49: Purchase Distribution Buckets

Task: Bucket purchases into Low/Medium/High and show count per bucket.

Results

3 rows returned

bucket
purchase_count

Low

8

Medium

11

High

5


Assignment 50: Revenue Contribution Stability

Task: For each user, compute standard deviation of purchase values (lower = more stable).

Results

8 rows returned

username
city
stdev_purchase_value

Rahul

Mumbai

NaN

Meera

Bangalore

2121.32

Vikram

Pune

3069.61

Neha

Jaipur

7015.87

Ananya

Delhi

8568.35

Rohit

Kolkata

31355.38

Suresh

Hyderabad

40023.78

Arjun

Chennai

51335.95

Last updated