Chapter - 5
Chapter - 5
Department
DNo DName Loc
1 Admin Chennai
2 Research Bangalore
3 Accounts Bangalore
DDL
CREATE TABLE Department(
DNo number(3) not null,
DName varchar2(10) not null,
Loc varchar2(15),
primary key (DNo));
CREATE TABLE Employee(
SSN number(4) not null,
Name varchar2(20) not null,
BDate date,
Salary number(10,2),
MgrSSN number(4),
DNo number(2) not null,
primary key (SSN),
foreign key (MgrSSN) references Employee(SSN),
foreign key (DNo) references Department(DNo));
Data Retrieval Statement (SELECT)
Syntax
SELECT *|{[DISTINCT] column | expression}
FROM table(s);
Output-1
SSN NAME BDATE SALARY MGRSSN DNO
---- -------------------- --------- --------- --------- ---------
4444 Prasad 11-JAN-57 32000 3
5555 Reena 15-JAN-85 8000 4444 3
1111 Deepak 05-JAN-62 22000 4444 1
2222 Nandagopal 10-DEC-60 30000 4444 3
3333 Pooja 22-JAN-65 18000 2222 2
Example-2
SELECT * FROM Employee
ORDER BY SSN;
Output-2
SSN NAME BDATE SALARY MGRSSN DNO
----- -------------------- --------- --------- --------- ---------------------------------
1111 Deepak 05-JAN-62 22000 4444 1
2222 Nandagopal 10-DEC-60 30000 4444 3
3333 Pooja 22-JAN-65 18000 2222 2
4444 Prasad 11-JAN-57 32000 3
5555 Reena 15-JAN-85 8000 4444 3
Using arithmetic operators
SELECT Name, Salary, Salary * 12
FROM Employee;
Using aliases
An alias when used for a column:
Renames a column heading
It is useful in arithmetic calculations.
AS keyword can optionally be used between column
name and alias name.
Example-3
SELECT Name, Salary, Salary * 12 AS YRLY_SALARY
FROM Employee;
OR
SELECT Name, Salary, Salary * 12 "YRLY_SALARY"
FROM Employee;
Example-4
DESCRIBE Employee;
OR
DESC Employee;
Output-4
Name Null? Type
------------------------------- -------- ----
SSN NOT NULL NUMBER(4)
NAME NOT NULL VARCHAR2(20)
BDATE DATE
SALARY NUMBER(10,2)
MGRSSN NUMBER(4)
DNO NOT NULL NUMBER(2)
Select Statement with Where
Example-5
SELECT Name, Salary
FROM Employee
WHERE Salary > 25000;
Example-6
SELECT DName, Loc
FROM Department
WHERE Loc = 'Bangalore';
Example-7
SELECT Name, BDate
FROM Employee
WHERE BDate = '11-Jan-57';
Example-8
SELECT Name, BDate
FROM Employee
WHERE Salary BETWEEN 25000 AND 30000;
Example-9
SELECT SSN, Name
FROM Employee
WHERE DNo IN (1, 2);
Example-10
SELECT Name
FROM Employee
WHERE Name LIKE 'P%';
Example-11
SELECT Name, DNo
FROM Employee
WHERE BDate LIKE '__-JAN-__';
Example-12
SELECT Name
FROM Employee
WHERE MgrSSN IS NULL;
Example-13
SELECT Name, Salary, DNo
FROM Employee
WHERE Salary > 30000 AND DNo = 3;
Example-14
SELECT Name, Salary
FROM Employee
WHERE Name LIKE 'P%' OR Salary <= 20000;
Example-15
SELECT Name, Salary, DNo
FROM Employee
ORDER BY DNo DESC, Name;
SQL Functions
ROUND(column | expr, n) Rounds to n decimal places. If n is negative,
numbers to the left are rounded.
TRUNC(column | expr, n) Truncates to n decimal places.
MOD(m, n) Returns the remainder of m/n.
ABS(n) Absolute value of n.
CEIL(n) Smallest integer larger than n.
FLOOR(n) Largest integer smaller than n.
EXP(n) en
POWER(n, m) nm
SQRT(n) Square root of n.
SIGN(n) 1 if n is positive, -1 if negative, 0 if zero.
LN(n) Natural log of n (lg n)
LOG(n) log10 n
SIN(n) Sine of n.
COS(n) Cosine of n.
TAN(n) Tangent of n.
ASIN(n) Arc sine of n (in radians).
ACOS(n) Arc cosine of n (in radians).
ATAN(n) Arc tangent of n (in radians).
SINH(n) Hyperbolic sine value of n.
COSH(n) Hyperbolic cosine value of n.
TANH(n) Hyperbolic tan value of n.
NVL(n, m) Null Value – Substitute m for n if n = null.
VSIZE(n) Storage size of n.
Working with Dates
Century 19
Year 99
Month 07
SELECT SYSDATE
Day 23
FROM DUAL;
Hour 4
Minute 10
Second 53
Example-16 (MONTHS_BETWEEN)
SELECT MONTHS_BETWEEN(SYSDATE, '09-JAN-1983')
"Experience"
FROM DUAL;
Output-16
Experience
---------------
247.73471
Example-23
SELECT TO_DATE('08/30/2003', 'DD/MM/YYYY')
FROM DUAL;
Output-23
ERROR at line 1:
ORA-01843: not a valid month
Character Functions
Program Output
SELECT LOWER('Bangalore') bangalore
FROM DUAL;
Both '=' and 'IN' works, because the inner query produces a single
tuple.
Find the Name and Salary of people who draw in
the range Rs. 20,000 to Rs. 40,000.
Select Name, Salary from Employee
where Salary =
(Select Salary from Employee
where Salary between 20000 and 40000);
Error: ORA-01427: single-row subquery returns more than one row
Correct Query:
Select Name, Salary from Employee
where Salary IN
(Select Salary from Employee
where Salary between 20000 and 40000);
ANY and ALL
Operator Meaning Example
<ANY Less than the e < ANY (5,3,8): e is less than any
maximum. single item in the list (5,3,8). Even
7 qualifies, because 7 < 8.
>ANY More than e > ANY (5,3,8): e is less than any
the minimum. single item in the list (5,3,8). Even
4 qualifies, because 4 > 3.
=ANY Same as IN. e = ANY(5,3,8). All values in the
list qualify.
<ALL Less than the e < ALL (5,3,8): Anything below 3
maximum. qualifies.
>ALL More than e > ALL (5,3,8): Anything greater
the minimum. than 8 qualifies.
!=ALL Not equal to e != (5,3,8): Anything other than
anything. 5,3, and 8 qualifies.
Example-34:
SELECT Name, Salary
FROM Employee
WHERE Salary < ANY
(SELECT Salary
FROM Employee
WHERE DNo = 3);
Example-35:
SELECT Name, Salary
FROM Employee
WHERE Salary > ANY
(SELECT Salary
FROM Employee
WHERE DNo = 3);
Example-35:
SELECT Name, Salary
FROM Employee
WHERE Salary < ALL
(SELECT Salary
FROM Employee
WHERE DNo = 3);
Example-36:
SELECT Name, Salary
FROM Employee
WHERE Salary > ALL
(SELECT Salary
FROM Employee
WHERE DNo = 3);
>ALL means greater than the greatest and <ALL means
less than the lowest value.
CREATING and ALTERING
DATABASE OBJECTS
Table: A tabular structure that stores data.
View: A tabular structure similar to a table
but it is a collection of one or more tables.
Sequence: Automatically generates a
sequence of numbers.
Index: Provides an efficient access
structure.
CREATE TABLE Employee(
SSN Number(4) not null,
Name Varchar2(20) not null,
BDate Date,
Salary Number(10,2),
MgrSSN Number(4),
DNo Number(2) not null, Primary Key (SSN),
Foreign Key (MgrSSN) references Employee(SSN),
Foreign Key (DNo) references Department(DNo));
Names of the tables/views
SELECT *
FROM TAB;
Schema details of a table
DESC Employee;
View created.
Restrictions on Views
Dropping a sequence
DROP SEQUENCE Dept_Seq;
Creating an index for Employee table on Name
CREATE INDEX IDXSSN ON Employee (Name);
Dropping an index
DROP INDEX IDXSSN;
Rights
SQL Server 2000
GRANT
{ ALL [ PRIVILEGES ] | permission [ ,...n ] }
{
[ ( column [ ,...n ] ) ] ON { table | view }
| ON { table | view } [ ( column [ ,...n ] ) ]
| ON { stored_procedure | extended_procedure }
| ON { user_defined_function }
}
TO security_account [ ,...n ]
[ WITH GRANT OPTION ]
[ AS { group | role } ]
Rights (Contd…)
Oracle 9i:
Inserting dates
INSERT INTO Employee
VALUES (6666, 'John', TO_DATE('5-Jan-2003 3:40',
'DD-MM-YYYY HH24:SS'), 22000, 4444, 1);
Inserting rows from an existing table
INSERT INTO EMP2
SELECT *
FROM Employee;
DELETE Statement
DELETE Employee
WHERE SSN = 1111;
UPDATE Statement
UPDATE Employee
SET DNo = 1
WHERE Name = 'Nandagopal';
To hike the salary of all employees by 10%.
UPDATE Employee
SET Salary = Salary * 1.05;
ADDITIONAL EXAMPLES
Company Database Example
Employee(SSN Char(9), Name Varchar2(20), Bdate Date, Address
Varchar2(30), Sex Char(1),Salary Number(10,2),
SuperSSN Char(9), DNo Number(2));
SELECT FName
FROM Faculty
WHERE SUBSTR(FName,1,1) LIKE 'P'
AND SUBSTR(FName,-2,1) LIKE 'A';
More Examples
Book Dealer Database
AUTHOR (Authorid : Int, Name : String, City : String,
Country : String)
PUBLISHER (Publisherid : Int, Name : String, City : String,
Country : String)
CATALOG (Bookid : Int, Title : String, Authorid : Int,
Publisherid : Int, Categoryid : Int, ear : Int, Price : Int)
CATEGORY (Categorid : Int, Description : String)
ORDER_DETAILS (OrderNo : Int, Bookid : Int, Quantity : Int)
Queries
Give the details of the authors who have 2 or more books
in the catalog and the price of the books is greater than
the average price of the books in the catalog and the
year of publication is after 2000.
Query
select C.Authorid, A.AName
from Catalog C, Author A
where A.Authorid = C.Authorid and C.Year > 2000 and C.Price >
(Select Avg(Price) from Catalog)
group by C.Authorid, A.AName
having count(C.Authorid) >= 2;
Find the number of the book which has
maximum sales.