Skip to main contentSkip to solution

Consider the following Table "SALE".

InvoiceNoCarIdCustIDSaleDate
I00001C123C00012019-05-24
I00002C128C00022020-02-21
I00003C139C00012020-03-23
I00004C145C00032020-04-17
I00005C147C00022021-04-11
I00006C145C00042022-05-08

What will be the output of the following SQL Query?

SELECT CustID, COUNT(*) FROM SALE GROUP BY CustID HAVING Count(*)>1;

Solution

✅ Correct Option: 1

GROUP BY CustID puts the rows into one group per customer, and HAVING COUNT(*) > 1 keeps only the groups with more than one row.

CustIDRows (COUNT(*))Kept by HAVING COUNT(*) > 1
C00012 (I00001, I00003)Yes
C00022 (I00002, I00005)Yes
C00031No
C00041No

The output has both selected columns, CustID and COUNT(*), for C0001 and C0002.


Option 2: leaves out C0002, which also has 2 sales.

Options 3 and 4: the query selects both CustID and COUNT(*), so both columns must appear in the output.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question