Unit 3 SQL and PL-SQL
Unit 3 SQL and PL-SQL
Datatype Description
date It is used to store the year,
month, and days value.
time It is used to store the hour,
minute, and second values.
timestamp It stores the year, month,
day, hour, minute, and the
second value.
SQL Commands
• SQL commands are instructions. It is used to
communicate with the database. It is also
used to perform specific tasks, functions, and
queries of data.
• SQL can perform various tasks like create a
table, add data to tables, drop the table,
modify the table, set permission for users.
Types of SQL Commands
• There are five types of SQL commands: DDL,
DML, DCL, TCL, and DQL.
1. Data Definition Language (DDL)
• Create table
• Drop table
• Delete table
• Rename table
SQL Create Table
SQL create table is used to create a table in the database. To define the table,
you should define the name of the table and also define its columns and
column's data type.
Syntax
create table "table_name"
("column1" "data type",
"column2" "data type",
"column3" "data type",
...
"columnN" "data type");
Example
SQL> CREATE TABLE EMPLOYEE (
EMP_ID INT NOT NULL,
EMP_NAME VARCHAR (25) NOT NULL,
PHONE_NO INT NOT NULL,
ADDRESS CHAR (30),
PRIMARY KEY (ID)
);
• If you create the table successfully, you can
verify the table by looking at the message by
the SQL server. Else you can use DESC
command as follows:
• SQL> DESC EMPLOYEE;
Field Type Null Key Default Extra
EMP_ID int(11) NO PRI NULL
• If you don't specify the WHERE condition, it will remove all the rows from the
table.
• DELETE FROM EMPLOYEE;
• Now, the EMPLOYEE table would not have any records.
SQL SELECT Statement
• The SQL DELETE statement is used to delete rows from a table. Generally,
DELETE statement removes one or more records form a table.
• Syntax
• DELETE FROM table_name WHERE some_condition;
Deleting Single Record
Delete the row from the table EMPLOYEE where EMP_NAME
= 'Kristen'. This will delete only the fourth row.
Query
DELETE FROM EMPLOYEE
WHERE EMP_NAME = 'Kristen';
Output: After executing this query, the EMPLOYEE table will
look like:
Deleting Multiple Record
Delete the row from the EMPLOYEE table where AGE is
30. This will delete two rows(first and third row).
Query
DELETE FROM EMPLOYEE WHERE AGE= 30;
Output: After executing this query, the EMPLOYEE table
will look like:
NAME ADDRESS
Stephan Delhi
Kathrin Noida
David Ghaziabad
3. Creating View from multiple tables
View from multiple tables can be created by simply
include multiple tables in the SELECT statement.
In the given example, a view is created named
MarksView from two tables Student_Detail and
Student_Marks.
Query:
CREATE VIEW MarksView AS
SELECT Student_Detail.NAME, Student_Detail.ADDRESS,
Student_Marks.MARKS
FROM Student_Detail, Student_Mark
WHERE Student_Detail.NAME = Student_Marks.NAME;
• To display data of View MarksView:
• SELECT * FROM MarksView;
Kathrin Noida 86
David Ghaziabad 74
Alina Gurugram 90
4. Deleting View
1 John 20 US 2000.00
4 Alina 29 UK 6500.00
4 Alina 29 UK 1625.00
1 John 20 US 2000.00
Item2 Com2 3 25 75
Item3 Com1 2 30 60
1. Example: COUNT() Item4 Com3 5 10 50
Item8 Com1 3 10 30
10
Item9 Com2 2 25 50
2. Example: COUNT with WHERE
Item10 Com3 4 30 120
SELECT COUNT(*)
FROM PRODUCT_MAST;
WHERE RATE>=20;
Output:
7
2. SUM Function
• Sum function is used to calculate the sum of all
selected columns. It works on numeric fields only.
Syntax
SUM()
Example: SUM()
SELECT SUM(COST)
FROM PRODUCT_MAST;
Output:
670
Example: SUM() with WHERE
SELECT SUM(COST)
FROM PRODUCT_MAST
WHERE QTY>3;
Output:
320
3. AVG function
Item2 Com2 3 25 75
Example: Item3 Com1 2 30 60
Item5 Com2 2 20 40
FROM PRODUCT_MAST; Item6 Cpm1 3 25 75
30 Item8 Com1 3 10 30
Item9 Com2 2 25 50
Item2 Com2 3 25 75
Example: Item3 Com1 2 30 60
SELECT MIN(RATE) Item4 Com3 5 10 50
Item9 Com2 2 25 50
101 1 Testing
PROJECT 102 2 Development
103 3 Designing
104 4 Development
• 1. INNER JOIN
• In SQL, INNER JOIN selects records that have
matching values in both tables as long as the
condition is satisfied. It returns the
combination of all rows from both the tables
where the condition satisfies.
Syntax
SELECT table1.column1, table1.column2, table2.column1,....
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;
Query
SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT
FROM EMPLOYEE
INNER JOIN PROJECT
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
2. LEFT JOIN
• The SQL left join returns all the values from left table
and the matching values from the right table. If there is
no matching join value, it will return NULL.
• Syntax
SELECT table1.column1, table1.column2, table2.column1,
....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
• Query
SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT
FROM EMPLOYEE
LEFT JOIN PROJECT
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
Output
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL
3. RIGHT JOIN
• In SQL, RIGHT JOIN returns all the values from
the values from the rows of right table and the
matched values from the left table. If there is
no matching in both tables, it will return NULL.
• Syntax
• SELECT table1.column1, table1.column2, table2.column1,....
• FROM table1
• RIGHT JOIN table2
• ON table1.matching_column = table2.matching_column;
• Query
• SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT
• FROM EMPLOYEE
• RIGHT JOIN PROJECT
• ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
EMP_NAME DEPARTMENT
Angelina Testing
Christian Designing
Kristen Development
4. FULL JOIN
Angelina Testing
Christian Designing
Kristen Development
Russell NULL
Marry NULL
SQL Set Operation
Syntax
SELECT column_name FROM table1
UNION
SELECT column_name FROM table2;
The First table The Second table
ID NAME
ID NAME
3 Jackson
1 Jack
4 Stephan
2 Harry
3 Jackson
5 David
4 Stephan
5 David
2. Union All
• Union All operation is equal to the Union operation. It
returns the set without removing duplication and sorting
the data.
Syntax:
SELECT column_name FROM table1
UNION ALL
SELECT column_name FROM table2;
Example: Using the above First and Second table.
Union All query will be like: ID NAME
SELECT * FROM First
1 Jack
UNION ALL
2 Harry
SELECT * FROM Second;
3 Jackson
3 Jackson
The result set table will look like:
4 Stephan
5 David
3. Intersect
It is used to combine two SELECT statements. The Intersect operation
returns the common rows from both the SELECT statements.
In the Intersect operation, the number of datatype and columns must
be the same.
It has no duplicates and it arranges the data in ascending order by
default.
Syntax
SELECT column_name FROM table1
INTERSECT
SELECT column_name FROM table2;
Example:
Using the above First and Second table.
Intersect query will be:
SELECT * FROM First
INTERSECT
SELECT * FROM Second; ID NAME
The result set table will look like:
3 Jackson
4. Minus
It combines the result of two SELECT statements. Minus
operator is used to display the rows which are present in
the first query but absent in the second query.
It has no duplicates and data arranged in ascending order
by default.
Syntax:
SELECT column_name FROM table1
MINUS
SELECT column_name FROM table2;
Example
Using the above First and Second table.
Minus query will be:
SELECT * FROM First
MINUS ID NAME
Disadvantages of SQL:
• SQL doesn’t provide the programmers with a technique
of condition checking, looping and branching.
• SQL statements are passed to Oracle engine one at a
time which increases traffic and decreases speed.
• SQL has no facility of error checking during
manipulation of data.
Features of PL/SQL
BEGIN
null;
END;
/
• Output:
• PL/SQL procedure successfully completed.
• Taking input from user:
SQL> DECLARE
BEGIN Output:
null; Enter value for a: 24
Enter value for b: ‘Branch'
END; PL/SQL procedure successfully
/ completed.
Note:
• Single Line Comment: To create a single line comment , the
symbol – – is used.
• Multi Line Comment: To create comments that span over several
lines, the symbol /* and */ is used.
Example on PL/SQL to demonstrate all above concepts in one single block of
code.
SQL> DECLARE
BEGIN
c := a + b ;
dbms_output.put_line('Sum of '||a||' and '||b||' is = '||c);
END;
/
Enter value for a: 2 Enter value for b: 3 Sum of 2 and 3 is = 5
PL/SQL procedure successfully completed.
Cursors in PL/SQL
• Cursor in SQL