Blinkit & Zepto Interview Questions
Blinkit & Zepto Interview Questions
questions .
( YoE 2+ years)
CTC= 16 LPA
• sale_date (DATE)
• sales_amount (INT)
SELECT
sale_date,
sales_amount,
AVG(sales_amount) OVER (
ORDER BY sale_date
) AS moving_avg_7d
FROM sales_data;
This calculates the 7-day moving average, including the current day and the past 6
days.
• employee_id (INT)
• in_time (DATETIME)
• out_time (DATETIME)
SQL Query:
SELECT
employee_id,
DATE(in_time) AS work_date,
FROM attendance
• product_id (INT)
• category_id (INT)
• revenue (DECIMAL)
WITH ranked_products AS (
SELECT
category_id,
product_id,
SUM(revenue) AS total_revenue,
FROM sales
FROM ranked_products
• customer_id (INT)
• transaction_date (DATETIME)
• amount (DECIMAL)
FIRST_VALUE(transaction_date) OVER (
PARTITION BY customer_id
ORDER BY transaction_date
) AS first_transaction,
LAST_VALUE(transaction_date) OVER (
PARTITION BY customer_id
ORDER BY transaction_date
) AS last_transaction
FROM transactions
Retrieves the first and last transaction dates within a given time range.
• product_id (INT)
• region (VARCHAR)
• sales_amount (DECIMAL)
• order_count (INT)
SELECT
region,
product_id,
sales_amount,
order_count,
RANK() OVER (
PARTITION BY region
FROM sales_data;
Ranks products by sales amount within each region and breaks ties using
order_count.
• salary (DECIMAL)
FROM employees
WITH salary_ranks AS (
FROM employees
FROM salary_ranks
WHERE rnk = 2;
• category_id (INT)
• revenue (DECIMAL)
SELECT
category_id,
product_id,
revenue,
FROM sales;
• invoice_id (INT)
• invoice_date (DATE)
WITH missing_invoices AS (
SELECT
invoice_id,
FROM invoices
FROM missing_invoices
• product_id (INT)
• sale_month (DATE)
• revenue (DECIMAL)
WITH monthly_sales AS (
SELECT
product_id,
sale_month,
revenue,
FROM sales_data
SELECT
product_id,
sale_month,
revenue,
prev_month_revenue,
ROUND(((revenue - prev_month_revenue) / prev_month_revenue) * 100, 2) AS
mom_growth_percentage
FROM monthly_sales
• customer_id (INT)
• purchase_date (DATE)
SELECT customer_id
FROM sales
GROUP BY customer_id
Identifies customers who made at least one purchase in every quarter of 2024.
11. Calculate Cumulative Sales for Each Product Over Time &
Compare with Average Sales
Assuming a table sales_data with columns:
• product_id (INT)
• sale_date (DATE)
• sales_amount (DECIMAL)
SELECT
product_id,
sale_date,
sales_amount,
SUM(sales_amount) OVER (
) AS cumulative_sales,
FROM sales_data
FROM cumulative_sales;
Computes cumulative sales per product over time and compares it with its average
sales.
• customer_id (INT)
• purchase_date (DATE)
WITH monthly_purchases AS (
SELECT
customer_id,
COUNT(*) AS purchase_count
FROM sales
),
comparison AS (
SELECT
customer_id,
year,
month,
purchase_count,
FROM monthly_purchases
FROM comparison
13. Identify Products That Have Never Been Sold Along with Their
Categories
Assuming two tables:
FROM products p
• customer_id (INT)
• sale_date (DATE)
• revenue (DECIMAL)
WITH customer_revenue AS (
SELECT
customer_id,
SUM(revenue) AS total_revenue
FROM sales
GROUP BY customer_id
FROM customer_revenue
LIMIT 3;
Retrieves top 3 customers with the highest revenue in the last year.
15. Write a Query to Compare Sales of the Same Product Across
Two Different Time Periods
Assuming a table sales_data with columns:
• product_id (INT)
• sale_date (DATE)
• revenue (DECIMAL)
SELECT
product_id,
FROM sales_data
GROUP BY product_id;
• customer_id (INT)
• purchase_date (DATE)
WITH purchase_streaks AS (
SELECT
customer_id,
purchase_date,
) DAY AS streak_group
FROM sales
SELECT
customer_id,
COUNT(*) AS longest_streak
FROM purchase_streaks
LIMIT 1;
• user_id (INT)
• activity_date (DATE)
• signup_date (DATE)
WITH daily_retention AS (
SELECT
activity_date,
COUNT(DISTINCT user_id) AS active_users,
FROM user_activity
GROUP BY activity_date
SELECT
activity_date,
active_users,
prev_day_users,
FROM daily_retention;
Computes daily rolling retention by comparing active users with the previous day.
FROM data_table
19. Delete Duplicate Rows but Keep the Row with the Earliest
Timestamp
Assuming a table data_table with a created_at timestamp column:
WITH duplicates_cte AS (
SELECT
*,
FROM data_table
SELECT col1, col2, col3, created_at FROM duplicates_cte WHERE row_num > 1
);
• department_id (INT)
• salary (DECIMAL)
SELECT
department_id,
FROM employees
GROUP BY department_id;
Finds the median salary per department using PERCENTILE_CONT(), which works in
PostgreSQL and SQL Server.
WITH ranked_salaries AS (
SELECT
department_id,
salary,
FROM employees
FROM ranked_salaries
GROUP BY department_id;
• product_id (INT)
• sale_date (DATE)
WITH monthly_sales AS (
SELECT
product_id,
DATE_FORMAT(sale_date, '%Y-%m') AS sale_month
FROM sales
),
sales_streaks AS (
SELECT
product_id,
sale_month,
FROM monthly_sales
SELECT product_id
FROM sales_streaks
Finds products sold for 3 consecutive months but missing in the 4th month.
• customer_id (INT)
• purchase_date (DATE)
WITH cohort_assignment AS (
SELECT
customer_id,
FROM sales
GROUP BY customer_id
),
cohort_analysis AS (
SELECT
ca.cohort_month,
FROM sales s
SELECT
cohort_month,
active_month,
active_users,
FROM cohort_analysis
• user_id (INT)
• start_time (DATETIME)
• end_time (DATETIME)
WITH ordered_sessions AS (
SELECT
user_id,
start_time,
end_time,
FROM user_sessions
SELECT
user_id,
GROUP BY user_id;
24. Find All Products That Were Never Purchased Together in the
Same Transaction
Assuming a table sales with columns:
• transaction_id (INT)
• product_id (INT)
FROM sales p1
SELECT 1
FROM sales s1
);
Finds product pairs that were never purchased together in the same transaction.
• product_id (INT)
• sales_amount (DECIMAL)
SQL Query (Using Recursive CTE for Date Generation and LEFT JOIN)
WITH date_series AS (
UNION ALL
FROM date_series
SELECT d.sale_date,
s.product_id,
COALESCE(SUM(s.sales_amount), 0) AS total_sales
FROM date_series d