3.chapter 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 69

SQL

P.Uruthiran
puruthiran@gmail.com
ICT-UNIVOTEC

ICT-UNIVOTEC Database Analysis and Design - B.Tech 1


SQL, the Structured
Query Language

Database Analysis and


ICT-UNIVOTEC 2 Design - B.Tech
Overview
Introduction
DDL Commands
DML Commands
SQL Statements, Operators, Clauses
Aggregate Functions

Database Analysis and


ICT-UNIVOTEC 3 Design - B.Tech
Structured Query Language (SQL)‫‏‬

The ANSI (American National Standards


Institute) standard language for the definition and
manipulation of relational database.

Includes data definition language (DDL),


statements that specify and modify database
schemas.

Includes a data manipulation language (DML),


statements that manipulate database content.

Database Analysis and


ICT-UNIVOTEC 4 Design - B.Tech
Some Facts on SQL
SQL data is case-sensitive, SQL commands are not.

First Version was developed at IBM by Donald D.


Chamberlin and Raymond F. Boyce. [SQL]

Developed using Dr. E.F. Codd's paper, “A Relational


Model of Data for Large Shared Data Banks.”

SQL query includes references to tuples variables


and the attributes of those variables

Database Analysis and


ICT-UNIVOTEC 5 Design - B.Tech
A language that allows the DBA or user to
describe and name the:
Entities
Attributes
Relationships
Integrity and Security Constraints.

ICT-UNIVOTEC Database Analysis and Design - B.Tech 6


SQL: DDL Commands
CREATE TABLE: used to create a table.

ALTER TABLE: modifies a table after it was created.

DROP TABLE: removes a table from a database.

Database Analysis and


ICT-UNIVOTEC 7 Design - B.Tech
SQL: CREATE TABLE Statement
Things to consider before you create your table are:
The type of data
the table name
what column(s) will make up the primary key
the names of the columns

CREATE TABLE statement syntax:


CREATE TABLE <table name>
( field1 datatype ( NOT NULL ),
field2 datatype ( NOT NULL )
);

Database Analysis and


ICT-UNIVOTEC 8 Design - B.Tech
SQL: Attributes Types

Database Analysis and


ICT-UNIVOTEC 9 Design - B.Tech
SQL: ALTER TABLE Statement
To add or drop columns on existing tables.

ALTER TABLE statement syntax:


ALTER TABLE <table name>
ADD attr datatype;
or
DROP COLUMN attr;

Database Analysis and


ICT-UNIVOTEC 10 Design - B.Tech
SQL: DROP TABLE Statement
Has two options:
CASCADE: Specifies that any foreign key constraint
violations that are caused by dropping the table will
cause the corresponding rows of the related table to be
deleted.

RESTRICT: blocks the deletion of the table of any


foreign key constraint violations would be created.

DROP TABLE statement syntax:


DROP TABLE <table name> [ RESTRICT|CASCADE ];
Database Analysis and
ICT-UNIVOTEC 11 Design - B.Tech
Example:
CREATE TABLE FoodCart
(
date varchar(10), FoodCart
food varchar(20), date food profit
profit float
);
FoodCart
ALTER TABLE FoodCart ( date food profit sold
ADD sold int
);
FoodCart
ALTER TABLE FoodCart( date food sold
DROP COLUMN profit
);

DROP TABLE FoodCart;


ICT-UNIVOTEC 12
Database Analysis and
Design - B.Tech
Data manipulation operations usually
include the following:
Insertion of new data into the
database
Modification of data stored in the
database
Retrieval of data contained in the
database
Deletion of data from the database

ICT-UNIVOTEC Database Analysis and Design - B.Tech 13


SQL: DML Commands
INSERT: adds new rows to a table.

UPDATE: modifies one or more attributes.

DELETE: deletes one or more rows from a table.

Database Analysis and


ICT-UNIVOTEC 14 Design - B.Tech
SQL: INSERT Statement
To insert a row into a table, it is necessary to have
a value for each attribute, and order matters.
INSERT statement syntax:
INSERT into <table name>
VALUES ('value1', 'value2', NULL);
Example: INSERT into FoodCart
VALUES (‟02/26/08', „pizza', 70 );
FoodCart
date food sold
date food sold
02/25/08 pizza 350
02/25/08 pizza 350
02/26/08 hotdog 500
02/26/08 hotdog 500
02/26/08 pizza 70
Database Analysis and
ICT-UNIVOTEC 15 Design - B.Tech
SQL: UPDATE Statement
To update the content of the table:
UPDATE statement syntax:
UPDATE <table name> SET <attr> = <value>
WHERE <selection condition>;
Example: UPDATE FoodCart SET sold = 349
WHERE date = ‟02/25/08‟ AND food = „pizza‟;
FoodCart
date food sold date food sold

02/25/08 pizza 350 02/25/08 pizza 349

02/26/08 hotdog 500 02/26/08 hotdog 500

02/26/08 pizza 70 02/26/08 pizza 70


Database Analysis and
ICT-UNIVOTEC 16 Design - B.Tech
SQL: DELETE Statement
To delete rows from the table:
DELETE statement syntax:
DELETE FROM <table name>
WHERE <condition>;
Example: DELETE FROM FoodCart
WHERE food = „hotdog‟;
FoodCart
date food sold date food sold
02/25/08 pizza 349 02/25/08 pizza 349
02/26/08 hotdog 500 02/26/08 pizza 70
02/26/08 pizza 70

Note: If the WHERE clause is omitted all rows of data are deleted from the table.
Database Analysis and
ICT-UNIVOTEC 17 Design - B.Tech
SQL Statements, Operations, Clauses
SQL Statements:
Select
SQL Operations:
Join
Left Join
Right Join
Like
SQL Clauses:
Order By
Group By
Having
Database Analysis and Design -
ICT-UNIVOTEC 18 B.Tech
SQL: SELECT Statement
A basic SELECT statement includes 3 clauses

SELECT <attribute name> FROM <tables> WHERE <condition>

SELECT FROM WHERE

Specifies the Specifies the Specifies the


attributes that tables that serve selection
are part of the as the input to condition,
resulting relation the statement including the join
condition.
Note: that you don't need to use WHERE

Database Analysis and


ICT-UNIVOTEC 19 Design - B.Tech
SQL: SELECT Statement (cont.)‫‏‬
Using a “*” in a select statement indicates that
every attribute of the input table is to be
selected.
Example: SELECT * FROM … WHERE …;

To get unique rows, type the keyword


DISTINCT after SELECT.
Example: SELECT DISTINCT * FROM …
WHERE …;

Database Analysis and


ICT-UNIVOTEC 20 Design - B.Tech
1) SELECT *
FROM person
Name Age Weight WHERE age > 30;
Harry 34 80 Name Age Weight
Sally 28 64 Harry 34 80
George 29 70 Helena 54 54
Helena 54 54 Peter 34 80
Peter 34 80
3) SELECT distinct
2) SELECT weight weight
FROM person FROM person
WHERE age > 30; WHERE age > 30;
Weight Weight
80 80
54 54
80 Database Analysis and Design - B.Tech 21
ICT-UNIVOTEC
A join can be specified in the FROM clause
which list the two input relations and the
WHERE clause which lists the join condition.
Example:

Emp Dept
ID State ID Division
1000 CA 1001 IT
1001 MA 1002 Sales
1002 TN 1003 Biotech
ICT-UNIVOTEC Database Analysis and Design - B.Tech 22
inner join = join
SELECT *
FROM emp join dept (or FROM emp, dept)‫‏‬
on emp.id = dept.id;

Emp.ID Emp.State Dept.ID Dept.Division


1001 MA 1001 IT
1002 TN 1002 Sales

Database
Analysis and
ICT-UNIVOTEC 23 Design - B.Tech
left outer join = left join
SELECT *
FROM emp left join dept
on emp.id = dept.id;

Emp.ID Emp.State Dept.ID Dept.Division


1000 CA null null
1001 MA 1001 IT
1002 TN 1002 Sales

Database
Analysis and
ICT-UNIVOTEC 24 Design - B.Tech
right outer join = right join
SELECT *
FROM emp right join dept
on emp.id = dept.id;

Emp.ID Emp.State Dept.ID Dept.Division


1001 MA 1001 IT
1002 TN 1002 Sales
null null 1003 Biotech

Database
Analysis and
ICT-UNIVOTEC 25 Design - B.Tech
 Join operations take two relations and
return as a result another relation.
 A join operation is a Cartesian product
which requires that tuples in the two
relations match (under some condition). It
also specifies the attributes that are
present in the result of the join
 The join operations are typically used as
subquery expressions in the from clause

ICT-UNIVOTEC Database Analysis and Design - B.Tech 26


 Relation course

 Relation prereq

 Observe that
prereq information is missing for CS-315 and
course information is missing for CS-437
ICT-UNIVOTEC Database Analysis and Design - B.Tech 27
 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.

ICT-UNIVOTEC Database Analysis and Design - B.Tech 28


 course natural left outer join prereq

ICT-UNIVOTEC Database Analysis and Design - B.Tech 29


 course natural right outer join prereq

ICT-UNIVOTEC Database Analysis and Design - B.Tech 30


 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, and what attributes are present in
the result of the join.
 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.

ICT-UNIVOTEC Database Analysis and Design - B.Tech 31


 course natural full outer join prereq

ICT-UNIVOTEC Database Analysis and Design - B.Tech 32


 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

ICT-UNIVOTEC Database Analysis and Design - B.Tech 33


 course natural right outer join prereq

 course full outer join prereq using


(course_id)

ICT-UNIVOTEC Database Analysis and Design - B.Tech 34


Pattern matching selection
% (arbitrary string)‫‏‬
SELECT *
FROM emp
WHERE ID like „%01‟;
 finds ID that ends with 01, e.g. 1001, 2001, etc
_ (a single character)‫‏‬
SELECT *
FROM emp
WHERE ID like „_01_‟;
 finds ID that has the second and third character
as 01, e.g. 1010, 1011, 1012, 1013, etc
ICT-UNIVOTEC
Database
Analysis and
35
Design - B.Tech
SQL: The ORDER BY Clause
Ordered result selection
desc (descending order)‫‏‬
SELECT *
FROM emp
order by state desc
 puts state in descending order, e.g. TN, MA, CA
asc (ascending order)‫‏‬
SELECT *
FROM emp
order by id asc
 puts ID in ascending order, e.g. 1001, 1002, 1003
Database Analysis and
ICT-UNIVOTEC 36 Design - B.Tech
SQL: The GROUP BY Clause
The function to divide the tuples into groups and
returns an aggregate for each group.
Usually, it is an aggregate function‟s companion
SELECT food, sum(sold) as totalSold
FROM FoodCart
group by food;
FoodCart
date food sold food totalSold
02/25/08 pizza 349 hotdog 500
02/26/08 hotdog 500 pizza 419
02/26/08 pizza 70
Database Analysis and
ICT-UNIVOTEC 37 Design - B.Tech
SQL: The HAVING Clause
The substitute of WHERE for aggregate functions
Usually, it is an aggregate function‟s companion
SELECT food, sum(sold) as totalSold
FROM FoodCart
group by food
having sum(sold) > 450;
FoodCart

date food sold food totalSold


02/25/08 pizza 349 hotdog 500
02/26/08 hotdog 500

02/26/08 pizza 70 Database Analysis and


ICT-UNIVOTEC 38 Design - B.Tech
SQL: Aggregate Functions
Are used to provide summarization information for
SQL statements, which return a single value.

COUNT(attr)‫‏‬
SUM(attr)‫‏‬
MAX(attr)‫‏‬
MIN(attr)‫‏‬
AVG(attr)‫‏‬

Note: when using aggregate functions, NULL values


are not considered, except in COUNT(*) .

Database Analysis and


ICT-UNIVOTEC 39 Design - B.Tech
SQL: Aggregate Functions (cont.)‫‏‬
FoodCart
date food sold

02/25/08 pizza 349

02/26/08 hotdog 500

02/26/08 pizza 70

COUNT(attr) -> return # of rows that are not null


Ex: COUNT(distinct food) from FoodCart; -> 2

SUM(attr) -> return the sum of values in the attr


Ex: SUM(sold) from FoodCart; -> 919

MAX(attr) -> return the highest value from the attr


Ex: MAX(sold) from FoodCart; -> 500 40
ICT-UNIVOTEC
Database Analysis and
Design - B.Tech
SQL: Aggregate Functions (cont.)‫‏‬
FoodCart
date food sold

02/25/08 pizza 349

02/26/08 hotdog 500

02/26/08 pizza 70

MIN(attr) -> return the lowest value from the attr


Ex: MIN(sold) from FoodCart; -> 70

AVG(attr) -> return the average value from the attr


Ex: AVG(sold) from FoodCart; -> 306.33
Note: value is rounded to the precision of the datatype
Database Analysis and
ICT-UNIVOTEC 41 Design - B.Tech
 These functions operate on the multiset of values of
a column of a relation, and return a value
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values

ICT-UNIVOTEC Database Analysis and Design - B.Tech 42


 Find the average salary of instructors in the Computer
Science department
◦ select avg (salary)
from instructor
where dept_name= ’Comp. Sci.’;
 Find the total number of instructors who teach a course in
the Spring 2010 semester
◦ select count (distinct ID)
from teaches
where semester = ’Spring’ and year = 2010
 Find the number of tuples in the course relation
◦ select count (*)
from course;

ICT-UNIVOTEC Database Analysis and Design - B.Tech 43


 Find the average salary of instructors in each department
◦ select dept_name, avg (salary)
from instructor
group by dept_name;
◦ Note: departments with no instructor will not appear in
result

ICT-UNIVOTEC Database Analysis and Design - B.Tech 44


 Attributes in select clause outside of aggregate functions
must appear in group by list
◦ /* erroneous query */
select dept_name, ID, avg (salary)
from instructor
group by dept_name;

ICT-UNIVOTEC Database Analysis and Design - B.Tech 45


 Find the names and average salaries of all departments
whose average salary is greater than 42000

select dept_name, avg (salary)


from instructor
group by dept_name
having avg (salary) > 42000;

Note: predicates in the having clause are applied after the


formation of groups whereas predicates in the where clause
are applied before forming groups

ICT-UNIVOTEC Database Analysis and Design - B.Tech 46


 Total all salaries
select sum (salary )
from instructor
◦ Above statement ignores null amounts
◦ Result is null if there is no non-null amount
 All aggregate operations except count(*) ignore tuples
with null values on the aggregated attributes
 What if collection has only null values?
◦ count returns 0
◦ all other aggregates return null

ICT-UNIVOTEC Database Analysis and Design - B.Tech 47


 SQL provides a mechanism for the nesting of subqueries.
 A subquery is a select-from-where expression that is
nested within another query.
 A common use of subqueries is to perform tests for set
membership, set comparisons, and set cardinality.

ICT-UNIVOTEC Database Analysis and Design - B.Tech 48


ICT-UNIVOTEC Database Analysis and Design - B.Tech 49
 Find courses offered in Fall 2009 and in Spring 2010

select distinct course_id


from section
where semester = ’Fall’ and year= 2009 and
course_id in (select course_id
from section
where semester = ’Spring’ and year= 2010);

 Find courses offered in Fall 2009 but not in Spring 2010


select distinct course_id
from section
where semester = ’Fall’ and year= 2009 and
course_id not in (select course_id
from section
where semester = ’Spring’ and year= 2010);

ICT-UNIVOTEC Database Analysis and Design - B.Tech 50


 Find the total number of (distinct) studentswho have taken
course sections taught by the instructor with ID 10101

select count (distinct ID)


from takes
where (course_id, sec_id, semester, year) in
(select course_id, sec_id, semester, year
from teaches
where teaches.ID= 10101);

 Note: Above query can be written in a much simpler manner.


The formulation above is simply to illustrate SQL features.

ICT-UNIVOTEC Database Analysis and Design - B.Tech 51


 Find names of instructors with salary greater than that of
some (at least one) instructor in the Biology department.

select distinct T.name


from instructor as T, instructor as S
where T.salary > S.salary and S.dept_name = ’Biology’;

 Same query using > some clause

select name
from instructor
where salary > some (select salary
from instructor
where dept_name = ’Biology’);

ICT-UNIVOTEC Database Analysis and Design - B.Tech 52


 Find the names of all instructors whose salary is greater
than the salary of all instructors in the Biology
department.
select name
from instructor
where salary > all (select salary
from instructor
where dept_name = ’Biology’);

ICT-UNIVOTEC Database Analysis and Design - B.Tech 53


 Yet another way of specifying the query “Find all courses
taught in both the Fall 2009 semester and in the Spring 2010
semester”
select course_id
from section as S
where semester = ’Fall’ and year= 2009 and
exists (select *
from section as T
where semester = ’Spring’ and year= 2010
and S.course_id= T.course_id);
 Correlated subquery
 Correlation name or correlation variable

ICT-UNIVOTEC Database Analysis and Design - B.Tech 54


 Find all students who have taken all courses offered in
the Biology department.
select distinct S.ID, S.name
from student as S
where not exists ( (select course_id
from course
where dept_name = ’Biology’)
except
(select T.course_id
from takes as T
where S.ID = T.ID));

ICT-UNIVOTEC Database Analysis and Design - B.Tech 55


 The unique construct tests whether a
subquery has any duplicate tuples in its
result.
◦ (Evaluates to “true” on an empty set)
 Find all courses that were offered at most
once in 2009
select T.course_id
from course as T
where unique (select R.course_id
from section as R
where T.course_id= R.course_id
and R.year = 2009);

ICT-UNIVOTEC Database Analysis and Design - B.Tech 56


 SQL allows a subquery expression to be used in the from clause
 Find the average instructors’ salaries of those departments where
the average salary is greater than $42,000.
select dept_name, avg_salary
from (select dept_name, avg (salary) as avg_salary
from instructor
group by dept_name)
where avg_salary > 42000;
 Note that we do not need to use the having clause
 Another way to write above query
select dept_name, avg_salary
from (select dept_name, avg (salary)
from instructor
group by dept_name)
as dept_avg (dept_name, avg_salary)
where avg_salary > 42000;

ICT-UNIVOTEC Database Analysis and Design - B.Tech 57


 And yet another way to write it: lateral clause
select name, salary, avg_salary
from instructor I1,
lateral (select avg(salary) as avg_salary
from instructor I2
where I2.dept_name= I1.dept_name);
 Lateral clause permits later part of the from
clause (after the lateral keyword) to access
correlation variables from the earlier part.
 Note: lateral is part of the SQL standard, but is
not supported on many database systems; some
databases such as SQL Server offer alternative
syntax

ICT-UNIVOTEC Database Analysis and Design - B.Tech 58


 The with clause provides a way of defining a temporary
view whose definition is available only to the query in
which the with clause occurs.
 Find all departments with the maximum budget

with max_budget (value) as


(select max(budget)
from department)
select budget
from department, max_budget
where department.budget = max_budget.value;

ICT-UNIVOTEC Database Analysis and Design - B.Tech 59


 With clause is very useful for writing complex queries
 Supported by most database systems, with minor syntax
variations
 Find all departments where the total salary is greater than
the average of the total salary at all departments

with dept _total (dept_name, value) as


(select dept_name, sum(salary)
from instructor
group by dept_name),
dept_total_avg(value) as
(select avg(value)
from dept_total)
select dept_name
from dept_total, dept_total_avg
where dept_total.value >= dept_total_avg.value;

ICT-UNIVOTEC Database Analysis and Design - B.Tech 60


 Scalar subquery is one which is used where a single value is
expected
 E.g. select dept_name,
(select count(*)
from instructor
where department.dept_name =
instructor.dept_name)
as num_instructors
from department;

 E.g. select name


from instructor
where salary * 10 >
(select budget from department
where department.dept_name =
instructor.dept_name)

 Runtime error if subquery returns more than one result tuple

ICT-UNIVOTEC Database Analysis and Design - B.Tech 61


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

ICT-UNIVOTEC Database Analysis and Design - B.Tech 62


 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’);

ICT-UNIVOTEC Database Analysis and Design - B.Tech 63


 Delete all instructors whose salary is less than the
average salary of instructors

delete from instructor


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

 Problem: as we delete tuples from deposit, 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)

ICT-UNIVOTEC Database Analysis and Design - B.Tech 64


 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);

ICT-UNIVOTEC Database Analysis and Design - B.Tech 65


 Add all instructors to the student relation with tot_creds set
to 0
insert into student
select ID, name, dept_name, 0
from instructor
 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 problems, if table1 did not have any primary
key defined.

ICT-UNIVOTEC Database Analysis and Design - B.Tech 66


 Increase salaries of instructors whose salary is over
$100,000 by 3%, and all others receive a 5% raise
◦ 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)

ICT-UNIVOTEC Database Analysis and Design - B.Tech 67


 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

ICT-UNIVOTEC Database Analysis and Design - B.Tech 68


 Recompute and update tot_creds value for all students
update student S
set tot_cred = ( select sum(credits)
from takes natural join course
where 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

ICT-UNIVOTEC Database Analysis and Design - B.Tech 69

You might also like