Hour 10, "Sorting and Grouping Data"

Quiz Answers

1: Will the following SQL statements work?
  1. SELECT SUM(SALARY), EMP_ID
    FROM EMPLOYEE_PAY_TBL
    GROUP BY 1 and 2;
    										
  2. SELECT EMP_ID, MAX(SALARY)
    FROM EMPLOYEE_PAY_TBL
    GROUP BY SALARY, EMP_ID;
    										
  3. SELECT EMP_ID, COUNT(SALARY)
    FROM EMPLOYEE_PAY_TBL
    ORDER BY EMP_ID
    GROUP BY SALARY;
    										
A1:
  1. No, this statement does not work. The and in the GROUP BY clause does not belong there, and you cannot use an integer in the GROUP BY clause. The correct syntax is

    SELECT SUM(SALARY), EMP_ID
    FROM EMPLOYEE_PAY_TBL
    GROUP BY SALARY, EMP_ID;
    
  2. Yes, this statement will work.

  3. No, this statement will not work. The ORDER BY clause and the GROUP BY clause are not in the correct sequence. Also, the EMP_ID column is required in the GROUP ...

Get Sams Teach Yourself SQL in 24 Hours, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.