Saturday, October 10, 2009

Few Important Points reg Joins

Qualifying Ambiguous Column Names :
  • Use table prefixes to qualify column names that are in multiple tables
  • Use table prefixes to improve performance
  • Use column aliases to distinguish columns that have identical names but reside in different tables
  • Do not use aliases on columns that are identified in the USING clause and listed elsewhere in the SQL statement
Using Table Aliases :
  • Use table aliases to simplify queries
  • Use table aliases to improve performance
SELECT e.empno, e.ename, d.loc, deptno FROM emp e JOIN dept d USING (deptno) ;

Friday, October 9, 2009

Creating Natural Joins

  • 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
Joining Column Names :



Retrieving Records with the USING Clause :
  • SELECT emp.empno, emp.ename, dept.loc, deptno FROM emp JOIN dept USIN (deptno) ;

Followers