Advance SQL Language
Advance SQL Language
Advance SQL Language
SELECT Fname
FROM Employee
WHERE Salary > ALL (SELECT Salary
FROM Employee
where Dno=5)
NESTED QUERIES & TSQL
A subquery can be nested inside the WHERE or HAVING
clause of an outer SELECT, INSERT, UPDATE, or DELETE
statement.
Slide
8-75
CORRELATED NESTED QUERIES
If a condition in the nested query references an attribute of a
relation declared in the outer query =>
Then two queries are said to be correlated
Retrieve the name of each employee who has a dependent with
the same first name as the employee.
Slide
8-76
Nested Correlated query is evaluated once for each tuple in outer query
CORRELATED NESTED QUERIES
A correlated subquery (also called repeating subquery) depends on
the outer query for its values.
• This means that the nested subquery is executed repeatedly, once
for each row that might be selected by the outer query.
Retrieve the name of each employee who has a dependent with
the same first name as the employee.
SELECT E.FNAME, E.LNAME
FROM EMPLOYEE AS E
WHERE E.SSN IN (SELECT ESSN FROM DEPENDENT AS D
WHERE E.SSN = D.ESSN AND FNAME=DEPENDENT_NAME)
Slide
8-77
NESTED QUERIES
A query written with nested SELECT... FROM... WHERE... blocks
and using the = or IN comparison operators can always be
expressed as a single block query.
Slide
8-82
EXISTS FUNCTION
Retrieve the name of each employee who works on all the projects
controlled by department number 4.
EXCEPT
(SELECT PNO
FROM WORKS_ON Slide
WHERE SSN=ESSN) 8-83
)
NESTED CORRELATED QUERIES
You can also use: s > ALL R
s > ANY R
EXISTS R
Find Employee whose salary is greater than the salary
of all employee in department 5
SELECT Fname
FROM Employee
WHERE Salary > ALL (SELECT Salary
FROM Employee
where Dno=5)
Complex Correlated Query
Find Employees (dno and salary) whose salary is greater than the
salaries of all employees in his department
Slide
8-87
CORRELATED NESTED QUERIES
Find the third highest salary
SELECT *
FROM Employee E1
WHERE (N-1) = (
SELECT COUNT(DISTINCT(E2.Salary))
FROM Employee E2
WHERE E2.Salary > E1.Salary)
Slide
8-88
Find names of the
departments such that all their
SELECT Dname employees have salary >30000
FROM Employee, Department
WHERE dno= dnumber Find names of the departments
GROUP BY Dnumber, Dname that have all employees with
HAVING 30000 < min(Salary) salary >30000
SELECT Dname
FROM Department
WHERE 300000 < ALL (SELECT Salary
FROM Employee
Almost equivalent… WHERE dno= dnumber)
SELECT Dname
FROM Department
WHERE Dnumber NOT IN (SELECT Dno
FROM Employee
WHERE Salary<= 300000)
Group By and Having
Count the number of employees whose salaries exceed
$30,000 in each department.
Consider only the departments with more than five
employees.
https://docs.microsoft.com/en-us/sql/relational-databases/performance/subqueries?view=sql-server-2017
Performance of NESTED QUERIES in
TSQL
Slide
8-95
Specifying Updates in SQL
There are three SQL commands to modify the database;
• INSERT,
• DELETE, and
• UPDATE
Example:
UPDATE PROJECT
SET PLOCATION = 'Bellaire', DNUM = 5
WHERE PNUMBER=10
Slide
8-101
UPDATE (cont.)
Example: Give all employees in the 'Research' department a
10% raise in salary.
UPDATE EMPLOYEE
SET SALARY = SALARY *1.1
WHERE DNO IN (SELECT DNUMBER
FROM DEPARTMENT
WHERE DNAME='Research')
Slide
8-102
Views in SQL
A view is a “virtual” table that is derived from other
tables
Allows for limited update operations (since the
table may not physically be stored)
Allows full query operations
Chapter 9-109
SQL Views: An Example
Chapter 9-110
SQL Views: An Example2
Slide
8-111
Query using a Virtual Table
DROP WORKS_ON1;
Chapter 9-112
Efficient View Implementation
Query modification: present the view query in terms of a query
on the underlying base tables
Disadvantage:
Inefficient for views defined via complex queries
Esp. if additional queries are to be applied within a short time period
Chapter 9-113
Efficient View Implementation
Chapter 9-114
View Update
Chapter 9-115
EXAMPLE – Complex View Update
Example:
UPDATE WORKS_ON1
SET PNAME=COMPUTERIZATION
WHERE FNAME=‘JOHN AND
LNAME='SMITH' AND
PNAME=‘PRODUCTX’
Chapter 9-116
EXAMPLE – Complex View Update
UPDATE WORKS_ON1
SET PNAME=COMPUTERIZATION
WHERE FNAME=‘JOHN AND LNAME='SMITH' AND PNAME=‘PRODUCTX’
A) UPDATE PROJECT
SET PNAME=‘COMPUTERIZATION’
WHERE PNAME=‘PRODUCTX’
Chapter 9-117
View Update
UPDATE WORKS_ON1
SET PNAME=COMPUTERIZATION
WHERE FNAME=‘JOHN AND LNAME='SMITH' AND PNAME=‘PRODUCTX’
B)UPDATE WORKS_ON
SET PNO = (SELECT PNUMBER
FROM PROJECT
WHERE PNAME=‘COMPUTERIZATION’)
WHERE ESSN IN (SELECT SSN
FROM EMPLOYEE
WHERE LNAME=‘SMITH’ AND FNAME=‘JOHN’)
AND
Slide
PNO = (SELECT PNUMBER FROM PROJECT 8-118
WHERE PNAME=‘PRODUCTX’)
Un-updatable Views
Views defined using groups and aggregate functions are
not updateable
Chapter 9-119
SQL Triggers
Triggers monitors a database and executes when an event
occurs in the database server.
• like insertion,
• deletion or
• updation of data.
It is a database object which is bound to a table and is
executed automatically.
https://www.codeproject.com/Articles/25600/Triggers-SQL-Server
Chapter 9-123
SQL Triggers: An Example
A trigger to compare an employee’s salary to his/her
supervisor after insert or update operations:
AS
https://docs.microsoft.com/en-us/sql/t-sql/statements/create-trigger-transact-sql?view=sql-server-2017
Chapter 9-126
127
Why Transactions?
Transaction is a process involving database queries
and/or modification.
Example- ATM
ACID TRANSACTIONS
Explicit transactions
BEGIN TRANSACTION
[SQL statements]
COMMIT or ROLLBACK
130
Nonrepeatable Read
Allowing another transaction to write a new value between
multiple reads of one transaction.
A transaction T1 reads a given value from a table.
If another transaction T2 later updates that value and T1 reads that
ACID TRANSACTIONS
Atomicity requirement :
if the transaction fails after step 3 and before step 6,
the system should ensure that :
its updates are not reflected in the database,
Consistency requirement :
the sum of A and B is:
unchanged by the execution of the transaction.
EXAMPLE OF FUND TRANSFER (CONT.)
Transaction to transfer $50 from account A to account B:
1. read(A)
2. A := A – 50
3. write(A)
4. read(B)
5. B := B + 50
6. write(B)
Isolation requirement —
if between steps 3 and 6, another transaction is allowed to access the
partially updated database,
it will see an inconsistent database (the sum A + B will be less than it should
be).
Isolation can be ensured trivially by:
running transactions serially, that is one after the other.
However, executing multiple transactions concurrently has significant
benefits.
EXAMPLE OF FUND TRANSFER (CONT.)
Transaction to transfer $50 from account A to account B:
1. read(A)
2. A := A – 50
3. write(A)
4. read(B)
5. B := B + 50
6. write(B)
Durability requirement :
once the user has been notified that the transaction has completed :
(i.e., the transfer of the $50 has taken place),
the updates to the database by the transaction must persist
despite failures.