SQL Interview Question
SQL Interview Question
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.
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?