RDBMS unit 4
RDBMS unit 4
4) Several operators can be used to join tables, such as =, <, >, <>, <=, >=, !=, BETWEEN,
LIKE, and NOT; they can all be used to join tables.
Join Styles :
Joins can be done using any of the following two styles :
i) ANSI – style:
2) NATURAL JOINS
3) NON-EQUIJOINS
4) OUTER JOINS
a) Left Outer Join
5) SELF JOINS
THETA STYLE :
SQL> select * from Student, Sdetails where Student.Rno = Sdetails.RNO ;
ANSI STYLE :
SQL>select * from Student inner join Sdetails on Student.Rno = Sdetails.RNO ;
For E.g. :
STUDENT Table :
3 RDBMS 18 19 37 18.5
1 RDBMS 20 19 39 19.5
4 RDBMS 19 19 38 19
2 RDBMS 20 19 39 19.5
5 RDBMS 20 17 37 18.5
OUTPUT :
2) NATURAL JOIN
This is something similar to Inner join. Natural join and inner join gives the same
output. The only difference is when “SELECT * FROM” is given in query Natural join
display the common column (here RNO) of both the tables (STUDENT and SDETAILS)
only once. While inner join display the common column two times in the output.
STUDENT Table :
SDETAILS Table :
3 RDBMS 18 19 37 18.5
1 RDBMS 20 19 39 19.5
2 RDBMS 19 19 38 19
OUTPUT :
4) OUTER JOIN
This join returns all the rows from one table and only those rows from second
table which meets the condition. They can be further Classified as :
SDETAILS Table :
3 RDBMS 18 19 37 18.5
1 RDBMS 20 19 39 19.5
2 RDBMS 19 19 38 19
For E.g. :
OUTPUT :
OUTPUT :
CROSS JOIN
The CROSS JOIN specifies that all rows from first table join with all of the rows of
second table. If there are "x" rows in table1 and "y" rows in table2 then the cross join
result set have x*y rows. It normally happens when no matching join columns are
specified.
In simple words you can say that if two tables in a join query have no join
condition, then the Oracle returns their Cartesian product.
Syntax :
Both the above syntax are same and used for Cartesian product. They provide
similar result after execution.
Customer Table
1 ABCD PATIL
2 RAMAN KAPOOR
3 AKSHAY SINGH
Supplier Table
OUTPUT :
Student Table
S_ID S_NAME
4 Praveen
5 Bisa
6 Suraj
1 Jack
2 Rithvik
3 Jaspreet
Marks Table
1004 1 23 fail
1008 6 95 pass
1012 2 97 pass
1016 7 67 pass
1020 3 100 pass
1025 8 73 pass
1030 4 88 pass
1035 9 13 fail
1040 5 16 fail
1050 10 53 pass
Details Table
OUTPUT :
Types of Subqueries
EMPLOYEES Table :
FNAME SALARY DEPT_ID AGE
101 100
102 100
103 103
Multiple Row Sub Query :
SQL > SELECT FNAME, DEPT_ID FROM employees WHERE DEPT_ID IN (SELECT DEPT_ID
FROM DEPARTMENTS WHERE LOCATION = 100) ;
Output :
FNAME DEPT_ID
XYZ 101
ABC 101
WER 102
JKL 102
DEF 102