SQL Difference between GROUP BY and DISTINCT


Example

GROUP BY is used in combination with aggregation functions. Consider the following table:

orderIduserIdstoreNameorderValueorderDate
143Store A2520-03-2016
257Store B5022-03-2016
343Store A3025-03-2016
482Store C1026-03-2016
521Store A4529-03-2016

The query below uses GROUP BY to perform aggregated calculations.

SELECT
    storeName,
    COUNT(*) AS total_nr_orders,
    COUNT(DISTINCT userId) AS nr_unique_customers,
    AVG(orderValue) AS average_order_value,
    MIN(orderDate) AS first_order,
    MAX(orderDate) AS lastOrder
FROM
    orders
GROUP BY
    storeName;

and will return the following information

storeNametotal_nr_ordersnr_unique_customersaverage_order_valuefirst_orderlastOrder
Store A3233.320-03-201629-03-2016
Store B115022-03-201622-03-2016
Store C111026-03-201626-03-2016

While DISTINCT is used to list a unique combination of distinct values for the specified columns.

SELECT DISTINCT
    storeName,
    userId
FROM
    orders;
storeNameuserId
Store A43
Store B57
Store C82
Store A21