You are on page 1of 13

Lab Manual for Introduction to Database Systems

Lab-06
SQL JOINS INNER and SELF
Lab 6: SQL JOINS INNER, and SELF

Table of Contents
1. Introduction 3

2. Activity Time boxing 3

3. Objective of the experiment 3

4.Concept Map 3
4.1 SELECT with JOIN 3
Figure 4 -- Use aliases for table names too 6
4.2 INNER JOIN 7
4.3. SELF JOIN 9

5. Homework before Lab 9


5.1 Problem Solution Modeling 9
Problem 1: 10
Problem 2: 10
5.2 Practices from home 10
Task-1 10
Task-2 10

6. Procedure & Tools 10


6.1. Tools 10
6.2. Setting-up and Setting Up XAMPP (MySQL, Apache) [Expected time = 5mins] 10
6.3.Walkthrough Task [Expected time = 30mins] 68

7. Practice Tasks 68
7.1.Practice Task 1 [Expected time = 40mins] 69
7.2 Outcomes 69

8. Evaluation Task (Unseen) [Expected time = 55mins for two tasks] 69


8.1 Evaluation criteria 69

9. Further Reading 69
9.1.Text Book 70
9.2.Slides 70

10. REFERENCES: 70
10.1. SQL-99 Complete, Really, by Peter Gulutzan & Trudy Pelzer. 70

Department of Computer Science, Page 2


M.A.J.U
Lab 6: SQL JOINS INNER, and SELF

Lab 6: SQL JOINS INNER, OUTER and LEFT

1. Introduction

The purpose of this lab is to familiarize you to SQL Joins. The SQL Joins clause is used to
combine records from two or more tables in a database. A JOIN is a means for combining fields
from two tables by using values common to each. In this lab discuss the SQL joins.

Relevant Lecture Material

a) Revise Lecture No. 11 and 12


b) Text Book: Java: Text Book: Database Systems, A practical approach to design,
implementation and management by Thomas Connolly, Carolyn Begg, Addison
Wesley , Fifth Edition,
1. Read URL:
i. http://www.dofactory.com/sql/join
2. Revise the concept of Joins INNER, OUTER, and LEFT JOIN

2. Activity Time boxing

Table 1: Activity Time Boxing


Task Activity Name Activity time Total Time
No.
6.2 Setting-up and Setting Up 5mins 5mins
XAMPP (MySQL, Apache)
6.3 Walkthrough Tasks 30mins 60mins
7 Practice tasks 20 to 30mins for each task 50mins
8 Evaluation Task 40mins for all assigned 40mins
task

3. Objective of the experiment

• To understand the use LEFT, OUTER, and INNER Joins.


• To be able to optimize queries.
• To understand the concept of joins.

4.Concept Map

4.1 SELECT with JOIN


SELECT command can be used to query and join data from two related tables. For example, to
list the employee name (in employees table) and city (in offices table), we could join the two
table via the two common officeCode columns:
Department of Computer Science, Page 3
M.A.J.U
Lab 6: SQL JOINS INNER, and SELF

For example,
SELECT employee.employeename, employee.jobTitle, employee.officeCode, offices.officeCode,
offices.city FROM employee JOIN offices ON employee.officeCode = offices.officeCode WHERE
salary > 20000;

Figure 1 USING JOINS

We need to use employee.officeCode and offices.officeCode to differentiate the two “codes”

JOIN USING WHERE CLAUSE (not recommended)

SELECT employee.employeename, employee.jobTitle, employee.officeCode, offices.officeCode, offices.city FROM


employee, offices WHERE employee.officeCode = offices.officeCode AND salary > 20000;

Department of Computer Science, Page 4


M.A.J.U
Lab 6: SQL JOINS INNER, and SELF

Figure 2JOIN USING WHERE CLAUSE (not recommended)

ALIASES
In the above query result, two of the columns have the same heading "officeCode". We could
create aliases for headings

-- Use aliases for column officeCode for display


SELECT employee.employeename, employee.jobTitle, employee.officeCode
->AS 'Employee Code', offices.officeCode AS 'offices Code',
-> offices.officeCode, offices.city FROM employee, offices
-> WHERE employee.officeCode = offices.officeCode AND salary > 20000;

Figure 3 -- Use aliases for column officeCode for display

-- Use aliases for table names too


mysql> SELECT e.employeename AS 'Employee Name', e.jobTitle, o.officeCode AS 'office Codes' FROM
employee AS e JOIN offices AS o ON o.officeCode = o.officeCode WHERE o.city = 'Lahore';

Department of Computer Science, Page 5


M.A.J.U
Lab 6: SQL JOINS INNER, and SELF

Figure 4 -- Use aliases for table names too


To make it easier for you to understand each type of join, we will use the  t1  and  t2  tables
with the following structures:
Example,

CREATE TABLE t1 (
    id INT PRIMARY KEY,
    pattern VARCHAR(50) NOT NULL
);
 
CREATE TABLE t2 (
    id VARCHAR(50) PRIMARY KEY,
    pattern VARCHAR(50) NOT NULL
);

Figure 5: join simple example illustration

Both  t1  and  t2  tables have the  pattern  column, which is also the common column
between tables.
The following statements insert data into both  t1  and  t2  tables:

INSERT INTO t1(id, pattern)

Department of Computer Science, Page 6


M.A.J.U
Lab 6: SQL JOINS INNER, and SELF

VALUES(1,'Divot'),
      (2,'Brick'),
      (3,'Grid');
 
INSERT INTO t2(id, pattern)
VALUES('A','Brick'),
      ('B','Grid'),
      ('C','Diamond');

Figure 6 insert values into t1 and t2

4.2 INNER JOIN

In an inner join of two tables, each row of the first table is combined (joined) with every row of
second table. Suppose that there are n1 rows in the first table and n2 rows in the second table,
INNER JOIN produces all combinations of n1×n2 rows - it is known as Cartesian product or
Cross Product
The following statement uses the INNER JOIN clause to join  t1  and  t2  tables:

Figure 7 inner join example


Department of Computer Science, Page 7
M.A.J.U
Lab 6: SQL JOINS INNER, and SELF

The following diagram illustrates INNER JOIN visually

Figure 8 visually illustrated inner join

Figure 9 inner join example

For classicModel example illustrated as bellow:

Figure 10 inner join example w.r.t classicmodel

Department of Computer Science, Page 8


M.A.J.U
Lab 6: SQL JOINS INNER, and SELF

4.3. SELF JOIN


A self JOIN is a regular join, but the table is joined with itself.
SELECT A.employeeName as EmployeeName1, B.employeeName AS EmployeeName2,
A.jobTitle FROM employee A, employee B WHERE A.employeeNumber <>
B.employeeNumber and A.jobTitle=B.jobTitle ORDER BY A.jobTitle;

Figure 11 self join illustration

Show status and comments of all customers which is ordered by same customer.

Figure 12 Self-join using where clause

5. Homework before Lab

You must solve the following problems at home before the lab.

5.1 Problem Solution Modeling

Department of Computer Science, Page 9


M.A.J.U
Lab 6: SQL JOINS INNER, and SELF

After reading the reference material mentioned in the introduction, now you are ready to perform
homework assigned to you.

Problem 1:

What is the purpose and utilities of Joins?

Problem 2:

Find and list down the different types of joins.

5.2 Practices from home

Solve the following subtasks.

Task-1

Difference between inner and self joins?

Task-2

Describe how can use the joins with examples.

6. Procedure & Tools

In this section, you will study how to make and run a customized exception.

6.1. Tools

In this section tools installation and setup is defined.

6.2. Setting-up and Setting Up XAMPP (MySQL, Apache) [Expected time = 5mins]

Refer to Lab 1 sec 6.2

Department of Computer Science, Page 10


M.A.J.U
Lab 6: SQL JOINS INNER and SELF JOIN

6.3.Walkthrough Task [Expected time = 30mins]

This task is designed to guide you towards to understanding of inner and self joins.

Show customer name, addressLine1, city, country from customer who perform order and city is
Nantes.

Figure 13 Join with other table using where clause

Other way is

Figure 14 INNER JOIN

7. Practice Tasks

This section will provide more practice exercises which you need to finish during the lab. You
need to finish the tasks in the required time. When you finish them, put these tasks in the
following folder:
\\dataserver\assignments$\Introduction To Databases\Lab6

Department of Computer Science, Page 68


C.U.S.T.
Lab 6: SQL JOINS INNER and SELF JOIN

7.1. Practice Task 1 [Expected time = 40mins]


Consider the schema given in Lab01 (7.1 Practice Task), write down following SQL queries.
You can use data types of your own choice.

1. Show product name, buy price and text description of those products whose buy price is
greater than 77rs.
2. Show number of order of all customers where number of ordered greater than 2;
3. Show customer name, customer number, customer address, total price of an order, order
date, status of the order, order number from customer, orders, and orderdetails of those
customer who’s Sale between 9000 to 10000.
4. Write a SQL statement to know which employee are working for which customer.
5. Write a SQL statement to make a list in ascending order for the employees who works
either through an employees or by own.
6. Write a SQL statement to make a report with customer name, city, order number, order
date, order amount employee name to find that either any of the existing customer have
placed no firstorder or placed one or more orders by their employee or by own.
7. List all orders with customers information.
8. List all orders with product names, quantities, and prices 

7.2 Outcomes

After completing this lab, student will be able to understand the usage inner and self joins.

8. Evaluation Task (Unseen) [Expected time = 55mins for two tasks]

The lab instructor will give you unseen task depending upon the progress of the class.

8.1 Evaluation criteria

The evaluation criteria for this lab will be based on the completion of the following tasks. Each
task is assigned the marks percentage which will be evaluated by the instructor in the lab whether
the student has finished the complete/partial task(s).

Table 3: Evaluation of the Lab


Sr. No. Task Description Marks
No
1 6 Procedures and Tools 05
2 7 Practice tasks and Testing 15
3 8 Evaluation Tasks (Unseen) 80

9. Further Reading

Department of Computer Science, Page 69


C.U.S.T.
Lab 6: SQL JOINS INNER and SELF JOIN

This section provides the references to further polish your skills.

9.1.Text Book
Database Systems, A practical approach to design, implementation and management by Thomas
Connolly, Carolyn Begg, Addison Wesley , Fifth Edition,

9.2.Slides

The slides and reading material can be accessed from the folder of the class instructor available
at \\dataserver\jinnah$\

10. REFERENCES:

10.1. SQL-99 Complete, Really, by Peter Gulutzan & Trudy Pelzer.


• More examples for the SELECT command:
http://dev.mysql.com/doc/mysql/en/select.html
 MySQL operators:
http://dev.mysql.com/doc/mysql/en/non-
typed_operators.html
• Built-in functions: http://dev.mysql.com/doc/mysql/en/functions.html
• Joining tables:
http://www.melonfire.com/community/columns/trog/article.php?id=148
• Using subqeries:
http://www.melonfire.com/community/columns/trog/article.php?id=204
 Using subqeries:
http://www.melonfire.com/community/columns/trog/article.php?id=204

Department of Computer Science, Page 70


C.U.S.T.

You might also like