IS273: Database Systems Spring 2020 Complex SQL
IS273: Database Systems Spring 2020 Complex SQL
Spring 2020
COMPLEX SQL
SQL for Data Retrieval:
Retrieving Information from Multiple Tables
• Joins
– One way of combining data is by using a join
• Join [also called an Inner Join]
• Left Outer Join
• Right Outer Join
• Full Outer Join
• (INNER) JOIN: Returns records that have matching values
in both tables
• LEFT (OUTER) JOIN: Returns all records from the left
table, and the matched records from the right table
• RIGHT (OUTER) JOIN: Returns all records from the right
table, and the matched records from the left table
• FULL (OUTER) JOIN: Returns all records when there is a
match in either left or right table
SQL for Data Retrieval:
JOIN…ON Example
• The JOIN…ON syntax can be used
in joins.
• It has the advantage of moving the
JOIN syntax into the FROM clause
SELECT EmpName
FROM EMPLOYEE AS E JOIN DEPARTMENT AS D
ON E.DeptID = D.DeptID
WHERE D.DeptName LIKE 'Account%';
SQL for Data Retrieval:
Tables used for SQL Examples
SQL for Data Retrieval:
Join Example
• Inner join
Default type of join in a joined table
Tuple is included in the result only if a matching
tuple exists in the other relation
SELECT * from Students S INNER JOIN Advisors
A ON S.Advisor_ID = A.Advisor_ID
SQL for Data Retrieval:
OUTER JOIN
• The OUTER JOIN syntax can be used to obtain data
that exists in one table without matching data in the
other table.
• LEFT OUTER JOIN
– Every tuple in left table must appear in result
– If no matching tuple
• Padded with NULL values for attributes of right table
• RIGHT OUTER JOIN
– Every tuple in right table must appear in result
– If no matching tuple
• Padded with NULL values for the attributes of left table
• FULL OUTER JOIN
– A full join is also known as a full outer join
– It combines the features of both a Left and Right outer join
SQL for Data Retrieval:
LEFT OUTER JOIN Example
SELECT * from Students S LEFT OUTER JOIN
Advisors A ON S.Advisor_ID=A.Advisor_ID
SQL for Data Retrieval:
FULL OUTER JOIN Example
SELECT * from Students S FULL OUTER JOIN
Advisors A ON S.Advisor_ID=A.Advisor_ID
SQL for Data Retrieval:
Retrieving Information from Multiple Tables
9
Motivation for Subqueries
• Find the name of the professor who teaches “CS
4604.”
SELECT Name
FROM Professors, Teach
WHERE (PID = ProfessorPID) AND (Number = ‘4604’)
AND(DeptName = ‘CS’) ;
SELECT DeptName
FROM Take
WHERE StudentPID IN
( SELECT PID
FROM Students
WHERE (Name = ‘Suri’)
);
Subquery Example
• Show all customers who have
placed an order
• SELECT CUSTOMER_NAME FROM
CUSTOMER_T
WHERE CUSTOMER_ID IN
(SELECT DISTINCT CUSTOMER_ID FROM
ORDER_T);
Subquery is embedded in
parentheses. In this case it
returns a list that will be
used in the WHERE clause
of the outer query
14
Correlated vs Uncorrelated
• The previous subqueries did not depend on anything
outside the subquery. These are called uncorrelated and
can run by its own.
• In case of correlated subquery inner query depends on
outer query. Outer query executed before inner query or
subquery while in case of uncorrelated subquery inner
query executes before outer query.
• Correlated Sub-queries are slower than uncorrelated
subquery and should be avoided in favor of sql joins.
• Common example of correlated subquery is using exits and
not exists keyword while uncorrelated query mostly use IN
or NOT IN keywords.
Correlated Subquery
Example
• Show all orders that include furniture finished
in natural ash
The EXISTS operator will return a
TRUE value if the subquery resulted
in a non-empty set, otherwise it
returns a FALSE
SELECT DISTINCT ORDER_ID FROM ORDER_LINE_T
WHERE EXISTS
(SELECT * FROM PRODUCT_T
WHERE PRODUCT_ID = ORDER_LINE_T.PRODUCT_ID
AND PRODUCT_FINISH = ‘Natural ash’);
17
Another Subquery Example
SELECT Name
FROM Professors
WHERE PID IN
(SELECT ProfessorPID
FROM Teach
WHERE (Number, DeptName) IN
( SELECT Number, DeptName
FROM TakeWHERE StudentPID IN
(SELECT PID
FROM Students
WHERE Name = ’Suri %’)
)
);
UNION Query Example
Triggers (Active database)
• Trigger: A procedure that starts automatically if
specified changes occur to the DBMS
• Three parts:
– Event (activates the trigger)
– Condition (tests whether the triggers should
run) [Optional]
– Action (what happens if the trigger runs)
• Semantics:
– When event occurs, and condition is satisfied,
the action is performed.
Triggers –
Event,Condition,Action
• Events could be :
BEFORE|AFTER INSERT|UPDATE|DELETE
ON <tableName>
e.g.: BEFORE INSERT ON Professor
BEGIN
END;
Example Trigger
CREATE TRIGGER minSalary BEFORE INSERT ON Professor
BEGIN
END;
Example Trigger
CREATE TRIGGER minSalary BEFORE INSERT ON Professor
BEGIN
END;
Example trigger
CREATE TRIGGER minSalary BEFORE INSERT ON Professor
FOR EACH ROW
BEGIN
IF (:new.salary < 60000)
THEN RAISE_APPLICATION_ERROR (-20004,
‘Violation of Minimum Professor Salary’);
END IF;
END;
Details of Trigger Example
• BEFORE INSERT ON Professor
– This trigger is checked before the tuple is inserted
• FOR EACH ROW
– specifies that trigger is performed for each row
inserted
• :new
– refers to the new tuple inserted
• If (:new.salary < 60000)
– then an application error is raised and hence the
row is not inserted; otherwise the row is inserted.
• Use error code: -20004;
– this is in the valid range
Example Trigger Using
Condition
CREATE TRIGGER minSalary BEFORE INSERT ON
Professor
FOR EACH ROW
WHEN (new.salary < 60000)
BEGIN
RAISE_APPLICATION_ERROR (-20004,
‘Violation of Minimum Professor Salary’);
END;
BEGIN
RAISE_APPLICATION_ERROR (-20004,
‘Violation of Minimum Professor Salary’);
END;
Example Trigger
CREATE TRIGGER minSalary
BEFORE UPDATE ON Professor
REFERENCING OLD AS oldTuple NEW as newTuple
FOR EACH ROW
WHEN (newTuple.salary < oldTuple.salary)
BEGIN
RAISE_APPLICATION_ERROR (-20004, ‘Salary
Decreasing !!’);
END;
.
Example:
CREATE VIEW EmployeePhoneView AS
SELECT FirstName, LastName,
Phone AS EmployeePhone
FROM EMPLOYEE;
Using an SQL SELECT Statement