0% found this document useful (0 votes)
17 views2 pages

Question 5

The document provides a SQL query that combines data from the 'customers' and 'orders' tables to retrieve customer ID, name, and the number of orders placed by each customer. It uses a LEFT JOIN to include customers with no orders and aggregates the data using GROUP BY. The results are sorted in ascending order of customer ID, ensuring all customers are listed with their respective order counts.

Uploaded by

raskeljay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

Question 5

The document provides a SQL query that combines data from the 'customers' and 'orders' tables to retrieve customer ID, name, and the number of orders placed by each customer. It uses a LEFT JOIN to include customers with no orders and aggregates the data using GROUP BY. The results are sorted in ascending order of customer ID, ensuring all customers are listed with their respective order counts.

Uploaded by

raskeljay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Q: Create a SQL query that combines data from the

'customers' and 'orders' tables to retrieve the customer

ID, name, and number of orders placed by each customer. Sort the results in
ascending order of customer ID

A: To combine data from the `customers` and `orders` tables and retrieve
the customer ID, name, and number of orders placed by each customer, we
can use an `INNER JOIN` along with the `GROUP BY` clause to aggregate the
data. Here’s how you can write the SQL query:

### SQL Query:

```sql

SELECT c.customer_id, c.name, COUNT(o.order_id) AS number_of_orders

FROM customers c

LEFT JOIN orders o ON c.customer_id = o.customer_id

GROUP BY c.customer_id, c.name

ORDER BY c.customer_id ASC;

```

### Explanation:

- `SELECT c.customer_id, c.name`: This selects the customer ID and name


from the `customers` table.

- `COUNT(o.order_id) AS number_of_orders`: This counts the number of


orders placed by each customer, labeling the count as `number_of_orders`.

- `FROM customers c`: Specifies the `customers` table as the primary table
(alias `c`).

- `LEFT JOIN orders o ON c.customer_id = o.customer_id`: This joins the


`orders` table (alias `o`) with the `customers` table on the `customer_id`.
The `LEFT JOIN` ensures that customers with no orders are still included in
the results with a count of zero.

- `GROUP BY c.customer_id, c.name`: Groups the results by customer ID and


name to aggregate the order counts correctly.

- `ORDER BY c.customer_id ASC`: Sorts the results in ascending order based


on the customer ID.

This query will provide a list of customers along with the number of orders
they have placed, sorted by customer ID. If a customer has not placed any
orders, they will still appear in the results with a count of zero.

You might also like