Using the ANY Operator in Multiple-Row Subqueries :
SELECT empno, ename, job, sal FROM empWHERE sal < ANY (SELECT sal FROM emp WHERE job = 'SALESMAN')AND job <> 'SALESMAN';
Using the ALL Operator in Multiple-Row Subqueries :
SELECT empno, ename, job, sal FROM empWHERE sal < ALL (SELECT sal FROM emp WHERE job = 'SALESMAN')AND job <> 'SALESMAN'; Null Values in a Subquery :
SELECT emp.ename FROM emp empWHERE emp.empno NOT IN (SELECT mgr.mgr FROM emp mgr);
The SQL statement in the slide attempts to display all the emp who do not have any subordinates. Logically, this SQL statement should have returned 12 rows. However, the SQL statement does not return any rows. One of the values returned by the inner query is a null value, and hence the entire query returns no rows
Here we have to use IS NOT NULL in the inner query
In SQL the join of two tables returning only matched rows is called an inner join
A join between two tables that returns the results of the inner join as well as the unmatched rows from the left (or right) tables is called a left (or right) outer join
A join between two tables that returns the results of an inner join as well as the results of a left and right join is a full outer join
LEFT OUTER JOIN :
SELECT e.ename, e.deptno, d.dname FROM emp e LEFT OUTER JOIN dept d ON (e.deptno = d.deptno) ;
RIGHT OUTER JOIN :
SELECT e.ename, e.deptno, d.dname FROM emp e RIGHT OUTER JOIN dept d ON (e.deptno = d.deptno) ;
FULL OUTER JOIN :
SELECT e.ename, e.deptno, d.dname FROM emp e FULL OUTER JOIN dept d ON (e.deptno = d.deptno) ;
If several columns have the same names but the data types do not match, the NATURALJOIN clause can be modified with the USING clause to specify the columns that should be used for an equijoin
Use the USING clause to match only one column when more than one column matches
Do not use a table name or alias in the referenced columns
The NATURAL JOIN and USING clauses are mutually exclusive
The NATURAL JOIN clause is based on all columns in the two tables that have the same name
It selects rows from the two tables that have equal values in all matched columns
If the columns having the same names have different data types, an error is returned
Retrieving Records with Natural Joins :
SELECT empno, dname,loc FROM emp NATURAL JOIN dept ;
Creating Joins with the USING Clause :
If several columns have the same names but the data types do not match, the NATURAL JOIN clause can be modified with the USING clause to specify the columns that should be used for an equijoin
Use the USING clause to match only one column when more than one column matches
Do not use a table name or alias in the referenced columns
The NATURAL JOIN and USING clauses are mutually exclusive
A Cartesian product is formed when: • A join condition is omitted • A join condition is invalid • All rows in the first table are joined to all rows in the second table
To avoid a Cartesian product, always include a valid join condition
Creating Cross Joins :
The CROSS JOIN clause produces the cross-product of two tables
This is also called a Cartesian product between the two tables