Q96.Marks: +2.0UGC NET Paper 2: Computer Science 18th June 2024 Shift 1 (Cancelled)
📄 Passage
Answer the following 5 questions based on the passage.
A company is implementing a new database system to manage its customer information. The database contains tables for customers, orders, products and sales representatives. Each customer can place multiple orders and each order can contain multiple products. Each order is associated with a specific sales representatives who manages the account. The database is designed to improve efficiency in tracking sales and customer interactions.
Which SQL query retrieves the total number of distinct customers who have placed orders ?
1.Select COUNT (DISTINCT customer_id) As total_customers From orders;✓ Correct
2.Select COUNT (*) As total_customers from customers;
3.Select COUNT (DISTINCT order_id) As total_customers from orders;
4.Select COUNT (DISTINCT customer_id) As total_customers from customers;
Solution
The correct answer is 1) Select COUNT (DISTINCT customer_id) As total_customers From orders;.
Key Points
The SQL query Select COUNT (DISTINCT customer_id) As total_customers From orders; retrieves the total number of distinct customers who have placed orders. This is achieved by using the COUNT function combined with DISTINCT to ensure that only unique customer IDs are counted from the orders table.
This query is useful to determine the number of unique customers who have made at least one order in the system.
Additional Information
Option 2:Select COUNT (*) As total_customers from customers; - This query counts the total number of records in the customers table, not the distinct customers who have placed orders. It will include all customers regardless of whether they have placed an order or not.
Option 3:Select COUNT (DISTINCT order_id) As total_customers from orders; - This query counts the total number of distinct orders, not customers. It will give the number of unique orders placed, not the number of unique customers.
Option 4:Select COUNT (DISTINCT customer_id) As total_customers from customers; - This query counts the number of distinct customer IDs in the customers table, which includes all customers regardless of whether they have placed an order.