The Databases Course
SQL Summary
This document contains all the SQL that this course requires you to know. This means that the syntax of the commands given here is (very) partial. You are welcome to explore further using Oracle’s ‘help [command]’ command.
Subselect general form:
select [all | distinct ] expression [alias] {, expression [alias], … }
from tablename [corr_name] {, tablename [corr_name], … }
where search_condition
group by column, column, …
having search_condition
Full select general form:
Subselect
[union | union all | intersect | minus
Subselect
…]
[ order by result_column [asc | desc] {, result_column [asc | desc], … } ]
Conceptual order of SELECT evaluation
- The Cartesian product of all tables in the from clause is formed.
- From this, rows not satisfying the where condition are eliminated.
- The remaining rows are grouped in accordance with the group by clause.
- Groups not satisfying the having clause are then eliminated.
- The expressions of the select clause target list are evaluated.
- If the keyword distinct is present, duplicate rows are now eliminated.
- The union/intersect/minus is taken after each subselect is evaluated.
- Finally, the set of all selected rows is sorted if an order by is present.
Search Conditions and predicates:
A search condition is: predicate | not(predicate) | (pred1 and pred2) | (pred1 or pred2).
A predicate can be:
- Comparison predicate: expr1 [ =, <>, >, <, >=, <= ] (expr2 | Subselect)
p.price > 5
- Between predicate: expr1 [not] between expr2 and expr3
c.discount between 10.0 and 14.0
- Quantified predicate: expr [=,<>,>,<,>=,<=][any | all] (Subselect)
c.price >=all (select price from parts)
- In predicate: expr [not] in (Subselect)
city in (‘New York’, ‘Atlanta’)
- Exists predicate: [not] exists (Subselect)
exists (select * from parts where p.weight > 100)
- Is null predicate: column_name is [not] null
c.address is not null
- Like predicate: column_name is [not] like ‘pattern’
cname is like ‘A%’
Managing tables
- create table tablename (field1 datatype [default expr], field2 datatype [default expr], …, primary key(field1, field2, …) ) as Subselect;
create table parts (p# integer, pname varchar(30), price numeric(8,2), department smallint default 1, primary key(p#));
create table my_parts as select * from parts;
- drop table tablename
drop table parts;
- alter table tablename [ add field datatype | drop field | modify field datatype], …
alter table parts add validity date, drop pname;
Data Types (partial list)
Smallint – 16 bits integer
Integer – 32 bits integer
Char(n) – fixed length character string
Varchar(n) – variable length character string
Numeric(precision, scale) – a real number
Float(precision, scale) – a real number
Date, Time – date or time types
Boolean – a true/false variable
Bytes(n) – Anything (user defined)
Updating tables:
- insert into table_name (field1, field2, …) values (value1, value2, …)
insert into parts (p#, weight, pname) values (11, 0.250, ‘Pacifier’);
- insert into table_name (Select)
insert into projects select * from old_proj where old_proj.duration > 365;
- delete from table_name where search_condition
delete from suppliers where city = ‘Rome’;
- update table_name set (field1) = value1{, (field2) = value2, …} where condition
update parts set (price)=0.0, (dept)='SendBack' where city='London';
Views
A view is a virtual table: It is actually a name for a query. You can use views in select, insert, update or delete statements instead of table_names.
- create [or replace] [force | noforce] view view_name as (Select)
create view partnames as select pname from parts;
- drop view view_name
drop view partnames;