Chapter 5 - Stucture Query Language
Chapter 5 - Stucture Query Language
2
3
4
Populated database state for COMPANY
5
Data Definition Language (DDL) to
Create, modify and delete table definitions
Define integrity constraints
Define views
6
The basic form of an SQL query is
SELECT select-list
FROM from-list
WHERE condition
8
Query 1: Retrieve the name and address of all employees
who work for the 'Research' department.
9
Query 2: For every project located in 'Stafford', list the project
number, the controlling department number, and the
department manager's last name, address, and birthdate.
10
In SQL, we can use the same name for two (or more)
attributes as long as the attributes are in different relations
A query that refers to two or more attributes with the same
name must qualify the attribute name with the relation
name by prefixing the relation name to the attribute name
Example:
EMPLOYEE.NAME, DEPARTMENT.NAME
11
Some queries need to refer to the same relation twice
In this case, aliases are given to the relation name
Query 3: For each employee, retrieve the employee's name, and
the name of his or her immediate supervisor.
12
A missing WHERE-clause indicates no condition; hence,
all tuples of the relations in the FROM-clause are
selected
This is equivalent to the condition WHERE TRUE
Query 4: Retrieve the SSN values for all employees.
13
To retrieve all the attribute values of the selected tuples, a *
is used, which stands for all the attributes
Examples:
Q5A : SELECT *
FROM EMPLOYEE
WHERE DNO=5
Q5B: SELECT*
FROM EMPLOYEE, DEPARTMENT
WHERE DNAME='Research' AND
DNO=DNUMBER
14
SQL does not treat a relation as a set; duplicate tuples can
appear
To eliminate duplicate tuples in a query result, the keyword
DISTINCT is used
For example, the result of Q6A may have duplicate SALARY
values whereas Q6B does not have any duplicate values
15
SQL has directly incorporated some set operations
There is a union operation (UNION), and in some
versions of SQL there are set difference (MINUS)
and intersection (INTERSECT) operations
Query 7: Make a list of all project numbers 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.
16
A complete SELECT query, called a nested query , can be specified within
the WHERE-clause of another query, called the outer query
Many of the previous queries can be specified in an alternative form
using nesting
Query 8: Retrieve the name and address of all employees who
work for the 'Research' department.
Q8: SELECT FNAME, LNAME, ADDRESS
FROM EMPLOYEE
WHERE DNO IN ( SELECT DNUMBER
FROM DEPARTMENT
WHERE DNAME='Research' )
17
EXISTS is used to check whether the result of a nested
query is empty (contains no tuples) or not
We can formulate Query 9 in an alternative form that
uses EXISTS as Q9B below
Query 9: Retrieve the name of each employee who has a
dependent with the same first name as the employee.
18
Query 10: Retrieve the names of employees who have no
dependents.
19
It is also possible to use an explicit (enumerated) set of
values in the WHERE-clause rather than a nested query
Query 11: Retrieve the social security numbers of all
employees who work on project number 1, 2, or 3.
20
SQL allows queries that check if a value is NULL (missing or
undefined or not applicable)
SQL uses IS or IS NOT to compare NULLs because it
considers each NULL value distinct from other NULL values,
so equality comparison is not appropriate .
Query 12: Retrieve the names of all employees who do not
have supervisors.
Q12: SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE SUPERSSN IS NULL
21
SQL has a number of operators which compute
aggregate values of columns
COUNT – the number of values in a column
SUM – the sum of the values in a column
AVG – the average of the values in a column
MAX – the maximum value in a column
MIN – the minimum value in a column
22
Query 14: Find the maximum salary, the minimum salary, and
the average salary among all employees.
Query 15: Find the maximum salary, the minimum salary, and the
average salary among employees who work for the 'Research'
department.
23
Queries 16 and 17 : Retrieve the total number of employees
in the company (Q16), and the number of employees in the
'Research' department (Q17).
Q16: SELECT COUNT (*)
FROM EMPLOYEE
25
Query 19: For each project, retrieve the project number, project name,
and the number of employees who work on that project.
26
Sometimes we want to retrieve the values of these functions for
only those groups that satisfy certain conditions
The HAVING-clause is used for specifying a selection condition on
groups (rather than on individual tuples).
Query 20: For each project on which more than two
employees work , retrieve the project number, project name,
and the number of employees who work on that project.
27
The LIKE comparison operator is used to compare partial strings
Two reserved characters are used: '%' (or '*' in some implementations)
replaces an arbitrary number of characters, and '_' replaces a single
arbitrary character.
28
Query 22: Retrieve all employees who were born during the
1950. Here, '5' must be the 8th character of the string
(according to our format for date), so the BDATE value is
'_ _5 _ _ _ _ _ _ _', with each underscore as a place holder for a
single arbitrary character.
29
The standard arithmetic operators '+', '-'. '*', and '/' (for
addition, subtraction, multiplication, and division, respectively)
can be applied to numeric values in an SQL query result.
Query 23: Show the effect of giving all employees who work on
the 'ProductX' project a 10% raise.
30
The ORDER BY clause is used to sort the tuples in a query result
based on the values of some attribute(s)
Query 24: Retrieve a list of employees and the projects each works
in, ordered by the employee's department, and within each
department ordered alphabetically by employee last name.
31
There are three SQL commands to modify the database;
INSERT, DELETE, and UPDATE
i. INSERT
Attribute values should be listed in the same order as the
attributes were specified in the CREATE TABLE command
Example:
U3B: INSERT INTO DEPTS_INFO (DEPT_NAME,
NO_OF_EMPS, TOTAL_SAL)
SELECT DNAME, COUNT (*), SUM (SALARY)
FROM DEPARTMENT, EMPLOYEE
WHERE DNUMBER=DNO
33
Removes tuples from a relation
Includes a WHERE-clause to select the tuples to be deleted
A missing WHERE-clause specifies that all tuples in the relation are to
be deleted; the table then becomes an empty table
Referential integrity should be enforced
Examples:
U2A: DELETE FROM EMPLOYEE
WHERE LNAME=‘Abebe’
U2B: DELETE FROM EMPLOYEE
WHERE SSN='123456789’
U2C: DELETE FROM EMPLOYEE
WHERE DNO IN
(SELECT DNUMBER
FROM DEPARTMENT
WHERE DNAME='Research')
U2D: DELETE FROM EMPLOYEE
34
Used to modify attribute values of one or more selected
tuples
A WHERE-clause selects the tuples to be modified
An additional SET- clause specifies the attributes to be
modified and their new values
Referential integrity should be enforced
35
Example: Give all employees in the 'Research' department a
10% raise in salary.
36