The SQL Standard PDF
The SQL Standard PDF
The SQL Standard PDF
SQL evolution
SQL- 86/89
SQL- 92 - SQL2
SQL- 99/03 - SQL3
(includes object relational features)
And the evolution continues.
Authorization
Specifies how to restrict a user / set of users to access only
certain parts of data, perform only certain types of queries etc.
UNIQUE (B1,B2,…,Bk)
specifies that {B1,B2,…,Bk} is a candidate key for the table
There can be more than one UNIQUE constraint but only one
PRIMARY KEY constraint for a table.
Using subqueries
makes the main query easy to understand / formulate
sometimes makes it more efficient also
• sub query result can be computed once and
used many times.
• not the case with all subqueries.
IN is equivalent to = ANY
NOT IN is equivalent to < > ALL
select name,phone
from department
Equivalent to:
(SELECT rollNo
FROM enrollment
WHERE (courseId = ‘CS230’ or courseID = ‘CS232’)
and sem = odd and year = 2005 )
select rollNo
from enrollment
where courseId = ‘CS230’ and
sem = odd and
year = 2005
INTERSECT
select rollNo
from enrollment
where courseId = ‘CS232’ and
sem = odd and year = 2005;
(SELECT rollNo
FROM enrollment
WHERE sem = odd and year = 2005 )
EXCEPT
(SELECT rollNo
FROM enrollment
WHERE courseId = ‘CS230’ and
sem = odd and year = 2005);
SUM ( [DISTINCT]A):
computes the sum of (distinct) values in column A
COUNT ( [DISTINCT]A):
computes the number of (distinct) values in column A or no.
of tuples in result
Chennai 84
Mysore 90
Bangalore 82
Select f.name
from professor as f, department as d
where f.deptNo = d.deptId and
d.name = ‘CSE’;
Join types:
1. inner join (default):
from (r1 inner join r2 on <predicate>)
use of just ‘join’ is equivalent to ‘inner join’
2. left outer join:
from (r1 left outer join r2 on <predicate>)
3. right outer join:
from (r1 right outer join r2 on <predicate>)
4. full outer join:
from (r1 full outer join r2 on <predicate>)
Prof P Sreenivasa Kumar 47
Department of CS&E, IITM
Natural join
The adjective ‘natural’ can be used with any of the join types to
specify natural join.
REMARKS
• Specifying join operation explicitly goes against the spirit of
declarative style of query specification
• But the queries may be easier to understand
• The feature is to be used judiciously
If the details of a new CSE professor are entered into professor table,
the above view gets updated automatically
Prof P Sreenivasa Kumar 50
Department of CS&E, IITM
Queries on Views
Once created a view can be used in queries just like any other
table.
select name
from profAft2K
where name like ‘Ram%’;
update professors
set phone = ‘9444422605’
where deptNo = (select deptId
from department
where name = ‘CSE’);
Use of ‘null’ to test for a null value, if the attribute can take null
e.g., Obtain roll numbers of students who
don’t have phone numbers
select rollNo
from student
where phoneNumber is null;
select name
from professor
where startYear between 1980 and 1990;
Data transfer –
takes place through specially declared HL variables