Lab 08 - SQL (DML)-03
Lab 08 - SQL (DML)-03
SQL JOIN
• A JOIN clause is used to combine rows from two
or more tables, based on a related column
between them.
• Ex.
– select departments.department_name,
locations.street_address from
departments join locations on
departments.location_ID =
locations.location_id;
Different Types of SQL JOINs
• (INNER) JOIN:
– Returns records that have matching values in both tables.
• SELECT column_name(s)
FROM table1 T1 join table1 T2
on T1.column_name = T2.column_name;
• SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
SQL Self JOIN
• Ex:
– select E.first_Name || ' '|| E.last_name as
Employee_Name, M.first_name||' '||
M.last_name as Manager_name from employees
E join employees M on
E.manager_id=M.employee_id;
• Ex:
– select E.first_Name || ' '|| E.last_name as
Employee_Name, M.first_name||' '||
M.last_name as Manager_name from employees
E, employees M where
E.manager_id=M.employee_id;
The SQL UNION Operator
• The UNION operator is used to combine the
result-set of two or more SELECT statements.
• SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHE
RE condition);
SQL EXISTS Operator
• EX:
– SELECT SupplierName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products
WHERE SupplierId =
Suppliers.supplierId AND Price < 20);
The SQL ANY and ALL Operators
• The ANY operator returns true if any of the
subquery values meet the condition.
• SELECT column_name(s)
FROM table_name
WHERE column_name operator ANY
(SELECT column_name FROM table_name WHERE cond
ition);
– Note: The operator must be a standard comparison operator (=, <>, !=, >, >=,
<, or <=).
• EX:
– SELECT ProductName
FROM Products
WHERE ProductID
= ANY (SELECT ProductID FROM OrderDetails WHERE
Quantity = 10);