Documente Academic
Documente Profesional
Documente Cultură
SQL
STRUCTURED QUERY
LANGUAGE
4.1 Introduction
Originally, SQL was called SEQUEL (for Structured English QUery
Language) and implemented at IBM Research as the interface for an
experimental relational database system called SYSTEM R. SQL is now the
language for IBM’s DB2 and SQL/DS commercial relational DBMSs.
Variations of SQL have been implemented by most commercial DBMSs
vendors.
A joint effort under the way by ANSI (the American National Standards
Institute) and ISO (the International Standards Organization) has led to a
standard version of SQL (ANSI 1986), called SQL1. A revised and much
expanded standard called SQL2 (also referred to as SQL-92) has also been
developed. Plans are already underway for SQL3, which will further extend
SQL with object-oriented and other recent database concepts.
It has facilities for defining views on the database, for creating and dropping
indexes on the files that represent relations, and for embedding SQL
statements into a high-level general-purpose programming language such as
C.
(1)
SQL
(2)
SQL Structured Query Language
Example:
CREATE SCHEMA COMPANY AUTHORIZATION KHALIL;
In addition to using the concept of schema, SQL2 uses the concept of catalog - a
named collection of schemas in an SQL environment. A catalog always contains
a special schema called INFORMATION_SCHEMA, which provides
information on all the element descriptors of all the schemas in the catalog to
authorized users.
Figure 4.1 shows sample data definition statements in SQL for the
relational schema for the COMPANY database - described in previous chapter.
Figure 4.1 also shows some data types that are supported by SQL2.
(3)
SQL
We can use SSN_TYPE in place of CHAR(9) in Figure 4.1 for all attributes
referring to SSN (Figure 4.2). A domain can also have an optional default
specification via a DEFAULT clause.
SQL2 allows NULLS for attribute values and a constraint NOT NULL may
be specified if NULL is not permitted for that attribute.
(4)
SQL Structured Query Language
(5)
SQL
continued
CREATE TABLE PROJECT
(PNAME VARCHAR(15) NOT NULL,
PNUMBER INT NOT NULL,
PLOCATION VARCHAR(15)
DNUM INT NOT NULL,
PRIMARY KEY (PNUMBER),
UNIQUE (PNAME)
FOREIGN KEY (DNUM) REFERENCES DEPARTMENT(DNUMBER));
CREATE TABLE WORKS_ON
(ESSN CHAR(9) NOT NULL,
PNO INT NOT NULL,
HOURS DECIMAL(3,1) NOT NULL,
PRIMARY KEY (ESSN, PNO),
FOREIGN KEY (ESSN) REFERENCES EMPLOYEE(SSN),
FOREIGN KEY (PNO) REFERENCES PROJECT(PNUMBER));
CREATE TABLE DEPENDENT
(ESSN CHAR(9) NOT NULL,
DEPENDENT_NAME VARCHAR(15) NOT NULL,
SEX CHAR,
BDATE DATE,
RELATIONSHIP VARCHAR(8)
PRIMARY KEY (ESSN, DEPENDENT_NAME),
FOREIGN KEY (ESSN) REFERENCES EMPLOYEE(SSN));
(6)
SQL Structured Query Language
Figure 4.2 SQL2 definitions for the COMPANY schema using the domain
SSN_TYPE (to be continued).
(7)
SQL
continued
CREATE TABLE PROJECT
(PNAME VARCHAR(15) NOT NULL,
PNUMBER INT NOT NULL,
PLOCATION VARCHAR(15)
DNUM INT NOT NULL,
PRIMARY KEY (PNUMBER),
UNIQUE (PNAME)
FOREIGN KEY (DNUM) REFERENCES DEPARTMENT(DNUMBER));
CREATE TABLE WORKS_ON
(ESSN SSN_TYPE NOT NULL,
PNO INT NOT NULL,
HOURS DECIMAL(3,1) NOT NULL,
PRIMARY KEY (ESSN, PNO),
FOREIGN KEY (ESSN) REFERENCES EMPLOYEE(SSN),
FOREIGN KEY (PNO) REFERENCES PROJECT(PNUMBER));
CREATE TABLE DEPENDENT
(ESSN SSN_TYPE NOT NULL,
DEPENDENT_NAME VARCHAR(15) NOT NULL,
SEX CHAR,
BDATE DATE,
RELATIONSHIP VARCHAR(8)
PRIMARY KEY (ESSN, DEPENDENT_NAME),
FOREIGN KEY (ESSN) REFERENCES EMPLOYEE(SSN));
Figure 4.2 SQL2 definitions for the COMPANY schema using the domain
SSN_TYPE.
(8)
SQL Structured Query Language
(10)
SQL Structured Query Language
For example, to remove the COMPANY database schema and all its tables,
domains, and other elements, the CASCADE option is used as follows:
(12)
SQL Structured Query Language
In the following sections, the basic features and facilities of the SELECT
statement are introduced. Examples of queries in SQL are given using the
sample instance for a simplified schema of the COMPANY database (Figure
4.5).
(13)
SQL
EMPLOYEE
SSN ENAME SEX DNO SUPERSSN SALARY
111 Khalil M 1 NULL 3000
123 Badran M 1 111 1000
444 Dalia F 4 111 1000
525 Saleh M 5 343 2000
343 Samr F 5 686 3000
686 Ahmed M 4 NULL 2500
DEPARTMENT
DNUMBER DNAME MGRSSN
1 Management 111
4 Research 686
5 Production 525
PROJECT
PNUMBER PNAME DNUM
1 ProjectX 5
2 ProjectY 5
4 ProjectZ 4
WORKS_ON
ESSN PNO HOURS
123 1 50
123 2 50
123 4 60
444 2 40
444 4 30
525 1 10
525 2 20
(14)
SQL Structured Query Language
Where
< attribute list > is a list of attribute names whose values are to be retrieved
by the query.
< table list > is a list of the relation names required to process the query.
< condition > is a conditional (Boolean) search expression that identifies the
tuples to be retrieved by the query. The WHERE clause is optional (That is
why it is included between []).
(15)
SQL
Examples:
Query (1):
Retrieve all information for all employees in the company.
SELECT *
FROM EMPLOYEE;
The asterisk (*) stands for all attributes of the specified relation EMPLOYEE.
The result of that query is an exact image of relation EMPLOYEE (Figure 4.6
).
RESULT (1):
SSN ENAME SEX DNO SUPERSSN SALARY
111 Khalil M 1 NULL 3000
123 Badran M 1 111 1000
444 Dalia F 4 111 1000
525 Saleh M 5 343 2000
343 Samr F 5 686 3000
686 Ahmed M 4 NULL 2500
(16)
SQL Structured Query Language
Query (2):
Retrieve all information for all employees who work in department number 5.
SELECT *
FROM EMPLOYEE;
WHERE DNO = 5;
That query produces a relation of the same structure as the base relation
EMPLOYEE but containing only the tuples representing the employees
working for department number 5 (Figure 4.7).
RESULT (2):
SSN ENAME SEX DNO SUPERSSN SALARY
525 Saleh M 5 343 2000
343 Samr F 5 686 3000
(17)
SQL
Query (3):
Retrieve all information for all employees who work in department number 5
and their salary greater than 1000.
SELECT *
FROM EMPLOYEE;
WHERE DNO = 5 AND SALARY > 2000;
This query involves only the EMPLOYEE relation listed in the FROM
clause. The query selects the EMPLOYEE tuples that satisfy the condition of
the WHERE clause. The result of that query is a relation of the same schema
structure as relation EMPLOYEE and it will contain the values of all
attributes for all tuples representing employees satisfying the given condition
(working for the department number 5 and having salary greater than 2000).
The result of Query (3) is shown in Figure 4.8.
RESULT (3)
SSN ENAME SEX DNO SUPERSSN SALARY
343 Samr F 5 686 3000
(18)
SQL Structured Query Language
Query (4):
Retrieve all information for all employees who work in department number 5
and their salary greater than 3000.
SELECT *
FROM EMPLOYEE;
WHERE DNO = 5 AND SALARY > 3000;
This query involves only the EMPLOYEE relation listed in the FROM
clause. The query selects the EMPLOYEE tuples that satisfy the condition of
the WHERE clause. The result of that query is a relation of the same schema
structure as relation EMPLOYEE and it will contain the values of all
attributes for all tuples representing employees satisfying the given condition
(working for the department number 5 and having salary greater than 3000).
The result of Query (3) is an empty relation because there are no tuples in
relation employee that satisfy the specified conditions (Figure 4.9).
RESULT (4):
SSN ENAME SEX DNO SUPERSSN SALARY
(19)
SQL
Query (5):
Retrieve the name and salary of all employees who work in department number
5 and their salary greater than 1000.
This query involves only the EMPLOYEE relation listed in the FROM
clause. The query selects the EMPLOYEE tuples that satisfy the condition of
the WHERE clause, then projects the result on the ENAME, and SALARY
attributes. The result of that query is a relation of two attributes ENAME and
SALARY and it will contain the values of these attributes for all tuples
representing employees satisfying the given condition ( working for the
department number 5 and having salary greater than 1000). Figure 4.10
illustrates the result of query (5).
RESULT (5):
ENAME SALARY
Saleh 2000
Samr 3000
(20)
SQL Structured Query Language
Query (6):
Retrieve the name and salary of all employees who work for the ‘Research’.
This query involves two relations: the EMPLOYEE relation and the
DEPARTMENT relation, where information will be collected from both. The
condition DNO = DNUMBER is a join condition, which corresponds to the
condition under which a JOIN is performed. Figure 4.11 illustrates the result
of query (6).
RESULT (6):
ENAME SALARY
Dalia 1000
Ahmed 2500
(21)
SQL
Query (7):
Retrieve the name of the manager of the department controlling project number
1.
SELECT ENAME
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM = DNUMBER
AND MGRSSN = SSN
AND PNUMBER = 1;
RESULT (7):
ENAME
Saleh
(22)
SQL Structured Query Language
Query (6A):
Ambiguity also arises in the case of queries that refer to the same relation
twice, as in the following example.
(23)
SQL
Query (8):
For each employee, retrieve the employee’s name and the name of his or her
direct supervisor.
RESULT (8):
E.ENAME S.ENAME
Badran Khalil
Dalia Khalil
Saleh Samr
Samr Ahmed
Figure 4.13 Result of Query (8)
(24)
SQL Structured Query Language
The user may want to see duplicate tuples in the result of a query.
For example, Query (9) retrieves the salary of every employee; if several
employees have the same salary, that salary value will appear as many times in
the result of the query, as shown in Figure 4.14.
(25)
SQL
Query (9):
Retrieve the salary of every employee.
SELECT SALARY
FROM EMPLOYEE;
RESULT (9):
SALARY
3000
1000
1000
2000
3000
2500
(26)
SQL Structured Query Language
Query (9A):
Retrieve the distinct salary values in the database.
RESULT (9A):
SALARY
3000
1000
2000
2500
(27)
SQL
The relations resulting from these set operations are sets of tuples; that is,
duplicate tuples are eliminated from the result (unless the operation is followed
by the keyword ALL). Because the set operations apply only to union-
compatible relations, we must be make sure that the two relations on which we
apply the operation have the same attributes and that the attributes appear in the
same order in both relations. Query (10) illustrates the use of UNION. The first
SELECT query retrieves the projects that involves a ‘Khalil’ as manager of the
department that controls the project, and the second retrieves the projects that
involve a ‘Khalil’ as a worker on the project.
(28)
SQL Structured Query Language
Query (10):
Make a list of all projects that involve an employee whose name is ‘Khalil’,
either as a worker or as a manager of the department that controls the project.
(SELECT PNUMBER
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM = DNUMBER
AND MGRSSN = SSN
AND NAME = ‘Khalil’)
UNION
(SELECT PNUMBER
FROM PROJECT, WORKS_ON, EMPLOYEE
WHERE PNUMBER = PNO
AND ESSN = SSN
AND NAME = ‘Khalil’);
(29)
SQL
Query (11):
(30)
SQL Structured Query Language
Query (11A):
The = any (or = some) operator which returns TRUE if the value v is equal to
some value in the set V and is hence equivalent to IN. The keyword ANY
and SOME have the same meaning.
Other operators that can be combined with ANY (or SOME) include <, >=, <,
<=, and <>.
The keyword ALL can also be combined with one of these operators. For
example, the comparison condition (v > ALL V) returns TRUE if the value v is
greater than all values in the set V.
(31)
SQL
Query (10A):
SELECT PNUMBER
FROM PROJECT
WHERE PNUMBER IN
(SELECT PNUMBER
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM = DNUMBER
AND MGRSSN = SSN
AND NAME = ‘Khalil’)
OR
PNUMBER IN
(SELECT PNO
FROM WORKS_ON, EMPLOYEE
WHERE ESSN = SSN
AND NAME = ‘Khalil’)
The first nested query selects the project numbers of projects that have a
‘Khalil’ involved as a manager, while the second selects the project numbers
of projects that have a ‘Khalil’ involved as a worker. In the outer query, we
select a PROJECT tuple if the PNUMBER value of that tuple is in the result
of either nested query.
Query (6) can be rewritten using the concept of nested queries as shown in
Query (6B).
Query (6B):
Retrieve the name and salary of all employees who work for the ‘Research’.
FROM EMPLOYEE
WHERE DNO IN (SELECT DNUMBER
FROM DEPARTMENT
WHERE DNAME = ‘Research’);
Query (12):
Retrieve the names of employees whose salary is greater than the salary of all
the employees in department 5.
SELECT ENAME
FROM EMPLOYEE
WHERE SALARY > ALL (SELECT SALARY
FROM EMPLOYEE
WHERE DNO = 5);
(33)
SQL
Query (13):
Retrieve the name of each employee who works on at least one project.
SELECT ENAME
FROM EMPLOYEE
WHERE EXISTS (SELECT *
FROM WORKS_ON
WHERE ESSN = SSN);
In Query (13), the nested query references the SSN of the EMPLOYEE
relation from the outer query. We can think of Query (13) as follows: for
each EMPLOYEE tuple, evaluate the nested query, which retrieves all
WORKS_ON tuples with the same SSN as the EMPLOYEE tuple; if at least
one tuple exists in the result of the nested query, then select that EMPLOYEE
tuple.
EXISTS(Q) returns TRUE if there is at least one tuple in the result of query
Q, and it returns FALSE otherwise.
RESULT (13):
ENAME
Badran
Dalia
Saleh
Figure 4.16 Result of Query (13)
Query (14):
Retrieve the name of each employee who does not work on any project.
SELECT ENAME
FROM EMPLOYEE
WHERE NOT EXISTS (SELECT *
FROM WORKS_ON
WHERE ESSN = SSN);
In Query (14), the nested query references the SSN of the EMPLOYEE
relation from the outer query. We can think of Query (13) as follows: for
each EMPLOYEE tuple, evaluate the nested query, which retrieves all
(34)
SQL Structured Query Language
WORKS_ON tuples with the same SSN as the EMPLOYEE tuple; if the
result of the nested query is empty, then select that EMPLOYEE tuple.
NOT EXISTS(Q) returns TRUE if there are no tuples in the result of query Q,
and it returns FALSE otherwise.
RESULT (14):
ENAME
Khalil
Samr
Ahmed
(35)
SQL
Query (15):
Retrieve the names of managers who do not work on any project.
SELECT ENAME
FROM EMPLOYEE
WHERE EXISTS (SELECT *
FROM DEPARTMENT
WHERE MGRSSN = SSN)
In Query (15), two nested queries are specified; the first selects all
DEPARTMENT tuples managed by an EMPLOYEE, and the second selects
all WORKS_ON tuples related to the EMPLOYEE. If at least one of the first
and none of the second exist, we select the EMPLOYEE tuple and retrieve its
ENAME.
RESULT (15):
ENAME
Khalil
Ahmed
(36)
SQL Structured Query Language
Query (16):
Retrieve the names of all employee who do not have supervisors.
SELECT ENAME
FROM EMPLOYEE
WHERE SUPERSSN IS NULL;
RESULT (16):
ENAME
Khalil
Ahmed
(37)
SQL
Query (17):
SELECT ENAME
FROM EMPLOYEE
WHERE SUPERSSN IS NOT NULL;
RESULT (17):
ENAME
Badran
Dalia
Saleh
Samr
(38)
SQL Structured Query Language
For example, Query (8A) below shows how Query (8) can be slightly
changed to retrieve the name of each employee and the name of his or her
supervisor, while renaming the resulting attribute names as
EMPLOYEE_NAME and SUPERVISOR_NAME. The new names will appear
as column headers in the query result.
Query (8A):
For each employee, retrieve the employee’s name and the name of his or her
direct supervisor.
RESULT (8A):
EMPLOYEE_NAME SUPERVISOR_NAME
Badran Khalil
Dalia Khalil
Saleh Samr
Samr Ahmed
Figure 4.21 Result of Query (8A)
(39)
SQL
For example, consider Query (6), which retrieves the name and salary of
every employee who works for the ‘Research’ department. For some users, it
may be easier first to specify the join of the EMPLOYEE and DEPARTMENT
relations, and then select the desired tuples and attributes. This can be written in
SQL2 as in Query (6B).
Query (6B):
The FROM clause in Query (6B) contains a single joined table, The attributes
of such a table are all the attributes of the first table, EMPLOYEE, followed
by all the attributes of the second table, DEPARTMENT.
(40)
SQL Structured Query Language
Query (18)
Find the sum of the salaries of all employees, the maximum salary, the
minimum salary, and the average salary.
RESULT (18):
SUM(SALARY) MAX(SALARY) MIN(SALARY) AVE(SALARY)
12500 3000 1000 2083
(41)
SQL
Query (19)
Find the sum of the salaries of all employees of the department number 5, as
well as the maximum salary, the minimum salary, and the average salary.
RESULT (19):
SUM(SALARY) MAX(SALARY) MIN(SALARY) AVE(SALARY)
5000 3000 2000 2500
Query (20)
RESULT (20):
COUNT(*)
6
(42)
SQL Structured Query Language
Query (21)
Retrieve the total number of employees working for department number 5.
RESULT (21):
COUNT(*)
2
The asterisk (*), in the previous queries, refers to the tuples, so COUNT
returns the number of tuples in the result of the query.
We may also use the COUNT function to count values in an attribute rather
than tuples, as in the next example.
Query (22)
Count the number of distinct salary values in the database.
RESULT (22):
COUNT(*)
4
Figure 4.26 Result of Query (22)
Query (23)
Retrieve the names of all employees who work on two or more projects.
SELECT ENAME
FROM EMPLOYEE
WHERE (SELECT COUNT (*)
FROM WORKS_ON
WHERE SSN = ESSN) >= 2;
nested query in the WHERE clause of an outer query. The correlated nested
query counts the number of projects that each employee works on; if this is
greater than or equal to 2, the employee tuple is selected.
RESULT (23):
ENAME
Badran
Dalia
Saleh
Query (24)
For each department, retrieve the department number, the number of employees
in the department, and their average salary.
In Query (24), the EMPLOYEE tuples are divided into groups - each group
having the same value for the grouping attribute DNO. The COUNT and
AVE functions are applied to each such group of tuples.
RESULT (24):
DNO COUNT (*) AVE (SALARY)
1 2 2000
4 2 1750
5 2 2500
(44)
SQL Structured Query Language
(45)
SQL
Query (25)
For each project, retrieve the project number, the project name, and the number
of employees who work on that project.
Query (25) shows how we can use a join condition in conjunction with
GROUP BY. In this case, the grouping and functions are applied after the
joining of the two relations.
RESULT (25):
PNUMBER PNAME COUNT (*)
1 ProjectX 2
2 ProjectY 3
4 ProjectZ 2
Figure 4.29 Result of Query (25)
For each project on which more than two employees work, retrieve the project
number, the project name, and the number of employees who work on that
project.
(46)
SQL Structured Query Language
Notice that, while WHERE selects tuples to which functions are applied, the
HAVING clause selects whole groups.
RESULT (26):
PNUMBER PNAME COUNT (*)
2 ProjectY 3
(47)
SQL
Query (27)
For each project, retrieve the project number, the project name, and the number
of employees from department 5 who work on that project.
RESULT (27):
(48)
SQL Structured Query Language
Query (28)
Find the total number of employees whose salaries exceed 2000 in each
department, but only for departments where more than 2 employees work.
RESULT (28):
(49)
SQL
Query (29)
Retrieve all employees whose name start with ‘S’.
SELECT ENAME
FROM EMPLOYEE
WHERE ENAME LIKE ‘S%’;
Partial strings are specified using two reserved characters: ‘%’ replaces an
arbitrary number of characters, and ‘_’ replaces a single arbitrary character.
RESULT (29):
ENAME
Saleh
Samr
(50)
SQL Structured Query Language
Query (3o)
Retrieve all employees whose name has character ‘m’ in the third position of the
name.
SELECT ENAME
FROM EMPLOYEE
WHERE ENAME LIKE ‘__m%’;
RESULT (30):
ENAME
Samr
Ahmed
(51)
SQL
Query (31)
Show the resulting salaries if every employee working on the ‘ProjectX’ project
is given a 10% raise.
RESULT (31):
ENAME 1.1*SALARY
Badran 1100
Saleh 2200
(52)
SQL Structured Query Language
4.3.14 Ordering
SQL allows the user to order the tuples in the result of a query by the values of
one or more attributes, using the ORDER BY clause. The default order is in
ascending order of values. We can specify the keyword DESC if we want a
descending order of values. The keyword ASC can be used to specify ascending
order explicitly. For example, suppose that we want to retrieve a list of
employees ordered by name of employee.
Query (32)
Get a list of all employees ordered by employee’s name.
SELECT *
FROM EMPLOYEE
ORDER BY ENAME;
RESULT (32):
SSN ENAME SEX DNO SUPERSSN SALARY
686 Ahmed M 4 NULL 2500
123 Badran M 1 111 1000
444 Dalia F 4 111 1000
111 Khalil M 1 NULL 3000
525 Saleh M 5 343 2000
343 Samr F 5 686 3000
(53)
SQL
Query (33)
Get a list of all employees ordered by department number and, within each
department, alphabetically by employee’s name descendingly.
SELECT *
FROM EMPLOYEE
ORDER BY DNO, ENAME DESC;
RESULT (33):
SSN ENAME SEX DNO SUPERSSN SALARY
111 Khalil M 1 NULL 3000
123 Badran M 1 111 1000
444 Dalia F 4 111 1000
686 Ahmed M 4 NULL 2500
343 Samr F 5 686 32000
525 Saleh M 5 343 2000
(54)
SQL Structured Query Language
INSERT,
DELETE, and
UPDATE.
U1:
(55)
SQL
U2:
U3:
DELETE FROM EMPLOYEE
WHERE ENAME = ‘Saleh’;
U4:
DELETE FROM EMPLOYEE
WHERE SSN = ‘123’;
U5:
DELETE FROM EMPLOYEE
WHERE DNO IN (SELECT DNUMBER
FROM DEPARTMENT
WHERE DNAME = ‘Research’);
U6:
DELETE FROM EMPLOYEE;
U7:
(56)
SQL Structured Query Language
UPDATE PROJECT
SET DNUM = 4
WHERE PNUMBER = 1;
U8:
UPDATE EMPLOYEE
SET SALARY = 1.1 * SALARY
WHERE DNO IN (SELECT DNUMBER
FROM DEPARTMENT
WHERE DNAME = ‘Research’);
(57)
SQL
(58)
SQL Structured Query Language
V1:
V2:
We can specify SQL queries on the defined views in the same way we specify
queries involving base tables. For example, to retrieve the names of all
employees who work on ‘ProjectX’, we can utilize the WORKS_ON1 view and
specify the query in QV1:
QV1:
SELECT DNAME
FROM WORKS_ON1
WHERE PNAME = ‘ProjectX’;
QV2:
(59)
SQL
If we do not a view any more, we can use the DROP VIEW command to
dispose of it. For example, to get rid of the two views in V1 and V2, we can use
the SQL statements in V1A and V2A:
(60)
SQL Structured Query Language
KEY POINTS
SQL is a comprehensive relational database language, it has its
own DDL (Data Definition Language) component including
statements for data definition and DML (Data Manipulation
Language) component including statements for query and update
operations.
(61)
SQL
(62)