Consider a table named 'SALE':
InvoiceNo CarId CustID Date_sale payment EmpId I001 D001 C001 2017-01-24 Cheque E002 I002 S001 C002 2017-12-11 Cash E003 I003 S002 C003 2018-09-15 Cash E005 I004 D002 C001 2018-12-24 Cheque E004 I005 S003 C002 2020-07-15 Cash E002
What will be the SQL query to find the number of different customer IDs?
Consider a table named 'SALE':
| InvoiceNo | CarId | CustID | Date_sale | payment | EmpId |
|---|---|---|---|---|---|
| I001 | D001 | C001 | 2017-01-24 | Cheque | E002 |
| I002 | S001 | C002 | 2017-12-11 | Cash | E003 |
| I003 | S002 | C003 | 2018-09-15 | Cash | E005 |
| I004 | D002 | C001 | 2018-12-24 | Cheque | E004 |
| I005 | S003 | C002 | 2020-07-15 | Cash | E002 |
What will be the SQL query to find the number of different customer IDs?
Solution
✅ Correct Option: 1
COUNT(DISTINCT CustID) counts each customer ID only once, so it returns the number of different customers. Here the CustID values are C001, C002, C003, C001, C002, so the result is .
Option 2: lists the distinct IDs (C001, C002, C003) but does not count them.
Options 3 and 4: count rows without removing duplicates, so both return 5.
Related questions:
2026: 20 May Shift 1
2025: 21 May Shift 1