Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

1). Is it possible to use where and having clause in the same sql querry.

yes as this example SELECT job_id, SUM(salary) PAYROLL FROM employees WHERE job_id NOT LIKE %REP% GROUP BY job_id HAVING SUM(salary) > 13000 ORDER BY SUM(salary) ====================================== 2) How to select/find the duplicates in a table SELECT * FROM departments WHERE LOCATION_ID IN ( SELECT LOCATION_ID FROM departments GROUP BY location_id HAVING COUNT(*) > 1 ) ================================== 3) Retrieving only the nth row SELECT * FROM ( SELECT ENAME,ROWNUM RN FROM EMP WHERE ROWNUM < 101 ) WHERE RN = 100 ==================================== 4) Left outer join teo formats when they are considered equivalent SELECT e.last_name, e.department_id, d.department_name FROM employees e LEFT OUTER JOIN departments d ON (e.department_id = d.department_id) is equivalent to SELECT e.last_name, e.department_id, d.department_name FROM employees e, departments d where d.department_id (+)= e.department_id ======================================== 5) Analytical functions select employee_id, department_id, avg(salary) over (partition by department_id) from employees ========================================= 6) Rank (as analytical function) select employee_id, salary,department_id,

rank() over (partition by department_id order by salary desc) rank from employees ========================================= 7) Dense_rank (as analytical function) select employee_id, salary,department_id, dense_rank() over (partition by department_id order by salary desc) rank from employees or SELECT employee_id, department_id,salary, MIN(salary) KEEP (DENSE_RANK FIRST ORDER BY salary) OVER (PARTITION BY depar tment_id) "Lowest", MAX(salary) KEEP (DENSE_RANK LAST ORDER BY salary) OVER (PARTITION BY depar tment_id) "Highest" FROM employees ORDER BY department_id, salary; ========================================

You might also like