155k views
5 votes
For each city, list the number of customers from that city, who

have placed 3 or more orders. Cities are listed in ascending
alphabetical order.

1 Answer

2 votes

Final answer:

To satisfy the request, an SQL query should be used to perform a natural join on the customer and ordertable to count customers per city that have placed an order, ensuring the results are returned in alphabetical order by city.

Step-by-step explanation:

To list the number of customers from each city who have placed orders, you will need to perform a query that joins your customer table and order table. The SQL query should leverage a natural join, ensuring that only matching records from both tables are combined. Since cust_id is common to both tables and the foreign key relationship is declared, it's appropriate for a natural join. The cities must also be listed in ascending alphabetical order. Here is an example SQL query to achieve the requested outcome:

SELECT
c.city,
COUNT(c.cust_id) AS number_of_customers
FROM
customer c
NATURAL JOIN ordertable o
GROUP BY
c.city
ORDER BY
c.city ASC;

This SQL query will group the results by the city and count the number of distinct customers that have placed orders, ensuring the cities are returned in ascending order.

User Hektor
by
7.8k points