Site icon IT Tutorial

Oracle SQL Tutorial -13 Data Processing With Sub-Questions

Hi ,

In this article I will tell you about multi-column subqueries and related subqueries .Subqueries are used to join data , and the value obtained with the subquery can be used in the parent query .Nested queries consist of a child and a parent query .

MULTIPLE COLUMN SUB-QUERY – DOUBLE WAY

SELECT EMPLOYEE_ID,FIRST_NAME,LAST_NAME,JOB_ID,SALARY FROM ADMIN.NEW_TABLE WHERE (EMPLOYEE_ID,JOB_ID) IN (SELECT EMPLOYEE_ID,JOB_ID FROM ADMIN.NEW_TABLE WHERE FIRST_NAME='Melike');

MULTIPLE COLUMN SUB-QUERY – ONE WAY

SELECT EMPLOYEE_ID,FIRST_NAME,LAST_NAME,JOB_ID,SALARY FROM ADMIN.NEW_TABLE WHERE EMPLOYEE_ID IN (SELECT EMPLOYEE_ID FROM ADMIN.NEW_TABLE WHERE FIRST_NAME='Melike') AND JOB_ID IN (SELECT JOB_ID FROM ADMIN.NEW_TABLE WHERE FIRST_NAME='Melike' )AND FIRST_NAME <> 'Melike';

SCALAR SUB QUESTIONS

Where to use ?

Related Subqueries

Associated subqueries run line by line , and subqueries are written separately for each row in the external query . There are 3 kinds of associated subqueries :

SELECT EMPLOYEE_ID,FIRST_NAME,LAST_NAME,JOB_ID,SALARY FROM ADMIN.NEW_TABLE e WHERE SALARY>(SELECT AVG(SALARY) FROM ADMIN.NEW_TABLE WHERE JOB_ID=e.JOB.ID);

EXISTS

SELECT OGRENCI_NO,AD,SOYAD FROM OGRENCI WHERE EXISTS(SELECT * FROM BOLUMLER WHERE OGRENCI.BOLUM_ID=BOLUMLER.BOLUM_ID);

NOT EXISTS

SELECT OGRENCI_NO,AD,SOYAD FROM OGRENCI WHERE NOT EXISTS(SELECT * FROM BOLUMLER WHERE OGRENCI.BOLUM_ID=BOLUMLER.BOLUM_ID);

RELATED UPDATE

ALTER TABLE OGRENCI ADD(BOLUM_ADI VARCHAR2(30));
UPDATE OGRENCI a SET BOLUM_ADI=(SELECT BOLUM_ADI FROM BOLUMLER b WHERE a.BOLUM_ID=b.BOLUM_ID);
SELECT * FROM OGRENCI;

WITH

WITH QUERY_NAME AS (
SQL QUERY
)
SELECT * FROM QUERY_NAME;

See you in my next post.

Exit mobile version