SELECT
customer_id
FROM orders
GROUP BY customer_id
HAVING COUNT(DISTINCT product_id) = (
SELECT COUNT(*)
FROM products
);
📌 Question 100: Rank Customers by Lifetime Revenue
Table: orders (customer_id, amount)
WITH customer_revenue AS (
SELECT
customer_id,
SUM(amount) AS lifetime_revenue
FROM orders
GROUP BY customer_id
)
SELECT
customer_id,
lifetime_revenue,
DENSE_RANK() OVER (
ORDER BY lifetime_revenue DESC
) AS revenue_rank
FROM customer_revenue;
💡 Pro Tip: Practice these regularly, understand the business logic behind each solution, and you'll be well-prepared for SQL interviews at product companies, startups, fintech firms, and MNCs.
❤️ Double Tap For More
Post #2564
2.13K
- ❤ 9