DB Lab 12
DB Lab 12
Lab 12
In this lab, we will explore how to write a SELECT statement to access data from more than one
table using equality and non equality join
Cartesian Product
A cartesian product is formed when all rows in the first table are joined to all rows in the second
table , and when join condition is omitted or invalid.
Cartesian products are useful for some tests when you need to generate a large number of rows to
simulate a reasonable amount of time.
Example:
Below example displays the employee name, department name from employee and department
table. It first displays the employee name along with all department. It shows all possible
combinations of both these attributes. Employee table contain 14 rows and department table
contain 4 rows. So in cartesian product, it displays 56 records in total.
Join
Join is used to query data from more than one table. Rows in one table can be joined to rows in
another table according to common values existing in corresponding columns, that is usually
primary and foreign key columns.
To join ntables together, you need a minimum of n-1join. For example, to join three tables, a
minumum of two joins is required.
Types of Join
o Equijoin
o Nonequijoin
o Outer join
o Self join
Equijoin
Equijoins are also called simple joins or inner joins. Tables are joined based on the equality
condition. An equijoin combines two or more tables based on a column that is common to the
tables.
Example:
To determine the employee’s department name, compare the value in the deptnocolumn in the
employee’s table with the deptnovalues in the department table. Values on the deptnocolumn on
both tables must be equal.
The SELECT clause specifies the column names to retrieve, FROM clause specifies the two
tables that the database must access, WHERE clause specifies how the tables are to be joined.
To avoid ambiguity having the same column name, prefix the column name with the table name.
You can also use table alias to simplify queries. It improve the performances by using table
prefixes. Table alias help to keep SQL code smaller, therefore using less memory.
Table alias can be upto 30 characters long, but the shorter they are better. If a table alias is used
for a particular table name in the FROM clause, then that table alias must be subsituted for that
table name throughout the SELECT statement. The table alias is valid only for current SELECT
statement.
Example:
In the example below, both tables are given the alias, and also specify in the query from which
table to retrive the data.
Non Equijoin:
A non equijoin is a join condition containing something other than an equality operator. The
relationship is obtained using an operator other than equals.
The SQL NON EQUI JOIN uses comparison operator instead of the equal sign like >, <, >=,
<= along with conditions.
Example:
In the example below, it displays the grade of the salary using non join condition. The salary is
between any ranges of high and low salary condition.
Outer Join:
The SQL OUTER JOIN returns all rows from both the participating tables which satisfy the join
condition along with rows which do not satisfy the join condition. The SQL OUTER JOIN
operator (+) is used only on one side of the join condition only.
You can use an outer join to also see rows that do not meet the join condition. The outer join
operator is the plus sign (+).
The missing rows can be returned if an outer join operator is used in the join condition. The
operator is a plus sign enclosed in parenthesis (+), and it is placed on the side of the join that is
deficient in information. This operator has the effect of creating one or more null rows, to which
one or more rows from the non deficient table can be joined.
Example:
In the example above, any employee does not belong to operation department. But still in the
result it shows the operation record with no employee name. Operation department number does
not match with any of the record of employee table.
It returns those rows from one table that has no direct match in the other table.
Self Join:
A self join is a join in which a table is joined with itself (which is also called Unary
relationships), especially when the table has a FOREIGN KEY which references its own
PRIMARY KEY. To join a table itself means that each row of the table is combined with
itself and with every other row of the table.
The self join can be viewed as a join of two copies of the same table. The table is not
actually copied, but SQL performs the command as though it were.
Example:
The example joins the EMP table to itself. To simulate the two clauses in the FROM clause, there
are two aliases namely worker and manager. The WHERE clause contains the join that means
“where a worker’s manager number matches the employee number for the manager.
The SQL CROSS JOIN produces a result set which is the number of rows in the first table
multiplied by the number of rows in the second table, if no WHERE clause is used along with
CROSS JOIN.
CROSS JOIN clause produces the cross product of two tables. It is same as the cartesian product
between the two tables.
Example:
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 name have different data types, then an error is returned.
Example:
In the example below, DEPT table is joined with the EMP table by the DEPTNO column which is
the only column of the same name in both tables.
If other columns were present, the join would have used them all.
In the example above, the additional restriction is put using the WHERE clause. It displays the
data of employees belong to department number 10 and 20.
If several columns have the same name but the datatypes do nor match, the NATURAL JOIN
clause can be modified with the USING clause to specify the columns that should be used for
equijoin.
The USING clause can be used to specify only those columns that should be used for equijoin.
Example:
The query below join the EMP and DEPT table using deptno attribute.
Creating Joins with the ON clause:
The join condition for the natural join is basically an equijoin of all columns with the same name.
In order to specify arbitrary condition or specify columns to join, the ON clause is used. It
seperates the join condition from other search conditions. The ON clause makes code easy to
understand.
Example:
Below example join the DEPT and EMP table and using the ON clause, condition is mentioned
where deptno of both tables should be same.
It will display all matching records, and the records which are in the left hand side table those that
are not in right hand side table.
Example:
The query implements the left outer join, it displays the records that are in left hand side but not
in the right hand side table.
It will display all the matching records which are in the right hand side table those that are not in
the left hand side table
Example:
The query implements the right outer join. It also displays the records that are not present in left
hand side table, but present in right hand side table.
Full Outer Join
It will display all the matching and non matching records from both tables
Example:
The example displays the matching and non matching records of DEPT and EMP table.
Inner join
Example:
This query only displays the matched records.
LAB TASKS
1. Write a query to display the last name, department number, and department name for all
employees.
2. Create a unique listing of all jobs that are in department 30. Include the location of department
40 in the output.
3. Write a query to display the employee name, department name and location of all employees
who earn a commission.
4. Display the employee name and department name for all employees who have an ‘a’ in their
name.
5. Write a query to display the employee name, job, department number and department name
for all employees who work in ‘DALLAS’.
6. Display the employee name and employee number along with their manager name and
manager number. Label the columns Employee, Emp #, Manager and Mgr # respectively.
7. Modify the above query to display all employees including King, who has no manager. Order
the results by employee number.
8. Create a query that displays the employee name, department number, and all the employees
who work in the same department as a given employee. Given each employee an appropriate
label.