0% found this document useful (0 votes)
54 views

LAB 08 Retrieving Data From Multiple Tables: Theory

This document discusses different types of joins in SQL to combine data from multiple tables. It describes inner joins, left joins, right joins, and full outer joins. It provides the syntax and examples for each type of join. The document also includes tasks that involve performing joins on sample relations to retrieve related data from multiple tables.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

LAB 08 Retrieving Data From Multiple Tables: Theory

This document discusses different types of joins in SQL to combine data from multiple tables. It describes inner joins, left joins, right joins, and full outer joins. It provides the syntax and examples for each type of join. The document also includes tasks that involve performing joins on sample relations to retrieve related data from multiple tables.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Relational Database Management Systems LAB 08

LAB 08 Retrieving Data from Multiple


Tables
OBJECTIVE
To obtain data from more than one table, using different methods.

THEORY
One of the most powerful features of SQL is its capability to gather and manipulate
data from across several tables. Without this feature you would have to store all the
data elements necessary for each application in one table. Without common tables you
would need to store the same data in several tables. Imagine having to redesign,
rebuild, and repopulate your tables and databases every time your user needed a query
with a new piece of information. The JOIN statement of SQL enables you to design
smaller, more specific tables that are easier to maintain than larger tables.

SQL JOIN (INNER JOIN)


An SQL JOIN clause is used to combine rows from two or more tables, based on a
common field between them.
The most common type of join is: SQL INNER JOIN (simple join). An SQL
INNER JOIN return all rows from multiple tables where the join condition is met.
Syntax
SELECT column_name FROM table1 INNER JOIN table2 ON table1.column_name
= table2.column_name;
Examples
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate FROM
Orders INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;
SELECT Customers.CustomerName, Orders.OrderID FROM Customers INNER
JOIN Orders ON Customers.CustomerID=Orders.CustomerID ORDER BY
Customers.CustomerName;
Relational Database Management Systems LAB 08

SQL LEFT JOIN Keyword


The LEFT JOIN keyword returns all rows from the left table (table1), with the
matching rows in the right table (table2). The result is NULL in the right side when
there is no match.

Syntax

SELECT column_name FROM table1 LEFT JOIN table2 ON table1.column_name =


table2.column_name;

Example

SELECT Customers.CustomerName, Orders.OrderID FROM Customers LEFT JOIN


Orders ON Customers.CustomerID=Orders.CustomerID ORDER BY
Customers.CustomerName;

SQL RIGHT JOIN Keyword


The RIGHT JOIN keyword returns all rows from the right table (table2), with the
matching rows in the left table (table1). The result is NULL in the left side when there
is no match.

Syntax

SELECT column_name FROM table1 RIGHT JOIN table2 ON table1.column_name


= table2.column_name;

Example

SELECT Orders.OrderID, Employees.FirstName FROM Orders RIGHT JOIN


Employees ON Orders.EmployeeID=Employees.EmployeeID ORDER BY
Orders.OrderID;
Relational Database Management Systems LAB 08

SQL FULL OUTER JOIN Keyword


The FULL OUTER JOIN keyword returns all rows from the left table (table1) and
from the right table (table2).
The FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT
joins.

Syntax

SELECT column_name(s) FROM table1 FULL OUTER JOIN table2 ON


table1.column_name = table2.column_name;

Example:
SELECT Customers.CustomerName, Orders.OrderID FROM Customers FULL
OUTER JOIN Orders ON Customers.CustomerID=Orders.CustomerID ORDER BY
Customers.CustomerName;

TASKS:
(I) Consider the following relation

EMPLOYEE (EMP_ID, EMP_NAME, EMP_ADDRESS, SKILL, PROJ-ID).


EQUIPMENT (EQP-ID, EMP_ID, EQP-TYPE, PROJECT).

ANS: create database office


create table employee(
eid int identity(1,1) primary key,
ename varchar(20),
eaddress varchar(20),
eskill varchar(20),
eproj_id int );
select * from employee;

create table equipment(


eqid int identity(1,1) primary key,
eid int not null,
eqtype varchar(20),
eqproj varchar(20),
Relational Database Management Systems LAB 08

constraint fk_equipment_eid foreign key (eid) references


employee (eid)
);
select * from equipment;

1. Find the join of relations EMPLOYEE and EQUIPMENT.

ANS: SELECT employee.eid, employee.ename, equipment.eqid FROM


employee LEFT OUTER JOIN equipment ON employee.eid = equipment.eid;

2. Get all employees for projects using EQP-TYPE as “Welding machine”.

ANS: select * from employee where eqtype=’welding’;

3. Get all machines being used at the Mumbai Project?

ANS: select eqtype from equipment where eqproj=’mumbai’;

4. Find all employees of the project using equipment number 110.

ANS: select * from employee where eid= (select eid from


equipment where eqid=110);

(II) Consider the following relation


SALESMAN (SALESMAN_ID, NAME, CITY, COMMISION)
CUSTOMER (CUSTOMER_ID, CUST_NAME, CITY, GRADE,
SALESMAN_ID)
ORDERS (ORD_NO, PURCH_AMT, ORD_DATE, CUSTOMER_ID,
SALESMAN_ID).
1. Write a SQL statement to know which salesman are working for which
customer.
ANS sELECT saleman.eid, saleman.ename, customer.cid FROM
saleman LEFT OUTER JOIN customer ON saleman.eid = customer.cid;
2. Write a SQL statement to make a list in ascending order for the customer who
works either through a salesman or by own.
ANS: select * from customer order by grade asc;
Relational Database Management Systems LAB 08

3. Write a SQL statement to make a list in ascending order for the salesmen who
works either for one or more customer or not yet join under any of the
customer.
ANS: select * from customer where eid=(select eid from saleman order by
eid asc);
4. Write a SQL statement to make a report with customer name, city, order no.
order date, purchase amount for those customers from the existing list who
placed one or more orders or which order(s) have been placed by the customer
who are not in the list

You might also like