Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

DBMS Unit-II B (SQL)

Uploaded by

naman.049259
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

DBMS Unit-II B (SQL)

Uploaded by

naman.049259
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Unit-II B

Structured Query Language (SQL)


SQL Statements
SQL statements supported by Oracle comply with industry standards. Oracle
Corporation ensures future compliance with evolving standards by actively involving
key personnel in SQL standards committees. The industry-accepted committees are
ANSI and International Standards Organization (ISO). Both ANSI and ISO have
accepted SQL as the standard language for relational databases.

Statement Description
SELECT Retrieves data from the database, enters new rows,
INSERT changes existing rows, and removes unwanted rows
UPDATE from tables in the database, respectively. Collectively
DELETE known as data manipulation language (DML)
MERGE
CREATE Sets up, changes, and removes data structures from
ALTER tables. Collectively known as data definition language
DROP (DDL)
RENAME
TRUNCATE
COMMENT
GRANT Provides or removes access rights to both the Oracle
REVOKE database and the structures
within it

COMMIT Manages the changes made by DML statements.


ROLLBACK Changes to the data can be grouped together into
SAVEPOINT logical transactions

 A table is uniquely identified by its name and consists of rows that contain the
stored information, each row containing exactly one tuple (or record).
 A table can have one or more columns. A column is made up of a column name
and a data type, and it describes an attribute of the tuples.
 The structure of a table, also called relation schema, thus is defined by its
attributes.
 The type of information to be stored in a table is defined by the data types of the
attributes at table creation time.
SQL uses the terms table, row, and column for relation, tuple, and attribute,
respectively. In this document we will use the terms interchangeably.
A table can have up to 254 columns which may have different or same data types and
sets of values (domains), respectively. Possible domains are alphanumeric data
(strings), numbers and date formats. Oracle offers the following basic data types:
 char(n): Fixed-length character data (string), n characters long. The maximum
size for n is 255 bytes (2000 in Oracle8). Note that a string of type char is always
padded on right with blanks to full length of n. (can be memory consuming).
Example: char(40)
 varchar2(n): Variable-length character string. The maximum size for n is 2000
(4000 in Oracle8). Only the bytes used for a string require storage. Example:
varchar2(80)

Page 1 of 17
Unit-II B

 number(o, d): Numeric data type for integers and reals. o = overall number of
digits, d= number of digits to the right of the decimal point. Maximum values: o
=38, d= −84 to +127. Examples: number(8), number(5,2)
Note that, e.g., number(5,2) cannot contain anything larger than 999.99
without resulting in an error. Data types derived from number are int[eger],
dec[imal], smallint and real.
 date: Date data type for storing date and time. The default format for a date is:
DD-MMM-YY. Examples: 13-OCT-94 , 07-JAN-98
 long: Character data up to a length of 2GB. Only one long column is allowed per
table.
Note: In Oracle-SQL there is no data type boolean. It can, however, be simulated by
using either char(1) or number(1).
As long as no constraint restricts the possible values of an attribute, it may have the
special value null (for unknown). This value is different from the number 0, and it is
also different from the empty string ’’. Further properties of tables are:
 the order in which tuples appear in a table is not relevant (unless a query requires
an explicit sorting).
 a table has no duplicate tuples (depending on the query, however, duplicate tuples
can appear in the query result).
A database schema is a set of relation schemas. The extension of a database schema at
database run-time is called a database instance or database, for short.
Data Definition in SQL
Creating Tables
The SQL command for creating an empty table has the following form:
create table <table> (
<column 1> <data type> [not null] [unique] [<column constraint>],
.........
<column n> <data type> [not null] [unique] [<column constraint>],
[<table constraint(s)>]
);
For each column, a name and a data type must be specified and the column name
must be unique within the table definition. Column definitions are separated by colons.
There is no difference between names in lower case letters and names in upper case
letters. In fact, the only place where upper and lower case letters matter are strings
comparisons.
A not null constraint is directly specified after the data type of the column and the
constraint requires defined attribute values for that column, different from null.
The keyword unique specifies that no two tuples can have the same attribute value for
this column.
Example: The create table statement for our EMP table has the form
create table EMP (
EMPNO number(4) not null,
ENAME varchar2(30) not null,
JOB varchar2(10),
MGR number(4),
HIREDATE date,
SAL number(7,2),
DEPTNO number(2)
);
Remark: Except for the columns EMPNO and ENAME null values are allowed.

Page 2 of 17
Unit-II B

Constraints
The definition of a table may include the specification of integrity constraints.
Basically two types of constraints are provided: column constraints are associated
with a single column whereas table constraints are typically associated with more than
one column. However, any column constraint can also be formulated as a table
constraint.
Here, we consider only very simple constraints. More complex constraints will be
discussed later on. The specification of a (simple) constraint has the following form:
[constraint <name>] primary key unique not null
A constraint can be named. It is advisable to name a constraint in order to get more
meaningful information when this constraint is violated due to, e.g., an insertion of a
tuple that violates the constraint. If no name is specified for the constraint, Oracle
automatically generates a name of the pattern SYS C<number>.
A primary key constraint enables a unique identification of each tuple in a table.
Based on a primary key, the database system ensures that no duplicates appear in a
table. For example, for our EMP table, the specification
create table EMP (
EMPNO number(4) constraint pk_emp primary key,
. . . );
defines the attribute EMPNO as the primary key for the table. Each value for the
attribute EMPNO thus must appear only once in the table EMP.
A table, of course, may only have one primary key. Note that primary key has
unique values and null values are also not allowed.
Example:
We want to create a table called PROJECT to store information about projects.
Furthermore, we have the following conditions:
- a project is identified by its project number,
- the name of a project must be unique,
- the manager and the budget must be defined.
Table definition:
create table PROJECT (
PNO number(3) constraint prj_pk primary key,
PNAME varchar2(60) unique,
PMGR number(4) not null,
PERSONS number(5),
BUDGET number(8,2) not null,
PSTART date,
PEND date);
A unique constraint can include more than one attribute. In this case the pattern
unique(<columni>, . . . , <column j>) is used. If it is required, for example, that no
two projects have the same start and end date, we have to add the table constraint
constraint no same dates unique(PEND, PSTART)

This constraint has to be defined in the create table command after both columns
PEND and PSTART have been defined. A primary key constraint that includes more
than only one column can be specified in an similar way.

Instead of a not null constraint it is sometimes useful to specify a default value for an
attribute if no value is given, e.g., when a tuple is inserted. For this, we use the default
clause.

Page 3 of 17
Unit-II B

Example:
If no start date is given when inserting a tuple into the table PROJECT, the project
start date should be set to January 1st, 1995:
PSTART date default (01-JAN-95)
Note: Unlike integrity constraints, it is not possible to specify a name for a default.
Integrity Constraints
We have discussed three types of integrity constraints: not null constraints, primary
keys, and unique constraints. In this section we introduce two more types of
constraints that can be specified within the create table statement: check constraints
(to restrict possible attribute values), and foreign key constraints (to specify
interdependencies between relations).
Check Constraints
Often columns in a table must have values that are within a certain range or that
satisfy certain conditions. They can be specified as column constraints or table
constraints. The syntax for a check constraint is
[constraint <name>] check(<condition>)
If a check constraint is specified as a column constraint, the condition can only refer
that column.
Example: The name of an employee must consist of upper case letters only; the
minimum salary of an employee is 500; department numbers must range between 10
and 100:
create table EMP
(...,
ENAME varchar2(30) constraint check_name
check(ENAME = upper(ENAME) ),
SAL number(5,2) constraint check_sal check(SAL >= 500),
DEPTNO number(3) constraint check_deptno
check(DEPTNO between 10 and 100) );
If a check constraint is specified as a table constraint, <condition> can refer to all
columns of the table. Note that only simple conditions are allowed.
Furthermore, the functions sysdate and user cannot be used in a condition. In principle,
thus only simple attribute comparisons and logical connectives such as and, or, and
not are allowed. A check condition, however, can include a not null constraint:
SAL number(5,2) constraint check_sal check(SAL is not null and SAL >= 500),
Example: At least two persons must participate in a project, and the project’s start
date must be before the project’s end date:
create table PROJECT
(...,
PERSONS number(5) constraint check_pers check (PERSONS > 2),
...,
constraint dates_ok check(PEND > PSTART) );
In this table definition, check_pers is a column constraint and dates_ok is a table
constraint.
The database system automatically checks the specified conditions each time a
database modification is performed on this relation.
For example, the insertion
insert into EMP
values (7999, SCOTT, CLERK, 7698, 31-OCT-94, 450, 10);
causes a constraint violation
ORA-02290: check constraint (CHECK SAL) violated and the insertion is rejected.

Page 4 of 17
Unit-II B

Foreign Key Constraints


A foreign key constraint (or referential integrity constraint) can be specified as a
column constraint or as a table constraint:
[constraint <name>] [foreign key (<column(s)>)]
references <table>[(<column(s)>)]
[on delete cascade]
This constraint specifies a column or a list of columns as a foreign key of the
referencing table. The referencing table is called the child-table, and the referenced
table is called the parent-table.
In other words, one cannot define a referential integrity constraint that refers to a table
R before that table R has been created.
The clause foreign key has to be used in addition to the clause references if the
foreign key includes more than one column. In this case, the constraint has to be
specified as a table constraint. The clause references defines which columns of the
parent-table are referenced. If only the name of the parent-table is given, the list of
attributes that build the primary key of that table is assumed.
Example: Each employee in the table EMP must work in a department that is
contained in the table DEPT:
create table EMP
( EMPNO number(4) constraint pk_emp primary key,
...,
DEPTNO number(3) constraint fk_deptno references DEPT(DEPTNO) );
The column DEPTNO of the table EMP (child-table) builds the foreign key and
references the primary key of the table DEPT (parent-table). Since in the table
definition above the referential integrity constraint includes only one column, the
clause foreign key is not used. It is very important that a foreign key must refer to the
complete primary key of a parent-table, not only a subset of the attributes that build
the primary key !
In order to satisfy a foreign key constraint, each row in the child-table has to satisfy
one of the following two conditions:
1. the attribute value (list of attribute values) of the foreign key must appear as
a primary key value in the parent-table, or
2. the attribute value of the foreign key is null (in case of a composite foreign
key, at least one attribute value of the foreign key is null )
According to the above definition for the table EMP, an employee must not
necessarily work in a department, i.e., for the attribute DEPTNO the value null is
admissible.
Example: Each project manager must be an employee:
create table PROJECT
( PNO number(3) constraint prj_pk primary key,
PMGR number(4) not null constraint fk_pmgr references EMP,
. . . );
Because only the name of the parent-table is given (DEPT), the primary key of this
relation is assumed. A foreign key constraint may also refer to the same table, i.e.,
parent-table and child-table are identical.
Example: Each manager must be an employee:
create table EMP
( EMPNO number(4) constraint emp_pk primary key,
...
MGR number(4) not null

Page 5 of 17
Unit-II B

constraint fk_mgr references EMP,


...
);
More about Column- and Table Constraints
If a constraint is defined within the create table command or added using the alter
table command, the constraint is automatically enabled. A constraint can be disabled
using the command
alter table <table> disable
constraint <name> primary key unique[<column(s)>]
[cascade];
To disable a primary key, one must disable all foreign key constraints that depend on
this primary key. The clause cascade automatically disables foreign key constraints
that depend on the (disabled) primary key.
Example: Disable the primary key of the table DEPT and disable the foreign key
constraint in the table EMP:
alter table DEPT
disable primary key cascade;
In order to enable an integrity constraint, the clause enable is used instead of disable.
A constraint can only be enabled successfully if no tuple in the table violates the
constraint. Otherwise an error message is displayed.
Note that for enabling/disabling an integrity constraint it is important that you have
named the constraints.
In order to identify those tuples that violate an integrity constraint whose activation
failed, one can use the clause exceptions into EXCEPTIONS with the alter table
statement. EXCEPTIONS is a table that stores information about violating tuples.
Each tuple in this table is identified by the attribute ROWID. Every tuple in a
database has a pseudo-column ROWID that is used to identify tuples. Besides the
rowid, the name of the table, the table owner as well as the name of the violated
constraint are stored.
Example: Assume we want to add an integrity constraint to our table EMP which
requires that each manager must earn more than 4000:
alter table EMP add constraint manager sal
check(JOB != MANAGER or SAL >= 4000)
exceptions into EXCEPTIONS;
If the table EMP already contains tuples that violate the constraint, the constraint
cannot be activated and information about violating tuples is automatically inserted
into the table EXCEPTIONS.
Detailed information about the violating tuples can be obtained by joining the tables
EMP and EXCEPTIONS, based on the join attribute ROWID:
select EMP.∗ , CONSTRAINT from EMP, EXCEPTIONS
where EMP.ROWID = EXCEPTIONS.ROW ID;

Tuples contained in the query result now can be modified (e.g., by increasing the
salary of managers) such that adding the constraint can be performed successfully.
Note that it is important to delete ‘old’ violations from the relation EXCEPTIONS
before it is used again.
If a table is used as a reference of a foreign key, this table can only be dropped using
the command drop table <table> cascade constraints;. All other database objects that
refer to this table (e.g., triggers, see Section 5.2) remain in the database system, but
they are not valid.

Page 6 of 17
Unit-II B

Information about integrity constraints, their status (enabled, disabled) etc. is stored in
the data dictionary, more precisely, in the tables USER CONSTRAINTS and USER
CONS CONSTRAINTS.
Checklist for Creating Tables
The following provides a small checklist for the issues that need to be considered
before creating a table.
• What are the attributes of the tuples to be stored? What are the data types of the
attributes? Should varchar2 be used instead of char ?
• Which columns build the primary key?
• Which columns do (not) allow null values? Which columns do (not) allow
duplicates ?
• Are there default values for certain columns that allow null values ?
Modifying Table- and Column Definitions
It is possible to modify the structure of a table (the relation schema) even if rows have
already been inserted into this table. A column can be added using the alter table
command
alter table <table>
add(<column> <data type> [default <value>] [<column constraint>]);
If more than only one column should be added at one time, respective add clauses
need to be separated by colons.
A table constraint can be added to a table using
alter table <table> add (<table constraint>);
Note that a column constraint is a table constraint, too. not null and primary key
constraints can only be added to a table if none of the specified columns contains a
null value. Table definitions can be modified in an analogous way. This is useful, e.g.,
when the size of strings that can be stored needs to be increased. The syntax of the
command for modifying a column is
alter table <table>
modify(<column> [<data type>] [default <value>] [<column constraint>]);
Note: In earlier versions of Oracle it is not possible to delete single columns from a
table definition. A workaround is to create a temporary table and to copy respective
columns and rows into this new table. Furthermore, it is not possible to rename tables
or columns. In the most recent version (9i), using the alter table command, it is
possible to rename a table, columns, and constraints. In this version, there also exists a
drop column clause as part of the alter table statement.
Deleting a Table
A table and its rows can be deleted by issuing the command
drop table <table>
[cascade constraints];
Data Modifications in SQL
After a table has been created using the create table command, tuples can be inserted
into the table, or tuples can be deleted or modified.
Insertions
The most simple way to insert a tuple into a table is to use the insert statement
insert into <table> [(<column i, . . . , column j>)]
values (<value i, . . . , value j>);
For each of the listed columns, a corresponding (matching) value must be specified.
Thus an insertion does not necessarily have to follow the order of the attributes as
specified in the create table statement. If a column is omitted, the value null is

Page 7 of 17
Unit-II B

inserted instead. If no column list is given, however, for each column as defined in the
create table statement a value must be given.
Examples:
insert into PROJECT(PNO, PNAME, PERSONS, BUDGET, PSTART)
values(313, DBS, 4, 150000.42, 10-OCT-94);
or
insert into PROJECT
values(313, DBS, 7411, null, 150000.42, 10-OCT-94, null);
If there are already some data in other tables, these data can be used for insertions into
a new table. For this, we write a query whose result is a set of tuples to be inserted.
Such an insert statement has the form
insert into <table> [(<column i, . . . , column j>)] <query>
Example: Suppose we have defined the following table:
create table OLDEMP (
ENO number(4) not null,
HDATE date);
We now can use the table EMP to insert tuples into this new relation:
insert into OLDEMP (ENO, HDATE)
select EMPNO, HIREDATE from EMP
where HIREDATE < 31-DEC-60 ;
Updates
For modifying attribute values of (some) tuples in a table, we use the update statement:
update <table>
Set <column i> = <expression i>, . . . , <column j> = <expression j>
[where <condition>];
An expression consists of either a constant (new value), an arithmetic or string
operation, or an SQL query. Note that the new value to assign to <column i> must a
the matching data type.
An update statement without a where clause results in changing respective attributes
of all tuples in the specified table. Typically, however, only a (small) portion of the
table requires an update.
Examples:
• The employee JONES is transferred to the department 20 as a manager and his
salary is increased by 1000:
update EMP set
JOB = MANAGER, DEPTNO = 20, SAL = SAL +1000
where ENAME = JONES;
• All employees working in the departments 10 and 30 get a 15% salary increase.
update EMP
Set SAL = SAL ∗ 1.15 where DEPTNO in (10,30);
Analogous to the insert statement, other tables can be used to retrieve data that are
used as new values. In such a case we have a <query> instead of an <expression>.
Example: All salesmen working in the department 20 get the same salary as the
manager who has the lowest salary among all managers.
update EMP set
SAL = (select min(SAL) from EMP where JOB = MANAGER)
where JOB = SALESMAN and DEPTNO = 20;
Explanation: The query retrieves the minimum salary of all managers. This value then
is assigned to all salesmen working in department 20.

Page 8 of 17
Unit-II B

It is also possible to specify a query that retrieves more than only one value (but still
only one tuple!). In this case the set clause has the form
set(<column i, . . . , column j>) = <query>.
It is important that the order of data types and values of the selected row exactly
correspond to the list of columns in the set clause.
Deletions
All or selected tuples can be deleted from a table using the delete command:
delete from <table> [where <condition>];
If the where clause is omitted, all tuples are deleted from the table. An alternative
command for deleting all tuples from a table is the truncate table <table> command.
However, in this case, the deletions cannot be undone.
Example:
Delete all projects (tuples) that have been finished before the actual date (system date):
delete from PROJECT where PEND < sysdate;
sysdate is a function in SQL that returns the system date. Another important SQL
function is user, which returns the name of the user logged into the current Oracle
session.
Commit and Rollback
A sequence of database modifications, i.e., a sequence of insert, update, and delete
statements, is called a transaction. Modifications of tuples are temporarily stored in
the database system. They become permanent only after the statement commit; has
been issued.
As long as the user has not issued the commit statement, it is possible to undo all
modifications since the last commit. To undo modifications, one has to issue the
statement rollback;.
It is advisable to complete each modification of the database with a commit (as long
as the modification has the expected effect). Note that any data definition command
such as create table results in an internal commit. A commit is also implicitly
executed when the user terminates an Oracle session.

Queries (Part I)
In order to retrieve the information stored in the database, the SQL query language is
used. In the following we restrict our attention to simple SQL queries and defer the
discussion of more complex queries to next Section.
In SQL a query has the following (simplified) form (components in brackets [ ] are
optional):
select [distinct] <column(s)>
from <table>
[ where <condition> ]
[ order by <column(s) [asc desc]> ]
Selecting Columns
The columns to be selected from a table are specified after the keyword select. This
operation is also called projection. For example, the query
select LOC, DEPTNO from DEPT;
lists only the number and the location for each tuple from the relation DEPT. If all
columns should be selected, the asterisk symbol ∗ can be used to denote all
attributes. The query
select ∗ from EMP;

Page 9 of 17
Unit-II B

retrieves all tuples with all columns from the table EMP. Instead of an attribute name,
the select clause may also contain arithmetic expressions involving arithmetic
operators etc.
select ENAME, DEPTNO, SAL ∗ 1.55 from EMP;
For the different data types supported in Oracle, several operators and functions are
provided:
• for numbers: abs, cos, sin, exp, log, power, mod, sqrt, +, −, ∗ , /, . . .
• for strings: chr, concat(string1, string2), lower, upper, replace(string, search string,
replacement string), translate, substr(string, m, n), length, to date, . . .
• for the date data type: add month, month between, next day, to char, . . .
The usage of these operations is described in detail in the SQL*Plus help system.
Consider the query
select DEPTNO from EMP;
which retrieves the department number for each tuple. Typically, some numbers will
appear more than only once in the query result, that is, duplicate result tuples are not
automatically eliminated. Inserting the keyword distinct after the keyword select,
however, forces the elimination of duplicates from the query result.
It is also possible to specify a sorting order in which the result tuples of a query are
displayed.
For this the order by clause is used and which has one or more attributes listed in the
select clause as parameter. desc specifies a descending order and asc specifies an
ascending order (this is also the default order). For example, the query
select ENAME, DEPTNO, HIREDATE from EMP;
from EMP
order by DEPTNO [asc], HIREDATE desc;
displays the result in an ascending order by the attribute DEPTNO. If two tuples have
the same attribute value for DEPTNO, the sorting criteria is a descending order by the
attribute values of HIREDATE. For the above query, we would get the following
output:
ENAME DEPTNO HIREDATE
FORD 10 03-DEC-81
SMITH 20 17-DEC-80
BLAKE 30 01-MAY-81
WARD 30 22-FEB-81
ALLEN 30 20-FEB-81
...........................
Selection of Tuples
Up to now we have only focused on selecting (some) attributes of all tuples from a
table. If one is interested in tuples that satisfy certain conditions, the where clause is
used.
In a where clause simple conditions based on comparison operators can be combined
using the logical connectives and, or, and not to form complex conditions. Conditions
may also include pattern matching operations and even subqueries .
Example: List the job title and the salary of those employees whose manager has the
number 7698 or 7566 and who earn more than 1500:
select JOB, SAL
from EMP
where (MGR = 7698 or MGR = 7566) and SAL > 1500;
For all data types, the comparison operators =, != or <>,<, >,<=, => are allowed in the
conditions of a where clause.

Page 10 of 17
Unit-II B

Further comparison operators are:


• Set Conditions: <column> [not] in (<list of values>)
Example: select ∗ from DEPT where DEPTNO in (20,30);
• Null value: <column> is [not] null,
i.e., for a tuple to be selected there must (not) exist a defined value for this column.
Example: select ∗ from EMP where MGR is not null;
Note: the operations = null and ! = null are not defined!
• Domain conditions: <column> [not] between <lower bound> and <upper bound>
Examples:
select EMPNO, ENAME, SAL from EMP
where SAL between 1500 and 2500;

select ENAME from EMP


where HIREDATE between 02-APR-81 and 08-SEP-81 ;
String Operations
In order to compare an attribute with a string, it is required to surround the string by
apostrophes,
e.g., where LOCATION = DALLAS .
A powerful operator for pattern matching is the like operator. Together with this
operator, two special characters are used: the percent sign % (also called wild card),
and the underline , also called position marker.
For example, if one is interested in all tuples of the table DEPT that contain two C in
the name of the department, the condition would be where DNAME like %C%C%.
The percent sign means that any (sub)string is allowed there, even the empty string. In
contrast, the underline stands for exactly one character. Thus the condition where
DNAME like %C C% would require that exactly on character appears between
the two Cs. To test for inequality, the not clause is used.
Further string operations are:
• upper(<string>) takes a string and converts any letters in it to uppercase,
e.g., DNAME = upper(DNAME)
(The name of a department must consist only of upper case letters.)
• lower(<string>) converts any letter to lowercase,
• initcap(<string>) converts the initial letter of every word in <string> to uppercase.
• length(<string>) returns the length of the string.
• substr(<string>, n [, m]) clips out a m character piece of <string>, starting at position
n. If m is not specified, the end of the string is assumed.
substr(DATABASE SYSTEMS, 10, 7) returns the string SYSTEMS.
Aggregate Functions
Aggregate functions are statistical functions such as count, min, max etc. They are
used to compute a single value from a set of attribute values of a column:
count ------Counting Rows
Example: How many tuples are stored in the relation EMP?
select count (*) from EMP;
Example: How many different job titles are stored in the relation EMP?
select count(distinct JOB) from EMP;
max ----- Maximum value for a column
min ------ Minimum value for a column
Example: List the minimum and maximum salary.
select min(SAL), max(SAL) from EMP;
Example: Compute the difference between the minimum and maximum salary.

Page 11 of 17
Unit-II B

select max(SAL) - min(SAL) from EMP;


sum Computes the sum of values (only applicable to the data type number)
Example: Sum of all salaries of employees working in the department 30.
select sum(SAL) from EMP
where DEPTNO = 30;
avg Computes average value for a column (only applicable to the data type number)
Note: avg, min and max ignore tuples that have a null value for the specified
attribute, but count considers null values.

Queries (Part II)


In previous section we have only focused on queries that refer to exactly one table.
Furthermore, conditions in a where were restricted to simple comparisons. A major
feature of relational databases, however, is to combine (join) tuples stored in different
tables in order to display more meaningful and complete information.
In SQL the select statement is used for this kind of queries joining relations:
select [distinct] [<alias ak>.]<column i>, . . . , [<alias al>.]<column j>
from <table 1> [<alias a1>], . . . , <table n> [<alias an>]
[where <condition>]
The specification of table aliases in the from clause is necessary to refer to columns
that have the same name in different tables. For example, the column DEPTNO
occurs in both EMP and DEPT. If we want to refer to either of these columns in the
where or select clause, a table alias has to be specified and put in the front of the
column name. Instead of a table alias also the complete relation name can be put in
front of the column such as DEPT.DEPTNO, but this sometimes can lead to rather
lengthy query formulations.
Joining Relations
Comparisons in the where clause are used to combine rows from the tables listed in
the from clause.
Example: In the table EMP only the numbers of the departments are stored, not their
name. For each salesman, we now want to retrieve the name as well as the
number and the name of the department where he is working:
select ENAME, E.DEPTNO, DNAME
from EMP E, DEPT D
where E.DEPTNO = D.DEPTNO
and JOB = SALESMAN;
Explanation: E and D are table aliases for EMP and DEPT, respectively. The
computation of the query result occurs in the following manner (without optimization):
1. Each row from the table EMP is combined with each row from the table DEPT
(this operation is called Cartesian product ). If EMP contains m rows and
DEPT contains n rows, we thus get n ∗ m rows.
2. From these rows those that have the same department number are selected
(where E.DEPTNO = D.DEPTNO).
3. From this result finally all rows are selected for which the condition JOB =
SALESMAN holds.
In this example the joining condition for the two tables is based on the equality
operator = . The columns compared by this operator are called join columns and
the join operation is called an equijoin. Any number of tables can be combined in a
select statement.
Example: For each project, retrieve its name, the name of its manager, and the name
of the department where the manager is working:

Page 12 of 17
Unit-II B

select ENAME, DNAME, PNAME


from EMP E, DEPT D, PROJECT P
where E.EMPNO = P.MGR
and D.DEPTNO = E.DEPTNO;
It is even possible to join a table with itself:
Example: List the names of all employees together with the name of their manager:
select E1.ENAME, E2.ENAME
from EMP E1, EMP E2
where E1.MGR = E2.EMPNO;
Explanation: The join columns are MGR for the table E1 and EMPNO for the table
E2. The equijoin comparison is E1.MGR = E2.EMPNO.
Subqueries
Up to now we have only concentrated on simple comparison conditions in a where
clause, i.e., we have compared a column with a constant or we have compared two
columns. As we have already seen for the insert statement, queries can be used for
assignments to columns.
A query result can also be used in a condition of a where clause. In such a case the
query is called a subquery and the complete select statement is called a nested query.
A respective condition in the where clause then can have one of the following forms:
1. Set-valued subqueries
<expression> [not] in (<subquery>)
<expression> <comparison operator> [any all] (<subquery>)
An <expression> can either be a column or a computed value.
2. Test for (non)existence
[not] exists (<subquery>)
In a where clause conditions using subqueries can be combined arbitrarily by using
the logical connectives and and or.
Example: List the name and salary of employees of the department 20 who are
leading a project that started before December 31, 1990:
select ENAME, SAL from EMP
where EMPNO in
(select PMGR from PROJECT
where PSTART < 31-DEC-90 )
and DEPTNO =20;
Explanation: The subquery retrieves the set of those employees who manage a project
that started before December 31, 1990. If the employee working in department 20 is
contained in this set (in operator), this tuple belongs to the query result set.
Example: List all employees who are working in a department located in BOSTON:
select ∗ from EMP
where DEPTNO in
(select DEPTNO from DEPT
where LOC = BOSTON );
The subquery retrieves only one value (the number of the department located in
Boston). Thus it is possible to use = instead of in. As long as the result of a
subquery is not known in advance, i.e., whether it is a single value or a set, it is
advisable to use the in operator.
A subquery may use again a subquery in its where clause. Thus conditions can be
nested arbitrarily. An important class of subqueries are those that refer to its
surrounding (sub)query and the tables listed in the from clause, respectively. Such
type of queries is called correlated subqueries.

Page 13 of 17
Unit-II B

Example: List all those employees who are working in the same department as their
manager (note that components in [ ] are optional:
select ∗ from EMP E1
where DEPTNO in
(select DEPTNO from EMP [E]
where [E.]EMPNO = E1.MGR);
Explanation: The subquery in this example is related to its surrounding query since it
refers to the column E1.MGR. A tuple is selected from the table EMP (E1) for the
query result if the value for the column DEPTNO occurs in the set of values select in
the subquery. One can think of the evaluation of this query as follows: For each tuple
in the table E1, the subquery is evaluated individually. If the condition where
DEPTNO in . . . evaluates to true, this tuple is selected.
Note that an alias for the table EMP in the subquery is not necessary since columns
without a preceding alias listed there always refer to the innermost query and tables.
Conditions of the form <expression> <comparison operator> [any all] <subquery>
are used to compare a given <expression> with each value selected by <subquery>.
• For the clause any, the condition evaluates to true if there exists at least on row
selected by the subquery for which the comparison holds. If the subquery yields an
empty result set, the condition is not satisfied.
• For the clause all, in contrast, the condition evaluates to true if for all rows selected
by the subquery the comparison holds. In this case the condition evaluates to true if
the subquery does not yield any row or value.
Example: Retrieve all employees who are working in department 10 and who earn at
least as much as any (i.e., at least one) employee working in department 30:
select ∗ from EMP
where SAL >= any
(select SAL from EMP
where DEPTNO = 30)
and DEPTNO = 10;
Note: Also in this subquery no aliases are necessary since the columns refer to the
innermost from clause.
Example: List all employees who are not working in department 30 and who earn
more than all employees working in department 30:
select ∗ from EMP
where SAL > all
(select SAL from EMP
where DEPTNO = 30)
and DEPTNO <> 30;
For all and any, the following equivalences hold:
in ⇔ = any
not in ⇔ <> all or != all
Often a query result depends on whether certain rows do (not) exist in (other) tables.
Such type of queries is formulated using the exists operator.
Example: List all departments that have no employees:
select ∗ from DEPT
where not exists
(select ∗ from EMP
where DEPTNO = DEPT.DEPTNO);
Explanation: For each tuple from the table DEPT, the condition is checked whether
there exists a tuple in the table EMP that has the same department number

Page 14 of 17
Unit-II B

(DEPT.DEPTNO). In case no such tuple exists, the condition is satisfied for the tuple
under consideration and it is selected. If there exists a corresponding tuple in the table
EMP, the tuple is not selected.
Set Operations on Result Sets
Sometimes it is useful to combine query results from two or more queries into a single
result. SQL supports three set operators which have the pattern:
<query 1> <set operator> <query 2>
The set operators are:
• union [all] returns a table consisting of all rows either appearing in the result of
<query 1> or in the result of <query 2>. Duplicates are automatically eliminated
unless the clause all is used.
• intersect returns all rows that appear in both results <query 1> and <query 2>.
• minus returns those rows that appear in the result of <query 1> but not in the result
of <query 2>.
Example: Assume that we have a table EMP2 that has the same structure and columns
as the table EMP:
• All employee numbers and names from both tables:
select EMPNO, ENAME from EMP
union
select EMPNO, ENAME from EMP2;
• Employees who are listed in both EMP and EMP2:
select ∗ from EMP
intersect
select ∗ from EMP2;
• Employees who are only listed in EMP:
select ∗ from EMP
minus
select ∗ from EMP2;
Each operator requires that both tables have the same data types for the columns to
which the operator is applied.

Grouping
In the previous section we have seen how aggregate functions can be used to compute
a single value for a column. Often applications require grouping rows that have
certain properties and then applying an aggregate function on one column for each
group separately. For this, SQL provides the clause group by <group column(s)>.
This clause appears after the where clause and must refer to columns of tables listed
in the from clause.
select <column(s)>
from <table(s)>
where <condition>
group by <group column(s)>
[having <group condition(s)>];
Those rows retrieved by the selected clause that have the same value(s) for <group
column(s)> are grouped. Aggregations specified in the select clause are then applied
to each group separately.
It is important that only those columns that appear in the <group column(s)> clause
can be listed without an aggregate function in the select clause !
Example: For each department, we want to retrieve the minimum and maximum
salary.

Page 15 of 17
Unit-II B

select DEPTNO, min(SAL), max(SAL)


from EMP
group by DEPTNO;
Rows from the table EMP are grouped such that all rows in a group have the same
department number. The aggregate functions are then applied to each such group. We
thus get the following query result:
EPTNO MIN(SAL) MAX(SAL)
10 1300 5000
20 800 3000
30 950 2850
Rows to form a group can be restricted in the where clause. For example, if we add
the condition where JOB = CLERK, only respective rows build a group. The query
then would retrieve the minimum and maximum salary of all clerks for each
department. Note that is not allowed to specify any other column than DEPTNO
without an aggregate function in the select clause since this is the only column listed
in the group by clause (is it also easy to see that other columns would not make any
sense).
Once groups have been formed, certain groups can be eliminated based on their
properties,
e.g., if a group contains less than three rows. This type of condition is specified using
the having clause. As for the select clause also in a having clause only <group
column(s)> and aggregations can be used.
Example: Retrieve the minimum and maximum salary of clerks for each department
having more than three clerks.
select DEPTNO, min(SAL), max(SAL)
from EMP
where JOB = CLERK
group by DEPTNO
having count(*) > 3;
Note that it is even possible to specify a subquery in a having clause. In the above
query, for example, instead of the constant 3, a subquery can be specified.
A query containing a group by clause is processed in the following way:
1. Select all rows that satisfy the condition specified in the where clause.
2. From these rows form groups according to the group by clause.
3. Discard all groups that do not satisfy the condition in the having clause.
4. Apply aggregate functions to each group.
5. Retrieve values for the columns and aggregations listed in the select clause.
Some Comments on Tables
Accessing tables of other users
Provided that a user has the privilege to access tables of other users (see also Section
3), she/he can refer to these tables in her/his queries. Let <user> be a user in the
Oracle system and <table> a table of this user. This table can be accessed by other
(privileged) users using the command
select ∗ from <user>.<table>;
In case that one often refers to tables of other users, it is useful to use a synonym
instead of <user>.<table>. In Oracle-SQL a synonym can be created using the
command create synonym <name> for <user>.<table> ;
It is then possible to use simply <name> in a from clause. Synonyms can also be
created for one’s own tables.

Page 16 of 17
Unit-II B

Adding Comments to Definitions


For applications that include numerous tables, it is useful to add comments on table
definitions or to add comments on columns. A comment on a table can be created
using the command comment on table <table> is <text> ;
A comment on a column can be created using the command
comment on column <table>.<column> is <text> ;
Comments on tables and columns are stored in the data dictionary. They can be
accessed using the data dictionary views USER TAB COMMENTS and USER COL
COMMENTS .
Views
In Oracle the SQL command to create a view (virtual table) has the form
create [or replace] view <view-name> [(<column(s)>)] as
<select-statement> [with check option [constraint <name>]];
The optional clause or replace re-creates the view if it already exists. <column(s)>
names the columns of the view. If <column(s)> is not specified in the view definition,
the columns of the view get the same names as the attributes listed in the select
statement (if possible).
Example: The following view contains the name, job title and the annual salary of
employees working in the department 20:
create view DEPT20 as
select ENAME, JOB, SAL∗ 12 ANNUAL SALARY from EMP
where DEPTNO = 20;
In the select statement the column alias ANNUAL SALARY is specified for the
expression SAL∗ 12
and this alias is taken by the view. An alternative formulation of the above view
definition is create view DEPT20 (ENAME, JOB, ANNUAL SALARY) as
select ENAME, JOB, SAL ∗ 12 from EMP
where DEPTNO = 20;
A view can be used in the same way as a table, that is, rows can be retrieved from a
view (also respective rows are not physically stored, but derived on basis of the select
statement in the view definition), or rows can even be modified. A view is evaluated
again each time it is accessed.
In Oracle SQL no insert, update, or delete modifications on views are allowed that use
one of the following constructs in the view definition:
• Joins
• Aggregate function such as sum, min, max etc.
• set-valued subqueries (in, any, all) or test for existence (exists)
• group by clause or distinct clause
In combination with the clause with check option any update or insertion of a row into
the view is rejected if the new/modified row does not meet the view definition, i.e.,
these rows would not be selected based on the select statement. A with check option
can be named using the constraint clause.
A view can be deleted using the command delete <view-name>.

Page 17 of 17

You might also like