0% found this document useful (0 votes)
3 views

SQL Interview Question

The document outlines SQL commands to create and populate tables for Customers, Products, and Purchases, along with various queries to analyze customer purchasing behavior. It includes finding the most expensive products bought by each customer, identifying customers who bought the same product in consecutive months, and ranking customers by total spend. Additionally, it discusses a hiring strategy for a data team based on budget constraints for senior and junior data engineers.

Uploaded by

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

SQL Interview Question

The document outlines SQL commands to create and populate tables for Customers, Products, and Purchases, along with various queries to analyze customer purchasing behavior. It includes finding the most expensive products bought by each customer, identifying customers who bought the same product in consecutive months, and ranking customers by total spend. Additionally, it discusses a hiring strategy for a data team based on budget constraints for senior and junior data engineers.

Uploaded by

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

-- Create Customers Table

CREATE TABLE Customers (


CustomerID INT,
CustomerName VARCHAR(50)
);

-- Inserting more customer data


INSERT INTO Customers (CustomerID, CustomerName)
VALUES
(1, 'John'),
(2, 'Alice'),
(3, 'Bob'),
(4, 'Charlie'),
(5, 'David'),
(6, 'Eve'),
(7, 'Frank'),
(8, 'Grace'),
(9, 'Hannah'),
(10, 'Isla');

-- Create Products Table


CREATE TABLE Products (
ProductID INT,
ProductName VARCHAR(50),
Price DECIMAL
);

-- Inserting more product data


INSERT INTO Products (ProductID, ProductName, Price)
VALUES
(101, 'Laptop', 1200.00),
(102, 'Tablet', 500.00),
(103, 'Smartphone', 700.00),
(104, 'Smartwatch', 150.00),
(105, 'Headphones', 100.00),
(106, 'Camera', 450.00),
(107, 'Monitor', 300.00),
(108, 'Mouse', 20.00),
(109, 'Keyboard', 30.00),
(110, 'Speaker', 60.00);

-- Create Purchases Table


CREATE TABLE Purchases (
PurchaseID INT,
CustomerID INT,
ProductID INT,
PurchaseDate DATE
);

-- Insert expanded purchase data


INSERT INTO Purchases (PurchaseID, CustomerID, ProductID, PurchaseDate)
VALUES
(1, 1, 101, '2024-01-15'),
(2, 1, 101, '2024-02-10'),
(3, 1, 101, '2024-03-05'),
(4, 2, 102, '2024-01-20'),
(5, 2, 102, '2024-03-15'),
(6, 2, 102, '2024-04-12'),
(7, 3, 103, '2024-02-10'),
(8, 3, 103, '2024-02-28'),
(9, 3, 103, '2024-03-15'),
(10, 4, 104, '2024-01-25'),
(11, 4, 104, '2024-02-20'),
(12, 4, 104, '2024-03-18'),
(13, 5, 105, '2024-03-01'),
(14, 5, 105, '2024-04-01'),
(15, 5, 105, '2024-05-03'),
(16, 6, 106, '2024-01-10'),
(17, 6, 106, '2024-02-08'),
(18, 6, 106, '2024-04-07'),
(19, 7, 107, '2024-03-12'),
(20, 7, 107, '2024-04-11'),
(21, 7, 107, '2024-05-15'),
(22, 8, 108, '2024-01-15'),
(23, 8, 108, '2024-02-14'),
(24, 8, 108, '2024-03-16'),
(25, 9, 109, '2024-02-20'),
(26, 9, 109, '2024-03-18'),
(27, 9, 109, '2024-04-15'),
(28, 10, 110, '2024-03-10'),
(29, 10, 110, '2024-04-12'),
(30, 10, 110, '2024-05-15');
1.Find the Most Expensive Products Bought by Each Customer
SELECT
c.customerid,
pd.productname
FROM
customers c
INNER JOIN
purchases p ON c.customerid = p.customerid
INNER JOIN
products pd ON p.productid = pd.productid
WHERE
(c.customerid, pd.price) IN (
SELECT customerid, MAX(price)
FROM purchases p
INNER JOIN products pd ON p.productid = pd.productid
GROUP BY customerid
)

2.Customers Who Bought the Same Product in Consecutive Months


WITH PurchasesWithLag AS (
SELECT
CustomerID,
ProductID,
DATE_TRUNC('month', PurchaseDate) AS PurchaseMonth,
LAG(DATE_TRUNC('month', PurchaseDate))
OVER (PARTITION BY CustomerID, ProductID
ORDER BY PurchaseDate) AS PreviousPurchaseMonth
FROM
Purchases
)
SELECT
CustomerID,
ProductID,
TO_CHAR(PurchaseMonth, 'YYYY-MM') AS CurrentMonth,
TO_CHAR(PreviousPurchaseMonth, 'YYYY-MM') AS PreviousMonth
FROM
PurchasesWithLag
WHERE
PreviousPurchaseMonth IS NOT NULL
AND DATE_PART('month', PurchaseMonth) -
DATE_PART('month', PreviousPurchaseMonth) = 1
AND DATE_PART('year', PurchaseMonth) =
DATE_PART('year', PreviousPurchaseMonth)
ORDER BY
CustomerID, ProductID, PurchaseMonth;
---------------------------------------------------------------------------------

3. Identify Top Customers with Minimal Purchases

-- Top Customers with Minimal Purchases


SELECT
c.CustomerID,
c.CustomerName,
COUNT(p.PurchaseID) AS TotalPurchases
FROM
Customers c
LEFT JOIN
Purchases p ON c.CustomerID = p.CustomerID
GROUP BY
c.CustomerID, c.CustomerName
ORDER BY
TotalPurchases ASC
LIMIT 5; -- Adjust this limit to display more or fewer top customers
-------------------------------------------------------------------------------
4.Find Customers with Most Expensive First Purchase
SELECT c.CustomerName
,pr.ProductName
,pr.Price
,p.PurchaseDate
FROM Customers c
JOIN Purchases p ON c.CustomerID = p.CustomerID
JOIN Products pr ON p.ProductID = pr.ProductID
WHERE (
c.CustomerID
,p.PurchaseDate
) IN (
SELECT CustomerID
,MIN(PurchaseDate)
FROM Purchases
GROUP BY CustomerID
)
AND pr.Price = (
SELECT MAX(pr2.Price)
FROM Purchases p2
JOIN Products pr2 ON p2.ProductID = pr2.ProductID
WHERE p2.CustomerID = c.CustomerID
);

5. Rank Customers by Total Spend in Each Month:

SELECT c.CustomerName
,EXTRACT(YEAR FROM p.PurchaseDate) AS Year
,EXTRACT(MONTH FROM p.PurchaseDate) AS Month
,SUM(pr.Price) AS TotalSpend
,RANK() OVER (
PARTITION BY EXTRACT(YEAR FROM p.PurchaseDate)
,EXTRACT(MONTH FROM p.PurchaseDate) ORDER BY SUM(pr.Price) DESC
) AS SpendRank
FROM Customers c
JOIN Purchases p ON c.CustomerID = p.CustomerID
JOIN Products pr ON p.ProductID = pr.ProductID
GROUP BY c.CustomerName
,EXTRACT(YEAR FROM p.PurchaseDate)
,EXTRACT(MONTH FROM p.PurchaseDate)
ORDER BY c.CustomerName;

6. Calculate the cumulative spend for each customer over time, ordered by purchase
date.
SELECT
c.CustomerName,
p.PurchaseDate,
SUM(pr.Price) OVER (PARTITION BY c.CustomerID
ORDER BY p.PurchaseDate) AS CumulativeSpend
FROM Customers c
JOIN Purchases p ON c.CustomerID = p.CustomerID
JOIN Products pr ON p.ProductID = pr.ProductID
ORDER BY c.CustomerName, p.PurchaseDate;

A company has secured a $1 million budget to build a data team and develop its data
platform. The hiring team is working with a dataset called "Candidate," which
contains a comprehensive list of potential hires, categorized as senior and junior
data engineers, along with details about their salary expectations. All senior and
junior candidates have similar skills within their respective levels and are
considered eligible for hiring.

The goal of the hiring team is -

1. To hire the maximum number of seniors first.

2. After hiring the maximum number of seniors, the remaining budget should be used
to hire as many juniors as possible.

You need to write a SQL query to get the optimal list of all the candidates who can
join the data team based on the above goals?

You might also like