Lab 10 SQL JOINS INNER SELF OUTER
Lab 10 SQL JOINS INNER SELF OUTER
Lab-06
SQL JOINS INNER and SELF
Lab 6: SQL JOINS INNER, and SELF
Table of Contents
1. Introduction 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
7. Practice Tasks 68
7.1.Practice Task 1 [Expected time = 40mins] 69
7.2 Outcomes 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
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.
4.Concept Map
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;
ALIASES
In the above query result, two of the columns have the same heading "officeCode". We could
create aliases for headings
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
);
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:
VALUES(1,'Divot'),
(2,'Brick'),
(3,'Grid');
INSERT INTO t2(id, pattern)
VALUES('A','Brick'),
('B','Grid'),
('C','Diamond');
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:
Show status and comments of all customers which is ordered by same customer.
You must solve the following problems at home before the lab.
After reading the reference material mentioned in the introduction, now you are ready to perform
homework assigned to you.
Problem 1:
Problem 2:
Task-1
Task-2
In this section, you will study how to make and run a customized exception.
6.1. Tools
6.2. Setting-up and Setting Up XAMPP (MySQL, Apache) [Expected time = 5mins]
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.
Other way is
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
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.
The lab instructor will give you unseen task depending upon the progress of the class.
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).
9. Further Reading
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: