57.9k views
0 votes
1.Use the Pine Valley Furniture (PVF) database for this question. Write a SQL query to display the customer ID and total number of orders placed for those customers who placed more than one order.

2.Use the Pine Valley Furniture (PVF) database for this question. Write a SQL query to display each customer ID and name who has bought computer desks and the total number of units bought by each customer.

3.Use the Pine Valley Furniture (PVF) database for this question. Write a SQL query to display the customer ID, name, and order ID for all customer orders. For those customers who do not have any orders, include them in the display once.

User Quaffel
by
7.8k points

1 Answer

1 vote

Final answer:

To display the customer ID and total number of orders placed, you can use the COUNT() function and GROUP BY clause. To display each customer ID and name who has bought computer desks, you can use JOIN statements and the WHERE clause. To display customer ID, name, and order ID for all customers, including those without orders, use the LEFT JOIN statement.

Step-by-step explanation:

To display the customer ID and total number of orders placed for those customers who placed more than one order, you can use the following SQL query:

SELECT customer_id, COUNT(order_id) AS total_orders FROM orders GROUP BY customer_id HAVING COUNT(order_id) > 1;

To display each customer ID and name who has bought computer desks and the total number of units bought by each customer, you can use the following SQL query:

SELECT c.customer_id, c.customer_name, SUM(od.quantity) AS total_units_bought FROM customers c JOIN orders o ON c.customer_id = o.customer_id JOIN order_details od ON o.order_id = od.order_id JOIN products p ON od.product_id = p.product_id WHERE p.product_name = 'computer desks' GROUP BY c.customer_id, c.customer_name;

To display the customer ID, name, and order ID for all customer orders, including those customers who do not have any orders, you can use the following SQL query:

SELECT c.customer_id, c.customer_name, o.order_id FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id;

User Obliquely
by
8.2k points