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

Mastering Pivot Table in Python: A Comprehensive Guide

Pivot tables are a powerful tool for summarizing and analyzing data, and Python’s Pandas library…

10 months ago

Data Science Interview Questions Section 3: SQL, Data Warehousing, and General Analytics Concepts

Welcome to Section 3 of our Data Science Interview Questions series! In this part, we…

10 months ago

Data Science Interview Questions Section 2: 25 Questions Designed To Deepen Your Understanding

Welcome back to our Data Science Interview Questions series! In the first section, we explored…

10 months ago

Data Science Questions Section 1: Data Visualization & BI Tools (Power BI, Tableau, etc.)

Data Science Questions in Section 1 focus on the essential concepts of Data Visualization and…

10 months ago

Optum Interview Questions: 30 Multiple Choice Questions (MCQs) with Answers

In this article, we’ve compiled 30 carefully selected multiple choice questions (MCQs) with answers to…

10 months ago

Day 15 of Learning Python for Data Science: Exploring Matplotlib Visualizations and EDA

Welcome to Day 15 of our Python for Data Science journey!On Day 15, we dived…

10 months ago