Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Lab 08 - SQL (DML)-03

Uploaded by

hayamousa78
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab 08 - SQL (DML)-03

Uploaded by

hayamousa78
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

SQL (DML)

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.

• LEFT (OUTER) JOIN:


– Return all records from the left table, and the matched records
from the right table.

• RIGHT (OUTER) JOIN:


– Return all records from the right table, and the matched records
from the left table.

• FULL (OUTER) JOIN:


– Return all records when there is a match in either left or right table
Different Types of SQL JOINs
SQL Self JOIN
• A self JOIN is a regular join, but the table is
joined with itself.

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

– Each SELECT statement within UNION must have


the same number of columns
– The columns must also have similar data types
The SQL UNION / UNION ALL OperatorS

• SELECT column_name(s) FROM table1


UNION
SELECT column_name(s) FROM table2;

• SELECT column_name(s) FROM table1


UNION ALL
SELECT column_name(s) FROM table2;
The INTERSECT Operator
• The intersect combines result-set of two
SELECT statements, which returns only those
rows returned by both queries:

• SELECT product_id FROM


inventories INTERSECT SELECT
product_id FROM order_items;
The MINUS Operator
• The minus combines result-set of two SELECT
statements, which returns only unique rows
returned by the first query but not by the
second:

• SELECT product_id FROM


Order_items MINUS SELECT
product_id FROM inventories;
SQL EXISTS Operator
• The EXISTS operator is used to test for the
existence of any record in a subquery.

• The EXISTS operator returns true if the


subquery returns one or more records.

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

• The ALL operator returns true if all 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);

You might also like