0% found this document useful (0 votes)
41 views1 page

Corelated Subquery & Joins

This document contains several SQL queries that join and select data from tables in HR and Scott databases to retrieve employee and department data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views1 page

Corelated Subquery & Joins

This document contains several SQL queries that join and select data from tables in HR and Scott databases to retrieve employee and department data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

SELECT * FROM HR.

EMPLOYEES;

SELECT department_id, last_name, salary


FROM HR.employees x
WHERE salary > (SELECT AVG(salary)
FROM HR.employees
WHERE x.department_id = department_id)
ORDER BY department_id;

SELECT EMPLOYEE_ID,SALARY,LAST_NAME,MANAGER_ID FROM HR.EMPLOYEES M


WHERE EXISTS
(SELECT EMPLOYEE_ID FROM HR.EMPLOYEES W
WHERE (W.MANAGER_ID = M.EMPLOYEE_ID));

SELECT EMPLOYEE_ID,SALARY,LAST_NAME,MANAGER_ID FROM HR.EMPLOYEES M


WHERE NOT EXISTS
(SELECT EMPLOYEE_ID FROM HR.EMPLOYEES W
WHERE (W.MANAGER_ID = M.EMPLOYEE_ID));

SELECT department_id, department_name


FROM HR.departments d
WHERE NOT EXISTS (
SELECT 'X'
FROM HR.employees
WHERE department_id
= d.department_id);

---INNER JOIN
SELECT EMPLOYEE_ID,SALARY,LAST_NAME,E.DEPARTMENT_ID,DEPARTMENT_NAME FROM
HR.EMPLOYEES E JOIN HR.DEPARTMENTS D
ON E.DEPARTMENT_ID=D.DEPARTMENT_ID;

SELECT * FROM SCOTT.EMP;

SELECT * FROM SCOTT.DEPT;

SELECT EMPNO,SAl,ENAME,E.DEPTNO,DNAME FROM SCOTT.EMP E JOIN SCOTT.DEPT D


ON E.DEPTNO=D.DEPTNO;

UPDATE SCOTT.EMP SET DEPTNO=NULL WHERE EMPNO=7698;

SELECT EMPNO,SAl,ENAME,D.DEPTNO,DNAME FROM SCOTT.EMP E RIGHT JOIN SCOTT.DEPT D


ON E.DEPTNO=D.DEPTNO;

SELECT EMPNO,SAl,ENAME,D.DEPTNO,DNAME FROM SCOTT.EMP E FULL JOIN SCOTT.DEPT D


ON E.DEPTNO=D.DEPTNO;

SELECT EMPLOYEE_ID,SALARY,LAST_NAME,E.DEPARTMENT_ID,DEPARTMENT_NAME FROM


HR.EMPLOYEES E JOIN HR.DEPARTMENTS D
ON E.DEPARTMENT_ID=D.DEPARTMENT_ID;

You might also like