SHAHEED ZULFIKAR ALI BHUTTO INSTITUTE
OF
SCIENCE AND TECHNOLOGY
(SZABIST)
Course: CSCL-2203 Database Systems
Department of Computer Science
Lab Manual
1) Stage J(Journey inside-out the concept)
2) Stage a1(Apply the learned)
3) Stage v(Verify the accuracy)
4) Stage a2(Assess your work)
1
LAB # 11
Statement Purpose:
Demonstrate the effective use joins more effectively to get results from multiple tables.
Activity Outcomes:
Understanding and implementation of Joins.
Instructor Note:
Discussion with students regarding home activities.
58
1) Stage J (Journey)
A sql join clause combines columns from one or more tables in a relational database. It
creates a set that can be saved as a table or used as it is. A join is a means for combining
columns from one (self-table) or more tables by using values common to each. ANSI-
standard SQL specifies five types of join: inner, left outer, right outer, full outer and
cross. As a special case, a table (base table, view, or joined table) can join to itself in
a self-join.
INNER JOIN: Returns all rows when there is at least one match in BOTH
tables
LEFT JOIN: Return all rows from the left table, and the matched rows from
the right table
RIGHT JOIN: Return all rows from the right table, and the matched rows
from the left table
FULL JOIN: Return all rows when there is a match in ONE of the tables
2) Stage a1 (apply)
Lab Activities:
SQL INNER JOIN Keyword
The INNER JOIN keyword selects all rows from both tables as long as there is a match
between the columns in both tables.
SQL INNER JOIN Syntax
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
or:
SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;
Example
SELECT [Link], [Link]
FROM Customers
INNER JOIN Orders
ON [Link]=[Link]
ORDER BY [Link];
59
SQL LEFT JOIN Syntax
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
or:
SELECT column_name(s)
FROM table1
LEFT OUTER JOIN table2
ON table1.column_name=table2.column_name;
Example
SELECT [Link], [Link]
FROM Customers
LEFT JOIN Orders
ON [Link]=[Link]
ORDER BY [Link];
SQL RIGHT JOIN Syntax
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;
or:
SELECT column_name(s)
FROM table1
RIGHT OUTER JOIN table2
ON table1.column_name=table2.column_name;
Example
SELECT [Link], [Link]
FROM Orders
RIGHT JOIN Employees
ON [Link]=[Link]
ORDER BY [Link];
SQL FULL OUTER JOIN Syntax
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;
60
Example
SELECT [Link], [Link]
FROM Customers
FULL OUTER JOIN Orders
ON [Link]=[Link]
ORDER BY [Link];
SQL UNION Syntax
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
Example
SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;
SQL UNION ALL Syntax
SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;
Example
SELECT City, Country FROM Customers
WHERE Country='Germany'
UNION ALL
SELECT City, Country FROM Suppliers
WHERE Country='Germany'
ORDER BY City;
3) Stage v (verify)
Use the following schema to answer the question.
61
1) Write a query to find the addresses (location_id, street_address, city,
state_province, country_name) of all the departments
2) Write a query to find the names (first_name, last name), department ID and name
of all the employees
3) Find the names (first_name, last_name), job, department number, and
department name of the employees who work in London
4) Write a query to find the employee id, name (last_name) along with their
manager_id, manager name (last_name)
5) Find the names (first_name, last_name) and hire date of the employees who were
hired after 'Jones'
6) Write a query to get the department name and number of employees in the
department
7) Find the employee ID, job title, number of days between ending date and starting
date for all jobs in department 90 from job history
8) Write a query to display the department ID, department name and manager first
name.
9) Write a query to display the department name, manager name, and city
10)Write a query to display the job title and average salary of employees
4) Stage a2(assess)
Students will show the solution of their home Activity
62