Csu 07314 Lecture 2-4 SQL DML & Select & Aggregate Fields
Csu 07314 Lecture 2-4 SQL DML & Select & Aggregate Fields
DATABASE MANAGEMENY
SYSTEMS
[DBMS]
2 Logistics
Instructor: Siphy, A. S.
email: [email protected]/
[email protected]
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 will Options
return a set of values
rather than a single value IN - checks to see if a
You can’t directly value is in the set
compare a single value to EXISTS - checks to see
a set
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 a WHERE <value>
given value is not in the set IN <set>
The set can be given SELECT <columns>
explicitly or from a subquery FROM <tables>
WHERE <value>
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
This gives
First the subquery
SELECT *
SELECT Manager
FROM Employee
FROM Employee
Manager WHERE Name NOT IN
Chrisis evaluated giving (‘Chris’,‘Jane’)
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
33 (NOT) EXISTS
SELECT *
FROM Employee E1
Employee WHERE EXISTS (
Name Department Manager SELECT * FROM
John Marketing Chris Employee E2
Mary Marketing Chris WHERE E2.Name =
Chris Marketing Jane E1.Manager)
Peter Sales Jane
Jane Management Name Department Manager
Chris Marketing Jane
Jane Management
08-Nov-
2016
SQL, More Select statements
34 ANY and ALL
ANY and ALL compare val = ANY (set) is
a single value to a set of true if there is at least
values one member of the set
They are used with equal to the value
comparison operators like val = ALL (set) 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
08-Nov-
SQL, More Select statements
2016
Aliases
39
Two forms:
Aliases rename
Column alias
columns or tables to
SELECT column
Make names more
AS newName...
meaningful
Table alias
Make names shorter
SELECT ...
and easier to type
FROM table
Resolve ambiguous
names AS newName
Intro to SQL,
42 Date functions
Commonly used date functions are:
Last_day
Gives the last day of the month. This is useful for
knowing when payment might be expected.
MONTHS_BETWEEN
Least
Which of the string of dates is the earliest.
Greatest
Which of the string of dates is the latest.
Intro to SQL, 01/11/16
43
Meaning…
TO_CHAR(date,format)
Converts the date to the specified format.
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
49 Grouping
Sometimes we need to have sub-
groups.
This allows us to get subtotals.
This is done by using the ‘GROUP
BY’ clause.
08-Nov-
SQL, More Select statements
2016
50 Having clause
08-Nov-
SQL, More Select statements
2016
51 Max() , Min() and Avg()
MAX(A) is used to find the maximum value in a
column
MIN(A) is used for finding the minimum value in a
column.
AVG(A) is used to find the average of the values in a
column. It calculates the average (mean) of all
selected values of a given field.
08-Nov-
SQL, More Select statements
2016
EX-1:Find age of the youngest sailor with age
18, foreach 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
7 45.0 1 33.0
1 33.0 3 25.5
8 55.5 3 63.5 rating minage
8 25.5 3 25.5 3 25.5
10 35.0 7 45.0 7 35.0
7 35.0 7 35.0 8 25.5
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, foreach
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, foreach 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