0% found this document useful (0 votes)
2 views

Structured Query Language (SQL)

Structured Query Language (SQL) is a widely used relational database language that includes Data Definition Language (DDL) for defining tables and views, and Data Manipulation Language (DML) for querying and modifying data. SQL supports various features such as nested queries, aggregate operations, outer joins, and views, allowing for complex data retrieval and manipulation. The document provides examples and explanations of SQL syntax, operations, and the handling of null values.

Uploaded by

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

Structured Query Language (SQL)

Structured Query Language (SQL) is a widely used relational database language that includes Data Definition Language (DDL) for defining tables and views, and Data Manipulation Language (DML) for querying and modifying data. SQL supports various features such as nested queries, aggregate operations, outer joins, and views, allowing for complex data retrieval and manipulation. The document provides examples and explanations of SQL syntax, operations, and the handling of null values.

Uploaded by

sonkararyan0000
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

Structured Query Language

(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

Ex: Find the names and ages of all sailors.

SELECT DISTINCT S.sname, S.age


FROM
Sailors S
SQL query execution ..

1. Compute the cross-product of the tables in the from-list.

2. Delete those rows in the cross-product that fail the qualification conditions.

3. Delete all columns that do not appear in the select-list.

4. If DISTINCT is specified, eliminate duplicate rows.


Sample execution
Find the names of sailors who have reserved boat number
103.

SELECT S.sname FROM Sailors S, Reserves R WHERE


S.sid = R.sid AND R.bid=103

Step1:Cross Product

Step2: Check S.sid = R.sid AND R.bid=103

Step 3: The third step is to eliminate unwanted columns; only


sname appears in the SELECT clause
Expressions and Strings in the SELECT Command
Pattern matching
through the LIKE
operator

wild-card symbols %

Thus, ‘ AB%’

denotes a pattern that


will match every string
that contains at least
three characters,

with the second and


third characters being A
Example: and B respectively
SELECT S.age
‘Jeff’ = ‘Jeff ’ could be
FROM
true
Sailors S
WHERE S.sname LIKE ‘B %B’ while ‘Jeff’ LIKE ‘Jeff ’ is
false
UNION, INTERSECT, AND EXCEPT
Find the names of sailors who have reserved a red or a green boat.

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.

1. COUNT ([DISTINCT] A): The number of (unique) values in the A column.

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.

4. MAX (A): The maximum value in the A column.

5. MIN (A): The minimum value in the A column.


Find the name and age of the oldest sailor.

Count the number of sailors.

SELECT COUNT (*) FROM Sailors S


Find the names of sailors who are older than the oldest sailor with a rating of 10.
The GROUP BY and HAVING Clauses
● To apply aggregate operations to each of a number of groups of rows in a relation
● Find the age of the youngest sailor for each rating level.

The select-list in the SELECT clause consists of


(1) a list of column names and
(2) a list of terms having the form aggop ( column-name ) AS new-name.
● Every column that appears in (1) must also appear in grouping-list. The reason is that each row in the result of the query
corresponds to one group.
● The expressions appearing in the group-qualification in the HAVING clause must have a single value per group.
Rule
Only columns that appear in the GROUP BY clause can appear in the HAVING clause, unless they appear as
arguments to an aggregate operator in the HAVING clause.
aggregate operations cannot be nested
Find those ratings for which the average age of sailors is the minimum over all ratings.
NULL VALUES
● We use null when the column value is either unknown or inapplicable
● if we compare two null values using <, >, =, and so on, the result is always unknown.
○ A special comparison operator IS NULL to test whether a column value is null;
● The SQL definition is that two rows are duplicates if corresponding columns are either equal,
or both contain null.
● Logical Connectives AND, OR, and NOT: We can only say unknown.
Null Values
 It is possible for tuples to have a null value, denoted by null, for some of
their attributes
 null signifies an unknown value or that a value does not exist.
 The result of any arithmetic expression involving null is null
• Example: 5 + null returns null
 The predicate is null can be used to check for null values.
• Example: Find all instructors whose salary is null.
select name
from instructor
where salary is null
 The predicate is not null succeeds if the value on which it is applied is
not null.
Null Values (Cont.)

 SQL treats as unknown the result of any comparison involving a null


value (other than predicates is null and is not null).
• Example: 5 < null or null <> null or null = null
 The predicate in a where clause can involve Boolean operations (and,
or, not); thus the definitions of the Boolean operations need to be
extended to deal with the value unknown.
• and : (true and unknown) = unknown,
(false and unknown) = false,
(unknown and unknown) = unknown
• or: (unknown or true) = true,
(unknown or false) = unknown
(unknown or unknown) = unknown
 Result of where clause predicate is treated as false if it evaluates to
unknown
Outer Joins
● Consider the join of two tables, say Sailors Conditional Join Reserves.
● Tuples of Sailors that do not match some row in Reserves according to the join condition c do not appear in
the result.
● In an outer join, on the other hand, Sailor rows without a matching Reserves row appear exactly once in the
result, with the result columns inherited from Reserves assigned null values.
● In a left outer join, Sailor rows without a matching Reserves row appear in the result, but not vice versa.
● In a right outer join, Reserves rows without a matching Sailors row appear in the result, but not vice versa.
● In a full outer join, both Sailors and Reserves rows without a match appear in the result.

SELECT Sailors.sid, Reserves.bid

FROM

Sailors NATURAL LEFT OUTER JOIN Reserves R


Student Relation
Takes Relation
student natural join takes
Dangerous in Natural Join
 Beware of unrelated attributes with same name which get equated
incorrectly
 Example -- List the names of students instructors along with the titles of
courses that they have taken
• Correct version
select name, title
from student natural join takes, course
where takes.course_id = course.course_id;
• Incorrect version
select name, title
from student natural join takes natural join course;
 This query omits all (student name, course title) pairs where the
student takes a course in a department other than the student's
own department.
 The correct version (above), correctly outputs such pairs.
Outer Join
 An extension of the join operation that avoids loss of information.
 Computes the join and then adds tuples form one relation that does not
match tuples in the other relation to the result of the join.
 Uses null values.
 Three forms of outer join:
• left outer join
• right outer join
• full outer join
Outer Join Examples
 Relation course

 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

 In relational algebra: course ⟕ prereq


Right Outer Join
 course natural right outer join prereq

 In relational algebra: course ⟖ prereq


Full Outer Join
 course natural full outer join prereq

 In relational algebra: course ⟗ prereq


Joined Types and Conditions
 Join operations take two relations and return as a result another
relation.
 These additional operations are typically used as subquery expressions
in the from clause
 Join condition – defines which tuples in the two relations match.
 Join type – defines how tuples in each relation that do not match any
tuple in the other relation (based on the join condition) are treated.
Joined Relations – Examples
 course natural right outer join prereq

 course full outer join prereq using (course_id)


Joined Relations – Examples
 course inner join prereq on
course.course_id = prereq.course_id

 What is the difference between the above, and a natural join?


 course left outer join prereq on
course.course_id = prereq.course_id
Joined Relations – Examples
 course natural right outer join prereq

 course full outer join prereq using (course_id)


Views
 In some cases, it is not desirable for all users to see the entire logical
model (that is, all the actual relations stored in the database.)
 Consider a person who needs to know an instructors name and
department, but not the salary. This person should see a relation
described, in SQL, by

select ID, name, dept_name


from instructor

 A view provides a mechanism to hide certain data from the view of


certain users.
 Any relation that is not of the conceptual model but is made visible to a
user as a “virtual relation” is called a view.
View Definition
 A view is defined using the create view statement which has the form
create view v as < query expression >
where <query expression> is any legal SQL expression. The view
name is represented by v.
 Once a view is defined, the view name can be used to refer to the virtual
relation that the view generates.
 View definition is not the same as creating a new relation by evaluating
the query expression
• Rather, a view definition causes the saving of an expression; the
expression is substituted into queries using the view.
View Definition and Use
 A view of instructors without their salary
create view faculty as
select ID, name, dept_name
from instructor
 Find all instructors in the Biology department

select name
from faculty
where dept_name = 'Biology'
 Create a view of department salary totals

create view departments_total_salary(dept_name, total_salary) as


select dept_name, sum (salary)
from instructor
group by dept_name;
Views Defined Using Other Views
 One view may be used in the expression defining another view
 A view relation v1 is said to depend directly on a view relation v2 if v2 is
used in the expression defining v1
 A view relation v1 is said to depend on view relation v2 if either v1
depends directly to v2 or there is a path of dependencies from v1 to v2
 A view relation v is said to be recursive if it depends on itself.
Views Defined Using Other Views
 create view physics_fall_2017 as
select course.course_id, sec_id, building, room_number
from course, section
where course.course_id = section.course_id
and course.dept_name = 'Physics'
and section.semester = 'Fall'
and section.year = '2017’;

 create view physics_fall_2017_watson as


select course_id, room_number
from physics_fall_2017
where building= 'Watson';
View Expansion
 Expand the view :
create view physics_fall_2017_watson as
select course_id, room_number
from physics_fall_2017
where building= 'Watson'
 To:
create view physics_fall_2017_watson as
select course_id, room_number
from (select course.course_id, building, room_number
from course, section
where course.course_id = section.course_id
and course.dept_name = 'Physics'
and section.semester = 'Fall'
and section.year = '2017')
where building= 'Watson';
View Expansion (Cont.)
 A way to define the meaning of views defined in terms of other views.
 Let view v1 be defined by an expression e1 that may itself contain uses of
view relations.
 View expansion of an expression repeats the following replacement step:
repeat
Find any view relation vi in e1
Replace the view relation vi by the expression defining vi
until no more view relations are present in e1
 As long as the view definitions are not recursive, this loop will terminate
Materialized Views
 Certain database systems allow view relations to be physically stored.
• Physical copy created when the view is defined.
• Such views are called Materialized view:
 If relations used in the query are updated, the materialized view result
becomes out of date
• Need to maintain the view, by updating the view whenever the
underlying relations are updated.
Update of a View
 Add a new tuple to faculty view which we defined earlier
insert into faculty
values ('30765', 'Green', 'Music');
 This insertion must be represented by the insertion into the instructor
relation
• Must have a value for salary.
 Two approaches
• Reject the insert
• Insert the tuple
('30765', 'Green', 'Music', null)
into the instructor relation
And Some Not at All
 create view history_instructors as
select *
from instructor
where dept_name= 'History';
 What happens if we insert
('25566', 'Brown', 'Biology', 100000)
into history_instructors?
View Updates in SQL
 Most SQL implementations allow updates only on simple views
• The from clause has only one database relation.
• The select clause contains only attribute names of the relation,
and does not have any expressions, aggregates, or distinct
specification.
• Any attribute not listed in the select clause can be set to null
• The query does not have a group by or having clause.
Modification of the Database

 Deletion of tuples from a given relation.


 Insertion of new tuples into a given relation
 Updating of values in some tuples in a given relation
Deletion

 Delete all instructors


delete from instructor

 Delete all instructors from the Finance department


delete from instructor
where dept_name= 'Finance’;

 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

• Problem: as we delete tuples from instructor, the average salary changes


• Solution used in SQL:
1. First, compute avg (salary) and find all tuples to delete
2. Next, delete all tuples found above (without recomputing avg or
retesting the tuples)

delete from instructor


where salary < (select avg (salary)
from instructor);
Insertion

 Add a new tuple to course


insert into course
values ('CS-437', 'Database Systems', 'Comp. Sci.', 4);

 or equivalently
insert into course (course_id, title, dept_name, credits)
values ('CS-437', 'Database Systems', 'Comp. Sci.', 4);

 Add a new tuple to student with tot_creds set to null


insert into student
values ('3003', 'Green', 'Finance', null);
Insertion (Cont.)

 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

 Give a 5% salary raise to all instructors


update instructor
set salary = salary * 1.05
 Give a 5% salary raise to those instructors who earn less than 70000
update instructor
set salary = salary * 1.05
where salary < 70000;
 Give a 5% salary raise to instructors whose salary is less than average
update instructor
set salary = salary * 1.05
where salary < (select avg (salary)
from instructor);
Updates (Cont.)

 Increase salaries of instructors whose salary is over $100,000 by 3%,


and all others by a 5%
• Write two update statements:
update instructor
set salary = salary * 1.03
where salary > 100000;
update instructor
set salary = salary * 1.05
where salary <= 100000;
• The order is important
• Can be done better using the case statement (next slide)
Case Statement for Conditional Updates

 Same query as before but with case statement


update instructor
set salary = case
when salary <= 100000 then salary * 1.05
else salary * 1.03
end
Updates with Scalar Subqueries
 Recompute and update tot_creds value for all students
update student S
set tot_cred = (select sum(credits)
from takes, course
where takes.course_id = course.course_id and
S.ID= takes.ID.and
takes.grade <> 'F' and
takes.grade is not null);
 Sets tot_creds to null for students who have not taken any course
 Instead of sum(credits), use:
case
when sum(credits) is not null then sum(credits)
else 0
end

You might also like