SQL Language
SQL Language
SQL Language
Not reasonable
in this scenario
CASCADE Slide
8-7
REFERENTIAL INTEGRITY OPTIONS
NULL
SET NULLSlide
8-8
REFERENTIAL INTEGRITY OPTIONS
);
SQL CONSTRAINTS
Assigning Names to Constraints
Slide
8-12
ALTER COMMAND
The definition of table can be changed using ALTER command
ALTER can be used to add an attribute to the relation
Initially, the new attribute will have NULLs in all the tuples of the relation
NOT NULL constraint is not allowed for such an attribute
Example :
The database user have to enter a value for the new attribute
Slide
JOB for each EMPLOYEE tuple. 8-13
SQL QUERIES
Basic form
SELECT <attributes>
FROM <one or more relations>
WHERE <conditions>
Slide
8-17
SIMPLE SQL QUERIES
Retrieve the birthdate and address of the employee whose
name is 'John B. Smith'.
SELECT BDATE, ADDRESS
FROM EMPLOYEE
“selection” and
WHERE FNAME='John' AND MINIT='B’ “projection”
AND LNAME='Smith’
Slide
8-18
ELIMINATING DUPLICATES
SELECT Salary SELECT DISTINCT Salary
FROM Employee FROM Employee
JOIN OPERATION
Retrieve the name of all projects that are offered by
'Research' department.
SELECT PNAME
FROM PROJECT JOIN DEPARTMENT ON DNUM=DNUMBER
WHERE DNAME='Research'
Slide
8-20
JOIN OPERATION
Retrieve the name of all projects that are offered by
'Research' department.
SELECT PNAME
FROM PROJECT , DEPARTMENT join condition
WHERE DNAME='Research’ and DNUM=DNUMBER
Slide
8-21
JOIN OPERATION
For projects located in 'Stafford', list the project no, the controlling
department no, and the department manager's last name, address
and birthdate.
SELECT PNUMBER, DNUM, LNAME, ADDRESS, BDATE
FROM ((PROJECT JOIN DEPARTMENT ON DNUM=DNUMBER)
JOIN EMPLOYEE ON MGRSSN=SSN)
WHERE PLOCATION = 'Stafford'
Slide
8-22
JOIN OPERATION
For projects located in 'Stafford', list the project no, the controlling
department no, and the department manager's last name, address
and birthdate.
SELECT PNUMBER, DNUM, LNAME, ADDRESS, BDATE
FROM ((PROJECT JOIN DEPARTMENT ON DNUM=DNUMBER)
JOIN EMPLOYEE ON MGRSSN=SSN )
WHERE PLOCATION='Stafford'
Slide
8-23
JOIN OPERATION
For projects located in 'Stafford', list the project no, the controlling
department no, and the department manager's last name, address
and birthdate.
SELECT PNUMBER, DNUM, LNAME, ADDRESS, BDATE
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM=DNUMBER AND MGRSSN=SSN AND
PLOCATION='Stafford'
Slide
8-24
UNSPECIFIED WHERE-clause
Retrieve the SSN values for all employees.
• SELECT SSN
• FROM EMPLOYEE
Missing WHERE-clause
• indicates there is no condition and is same as WHERE TRUE
Slide
8-29
ALIASES
For each employee, retrieve the employee's name, and the
name of his or her immediate supervisor.
Using AS keyword to specify aliases
SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME
FROM EMPLOYEE AS E, EMPLOYEE AS S
WHERE E.SUPERSSN=S.SSN
SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME
FROM EMPLOYEE E S
WHERE E.SUPERSSN=S.SSN
Slide
8-30
ARITHMETIC OPERATIONS
Arithmetic operators '+', '-'. '*', and '/’) can be applied to
numeric values in an SQL query result
Give all employees who work on the 'ProductX' project a 10% raise.
SELECT FNAME, LNAME, 1.1*SALARY
FROM (WORKS_ON JOIN PROJECT ON PNO=PNUMBER)
JOIN EMPLOYEE ON ESSN=SSN
WHERE PNAME='ProductX’
Slide
8-31
ORDER BY
The ORDER BY clause sort the tuples in a query result
Retrieve a list of employees and the projects each works in,
ordered by the employee's department number
Slide
8-34
SQL QUERIES
Retrieve the names of all employees who do not have supervisors.
SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE SUPERSSN IS NULL
Note: If a join condition is specified, tuples with NULL values for the join
attributes are not included in the result
Slide
8-35
SUBSTRING COMPARISON
LIKE operator is used to compare partial strings
Slide
8-39
SET OPERATIONS
Make a list of Pname’s for projects that involve an employee
whose last name is 'Smith’
as a worker OR
as a manager of the department that controls the project.
(SELECT PNAME
FROM (PROJECT JOIN DEPARTMENT ON DNUM=DNUMBER ) JOIN
EMPLOYEE ON MGRSSN=SSN
WHERE LNAME='Smith')
UNION
(SELECT PNAME
FROM (PROJECT JOIN WORKS_ON ON PNUMBER=PNO ) JOIN
EMPLOYEE ON ESSN=SSN Slide
WHERE LNAME='Smith') 8-40
SET OPERATIONS
Find SSN of employees who work on project named ProductX
and as well as on project named ProductY
Slide
8-41
AGGREGATE FUNCTIONS
Include COUNT, SUM, MAX, MIN, and AVG
Find the maximum salary, the minimum salary, and the average
salary among all employees.
Slide
8-42
AGGREGATE FUNCTIONS
Retrieve the no. of employees in the ‘Administration' department
Slide
8-43
AGGREGATE EXAMPLE
Count the number of distinct salary values in the
database
Slide
8-45
GROUPING
For each department, retrieve the department number, the
number of employees in the department, and their average
salary.
Slide
8-46
GROUPING
For each project, retrieve the project number, project name,
and the number of employees who work on that project.
Slide
8-47
GROUPING
For each project, retrieve the project number, project name,
and the number of employees who work on that project.
Grouping & Aggregate functions are applied after the joining two relations Slide
8-48
Select clause can only include the grouping attributes and aggregate functions
HAVING-CLAUSE
HAVING-clause specify a selection condition on groups
Slide
8-50
HAVING-CLAUSE
Slide
8-51
General form of Grouping and Aggregation
Evaluation steps: