Computer Science
Table EXAM_RESULTS
STU ID | FNAME | LNAME | EXAM ID | EXAM_SCORE |
---|---|---|---|---|
10 | LAURA | LYNCH | 1 | 90 |
10 | LAURA | LYNCH | 2 | 85 |
11 | GRACE | BROWN | 1 | 78 |
11 | GRACE | BROWN | 2 | 72 |
12 | JAY | JACKSON | 1 | 95 |
12 | JAY | JACKSON | 2 | 92 |
13 | WILLIAM | BISHOP | 1 | 70 |
13 | WILLIAM | BISHOP | 2 | 100 |
14 | CHARLES | PRADA | 2 | 85 |
What SQL statement do we use to find the average exam score for EXAM_ID = 1?
- SELECT AVG(EXAM_SCORE) FROM EXAM_RESULTS;
- SELECT AVG(EXAM_SCORE) FROM EXAM_RESULTS GROUP BY EXAM_ID WHERE EXAM_ID = 1;
- SELECT AVG(EXAM_SCORE) FROM EXAM_RESULTS GROUP BY EXAM_ID HAVING EXAM_ID = 1;
- SELECT COUNT(EXAM_SCORE) FROM EXAM_RESULTS WHERE EXAM_ID = 1;
SQL Joins & Grouping
1 Like
Answer
SELECT AVG(EXAM_SCORE) FROM EXAM_RESULTS GROUP BY EXAM_ID HAVING EXAM_ID = 1;
Output
+-----------------+
| AVG(EXAM_SCORE) |
+-----------------+
| 83.2500 |
+-----------------+
Explanation
SELECT AVG(EXAM_SCORE) FROM EXAM_RESULTS;
— This statement calculates the average exam score across all exam IDs in the EXAM_RESULTS table.SELECT AVG(EXAM_SCORE) FROM EXAM_RESULTS GROUP BY EXAM_ID WHERE EXAM_ID = 1;
— This statement is incorrect because the WHERE clause should come before the GROUP BY clause. Additionally, grouping by EXAM_ID and then trying to filter by EXAM_ID = 1 within the GROUP BY clause will result in an error because grouping should be done before filtering.SELECT AVG(EXAM_SCORE) FROM EXAM_RESULTS GROUP BY EXAM_ID HAVING EXAM_ID = 1;
— This query groups the exam results by EXAM_ID and then calculates the average exam score for each group. The HAVING clause filters the groups and returns only those where the EXAM_ID is equal to 1, giving us the average exam score for the exam with EXAM_ID equal to 1.SELECT COUNT(EXAM_SCORE) FROM EXAM_RESULTS WHERE EXAM_ID = 1;
— This statement calculates the count of exam scores for EXAM_ID = 1, but it doesn't calculate the average score.
Answered By
1 Like
Related Questions
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
Table EXAM_RESULTS
STU ID FNAME LNAME EXAM ID EXAM_SCORE 10 LAURA LYNCH 1 90 10 LAURA LYNCH 2 85 11 GRACE BROWN 1 78 11 GRACE BROWN 2 72 12 JAY JACKSON 1 95 12 JAY JACKSON 2 92 13 WILLIAM BISHOP 1 70 13 WILLIAM BISHOP 2 100 14 CHARLES PRADA 2 85 Which SQL statement do we use to find out how many students took each exam?
- SELECT COUNT(DISTINCT STU_ID) FROM EXAM_RESULTS GROUP BY EXAM_ID;
- SELECT EXAM_ID, MAX(STU_ID) FROM EXAM_RESULTS GROUP BY EXAM_ID;
- SELECT EXAM_ID, COUNT(DISTINCT STU_ID) FROM EXAM_RESULTS GROUP BY EXAM_ID;
- SELECT EXAM_ID, MIN(STU_ID) FROM EXAM_RESULTS GROUP BY EXAM_ID;
Table EXAM_RESULTS
STU ID FNAME LNAME EXAM ID EXAM_SCORE 10 LAURA LYNCH 1 90 10 LAURA LYNCH 2 85 11 GRACE BROWN 1 78 11 GRACE BROWN 2 72 12 JAY JACKSON 1 95 12 JAY JACKSON 2 92 13 WILLIAM BISHOP 1 70 13 WILLIAM BISHOP 2 100 14 CHARLES PRADA 2 85 What SQL statement do we use to print out the record of all students whose last name starts with 'L'?
- SELECT * FROM EXAM_RESULTS WHERE LNAME LIKE 'L%' ;
- SELECT * FROM EXAM_RESULTS WHERE LNAME LIKE 'L';
- SELECT * FROM EXAM_RESULTS WHERE LNAME 'L';
- SELECT * FROM EXAM_RESULTS WHERE LNAME <> 'L';