ByteBoard Notes Notes

SQL Set 4: cartesian product, equi-join and natural join on EMPLOYEE and DEPARTMENT

Computer Science XII · Experiment 22 of 29 · SQL

12-CSSQL query sets
Expt. No.22
Page No.44

Joins on two tables

Aim

To write SQL queries that combine two tables, EMPLOYEE and DEPARTMENT, using a cartesian product, an equi-join and a natural join.

Concepts used

Queries

USE company;-- Cartesian product: every employee with every departmentSELECT name, dname FROM employee, department;-- Equi-join: match rows on deptno (the column appears twice)SELECT * FROM employee, department WHERE employee.deptno = department.deptno;-- Natural join: the common column appears only onceSELECT * FROM employee NATURAL JOIN department;-- Equi-join with table aliases and an extra conditionSELECT e.name, d.dname, d.location FROM employee e, department dWHERE e.deptno = d.deptno AND d.location = 'Delhi';

A marked line continues the line above it. In your file, write it on the same line.

Tables used

Table: employee
+-------+--------+--------+--------+
| empno | name   | deptno | salary |
+-------+--------+--------+--------+
|     1 | Anil   |     10 |  45000 |
|     2 | Charu  |     20 |  68000 |
|     3 | Farhan |     20 |  75000 |
|     4 | Kavita |     30 |  41000 |
+-------+--------+--------+--------+
Table: department
+--------+-------+-----------+
| deptno | dname | location  |
+--------+-------+-----------+
|     10 | Sales | Delhi     |
|     20 | IT    | Bengaluru |
|     30 | HR    | Delhi     |
+--------+-------+-----------+

Output

mysql> USE company; Database changed
mysql> SELECT name, dname FROM employee, department; +--------+-------+ | name | dname | +--------+-------+ | Anil | HR | | Anil | IT | | Anil | Sales | | Charu | HR | | Charu | IT | | Charu | Sales | | Farhan | HR | | Farhan | IT | | Farhan | Sales | | Kavita | HR | | Kavita | IT | | Kavita | Sales | +--------+-------+ 12 rows in set
mysql> SELECT * FROM employee, department WHERE employee.deptno = department.deptno; +-------+--------+--------+--------+--------+-------+-----------+ | empno | name | deptno | salary | deptno | dname | location | +-------+--------+--------+--------+--------+-------+-----------+ | 1 | Anil | 10 | 45000 | 10 | Sales | Delhi | | 2 | Charu | 20 | 68000 | 20 | IT | Bengaluru | | 3 | Farhan | 20 | 75000 | 20 | IT | Bengaluru | | 4 | Kavita | 30 | 41000 | 30 | HR | Delhi | +-------+--------+--------+--------+--------+-------+-----------+ 4 rows in set
mysql> SELECT * FROM employee NATURAL JOIN department; +--------+-------+--------+--------+-------+-----------+ | deptno | empno | name | salary | dname | location | +--------+-------+--------+--------+-------+-----------+ | 10 | 1 | Anil | 45000 | Sales | Delhi | | 20 | 2 | Charu | 68000 | IT | Bengaluru | | 20 | 3 | Farhan | 75000 | IT | Bengaluru | | 30 | 4 | Kavita | 41000 | HR | Delhi | +--------+-------+--------+--------+-------+-----------+ 4 rows in set
mysql> SELECT e.name, d.dname, d.location FROM employee e, department d -> WHERE e.deptno = d.deptno AND d.location = 'Delhi'; +--------+-------+----------+ | name | dname | location | +--------+-------+----------+ | Anil | Sales | Delhi | | Kavita | HR | Delhi | +--------+-------+----------+ 2 rows in set

✓ Printed by running this query set, not typed by hand.

Result

The cartesian product gave 4 × 3 = 12 rows. The equi-join kept the 4 matching rows and showed deptno twice; the natural join showed it once. The last query found Anil and Kavita, whose departments are in Delhi.

Viva voce: what the examiner may ask

01What is a cartesian product?

Every row of one table combined with every row of the other; its degree is the sum of the degrees and its cardinality the product of the cardinalities.

02What is an equi-join?

A join with an equality condition on the common column, e.g. employee.deptno = department.deptno.

03How does a natural join differ from an equi-join?

A natural join matches columns with the same name automatically and shows the common column only once.

04Why is employee.deptno written with the table name?

Both tables have a deptno column, so the name alone would be ambiguous.

Variations for the lab test

  1. Display the employee names working in the IT department.
  2. Display each department name with the total salary of its employees.
  3. Display employees earning above 50000 with their location.
! Common mistake Forgetting the join condition turns the query into a cartesian product with many meaningless rows.
Back to the 12-CS index