CSU 07314 Lecture 2-3
CSU 07314 Lecture 2-3
ITU07314
DATABASE MANAGEMENY
SYSTEMS
[DBMS]
2 Logistics
Instructor: Siphy, A. S.
email: [email protected]/
[email protected]
04/11/20
Intro to SQL,
22
3 OVERVIEW
SQL DML
INSERT(
DELETE
UPDATE
SQL SELECT(BASIC)
DISTINCT CLAUSE
WHERE CLAUSE
ORDER BY CLAUSE
DATE FUNCTION, & AGGREGATE
Intro to SQL,
FIELD
4 DML [INSERT, UPDATE,
DELETE] UPDATE and DELETE use
‘WHERE clauses’ to specify
INSERT - add a row to which rows to change or
a table remove
BE CAREFUL with these - an
incorrect WHERE clause can
UPDATE - change destroy lots of data
row(s) in a table
DELETE - remove
row(s) from a table
Intro to SQL,
Populate relational tables
5 To create a tuple in SQL the following ‘Insert’ command is required:
UPDATE Emp
SET sal = sal *0.15 If the where clause is omitted then all
WHERE deptno = 10; tuples in the relation will be altered!
Intro to SQL,
8 UPDATE -Examples
Student
ID Name Year
UPDATE Student 1 John 1
SET Year = 1, 2 Mark 3
Student Name = ‘Jane’ 3 Anne 2
4 Jane 1
ID Name Year WHERE ID = 4
1 John 1
2 Mark 3
3 Anne 2 Student
4 Mary 2 ID Name Year
UPDATE Student 1 John 2
SET Year = Year + 1 2 Mark 4
3 Anne 3
4 Mary 3
Intro to SQL,
SQL - Delete statement
9
To delete a set of tuples in SQL the following ‘Delete’ command is used:
DELETE FROM R
[where condition-expression]
Intro to SQL,
10 DELETE -Example
Student
Intro to SQL,
SQL - Group Insert statement-copy
11
To create a set of tuples in SQL the following ‘Insert’
command can be used:
INSERT INTO R (attribute1, attribute2, … attributen )
SELECT (attribute1, attribute2, … attributen)
FROM relation1, relation2, … relationn
[WHERE condition-expression]
[GROUP BY attribute1, attribute2, … attributen ]
[HAVING condition-expression]
[ORDER BY attribute1, attribute2, … attributen ]
Restriction on
where conditions 2
Compute
Sort on aggregates Restriction
Group
By?
Group BY and reduce on HAVING
Yes columns each group conditions
to 1 row
No 3 5
4
Order By?
Sort
Yes
No columns in 6
ORDER BY
Project columns 7
in SELECT
Finish
Retrieval Queries in SQL..
Basic form of the SQL SELECT statement is called a
mapping or a SELECT-FROM-WHERE block ( 3
clauses)
Intro to SQL,
23 Retrieval Queries in SQL…
Selecting Specific Columns
Specify the column names to be
displayed in the result set by typing the
exact, complete column names.
Separate each column name with a
comma (,).
Specify the name of the table after the
FROM clause.
Terminate the query with a semi-colon (;).
26
27 Queries-Special Operators
Often a subquery Options
will return a set of
values rather than a IN - checks to see if
single value a value is in the set
You can’t directly EXISTS - checks to
compare a single
value to a set see if the set is
empty or not
ALL/ANY - checks to
see if a relationship
holds for every/one
member of the set
08-Nov-
2016
SQL, More Select statements
28 (NOT) IN
Using IN we can see if a SELECT
given value is in a set of <columns>
values FROM <tables>
NOT IN checks to see if WHERE <value>
a given value is not in IN <set>
the set SELECT <columns>
The set can be given FROM <tables>
explicitly or from a WHERE <value>
subquery NOT IN <set>
08-Nov-
2016
SQL, More Select statements
29 (NOT) IN
SELECT *
FROM Employee
WHERE Department
Employee IN(‘Marketing’,‘Sales’)
Name Department Manager
John Marketing Chris
Mary Marketing Chris Name Department Manager
Chris Marketing Jane John Marketing Chris
Peter Sales Jane Mary Marketing Chris
Jane Management Chris Marketing Jane
Peter Sales Jane
08-Nov-
2016
SQL, More Select statements
30 (NOT) IN
Employee SELECT *
Name Department Manager FROM Employee
John Marketing Chris WHERE Name NOT IN
Mary Marketing Chris (SELECT Manager
Chris Marketing Jane FROM
Peter Sales Jane Employee);
Jane Management
08-Nov-
2016
SQL, More Select statements
31 (NOT) IN
First the This gives
subquery
SELECT Manager SELECT *
FROM Employee FROM Employee
is evaluated WHERE Name NOT IN
(‘Chris’,‘Jane’)
givingManager
Chris
Chris
Jane Name Department Manager
Jane
John Marketing Chris
Mary Marketing Chris
Peter Sales Jane
08-Nov-
2016
SQL, More Select statements
32 (NOT) EXISTS
08-Nov-
2016
SQL, More Select statements
34 ANY and ALL
ANY and ALL val = ANY (set)
compare a single is true if there is at
value to a set of least one member
values of the set equal to
They are used with the value
comparison val = ALL (set)
operators like is true if all
=, >, <, <>, >=, <= members of the set
are equal to the
value
08-Nov-
2016
SQL, More Select statements
35 ANY
Find the names of the employee(s)
who earn the highest salary
Employee
Name Salary SELECT Name
Mary 20,000 FROM Employee
John 15,000 WHERE Salary >=
Jane 25,000 ALL (
Paul 30,000
SELECT Salary
FROM Employee)
08-Nov-
2016
SQL, More Select statements
36 ANY
Find the names of
employee(s) who earn
more than someone else
Employee
Name Salary
SELECT Name
Mary 20,000
John 15,000 FROM Employee
Jane 25,000 WHERE Salary >
Paul 30,000
ANY (
Haule 25,000
SELECT Salary
FROM Employee)
08-Nov-
2016
SQL, More Select statements
37 Special operators…
BETWEEN
Used to check whether attribute
value is within a range
IS NULL
Used to check whether attribute
value is null
Usual mathematical operators
+-*/^
Intro to SQL,
38 SQL CONCATENATE
Sometimes it is necessary to
combine together (concatenate)
the results from several different
fields.
Each database provides a way to
do……
MSQL : CONCAT()
Oracle: CONCAT(), ||
SQL Sever: +
08-Nov-
SQL, More Select statements
2016
Aliases
39
Two forms:
Aliases rename
Column alias
columns or
SELECT column
tables to
AS newName...
Make names
Table alias
more meaningful
SELECT ...
Make names
shorter and FROM table
easier to type AS newName
Resolve
ambiguous
names This ‘AS’ is optional, but Oracle
doesn’t accept it at all 08-Nov-
2016
SQL, More Select statements
Queries- Example:
1) SELECT *
Person 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
NEXT_DAY(date,dayofweek)
Gives the date of the next ‘dayofweek’ after the date given.
LAST_DAY(date)
Gives the last day of the month in the date specified.
ADD_MONTHS (date,int)
Adds int months to the given date.
08-Nov-
SQL, More Select statements
2016
48 Grouping clause
The GROUP BY clause allows you to form
groups based on the specified condition.
The syntax for using this command is
08-Nov-
SQL, More Select statements
2016
50 Having clause
08-Nov-
SQL, More Select statements
2016
EX-1:Find age of the youngest sailor
with age 18, for each rating with at
least 2 such sailors
Sailors instance:
SELECT S.rating, MIN (S.age)
AS minage sid sname rating age
FROM Sailors S 22 dustin 7 45.0
WHERE S.age >= 18
29 brutus 1 33.0
GROUP BY S.rating
HAVING COUNT (*) > 1 31 lubber 8 55.5
32 andy 8 25.5
58 rusty 10 35.0
rating minage
64 horatio 7 35.0
3 25.5
71 zorba 10 16.0
7 35.0
Answer relation: 74 horatio 9 35.0
8 25.5
85 art 3 25.5
95 bob 3 63.5
96 frodo 3 25.5
Find age of the youngest sailor with age
18, for each rating with at least 2 such
sailors. rating age
rating age
1 33.0
7 45.0
3 25.5
1 33.0
3 63.5 rating minage
8 55.5
3 25.5 3 25.5
8 25.5
7 45.0 7 35.0
10 35.0
7 35.0 8 25.5
7 35.0
10 16.0 8 55.5
9 35.0 8 25.5
3 25.5 9 35.0
3 63.5 10 35.0
3 25.5
EX-2: Find age of the youngest sailor with age 18,
for each rating with at least 2 such sailors and with
every sailor under 60.
HAVING COUNT (*) > 1 AND EVERY (S. age <=60)
rating age
rating age
7 45.0 1 33.0
1 33.0 3 25.5
rating minage
8 55.5 3 63.5
8 25.5 7 35.0
3 25.5
10 35.0 8 25.5
7 45.0
7 35.0
10 16.0
7 35.0
9 35.0 8 55.5 What is the result of
3 25.5 8 25.5 changing EVERY to
3 63.5 9 35.0 ANY?
3 25.5 10 35.0
EX-3: Find age of the youngest sailor
with
age 18, for each rating with at
least 2 sailors between 18 and 60.
Sailors instance:
SELECT S.rating, MIN (S.age) AS minage
FROM Sailors S sid sname rating age
WHERE S.age >= 18 AND S.age <= 60 22 dustin 7 45.0
GROUP BY S.rating
29 brutus 1 33.0
HAVING COUNT (*) > 1
31 lubber 8 55.5
32 andy 8 25.5
58 rusty 10 35.0
64 horatio 7 35.0
rating minage 71 zorba 10 16.0
Answer relation: 3 25.5 74 horatio 9 35.0
7 35.0 85 art 3 25.5
8 25.5 95 bob 3 63.5
96 frodo 3 25.5
56 Examples
select count(*) from Grade; refer Grade table
select max(mark) from Grade;
select min(mark) from Grade;
select avg(mark) from Grade;
select count (*), code from Grade
group by code;
For more examples visit
www.w3schools.com/sql
08-Nov-
SQL, More Select statements
2016