SQL Data Manipulation: Prepared By: Dr. Vipul Vekariya
SQL Data Manipulation: Prepared By: Dr. Vipul Vekariya
DATA MANIPULATION
CHAR
NUMERIC
VARCHAR/VARCHAR 2
DATE
CHAR(size)
EXAMPLE
Name CHAR(60);
VARCHAR(size)/VARCHAR2(size)
Used to stored Variable Length string
EXAMPLE:
Name VARCHAR2(20);
DATE
Used to represent Date and Time
Standard format is dd-mon-yy(21-JUL-08)
To Enter other format special function is used.
Default Date is 1st Day of Current Month.
Example:
Rupee NUMBER(7,2).
Rules of SQL
SQL Statements are start with Verb.( example SELECT)
Each verb followed by number of clause. Example(FROM,
WHERE).
A space separates clause like DROP TABLE EMP;
SQL Statements are end with Semicolon(;).
SQL Parameters are separated by Comma(,).
Statement may be split across lines but keyword may not.
Reserved verb can not used as Identifier.
Identifier must start with Alphabet & should not more than
30 characters.
Comments may be enclosed between
/* */ symbol.
Basic Structure of SQL
Consists of three clauses:
(i) Select
- Used to list the attributes desired in the result of a
query.
(ii) From
- Lists the relations to be scanned in the evaluation
of the expression.
(iii) Where
- Consists of a predicate involving attributes of the
relations that appear in the from clause.
CREATE Clause
Used to Create Table in Database.
Syntax:
CREATE TABLE <table_name>(<Colname1> <DataType> (<Size>),
(<Colname1> <DataType> (<Size>));
Example:
CREATE TABLE Student_Master
(Roll_No NUMBER(10),
Name VARCHAR2(50),marks number(5,2));
Rules For Creating Table
Syntax:
INSERT INTO <tablename> (colname1,colname2)
VALUES (<expression1>,<expression2>);
Example:
INSERT INTO Student_Master (Rollno,Name)
VALUES ( 123, ‘XYZ’);
SELECT Clause
Used to View or Retrieve Data of Table.
Syntax:
SELECT <col1> ,<col2>,…<coln> FROM <Tablename>;
Example:
SELECT Rollno,Name FROM Student_Master;
Filtering of Data
Student_Master
ROLLNO NAME
01 XYZ
02 PQR
03 TUV
Selected Column All Rows
Retrieve only Specific column from Table.
Syntax:
SELECT <col1>,<col2> FROM <tablename>;
Example:
SELECT Rollno From Student_Master;
Rollno
01
02
03
Selected Row All Columns
Example:
◦ SELECT * FROM Student_Master
WHERE Rollno = 01;
Example:
SELECT * FROM Student_Master
ORDER BY Rollno DESC; Rollno Name
03 STU
02 PQR
01 XYZ
DISTINCT Clause
Used with Select clause to eliminate duplication of
row.
Syntax:
SELECT DISTINCT <colname> FROM <tablename>;
Example:
SELECT DISTINCT Occupation
FROM Customer_Master;
DELETE CLASUE
Example:
ROLL_NO NAME
ROLL_NO NAME
1 X
1 X
2 Y
2 Y
3 Z
UPDATE CLAUSE
Two Methods:
All rows Update at a Time
Selected rows Update at a Time
Update of All Rows
Syntax:
UPDATE <TABLE_NAME>
SET <COL_NAME> = <EXPERSSION>;
Example:
UPDATE SALARY
SET AMOUNT = 25000;
NAME AMOUNT NAME AMOUNT
X 5000 X 25000
Y 6000 Y 25000
Z 7000 Z 25000
W 8000 W 25000
UPDATE OF SELECTED ROWS
SYNTAX:
UPDATE <TABLE_NAME>
SET <COL_NAME> = <EXPRESSION>
WHERE <CONDITION>;
EXAMPLE:
UPDATE SALARY SET AMOUNT = 5000
WHERE NAME = ‘X’;
NAME AMOUNT NAME AMOUNT
X 2500 X 5000
Y 2500 Y 2500
Z 2500 Z 2500
ALTER TABLE CLAUSE
Used to modify the structure of Table.
Used to
EXAMPLE:
SYNTAX:
RENAME <TABLE_NAME> TO <NEW_TABLE_NAME>;
EXAMPLE:
RENAME STAFF_MASTER TO FACULTY_MASTER;
TRUNCATE TABLE CLAUSE
Used to Empty Table completely.
SYNTAX:
TRUNCATE TABLE <TABLE_NAME>;
EXAMPLE:
TRUNCATE TABLE STUDENT_MASTER;
DROP TABLE CLAUSE
SYNTAX:
DROP TABLE <Table_Name>;
EXAMPLE:
DROP TABLE Loan_Transaction;
TRUNCATE TABLE DELETE
CLAUSE CLAUSE
SYNTAX:
DESCRIBE <Table_Name>;
Search Conditions
The Comparison Test
Syntax:
SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE SALARY BETWEEN 30000 AND 50000;
SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE SALARY NOT BETWEEN 30000 AND 50000;
Set Membership Test
Test Whether a data value matches one of a list of target values.
IN Operator is used
Syntax:
Test-Exp. IN (constant)
Test-Exp. NOT IN (constant)
Example:
SELECT * FROM STUDENT_MASTER
WHERE ROLL_NO IN (11,12,13);
• SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE POSITION NOT IN ('Manager', 'Staff');
Compound Condition
AND, OR and NOT Operators are used
The AND operator joins two or more conditions, and displays a row only if that row's
data satisfies ALL conditions listed (i.e. all conditions hold true). For example, to display
all staff making over $40,000, use:
Example:
SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE SALARY > 40000 AND POSITION = 'Staff';
OR Operator
The OR operator joins two or more conditions, but returns a row if ANY of the
conditions listed hold true. To see all those who make less than $40,000 or have less than
$10,000 in benefits, listed together, use the following query:
Example:
SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE SALARY < 40000 OR BENEFITS < 10000;
SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE POSITION = 'Manager' AND SALARY > 60000 OR BENEFITS > 12000;
Null Value Test
Used to check whether null values are present in the column
or not.
IS NULL operator is Used.
Syntax:
WHERE Column-name IS NULL
WHERE Column-name IS NOT NULL
Example:
SELECT NAME FROM SALES
WHERE SALE IS NOT NULL
The Patten Matching Test
Used to compare one string value to another string value.
Syntax:
Column-name LIKE ‘patternstring %’;
Example:
SELECT EMPLOYEEIDNO
FROM EMPLOYEEADDRESSTABLE
WHERE LASTNAME LIKE 'S%';
LIKE with _
Used to match a Single Character.
Syntax:
Column-name LIKE ‘character _’;
Example:
Select Name from StudentMaster
Where Name LIKE ‘ _a %‘;
Example
‘computer%’ – matches any string begin with computer
‘%engg’ – matches any string containing ‘engg’ as substring
‘_s%’ – matches any string with second character ‘s’
‘_ _ _’ – matches any string with exactly three characters
‘_ _ _ %’ – matches any string of at least three character