Teradata SQL Test – 2

We have an EmployeeSalary Table which holds information about Employee and the Salary which the Employee is getting in respective months. The table is holding the data for past six months. Below is the snapshot of the EMP_SAL table:

SNAPSHOT of EMP_SAL Table
SNAPSHOT of EMP_SAL Table

Now we need to write an SQL Query which can retrieve the name of EMPLOYEES who have received more than INR 120,000 in total as their SAL in last six months. The answer as per the snapshot of the table is RAJ

Can you write the correct query? Leave your SQL Query as comment below. There may be several ways of getting the desired output. You can check the answer on ANSWERS PAGE too. However for better understanding of Teradata SQL , it is suggested that you leave a comment below with your SQL Query and then check our answer. We may get better and more ways of getting the same result.

Hint: Try it via GROUP BY HAVING clause

16 thoughts on “Teradata SQL Test – 2

  1. SELECT emp_name, SUM(salary) FROM employee WHERE SAL_MONTH>MONTH(CURRENT_DATE)-6 GROUP BY emp_name HAVING SUM(SALARY) > 120000;

  2. SEL DISTINCT EMP_NAME FROM EMP_SAL QUALIFY SUM(SAL) OVER (PARTITION BY EMP_ID ORDER BY SAL ASC ROWS BETWEEN 5 PRECEDING AND CURRENT ROW)>120000;

  3. Hi Himanshu

    Just a small correction in your query, use ‘IN’ instead of ‘=’:
    SEL DISTINCT(EMP_NAME) FROM EMP_SAL WHERE EMP_ID IN ( SEL EMP_ID FROM EMP_SAL GROUP BY EMP_ID HAVING SUM(SAL) > 120000)

  4. This can also be done in this way :

    SEL DISTINCT(EMP_NAME) FROM EMP_SAL WHERE EMP_ID = ( SEL EMP_ID FROM EMP_SAL GROUP BY EMP_ID HAVING SUM(SAL) > 120000)

Leave a Reply

Your email address will not be published. Required fields are marked *