Welcome Back

Google icon Sign in with Google
OR
I agree to abide by Pharmadaily Terms of Service and its Privacy Policy

Create Account

Google icon Sign up with Google
OR
By signing up, you agree to our Terms of Service and Privacy Policy
Instagram
youtube
Facebook

Find Top 3 Customers by Order Count

Find Top 3 Customers by Order Count
Customers Table:


Orders Table:

Query Explanation:

  • "Top 3 customers" → we use TOP 3.

  • "By order count" → we use COUNT(o.OrderID) and sort using ORDER BY TotalOrders DESC.

  • The JOIN connects customer data with their orders.

  • GROUP BY ensures we count per customer.

SQL Query:

USE Techsolve;

SELECT TOP 3
    c.CustomerID,
    c.Name,
    COUNT(o.OrderID) AS TotalOrders
FROM 
    Customers c
JOIN 
    Orders o ON c.CustomerID = o.CustomerID
GROUP BY 
    c.CustomerID, c.Name
ORDER BY 
    TotalOrders DESC;

Output: