Site icon IT Tutorial

Oracle SQL Tutorial -5 Getting Data From More Than One Tables

Hi ,

in this article I will tell you about the joins . We use Join to join two tables .The JOIN-related column values must be in two tables .

JOINING TABLES – JOIN

INNER JOIN

SELECT * FROM SH.CUSTOMERS a INNER JOIN SH.SALES b ON a.CUST_ID=b.CUST_ID;

LEFT OUTER JOIN

SELECT a.CUST_FIRST_NAME,a.CUST_LAST_NAME,a.CUST_ID FROM SH.CUSTOMERS a LEFT OUTER JOIN SH.SALES b ON (a.CUST_ID=b.CUST_ID);

RIGHT OUTER JOIN

SELECT a.CUST_FIRST_NAME,a.CUST_LAST_NAME,a.CUST_ID FROM SH.CUSTOMERS a RIGHT OUTER JOIN SH.SALES b ON (a.CUST_ID=b.CUST_ID);

CARTRESSIAN CRASH

SELECT * FROM ADMIN.NEW_TABLE,ADMIN.OGRENCI;

SET OPERATORS

UNION OPERATOR

SELECT PROMO_ID FROM SH.PROMOTIONS UNION SELECT PROMO_ID FROM SH.SALES;

UNION ALL OPERATOR

SELECT PROD_ID,TIME_ID,PROMO_ID FROM SH.COSTS UNION ALL SELECT PROD_ID,TIME_ID,PROMO_ID FROM SH.SALES;

INTERSECT OPERATOR

SELECT CUST_FIRST_NAME,CUST_LAST_NAME,CUST_CREDIT_LIMIT FROM SH.CUSTOMERS WHERE CUST_CREDIT_LIMIT BETWEEN 1500 AND 5000 INTERSECT SELECT CUST_FIRST_NAME,CUST_LAST_NAME,CUST_CREDIT_LIMIT FROM SH.CUSTOMERS WHERE CUST_CREDIT_LIMIT BETWEEN 3000 AND 10000;

MINUS OPERATOR

SELECT CUST_ID FROM SH.CUSTOMERS MINUS SELECT CUST_ID FROM SH.SALES;

SUB Queries

SELECT FIRST_NAME,LAST_NAME,SALARY FROM ADMIN.NEW_TABLE WHERE SALARY > (SELECT SALARY FROM ADMIN.NEW_TABLE WHERE LAST_NAME='Nabi');

SINGLE LINE SUB QUOTES1

SELECT FIRST_NAME,LAST_NAME,JOB_ID,SALARY FROM ADMIN.NEW_TABLE WHERE JOB_ID=(SELECT JOB_ID FROM ADMIN.NEW_TABLE WHERE LAST_NAME='Nabi') AND SALARY < (SELECT SALARY FROM ADMIN.NEW_TABLE WHERE LAST_NAME='Nabi');

SINGLE LINE SUB QUOTES –2

SELECT CUST_FIRST_NAME,CUST_LAST_NAME,CUST_YEAR_OF_BIRTH FROM SH.CUSTOMERS WHERE CUST_YEAR_OF BIRTH = (SELECT MIN(CUST_YEAR_OF_BIRTH) FROM SH.CUSTOMERS);

SINGLE LINE SUB QUOTES –3

MULTI LINE SUB QUOTES

IN OPERATOR

SELECT CUST_ID,CUST_FIRST_NAME,CUST_LAST_NAME,CUST_CREDIT_LIMIT FROM SH.CUSTOMERS WHERE CUST_CREDIT_LIMIT IN (SELECT MIN(CUST_CREDIT_LIMIT) FROM SH.CUSTOMERS GROUP BY CUST_ID);

ANY OPERATOR

SELECT CUST_ID,CUST_LAST_NAME,CUST_CREDIT_LIMIT,COUNTRY_ID FROM SH.CUSTOMERS WHERE CUST_CREDIT_LIMIT < ANY (SELECT CUST_CREDIT_LIMIT FROM SH.CUSTOMERS WHERE COUNTRY_ID=52770);

ALL OPERATOR

Our example for the All operator :

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

See you in my next post.

Exit mobile version