0% found this document useful (0 votes)
14 views10 pages

SQL 1729830819

Uploaded by

Priyadharsshini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views10 pages

SQL 1729830819

Uploaded by

Priyadharsshini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Sravya Madipalli

SQL Interview
Questions
E-Commerce
Schema Details
To work through the following interview
questions, assume you are working with a
basic e-commerce database. Below are the
core tables and their structures:

Customers : Stores customer information.

Orders : Tracks orders placed by customers.


Schema Details

Products : Stores product information

order_items: Stores information about


each product included in an order
Question 1: Aggregation with Group By

Scenario: Counting the total sales (in dollars) for


each product category in January 2024

Explanation: This query calculates the total sales


for each product category by summing the
subtotals of products sold in January 2024. The
`JOIN` operations link the product and order
details, and the `GROUP BY` function ensures the
sales are aggregated by category.
Question 2: Joining Tables and
Aggregation
Scenario: Retrieving the full name and total
amount spent by each customer who made
purchases in 2023.

Explanation: This query retrieves customer


names and calculates the total amount spent by
each customer during 2023. It uses a `JOIN` to
link customer data with their respective orders,
applying a `GROUP BY` to aggregate their
spending.
Question 3: Window Functions with Running
Totals
Scenario: Calculating the running total of
sales for each day in January 2024.

Explanation: This query uses the `OVER()` clause


with the `SUM()` function to calculate a running
total of sales by date. It sums up sales amounts in
a cumulative manner, ordered by the
`order_date`.
Question 4: Using CTEs for Complex
Aggregation

Scenario: Calculating total sales per product


category, but only for products that have
sold more than 100 units in 2023.
Question 4: Using CTEs for Complex
Aggregation

Explanation: This query uses a CTE to calculate


total product sales based on quantity sold, then
filters the products with more than 100 units sold.
The main query calculates total sales for those
filtered products, grouped by category.
Question 5: Using LEAD() for Analyzing
Repeat Orders

Scenario: Calculating the number of days


between a customer’s consecutive orders.

Explanation: This query uses the `LEAD()` window


function to retrieve the order date of a
customer’s next purchase. It then calculates the
number of days between consecutive orders
using the `DATEDIFF()` function, partitioning by
each customer.
Sravya Madipalli

Was this Helpful?


Save it
Follow Me
Repost and Share it with
your friends

You might also like