Structured Query Language
Structured Query Language
Overview of SQL
DML
DDL
Triggers and integrity constraints
Embedded and Dynamic SQL
Security
Basic SQL Commands
Statement Description
SELECT Data retrieval statement.
INSERT Data Manipulation Language
UPDATE (DML).
DELETE Add rows, change data, and delete
few rows.
CREATE Create new tables/views, remove
ALTER tables/ views, and change the
DROP schema.
RENAME
COMMIT Modified values of database are
ROLLBACK permanently written into disk,
SAVEPOINT rollback the changes made.
GRANT Access control can be assigned or
REVOKE changed.
Example tables
Employee
SSN Name BDate Salary MgrSSN DNo
1111 Deepak 5-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 Null 3
5555 Reena 15-Jan-85 8000 4444 3
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,
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
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.
VSIZE(n) Storage size of n.
Working with Dates
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;
COUNT
AVG
MAX
MIN
STDDEV
SUM
VARIANCE
Example-24
SELECT COUNT(*) AS "No. of Employees"
FROM Employee;
Example-25
SELECT SUM(Salary) AS Total
FROM Employee;
Example-26
SELECT Name, MAX(Salary), MIN(Salary)
FROM Employee;
GROUP BY Clause
The rules to be followed while using GROUP BY
clause are given below:
You can't have non-group function or column in
SELECT clause.
Group functions ignore nulls.
By default the result of GROUP BY clause sort the
data in ascending order.
Example:
SELECT DNo, SUM(Salary), COUNT(*), AVG(Salary)
FROM Employee
GROUP BY DNo;
Example-27
SELECT DNo, SUM(Salary), COUNT(*),
AVG(Salary)
FROM Employee
GROUP BY DNo, MgrSSN;
HAVING clause
SELECT DNo, AVG(Salary)
FROM Employee
GROUP BY DNo
HAVING DNo = 3;
The order of evaluation when all the clauses are
specified is given below:
1. First all rows matching the WHERE conditions
are retrieved.
2. These rows are grouped using the column(s) in
GROUP BY clause.
3. Finally, groups matching the HAVING clause
condition are retained.
SELECT DNo, AVG(Salary)
FROM Employee
WHERE BDate LIKE '__-JAN-__'
GROUP BY DNo
HAVING MAX(Salary) > 10000;
MULTITABLE QUERIES
(SELECT Salary
FROM
Employee
WHERE SSN = 1111);
SSN Name BDate Salary MgrSSN DNo
1111 Deepak 5-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 Null 3
Example-33 5555 Reena 15-Jan-85 8000 4444 3
Display all the employees
drawing more than or equal
to the average salary of
DNo DName Loc
department number 3. 1 Admin Chennai
SELECT Name 2 Research Bangalore
FROM Employee 3 Accounts Bangalore
View created.
Restrictions on Views
Dropping a sequence
DROP SEQUENCE Dept_Seq;
DML Statements
INSERT Statement
INSERT INTO Employee
VALUES (1111, 'Deeapk', '5-Jan-62', 22000, 4444, 1);
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;
Thank You
ADDITIONAL EXAMPLES
Company Database Example
Employee(SSN , Name , Bdate, Address ,Sex,Salary,
SuperSSN ,DNo);
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.