Blog

Write a query to retrieve the first order placed by each customer.

This question was asked in Interview at black rock. Read more

Company: BlackRock

CTC: 26LPA

SourceLinkedIn

SQL Interview Question

Q. Write a query to retrieve the first order placed by each customer.

-- Create the Customers table
CREATE TABLE Customers (
    CustomerID INT PRIMARY KEY,
    FirstName VARCHAR(50),
    LastName VARCHAR(50),
    Email VARCHAR(100),
    City VARCHAR(50)
);

-- Create the Orders table
CREATE TABLE Orders (
    OrderID INT PRIMARY KEY,
    CustomerID INT,
    OrderDate DATE,
    TotalAmount DECIMAL(10, 2),
    FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);

-- Insert sample data into Customers table
INSERT INTO Customers VALUES
(1, 'John', 'Doe', 'john.doe@example.com', 'New York'),
(2, 'Jane', 'Smith', 'jane.smith@example.com', 'Los Angeles'),
(3, 'Emily', 'Johnson', 'emily.j@example.com', 'Chicago'),
(4, 'Michael', 'Brown', 'michael.b@example.com', 'Houston');

see this code on db-fiddle

Solution

Solution 1

SELECT 
 customerid, 
    MIN(OrderDate) AS first_order_date
FROM Orders
GROUP BY customerid;

Solution 2

SELECT *
    FROM (
        SELECT
                *,
               RANK() OVER (PARTITION BY CustomerID ORDER BY OrderDate ASC) AS order_num
        FROM Orders
     ) AS RankedOrders
WHERE order_num = 1;

Solution 3

WITH RankedOrders AS (
    SELECT 
        *,
        RANK() OVER (PARTITION BY CustomerID ORDER BY OrderDate ASC) AS order_num
    FROM Orders
)
SELECT *
FROM RankedOrders
WHERE order_num = 1;

Explanation

While there are multiple ways to solve this problem, but most simple answer would be the solution 1. Wherein we have just took minimum on order date.


I hope this would have been helpful for you, consider sharing it with your friends. thank you.

Spread the love

Recent Posts

SQL Interview Question at Zomato for a Data analyst Position (0-3 Years) – | Shared By An Experienced Data Analyst

SQL Interview Question at Zomato: These questions were recently asked in interview at Zomato, you…

6 days ago

The Ultimate Guide to SQL Indexing and Query Optimization

Introduction: SQL Indexing and Query Optimization SQL indexing is a critical concept that can drastically…

2 weeks ago

SQL Interview Questions Asked In Walmart For Data Analyst Post | CTC – 18 LPA | Learn With Curious Club!!

This article is about the SQL Interview Questions asked by Walmart for their Data Analyst…

2 weeks ago

SQL Interview Questions for Deloitte Data Engineer Roles: Your Ultimate Prep Guide

You must be able to answer these SQL Interview Questions if you are applying for…

3 weeks ago

Data Analyst SQL Interview Questions | EY (Ernst & Young) | Shared By An Experienced Data Analyst

This article tackles common SQL Interview Questions asked by EY, offering detailed solutions and explanations…

3 weeks ago

1164 Product Price at a Given Date

1164. Product Price at a Given Date: Learn how to track and select price from…

1 month ago