1
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
Data Manipulation Language (DML) to
Write queries
Insert, delete and modify records
6
The basic form of an SQL query is
SELECT select-list
FROM from-list
WHERE condition
The select-list is a list of column names belonging to
tables named in the from-list
Column names may be prefixed by their table name or a
range variable to remove ambiguity
The from-list is a list of table names
A table name may be followed by a range variable
The condition is a Boolean expression
Using the <, <=, =, <>, >=, and > operators, and
AND, NOT, and OR to join expressions
7
Basic SQL queries correspond to using the SELECT, PROJECT, and JOIN
operations of the relational algebra
All subsequent examples use the COMPANY database
Example of a simple query on one relation
Query 0: Retrieve the birthdate and address of the employee whose
name is 'John B. Smith'.
Q0: SELECT BDATE, ADDRESS
FROM EMPLOYEE
WHERE FNAME='John' AND MINIT='B’ AND LNAME='Smith’
Similar to a SELECT-PROJECT pair of relational algebra operations; the
SELECT-clause specifies the projection attributes and the WHERE-clause
specifies the selection condition .However, the result of the query may contain
duplicate tuples
8
Query 1: Retrieve the name and address of all employees
who work for the 'Research' department.
Q1:SELECT FNAME, LNAME, ADDRESS
FROM EMPLOYEE, DEPARTMENT
WHERE DNAME='Research' AND DNUMBER=DNO
Similar to a SELECT-PROJECT-JOIN sequence of relational
algebra operations
(DNAME='Research') is a selection condition (corresponds
to a SELECT operation in relational algebra)
(DNUMBER=DNO) is a join condition (corresponds to a
JOIN operation in relational algebra)
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.
Q2: SELECT PNUMBER, DNUM, LNAME, BDATE, ADDRESS
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM=DNUMBER AND MGRSSN=SSN AND PLOCATION='Stafford'
In Q2, there are two join conditions
The join condition DNUM=DNUMBER relates a project to its
controlling department
The join condition MGRSSN=SSN relates the controlling
department to the employee who manages that department
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.
Q3: SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME
FROM EMPLOYEE E S, EMPLOYEE AS E
WHERE E.SUPERSSN=S.SSN
In Q3, the alternate relation names E and S are called aliases or
tuple variables for the EMPLOYEE relation
We can think of E and S as two different copies of EMPLOYEE; E
represents employees in role of supervisees and S represents
employees in role of supervisors
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.
Q4: SELECT SSN FROM EMPLOYEE
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
Q6A: SELECT SALARY
FROM EMPLOYEE
Q6B: SELECT DISTINCT SALARY
FROM EMPLOYEE
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.
Q7: (SELECT PNAME, PNumber
FROM PROJECT, DEPARTMENT, EMPLOYEE,WORKS_ON
WHERE (DNUM=DNUMBER AND MGRSSN=SSN AND LNAME='Smith‘)
Or
(PNUMBER=PNO AND ESSN=SSN AND LNAME='Smith')
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.
Q9B: SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE EXISTS ( SELECT *
FROM DEPENDENT
WHERE SSN=ESSN)
18
Query 10: Retrieve the names of employees who have no
dependents.
Q10: SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE NOT EXISTS (SELECT *
FROM DEPENDENT
WHERE SSN=ESSN)
In Q10, the correlated nested query retrieves all DEPENDENT tuples
related to an EMPLOYEE tuple. If none exist , the EMPLOYEE tuple is
selected
EXISTS is necessary for the expressive power of SQL
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.
Q11: SELECT DISTINCT ESSN
FROM WORKS_ON
WHERE PNO IN (1, 2, 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.
Q14: SELECT MAX (SALARY), MIN (SALARY), AVG (SALARY)
FROM EMPLOYEE
Query 15: Find the maximum salary, the minimum salary, and the
average salary among employees who work for the 'Research'
department.
Q15: SELECT MAX (SALARY), MIN (SALARY), AVG (SALARY)
FROM EMPLOYEE, DEPARTMENT
WHERE DNO=DNUMBER AND
DNAME='Research’
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
Q17: SELECT COUNT (*)
FROM EMPLOYEE, DEPARTMENT
WHERE DNO=DNUMBER AND DNAME='Research’
In many cases, we want to apply the aggregate functions to
subgroups of tuples in a relation
Each subgroup of tuples consists of the set of tuples that
have the same value for the grouping attribute(s)
SQL has a GROUP BY-clause for specifying the grouping
attributes, which must also appear in the SELECT-clause
24
Query 18: For each department, retrieve the department
number, the number of employees in the department, and
their average salary.
Q18: SELECT DNO, COUNT (*), AVG (SALARY)
FROM EMPLOYEE
GROUP BY DNO
In Q18, the EMPLOYEE tuples are divided into groups--each group having
the same value for the grouping attribute DNO
The COUNT and AVG functions are applied to each such group of tuples
separately
The SELECT-clause includes only the grouping attribute and the functions
to be applied on each group of tuples
A join condition can be used in conjunction with grouping
25
Query 19: For each project, retrieve the project number, project name,
and the number of employees who work on that project.
Q19: SELECT PNumber ,PNAME, COUNT (*)
FROM PROJECT, WORKS_ON
WHERE PNUMBER=PNO
GROUP BY PNAME
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.
Q20: SELECT PNUMBER, PNAME, COUNT (*)
FROM PROJECT, WORKS_ON
WHERE PNUMBER=PNO
GROUP BY PNUMBER
HAVING COUNT (*) > 2
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.
Query 21: Retrieve all employees whose address is in
Houston, Texas. Here, the value of the ADDRESS attribute
must contain the substring 'Houston,TX‘.
Q21: SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE ADDRESS LIKE '%Houston,TX%’
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.
Q22: SELECT FNAME,LNAME
FROM EMPLOYEE
WHERE BDATE LIKE '_ _ 5 _ _ _ _ _ _ _’
The LIKE operator allows us to get around the fact that each
value is considered atomic and indivisible; hence, in SQL,
character string attribute values are not atomic
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.
Q23: SELECT FNAME,LNAME,1.1*SALARY
FROM EMPLOYEE,WORKS_ON,PROJECT
WHERE SSN=ESSN AND PNO=PNUMBER AND
PNAME='ProductX’
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.
Q24: SELECT DNAME, LNAME, FNAME, PNAME
FROM DEPARTMENT, EMPLOYEE, WORKS_ON, PROJECT
WHERE DNUMBER=DNO AND SSN=ESSN AND PNO=PNUMBER
ORDER BY DNAME, LNAME
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:
U1: INSERT INTO EMPLOYEE VALUES (‘Tesema',‘L',‘Tasew', '653298653',
'30-DEC-52‘,‘ Astu, SOE, compt. depart', 'M', 37000,'987654321', 4 )
a). Attributes with NULL values can be left out
Example: Insert a tuple for a new EMPLOYEE for whom we only know the
FNAME, LNAME, and SSN attributes.
U1A: INSERT INTO EMPLOYEE (FNAME, LNAME, SSN)
VALUES (‘Tesema', Tasew', '653298653')
32
b) . Another variation of INSERT allows insertion of multiple
tuples resulting from a query into a relation
Example: Suppose we want to create a temporary table that has
the name, number of employees, and total salaries for each
department. A table DEPTS_INFO is created by U3A, and is
loaded with the summary information retrieved from the
database by the query in U3B.
U1B: CREATE TABLE DEPTS_INFO
(DEPT_NAME VARCHAR(10),
NO_OF_EMPS INTEGER,
TOTAL_SAL INTEGER);
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
Example: Change the location and controlling department
number of project number 10 to 'Bellaire' and 5,
respectively.
U3: UPDATE PROJECT
SET PLOCATION = 'Bellaire', DNUM = 5
WHERE PNUMBER=10
35
Example: Give all employees in the 'Research' department a
10% raise in salary.
U4: UPDATE EMPLOYEE, Department
SET SALARY = SALARY *1.1
WHERE DNO = DNumber AND
DNAME='Research'
36