Computer Science

Table SALES

Column Name
STORE_ID
SALES_DATE
SALES_AMOUNT

Which SQL statement lets you find the total number of stores in the SALES table?

  1. SELECT COUNT(STORE_ID) FROM SALES;
  2. SELECT COUNT(DISTINCT STORE_ID) FROM SALES;
  3. SELECT DISTINCT STORE_ID FROM SALES;
  4. SELECT COUNT(STORE_ID) FROM SALES GROUP BY STORE_ID;

SQL Joins & Grouping

3 Likes

Answer

SELECT COUNT(DISTINCT STORE_ID) FROM SALES;
Explanation
  1. SELECT COUNT(STORE_ID) FROM SALES; — This query uses the COUNT() aggregate function with the STORE_ID column in the SELECT statement. It counts the number of non-null values in the STORE_ID column, and this count includes duplicates.
  2. SELECT COUNT(DISTINCT STORE_ID) FROM SALES; — This option uses COUNT(DISTINCT STORE_ID) to count the number of unique store IDs in the SALES table. The DISTINCT keyword ensures that only distinct (unique) values are counted, avoiding overcounting due to duplicates.
  3. SELECT DISTINCT STORE_ID FROM SALES; — This option selects distinct (unique) store IDs from the SALES table but does not count or provide the total number of stores.
  4. SELECT COUNT(STORE_ID) FROM SALES GROUP BY STORE_ID; — This option attempts to count the number of occurrences of each store ID by using COUNT(STORE_ID) and grouping by store ID with GROUP BY STORE_ID. However, this results in a count for each unique store ID separately, not the total number of stores in the table.

Answered By

1 Like


Related Questions