Structured Query Language (SQL)
Structured Query Language (SQL)
(SQL)
Introduction
● Structured Query Language (SQL) is the most widely used commercial relational database language
● It was originally developed at IBM in the SEQUEL-XRM and System-R projects (1974 – 1977).
● The SQL language has several aspects to it:
● The Data Definition Language (DDL): This subset of SQL supports the creation, deletion, and modification
of definitions for tables and views.
○ Integrity constraints can be defined on tables, either when the table is created or later.
○ The DDL also provides commands for specifying access rights or privileges to tables and views.
● The Data Manipulation Language (DML): This subset of SQL allows users to pose queries and to insert,
delete, and modify rows.
● Embedded and dynamic SQL: Embedded SQL features allow SQL code to be called from a host language
such as C or COBOL.
● Triggers: The new SQL:1999 standard includes support for triggers, which are actions executed by the
DBMS whenever changes to the database meet conditions specified in the trigger
THE EXAMPLES
THE FORM OF A BASIC SQL QUERY
2. Delete those rows in the cross-product that fail the qualification conditions.
Step1:Cross Product
wild-card symbols %
Thus, ‘ AB%’
Union compatible
Find the names of sailors who have reserved both red and green boats.
NESTED QUERIES
● A nested query is a query that has another query embedded within it;
○ The embedded query is called a subquery.
○ When writing a query, we sometimes need to express a condition that refers to a table that must itself
be computed.
○ The query used to compute this subsidiary table is a subquery and appears as part of the main
query.
○ A subquery typically appears within the WHERE clause of a query.
○ Subqueries can sometimes appear in the FROM clause or the HAVING clause
○ Set comparison operators: IN, NOT IN
Find the names of sailors who have not reserved a red boat.
Correlated Nested Queries
● In the nested queries that we have seen thus far, the inner subquery has been completely independent of the outer query.
● In general the inner subquery could depend on the row that is currently being examined in the outer query
● The EXISTS operator is another set comparison operator, such as IN. It allows us to test whether a set is nonempty. Thus, for
each Sailor row S, we test whether the set of Reserves rows R such that R.bid = 103 AND S.sid = R.sid is nonempty.
● The subquery clearly depends on the current row S and must be re-evaluated for each row in Sailors. The occurrence of S in
the subquery (in the form of the literal S.sid) is called a correlation, and such queries are called correlated queries.
Set-Comparison Operators
● set-comparison operators EXISTS, IN, and UNIQUE, along with their negated versions.
● SQL also supports op ANY and op ALL, where op is one of the arithmetic comparison operators {<,
<=, =, <>, >=, >}.
● SOME is also available, but it is just a synonym for ANY.
Find the names of sailors who have reserved all boats.
AGGREGATE OPERATORS
To perform some computation or summarization.
2. SUM ([DISTINCT] A): The sum of all (unique) values in the A column.
3. AVG ([DISTINCT] A): The average of all (unique) values in the A column.
FROM
Relation prereq
Observe that
course information is missing CS-347
prereq information is missing CS-315
Left Outer Join
course natural left outer join prereq
select name
from faculty
where dept_name = 'Biology'
Create a view of department salary totals
Delete all tuples in the instructor relation for those instructors associated
with a department located in the Watson building.
delete from instructor
where dept name in (select dept name
from department
where building = 'Watson');
Deletion (Cont.)
Delete all instructors whose salary is less than the average salary of instructors
or equivalently
insert into course (course_id, title, dept_name, credits)
values ('CS-437', 'Database Systems', 'Comp. Sci.', 4);
Make each student in the Music department who has earned more than
144 credit hours an instructor in the Music department with a salary of
$18,000.
insert into instructor
select ID, name, dept_name, 18000
from student
where dept_name = 'Music' and total_cred > 144;
The select from where statement is evaluated fully before any of its
results are inserted into the relation.
Otherwise queries like
insert into table1 select * from table1
would cause problem
Updates