Unit-3 DBMS - SQL
Unit-3 DBMS - SQL
Unit-3 DBMS - SQL
TRUNCATE command
RENAME Command
DELETE command
TRUNCATE command
• old-name as new-name
GRANT Command
Syntax:
GRANT privilege_name
ON object_name
TO {user_name |PUBLIC}
13
privilege_name is the access right or privilege granted
to the user. Some of the access rights are ALL, and
SELECT
14
Example:
15
Syntax:
REVOKE privilege_name
ON object_name
FROM {user_name |PUBLIC}
Eample:
REVOKE SELECT ON employee FROM user1;
16
TCL (Transaction Control Language)
COMMIT command
Question: Find the names of sailors who have reserved boat number
103
Output:
SNAME
Dustin
Lubber
Horatio
19
Question: Find the names of sailors who have reserved a red or green
boat
20
IN
Query: SELECT *
FROM Sailors
WHERE age IN (15.0,33.2,45.7,63.5)
Output:
Question: Find all sailors whose age is not present in the list of
values(15.0,33.2,45.7,63.5)
Query: SELECT *
FROM Sailors
WHERE age NOT IN (15.0,33.2,45.7,63.5)
22
STRING operators
Example1
SELECT *
FROM Sailors
WHERE sname LIKE '_u%'
23
Output:
RATIN
SID SNAME AGE
G
22 Dustin 7 45
31 Lubber 8 55.5
Example2 58 Rusty 10 35
SELECT *
FROM Sailors
WHERE sname LIKE 'A_d_'
Output:
RATIN
SID SNAME AGE
G 24
SET operators
25
Example (union)
SELECT *
FROM Sailors
UNION
SELECT *
FROM Sailors1
26
Example (intersect)
SELECT *
FROM Sailors
INTERSECT
SELECT *
FROM Sailors1
27
Example (minus)
SELECT *
FROM Sailors
MINUS
SELECT *
FROM Sailors1
28
Nested Queries
Example:
SELECT S.sname
FROM Sailors S
WHERE S.sid IN
(SELECT R.sid FROM Reserves R WHERE R.bid=103)
Find the names of sailors who have reserved a blue boat
SELECT S.sname
FROM Sailors S
WHERE S.sid IN
(SELECT R.sid FROM Reserves R
WHERE R.bid IN
(SELECT B.bid FROM Boats B WHERE
B.color='blue'))
Correlated Nested Queries
33
SET-COMPARISION OPERATORS
34
Example
Find sailors whose rating is better than some sailor called
Horatio
SELECT S1.sname, S1.rating
FROM Sailors S1
WHERE S1.rating > ANY (SELECT S2.rating FROM
Sailors S2 WHERE S2.sname='Horatio' )
SNAME RATING
Rusty 10
Zorba 10
Horatio 9
Lubber 8
Andy 8 35
Find sailors whose rating is better than every sailor called
Horatio
SNAME RATING
Rusty 10
Zorba 10
36
AGGREGATE OPERATORS
In addition to simply retrieving data, we often want to
perform some computation or summarization
Examples:
Question: Find the average age of all sailors
Query:
SELECT AVG (age)
FROM Sailors
Output:
AVG(AGE)
36.9
38
Question: Find the name and age of the oldest sailor
Query:
SELECT S1.sname, S1.age
FROM Sailors S1
WHERE S1.age = ( SELECT MAX (S2.age) FROM Sailors
S2 )
Output:
SNAME AGE
Bob 63.5
Question: Count the number of sailors
Query:
SELECT COUNT (*)
FROM Sailors
Output: COUNT(*)
10 39
The GROUP BY and HAVING Clauses
general form:
SELECT [DISTINCT] fieldname
FROM table names
GROUP BY fieldname
HAVING group-condition
40
Examples
Question: Find the number of sailors belongs to each rating
level
Query:
SELECT rating, COUNT(rating)
FROM Sailors
GROUP BY rating RATING COUNT(RATING)
Output: 1 1
3 2
7 2
8 2
9 1
10 2 41
Question: Find the age of the youngest sailor for each rating
level
Query:
SELECT rating, MIN (age)
FROM Sailors
GROUP BY rating
Output:
RATING MIN(AGE)
1 33
3 25.5
7 35
8 25.5
9 35
10 16 42
Question: Find the age of the youngest sailor for each rating
level, which is greater than 7
Query:
SELECT rating, MIN(age)
FROM Sailors
GROUP BY rating
HAVING rating>7
Output:
RATING MIN(AGE)
8 25.5
9 35
10 16
43
ORDER BY
Query:
SELECT *
FROM Sailors
ORDER BY sname
44
Output:
RATIN
SID SNAME AGE
G
32 Andy 8 25.5
85 Art 3 25.5
95 Bob 3 63.5
29 Brutus 1 33
22 Dustin 7 45
64 Horatio 7 35
74 Horatio 9 35
31 Lubber 8 55.5
58 Rusty 10 35 45
Question: display the sailors table in the descending order of
sname
Query:
SELECT *
FROM Sailors
ORDER BY sname DESC
46
Output:
RATIN
SID SNAME AGE
G
71 Zorba 10 16
58 Rusty 10 35
31 Lubber 8 55.5
64 Horatio 7 35
74 Horatio 9 35
22 Dustin 7 45
29 Brutus 1 33
95 Bob 3 63.5
85 Art 3 25.5 47
Null Values
Column values in a row are sometimes
IS NULL
52
Example
Query:
SELECT *
FROM sailors
WHERE rating IS NULL
Output:
RATIN
SID SNAME AGE
G
98 Dan 39
53
Impact on SQL Constructs
Example
Query:
SELECT sid, rating, sid+rating
FROM Sailors
54
Output: SID RATING SID + RATING
22 7 29
29 1 30
31 8 39
32 8 40
58 10 68
64 7 71
71 10 81
74 9 83
85 3 88
95 3 98
98 - -
55
COUNT(*) handles null values just like other values,
that is, they get counted
Example
Query:
SELECT COUNT(*)
FROM Sailors
Output:
COUNT(*)
11
56
Disallowing Null Values
57