SQL offers advanced features for complex data retrieval, including nested queries, joined tables, outer joins, aggregate functions, and grouping.
IS NULL
or IS NOT NULL
.SELECT-FROM-WHERE
blocks within the WHERE
clause of another query.IN
: evaluates to TRUE if a value is in a set of values.To list project numbers for projects involving an employee named 'Smith' as a worker or department manager:
(SELECT DISTINCT Pnumber
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE Dnum = Dnumber AND Mgr_ssn = Ssn AND Lname = 'Smith')
UNION
(SELECT DISTINCT Pnumber
FROM PROJECT, WORKS_ON, EMPLOYEE
WHERE Pnumber = Pno AND Essn = Ssn AND Lname = 'Smith');
Alternative using IN
:
SELECT DISTINCT Pnumber
FROM PROJECT
WHERE Pnumber IN
(SELECT Pnumber
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE Dnum = Dnumber AND Mgr_ssn = Ssn AND Lname = 'Smith')
OR Pnumber IN
(SELECT Pno
FROM WORKS_ON, EMPLOYEE
WHERE Essn = Ssn AND Lname = 'Smith');
= ANY
(or = SOME
) operator: Returns TRUE if the value is equal to some value in the set; equivalent to IN
.ANY
(or SOME
): >, >=, EXISTS
function: Checks whether the result of a correlated nested query is empty or not.EXISTS
and NOT EXISTS
are typically used with correlated nested queries.UNIQUE(Q)
function: Returns TRUE if there are no duplicate tuples in the result of query Q.WHERE
clause.AS
followed by desired new name to rename any attribute in the result of a query.FROM
clause.NATURAL JOIN
, various types of OUTER JOIN
.NATURAL JOIN
on relations R and S: Implicit EQUIJOIN
condition for each pair of attributes with the same name from R and S.Assuming tables foot
and tennis
with a common column scode
:
Inner Join:
SELECT * FROM foot INNER JOIN tennis ON foot.scode = tennis.scode
Left Outer Join:
SELECT * FROM foot LEFT OUTER JOIN tennis ON foot.scode = tennis.scode
Right Outer Join:
SELECT * FROM foot RIGHT OUTER JOIN tennis ON foot.scode = tennis.scode
Full Outer Join:
Requires specific database support, may be simulated using UNION
of left and right outer joins.
COUNT
, SUM
, MAX
, MIN
, and AVG
.SELECT
clause or in a HAVING
clause.GROUP BY
clause: Specifies grouping attributes.HAVING
clause: Provides a condition on the summary information.For each department, retrieve the department number, the number of employees, and their average salary:
SELECT Dno, COUNT(*), AVG(Salary)
FROM EMPLOYEE
GROUP BY Dno;
Count the total number of employees in each department, but only for departments where more than five employees work whose salaries exceed $40,000:
SELECT Dno, COUNT(*)
FROM DEPARTMENT, EMPLOYEE
WHERE Dnumber = Dno AND Salary > 40000
GROUP BY Dno
HAVING COUNT(*) > 5;
Retrieve the department number, the number of employees in the department, and the maximum and minimum salary per department, for departments where the employee is not supervised:
SELECT Dno, COUNT(*), MAX(Salary), MIN(Salary)
FROM EMPLOYEE
WHERE Super_SSN is not NULL
GROUP BY Dno
HAVING COUNT(*) >= 1;
CREATE ASSERTION
: Specifies additional constraints outside the scope of built-in relational model constraints.CREATE TRIGGER
: Specifies automatic actions that the database system will perform when certain events and conditions occur.CREATE ASSERTION
: Specifies a query that selects any tuples that violate the desired condition.CHECK
on attributes and domains is insufficient.DROP
command.CREATE ASSERTION RichMGR CHECK (
NOT EXISTS (
SELECT *
FROM dept, emp
WHERE emp.name = dept.mgrname AND emp.salary < 50000
)
);
This assertion guarantees that each manager makes more than $50,000. Any insertion/update violating this condition will be rejected.
A statement that is automatically executed by the system as a side effect of an event.
CREATE TRIGGER
statement: Used to monitor the database.
Typical trigger components:
Format:
CREATE TRIGGER <trigger_name>
{BEFORE|AFTER} {INSERT|DELETE|UPDATE} ON <table_name>
[FOR EACH ROW [WHEN (<trigger_condition>)]]
<trigger_body>
If FOR EACH ROW
is specified, the trigger is row-level; otherwise, statement-level.
CREATE TRIGGER SALARY_VIOLATION
BEFORE INSERT OR UPDATE OF SALARY, SUPERVISOR_SSN ON EMPLOYEE
FOR EACH ROW
WHEN (NEW.SALARY > (SELECT SALARY FROM EMPLOYEE WHERE SSN = NEW.SUPERVISOR_SSN))
INFORM_SUPERVISOR(NEW.Supervisor_ssn, New.Ssn);
Used to drop named schema elements, such as tables, domains, or constraints.
Drop behavior options: CASCADE
and RESTRICT
.
DROP SCHEMA COMPANY CASCADE;
Alter table actions include:
Example:
ALTER TABLE COMPANY.EMPLOYEE ADD COLUMN Job VARCHAR(12);
To drop a column, choose either CASCADE
or RESTRICT
.
Change constraints specified on a table.
Add or drop a named constraint
CREATE VIEW
command: Give table name, list of attribute names, and a query to specify the contents of the view.DROP VIEW
command is used to dispose of a view.WITH CHECK OPTION
must be added at the end of the view definition if a view is to be updated.FROM
clause of an SQL query.CREATE ASSERTION
and CREATE TRIGGER
add constraints and actions.