Computer Science
Table SALES
Column Name |
---|
STORE_ID |
SALES_DATE |
SALES_AMOUNT |
Which SQL statement lets you list all stores whose total sales amount is over 5000 ?
- SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES GROUP BY STORE_ID HAVING SUM(SALES_AMOUNT) > 5000;
- SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES GROUP BY STORE_ID HAVING SALES_AMOUNT > 5000;
- SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES
WHERE SUM(SALES_AMOUNT) > 5000 GROUP BY STORE_ID; - SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES
WHERE SALES_AMOUNT > 5000 GROUP BY STORE_ID;
SQL Joins & Grouping
3 Likes
Answer
SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES
GROUP BY STORE_ID HAVING SUM(SALES_AMOUNT) > 5000;
Explanation
SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES GROUP BY STORE_ID HAVING SUM(SALES_AMOUNT) > 5000;
— This statement selects the store ID and calculates the total sales amount for each store using the SUM() aggregate function. The GROUP BY STORE_ID clause ensures that the results are grouped by store ID. The HAVING SUM(SALES_AMOUNT) > 5000 condition then filters the grouped data, selecting only those stores whose total sales amount is over 5000.SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES GROUP BY STORE_ID HAVING SALES_AMOUNT > 5000;
— This option is incorrect because the HAVING clause cannot directly reference SALES_AMOUNT without an aggregate function like SUM() since SUM(SALES_AMOUNT) is used in the SELECT statement.SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES WHERE SUM(SALES_AMOUNT) > 5000 GROUP BY STORE_ID;
— This option is incorrect because aggregate functions like SUM(SALES_AMOUNT) cannot be used directly in the WHERE clause. The WHERE clause is used for filtering individual rows before grouping.SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES WHERE SALES_AMOUNT > 5000 GROUP BY STORE_ID;
— This option is incorrect because it tries to filter individual sales amounts (SALES_AMOUNT) directly without using the SUM() aggregate function to calculate the total sales amount for each store. The GROUP BY STORE_ID clause is used for grouping after the filtering, which is not the correct approach for filtering based on the total sales amount.
Answered By
1 Like
Related Questions
Table BOOK_INFORMATION
Column Name BOOK_ID BOOK_TITLE PRICE Which SQL statement allows you to find the highest price from the table BOOK_INFORMATION?
- SELECT BOOK_ID, BOOK_TITLE, MAX(PRICE) FROM BOOK_INFORMATION;
- SELECT MAX(PRICE) FROM BOOK_INFORMATION;
- SELECT MAXIMUM(PRICE) FROM BOOK_INFORMATION;
- SELECT PRICE FROM BOOK_INFORMATION ORDER BY PRICE DESC;
Table SALES
Column Name STORE_ID SALES_DATE SALES_AMOUNT Which SQL statement lets you find the sales amount for each store?
- SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES;
- SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES ORDER BY STORE_ID;
- SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES GROUP BY STORE_ID;
- SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES HAVING UNIQUE STORE_ID;
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?
- SELECT COUNT(STORE_ID) FROM SALES;
- SELECT COUNT(DISTINCT STORE_ID) FROM SALES;
- SELECT DISTINCT STORE_ID FROM SALES;
- SELECT COUNT(STORE_ID) FROM SALES GROUP BY STORE_ID;
Table SALES
Column Name STORE_ID SALES_DATE SALES_AMOUNT Which SQL statement allows you to find the total sales amount for Store ID 25 and the total sales amount for Store ID 45?
- SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES
WHERE STORE_ID IN (25, 45) GROUP BY STORE_ID; - SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES
GROUP BY STORE_ID HAVING STORE_ID IN (25, 45); - SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES WHERE STORE_ID IN (25, 45);
- SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES
WHERE STORE_ID = 25 AND STORE_ID = 45 GROUP BY STORE_ID;
- SELECT STORE_ID, SUM(SALES_AMOUNT) FROM SALES