Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge: Top Customers by Order Count | Subqueries in E-commerce Analytics
Subqueries in SQL
Abschnitt 1. Kapitel 3
single

single

bookChallenge: Top Customers by Order Count

Swipe um das Menü anzuzeigen

Aufgabe

Wischen, um mit dem Codieren zu beginnen

Write an SQL query to find the top 3 customers who have placed the highest number of orders, but do not use any JOINs.

You are working with two tables: customers (contains customer_id, name) and orders (contains customer_id for each order).

Follow these steps:

  • Count how many orders each customer has made by grouping the orders table by customer_id and counting the rows. Name this result order_count.
  • For each customer, retrieve the customer’s name from the customers table using a subquery in the SELECT clause or WHERE clause (but do not use JOIN syntax).
  • Select the following columns: customer_id, name, and order_count.
  • Sort the results:
    • First by order_count in descending order (more orders first);
    • Then by customer_id in ascending order (to break ties).
  • Return only the top 3 rows.

The result should show the 3 customers with the highest number of orders, along with their names and order counts.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 3
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

some-alt