0% found this document useful (0 votes)
10 views33 pages

DBI202 - Chapter6 - Group5

Uploaded by

MTL Crafter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views33 pages

DBI202 - Chapter6 - Group5

Uploaded by

MTL Crafter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Chapter 6:

THE DATABASE
LANGUAGE SQL
(6.3 Subqueries)
DBI202 – SE1806
Group 5 - Jupiter

♃ ‘’
THE # ♃
MEMBER
OF GROUP.
Toan Thang 01
Minh Huy 02
Quang Huy 03
Tan Loc 04
Quoc Thai 05

CONTENTS.
01 02
Subqueries that Produce Scalar Conditions Involving Relations
Value & Tuples
Toan Thang Minh Huy

03 04
Correlated Subqueries Subqueries in FROM
TABLE OF

Quang Huy Clauses


Quang Huy

05 06
SQL Join Expressions Natural & Outer Joins
Quoc Thai Quoc Thai & Tan Loc


01
Subqueries that Produce
Scalar Value
Toan Thang

#
.00 Subqueries? ♃

# What is Subqueries?!
Subqueries, also known as nested queries or subqueries, are a feature of SQL
used to perform multi-step operations. A subquery is a query nested within
another query. Subqueries can be used in various parts of an SQL statement such
as “SELECT,” “FROM,” “WHERE,” “HAVING,” and “JOIN.” They allow for
more complex and detailed data retrieval and processing.


01. Subqueries that Produce Scalar Value ♃

Definition
A subquery is an SQL statement nested within another SQL statement. When this
subquery returns a single value, it is called a scalar subquery.


01. Subqueries that Produce Scalar Value ♃
WHERE
SELECT
employee_id,
first_name,
last_name
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

HAVING
SELECT
department_id,
AVG(salary) AS avg_salary
FROM employees
GROUP BY department_id
HAVING AVG(salary) > (SELECT AVG(salary) FROM employees);


02
Conditions Involving
Relations & Tuples
Minh Huy

#
02. Conditions Involving Relations ♃

There are some SQL operators that can be applied to a relation R to produce a
boolean result. The relation R must be represented as a subquery. If you want to
apply these operators to a saved table Foe, you can use the subquery (SELECT *
FROM Foe). This trick also applies to the union, intersection, and difference of
relations.


3.2 Rules About Functional Dependencies ♃
Operators:
1. EXISTS R: True if and only if R is not empty.
2. s IN R: True if and only if s equals one of the values in R. Similarly, s NOT IN R is true
if s does not equal any value in R.
3. s > ALL R: True if s is greater than every value in R. Other comparison operators apply
similarly. For example, s <> ALL R is equivalent to s NOT IN R.
4. s > ANY R: True if s is greater than at least one value in R. Other comparison operators
apply similarly. For example, s = ANY R is equivalent to s IN R.


02. Conditions Involving Relations ♃

Negation:
• NOT EXISTS R: True if R is empty.
• NOT s >= ALL R: True if s is not the greatest value in R.
• NOT s > ANY R: True if s is the smallest value in R.


02. Conditions Involving Relations (Example) ♃
Employees
CREATE TABLE Employees (
EmployeeID INT,
Name VARCHAR(50),
Age INT,
DepartmentID INT
);

INSERT INTO Employees (EmployeeID, Name, Age, DepartmentID) VALUES


(1, 'John Doe', 30, 101),
(2, 'Jane Smith', 25, 102), Departments
(3, 'Sam Brown', 35, 103),
CREATE TABLE Departments (
(4, 'Chris Green', 40, 101);
DepartmentID INT,
DepartmentName VARCHAR(50)
);

INSERT INTO Departments (DepartmentID, DepartmentName) VALUES


(101, 'HR'),
(102, 'Finance'),
(103, 'Engineering');


02. Conditions Involving Relations (Example) ♃
1. USING EXISTS
SELECT 'Exists'
FROM dual
WHERE EXISTS (SELECT * FROM Employees WHERE DepartmentID = 101);
2. USING IN
SELECT Name
FROM Employees
WHERE DepartmentID IN (SELECT DepartmentID FROM Departments WHERE
DepartmentID IN (101, 102));
3. USING ALL
SELECT Name
FROM Employees
WHERE Age > ALL (SELECT Age FROM Employees WHERE DepartmentID = 101);


02. Conditions Involving Relations (Example) ♃
4. USING ANY
SELECT Name
FROM Employees
WHERE Age > ANY (SELECT Age FROM Employees WHERE DepartmentID = 102);
5. USING NOT EXISTS
SELECT DepartmentName
FROM Departments
WHERE NOT EXISTS (SELECT * FROM Employees WHERE Employees.DepartmentID =
Departments.DepartmentID);


02. Conditions Involving Tuples ♃

• A tuple in SQL is represented by a list of scalar values between ()


• If a tuple t has the same number of components as a relation R, then we may
compare t and R with IN, ANY, ALL


02. Conditions Involving Tuples (Example) ♃
1. USING EXISTS
SELECT *
FROM employees
WHERE (employee_id, salary) IN ((101, 50000), (102, 60000), (103, 70000));
2. USING IN
SELECT *
FROM employees
WHERE salary > ANY (SELECT salary FROM employees WHERE department_id = 10);
3. USING ALL
SELECT *
FROM employees
WHERE salary > ALL (SELECT salary FROM employees WHERE department_id = 10);


03
Correlated Subqueries
Quang Huy

#
02. Correlated Subqueries ♃
Correlated subqueries depend on the outer query for their values. They are
evaluated once for each row processed by the outer query.
Example 13: Another Example:
Find all those projects have the same location Find the titles that have been used for two or
with projectA movies

_ ▭ X _ ▭ X

/*13*/ SELECT title


SELECT * FROM tblProject FROM Movies Old
WHERE locNum = WHERE year < ANY
(SELECT p.locNum (SELECT year
FROM tblProject p FROM Movies
WHERE p.proName=N'ProjectA') WHERE title =Old.title)
GO



04
Subqueries in FROM
Clauses
Quang Huy

#
02. Correlated Subqueries
Subqueries can be used as relations in the FROM clause of a query. In such cases, a subquery is
enclosed in parentheses and must be given an alias, allowing its results to be referenced like any
other table in the FROM list.
Example 13:
Find the employees of Phòng Phần mềm trong nước
_ ▭ X

SELECT *
FROM tblEmployee e,
(SELECT depNum
FROM tblDepartment
WHERE depName=N'Phòng phần mềm trong nước') d
WHERE e.depNum=d.depNum



05
SQL Join Expressions
Quoc Thai

#
05. SQL Join Expressions ♃
• SQL Join Expressions can be stand as a query itself or can be used as sub-queries
in FROM clauses
• Cross Join in SQL= Cartesian Product
o Syntax: R CROSS JOIN S;
o Meaning: Each tuple of R connects to each tuple of S

Table R Table S The result of R CROSS JOIN S will be:


A B
A B 1 X

1 X 1 Y
2 X
2 Y 2 Y


05. SQL Join Expressions ♃
Theta Join with ON keyword
• Systax: R JOIN S ON R.A=S.A;
• Meaning: Each tuple of R connects to those tuples of S, which satisfy the
condition after ON keyword

Table R Table R The result of R JOIN S ON R.A = S.A will be:

A Name A Age A Name Age

1 John 1 25 1 John 25

2 Jane 2 30 2 Jane 30



06
Natural Joins
& Outer Joins
Quoc Thai & Tan Loc

#
06. Natural Joins & Outer Joins ♃
6.1 Natural Joins
Definition:
A natural join differs from a theta-join in that:
• The join condition: all pairs of attributes from the two relations having a
common name are equated, and there are no other condition
• One of each pair of equated attributes is projected out
• Syntax : Table1 NATURAL JOIN Table2
• Microsoft SQL SERVER DONOT SUPPORT NATURAL JOINS AT ALL


06. Natural Joins & Outer Joins ♃
6.1 Natural Joins (Example)
Table Employees Table Department

Emp_id Name Dept_id Dept_id Dept_name

1 John 10 10 HR

2 Jane 20 20 IT

SELECT * FROM Employees NATURAL JOIN Departments;

Emp_id Name Dept_id Dept_name

1 John 10 HR

2 Jane 20 IT


06. Natural Joins & Outer Joins ♃
6.2 Outer Joins
Definition:
Outer Join in SQL is used to combine rows from two or more
tables based on a related column, while also including rows that do not have
matching results. For easier understanding, Outer Join is mainly divided into
three types:
• Left Outer Join.
• Right Outer Join.
• Full Outer Join.


02. Natural Joins & Outer Joins ♃
2.2 Outer Joins
• Left Outer Join:
Returns all rows from the left table and the matching rows from the right table. If
there is no match, the result from the right table will be NULL.
• Right Outer Join:
Returns all rows from the right table and the matching rows from the left table. If
there is no match, the result from the left table will be NULL.
• Full Outer Join:
Returns all rows when there is a match in either the left or right table. If there are
rows that do not match in either table, the result will include NULL values.


02. Natural Joins & Outer Joins ♃
2.2 Outer Joins (Example)

Ex: Consider two tables, Employees and Departments.


Using Left, Right and Full Outer Joins.

Table Emplyees: Table Departments:

EmployeeID Name DepartmentID DepartmentID DepartmentName


1 Alice 10 10 HR
2 Bob 20 20 Engineering
3 Charlie NULL NULL Marketing


06. Natural Joins & Outer Joins ♃
6.2 Outer Joins (Example)
Using Left Outer Joins.
Syntax:
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
LEFT JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;

Table Emplyees: Table Departments: Result


EmployeeID Name DepartmentID DepartmentID DepartmentName Name DepartmentName
1 Alice 10 10 HR Alice HR
2 Bob 20 20 Engineering Bob Engineering
3 Charlie NULL NULL Marketing Charlie NULL


06. Natural Joins & Outer Joins ♃
6.2 Outer Joins (Example)
Using Right Outer Joins.
Syntax:
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
RIGHT JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;

Table Emplyees: Table Departments: Result


EmployeeID Name DepartmentID DepartmentID DepartmentName Name DepartmentName
1 Alice 10 10 HR Alice HR
2 Bob 20 20 Engineering Bob Engineering
3 Charlie NULL NULL Marketing NULL Marketing


06. Natural Joins & Outer Joins ♃
6.2 Outer Joins (Example)
Using Full Outer Joins.
Syntax:
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
FULL OUTER JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;

Table Emplyees: Table Departments: Result


EmployeeID Name DepartmentID DepartmentID DepartmentName Name DepartmentName
1 Alice 10 10 HR Alice HR
2 Bob 20 20 Engineering Bob Engineering
3 Charlie NULL NULL Marketing Charlie NULL
NULL Marketing


Q&A.
Thanks for your attention!

#
001

You might also like