Lab 11 SQL JOINS INNER SELF OUTER

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Lab Manual for Introduction to Database Systems

Lab-11
SQL JOINS INNER and SELF
Lab 11: SQL JOINS INNER, and SELF

Contents
1. Introduction .............................................................................................................................................................2
1.1 Relevant Lecture Material ...................................................................................................................................2
2. Activity Timeboxing ...............................................................................................................................................2
3. Objective of theexperiment .....................................................................................................................................2
4. ConceptMap ............................................................................................................................................................2
4.1 SELECT with JOIN ............................................................................................................................................2
4.2 INNER JOIN .......................................................................................................................................................7
4.3 SELF JOIN ..........................................................................................................................................................9
5. Homework beforeLab ...........................................................................................................................................10
5.1 Problem Solution Modeling ..............................................................................................................................10
5.2 Practices from home..........................................................................................................................................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] ..................................................................................................11
7. PracticeTasks.........................................................................................................................................................12
7.1 Practice Task 1 [Expected time = 40mins]........................................................................................................12
7.2 Outcomes ..........................................................................................................................................................12
8. EvaluationTask(Unseen) [Expected time = 55mins for two tasks] .......................................................................12
8.1 Evaluation criteria .............................................................................................................................................12
9. FurtherReading......................................................................................................................................................13
9.1 Text Book ..........................................................................................................................................................13
9.2 Slides .................................................................................................................................................................13
10. REFERENCES:.................................................................................................................................................13
10.1 SQL-99 Complete, Really, by Peter Gulutzan & Trudy Pelzer. .......................................................................13

Department of Computer Science, Page 1


C.U.S.T.
Lab 11: SQL JOINS INNER, and SELF

Lab 11: 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.

1.1 Relevant Lecture Material

a) Revise Lecture No. 11 and12


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

2. Activity Timeboxing

Table 1: Activity Time Boxing


Task Activity Name Activity time Total Time
No.
6.2 Setting-up and Setting Up 20mins 20mins
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 theexperiment

• To understand the use LEFT, OUTER, and INNERJoins.


• To be able to optimizequeries.
• To understand the concept ofjoins.

4.ConceptMap

4.1 SELECT withJOIN

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 job title (in jobs table), we could join the two

Department of Computer Science, Page 2


C.U.S.T.
Lab 11: SQL JOINS INNER, and SELF

tables via the common job id:


For example,
SELECT employees.first_name, jobs.job_title FROM employees JOIN jobs ON employees.job_id
=jobs.job_id;

Figure 1 USING JOINS

JOIN USING WHERE CLAUSE (not recommended)

SELECT employees.first_name, jobs.job_title FROM employees, jobs where employees.job_id

= jobs.job_id;)

Figure 2JOIN USING WHERE CLAUSE (not recommended)

Department of Computer Science, Page 3


C.U.S.T.
Lab 11: SQL JOINS INNER, and SELF

ALIASES

In the above query result, two of the columns have the same name "job_id". We could create
aliases for column names

-- Use aliases for column job_title for display


SELECT employees.first_name, jobs.job_title, jobs.job_id AS ‘jobs job_id’, employees.job_id AS ‘employeesjob_id’
FROM employees JOIN jobs ON employees.job_id =jobs.job_id;

Figure 3 -- Use aliases for column officeCode for display

-- Use aliases for table names too


mysql>SELECT e.first_name, j.job_title,j.job_id AS ‘jobs job_id’, e.job_id AS ‘employees job_id’
FROM employees AS e JOIN jobs AS j ON e.job_id =j.job_id;

Department of Computer Science, Page 4


C.U.S.T.
Lab 11: 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 with the following structures:
Example,

CREATE TABLE t1(


idINT PRIMARY KEY,
patternVARCHAR(50)NOT NULL
);

CREATE TABLE t2(


idVARCHAR(50) PRIMARY KEY,
patternVARCHAR(50)NOT NULL
);

Figure 5: join simple example illustration

Department of Computer Science, Page 5


C.U.S.T.
Lab 11: SQL JOINS INNER, and SELF

Both t1 and t2 tables have the pattern column, which is also the common column
between tables.
The following statements insert data intoboth t1 and t2 tables:
INSERT INTO t1(id,pattern)

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

Department of Computer Science, Page 6


C.U.S.T.
Lab 11: SQL JOINS INNER, and SELF

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 tojoin t1 and t2 tables:

Figure 7 inner join example

Department of Computer Science, Page 7


C.U.S.T.
Lab 11: SQL JOINS INNER, and SELF

The following diagram illustrates INNER JOIN visually

Figure 8 visually illustrated inner join

Figure 9 inner join example

For HR database, example is illustrated as below:

Department of Computer Science, Page 8


C.U.S.T.
Lab 11: SQL JOINS INNER, and SELF

Figure 10 inner join example w.r.t oracle database

4.3 SELF JOIN


A self JOIN is a regular join, but the table is joined with itself.
SELECT A.first_name as EmployeeName, B.employeeName AS ManagerName FROM
employee A JOIN employee B ON A.manager_id=B.employee_id;

Figure 11 self join illustration

Department of Computer Science, Page 9


C.U.S.T.
Lab 11: SQL JOINS INNER, and SELF

5. Homework beforeLab

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

5.1 Problem Solution Modeling

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

What are the differences 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


C.U.S.T.
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 names of department that are present in city “Seattle”.

Figure 13 INNER JOIN

Figure 14 INNER JOIN(the other way)

Department of Computer Science, Page 11


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

7. PracticeTasks

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:
\\fs\assignments$\IDBS\Lab10$
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 ownchoice.

1. Show region name along with country names present in it.


2. Show street addresses, postal code, country name and region id for countries whose name ends at
‘a’.
3. Show employees first names, last names and job title of employees whose salary is greater than
20000.
4. Show employees first names, phone number, department name and location id of
employees who were hired in 2007.
5. Write a SQL statement to know which managers are working in which country.
6. Write a SQL statement to make a list of employees’ first name, start date and end date of
employees who are working in department whose department id is 30. Sort the result in
ascending order of start date.
7. Write a SQL statement to show names of departments existing in Canada.
8. Show total number of locations in each region. Sort the result in descending order of number of
locations.

7.2 Outcomes

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

8. EvaluationTask(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


Department of Computer Science, Page 12
C.U.S.T.
Lab 6: SQL JOINS INNER and SELF JOIN

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. FurtherReading

This section provides the references to further polish your skills.

9.1Text 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\\fs\lectures$\

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/s
elect.html MySQLoperators:
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 13


C.U.S.T.

You might also like