Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 32

Pract-1 SQL statement

A-Writing basic SQL statements:-


Que-Create a table “course” and insert a data of different 10 courses.
Attributes-I'd , Name , Duration, Fees
Syntax:=CREATE TABLE table_name
(column_name datatype,
column_name datatype,
…………
column_name datatype);
Code:=

Que:-Display all tha data from course table.


Syntax:=[SELECT * FROM tab_name;]
Code:=
Que:=Display id and name from the course table.
Syntax:=[SELECT col_name1,col_name2 FROM tab_name;]
Code:= select id,name from course;
Output:=

Que:= Display the course where the fees is less then 50000.
Syntax:=[SELECT * FROM table_name WHERE condition;]
Code:= select *from course where fees<50000;
Output:=

Que:= Display the course where the fees is more then 50000.
Syntax:=[ SELECT * FROM table_name WHERE condition;]
Code:= select *from course where fees>50000;
Output:

Que:= Display the course where the fees is equal then 50000.
Syntax:=[ SELECT * FROM table_name WHERE condition;]
Code:= select *from course where fees=50000;
Output:=

Que:= Display the courses which start with B.


Syntax:=[SELECT * FROM table_name WHERE col_name LIKE ‘word%’;]
Code:= select * from course where name like 'B%';
Output:=

Que:= Display the courses which start with H and end with g.
Syntax:=[SELECT * FROM table_name WHERE col_name LIKE ‘start_word%last_word’;]
Code:= select * from course where name like 'H%g';
Output:=
B-Restricting and sorting data:-
Que:= Display the course name in assending order.
Syntax:=[SELECT * FROM table_name ORDER BY col_name;]
Code:= select * from course order by name;
Output:=

Que:= Display the course name in descending order.


Syntax:=[SELECT * FROM table_name ORDER BY col_name DESC;]
Code:= select * from course order by name desc;
Output:=

C-Single row function:-

Que:= Display course-name using single row function.


1-Upper case-:
Syntax:=[SELECT UPPER(col_name) FROM table_name;]
Code:= select upper (name) from course;
2-Lower case:-
Syntax:=[SELECT LOWER(col_name) FROM table_name;]
Code:= select lower (name) from course;
3-Initcap(first letter in caps):-
Syntax:=[SELECT INITCAP(col_name) FROM table_name;]
Code:= select initcap (name) from course;

Output:=

Que:= Find the length of the course_name from the given table.
Syntax:=[SELECT col_name,LENGTH(col_name) FROM tab_name;]
Code:= select name,length(name)from course;
Output:=

Que:= Return a portion of the string from a given start point to end point.
Syntax:=[SELECT col_name,SUBSTR(col_name,stratindex,length) FROM tab_name;]
Code:= select name,substr(name,3,2)from course;
Output:=

Que:= Find numeric position of the character.


Syntax:=[SELECT col_name,INSTR(col_name,’word’)FROM tab_name;]
Code:= select name,instr(name,’M’)from course;

Que:= :- Remove spaces from the given character.


Syntax:=[SELECT col_name,TRIM(col_name)FROM tab_name;]
Code:= select name,trim(name)from course;
Output:=
Que:= Make use of round and mod function.
Code:=
1: select * from dual;
2: select round (84.551,1) from dual;
3: select mod (10,2) from dual;
Output:=

Pract-2 SQL statement-2


A-Displaying data from multiple tables :-
Que:=Inner join.
First of all create two table(1-student,2-course) as follows.

Syntax:=[SELECT col_name1,col_name2,..col_nameN FROM tab_name INNER JOIN ON


tab_name1.reference_colname= tab_name2.primary_colname;]
Code:=select id,name,c_name,duration From student Inner join course On
student.course_id=course.c_id;
Output:=
Que:= Left Join
Syntax:=[SELECT col_name1,col_name2,..col_nameN FROM tab_name LEFT JOIN ON
tab_name1.reference_colname= tab_name2.primary_colname;]
Code:= select id,name,c_name,duration From student left join course On
student.course_id=course.c_id;
Output:=

Que:=Right join.
Syntax:=[SELECT col_name1,col_name2,..col_nameN FROM tab_name RIGHT JOIN ON
tab_name1.reference_colname= tab_name2.primary_colname;]
Code:= select id,name,c_name,duration From student right join course On
student.course_id=course.c_id;
Output:=
Que:= Full Join.
Syntax:=[SELECT col_name1,col_name2,..col_nameN FROM tab_name FULL JOIN ON
tab_name1.reference_colname= tab_name2.primary_colname;]
Code:= select id,name,c_name,duration From student Full join course On
student.course_id=course.c_id;
Output:=

B: Aggregating Data Using Group Functions:=

Que:=count()
Syntax:=[SELECT COUNT(col_name) FROM tab_name WHERE COL_NAME LIKE ‘word%’;]
Code:= select count(c_name) from course where c_name like 'B%';
Output:=

Que:= Sum()
Syntax:=[SELECT SUM(col_name) FROM tab_name;]
Code:= select sum(fee) from course;
Output:=
Que:=min()
Syntax:=[SELECT * FROM tab_name WHERE col_name=(SELECT MIN(col_name) FROM tab_name);]
Code:= select * from course where fee=(Select min(fee) from course);
Output:=

Que:max()
Syntax:=[SELECT * FROM tab_name WHERE col_name=(SELECT MAX(col_name) FROM tab_name);]
Code:= select * from course where fee=(Select max(fee) from course);
Output:=

Que:=avg()
Syntax:=[SELECT AVG(col_name) FROM tab_name;]
Code:=select avg(fee) from course;
Output:=

Pract 3 Manipulating data:-

A-Using insert statement:=

Que-Create a table “course” and insert a data of different 10 courses. Attributes-I'd , Name ,
Duration, Fees.
Syntax:=[INSERT INTO tab_name VALUES(val1,val2,val3);]
Code:=
B- Using DELETE statement:-
Que:For deleting a single row from the table.
Syntax:=[DELETE tab_name WHERE condition;] Code:= delete cours Where c_id=10;
Output:=

Que:=For deleting the entire entry from the table.


Syntax:=DELETE tab_name;
Code:= delete cours;
Output:=

Que:= For deleting the entire data of table as well as it’s structure.
Syntax:=DROP TABLE tab_name;
Code:=DROP TABLE cours;
Output:=

C- Using UPDATE statement:-


Que:=update value of the table using update method.
Syntax:=[UPDATE tab_name SET col_name=value WHERE condition;]
Code:= update course set fee=40000 where c_id=3;
Output:=
Pract 4- Creating and Managing Tables:-
A- Creating and Managing Tables :=

Que:=Creating new table same as existing table.


Syntax:=[CREATE TABLE new_tab AS SELECT * FROM tab_name;]
Code:=create table new_course as select * from course;
Output:

Que:=Creating new table having specific fields but all the records from existing table.
Syntax:=[CREATE TABLE new_tab AS SELECT col_name1,col_name2 FROM tab_name;]
Code:=create table new_course1 as select c_id,c_name from course;
Output:

Que:=Creating new table having specific records but all fields from existing table.
Syntax:=[CREATE TABLE new_tab AS SELECT * FROM tab_name WHERE condition;]
Code:=create table new_course3 as select * from course where fee<80000;
Output:=
Que:=Creating new table having no records but all the fields from the existing table.
Syntax:=[CRAETE TABLE new_tab AS SELECT * FROM tab_name WHERE false_condition;]
Code:= create table new_course3 as select * from course where 1=2;
Output:=

Que:=Adding new column in a table.


Syntax:=[ALTER TABLE tab_name ADD new_col datatype;]
Code:=ALTER TABLE course ADD C_HOD varchar2(20);
Output:=

Que:=Dropping column from the table.


Syntax:= [ALTER TABLE tab_name DROP col_name;]
Code:= ALTER TABLE course DROP COLUMN C_HOD;
Output:=

Que:=Modifying column of a table.


Syntax:= [ALTER TABLE tab_nameMODIFY col_name datatype;]
Code:=ALTER TABLE course MODIFY c_name varchar2(15);
Output:=
Que:=Renaming table.
Syntax:=[RENAME TABLE tab_name TO new_tab_name;]
Code:=rename table course to coursedetails;
Output:=

B-Including Constraints:-
Que:=Not Null constraints.
Syntax:=[CREATE TABLE tab_name(col_name datatype) NOT NULL;]
Code:=create table coursedetils(c_id number(3) NOTT NULL);
Output:=

Que:=Unique constraints.
Syntax:=[CREATE TABLE tab_name(col_name datatype) UNIQUE;]
Code:=create table coursedetails(c_name varchar2(15) UNIQUE);
Output:=
Que:=Primary Key constraints.
Syntax:=[CREATE TABLE tab_name(col_name datatype) PRIMARY KEY;]
Code:= create table coursedetails(c_id number(3) PRIMARY KEY);
Output:=

Que:=Check constraints.
Syntax:=[CREATE TABLE tab_name(col_name datatype) CHECK(condition);]
Code:= create table coursedetails(fee number(15) CHECK(fee>=40000);
Output:=

Pract 6-: Using SET operators,Date/Time Function,GROUP BY clause (advanced features)


and advanced sub queries.
A-Using SET Operators-
Que:=Union operator.
Syntax:=[SELECT col_name FROM tab_name UNION SELECT col_name2 FROM tab_name2;]
Code:= select c_name from coursedetails union select name from student;
Output:=

Que:=Union all operator.


Syntax:= [SELECT col_name FROM tab_name UNION ALL SELECT col_name2 FROM tab_name2;]
Code:= select c_name from coursedetails union all select name from student;
Output:=

Que:=Intersect operator.
Syntax:=[select col_name from tab_name intersect select col_name2 from tab_name2;]
Code:= select c_name from coursedetails intersect select name from student;
Output:=
Que:=Minus operator.
Syntax:=[SELECT col_name FROM tab_name MINUS SELECT col_name2 FROM tab_name2;]
Code:= select c_name from coursedetails minus select name from student;
Output:=

Pract 7-PL/SQL Basics:-


A-Declaring variables-
Syntax:=[variable_name datatype [NOT NULL] [:= | DEFAULT initial_value]
Que:=Displaying sum of two numbers.
Code:=

Output:

B:=Writing executable statements-


Que:=Displaying fee of a spefic course.
Code:=
Output:=

C-Interacting with the server:


Que:=Getting value from user side.
Code:=

Output:=

D-Working control structure:

Que:=Using If statement.
Syntax:=[ IF condition THEN statements;
END IF;]
Code:=
Output:=

Que:=Using If-then-Else statement.


Syntax:=[ IF condition THEN statement1;
ELSE sttemenet2;
END IF;]
Code:=
Output:=

Que:=Nested If-then-else statements.


Syntax:= IF(condition1)THEN statement; (executes when the condition1 is true)
ELSIF(condition2) THEN statement; (executes when the condition2 is true)
ELSIF(condition3) THEN statement; (executes when the condition3 is true)
ELSE statement; (executes when any condition is not true)
END IF;
Code:=

Output:=

Que:=Using CASE-WHEN statements.


Syntax:= CASE selector
WHEN 'value1' THEN statement1;
WHEN 'value2' THEN statement2;
...
ELSE statement; (default case)
END CASE;
Code:=

Output:=

Que:=Using while loop.


Syntax:= WHILE condition LOOP
sequence_of_statements
END LOOP;
Code:=

Output:=
Que:=Using For loop.
Syntax:= FOR counter IN ini_value ..final_value LOOP
sequence_of_statements;
END LOOP;
Code:=

Output:=

Que:Using Continue-When in Loop.


Syntax:= WHILE condition LOOP
sequence_of_statements;
CONTINUE condition;
END LOOP;
Code:=
Output:=

Pract 8 Composite data types,cursors and exception.


Que:=Working with Composite Data Type.
Syntax:=TYPE type_name IS TABLE OF datatype
INDEX BY BINARY_INTEGER;
var_name type_name;
Code:=

Output:=
B-Writing Explicit Cursors
Que:=Using explicit cursor.
Syntax:=
Creating cursor:= CURSOR cursor_name IS select_statement;
Opening the Cursor:=OPEN cursor_name;
Fetching the cursor:= FETCH cursor_name INTO col_name1,col_name2,col_name3;
Closing the cursor:=Close cursor_name;
Code:=

Output:=
Que:=Using loops in cursor.
Code:=

Output:=

C-Handelling Exception
Syntax:=
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
WHEN exception1 THEN exception1-handling-statements
WHEN exception2 THEN exception2-handling-statements
WHEN exception3 THEN exception3-handling-statements
WHEN others THEN
exception3-handling-statements
END;
Code:=

Output:=
Pract-9 Procedure and Function.
A-Creating Procedure.
Que:=Create a procedure to find the minimum of two value.
Syntax:= CREATE OR REPLACE PROCEDURE procedure_name [(parameter_name[IN|OUT|IN
OUT] type [])] IS / AS
BEGIN
< procedure_body >
END procedure_name;
Code:=

Output:=

B-Creating Function:
Que:=Create a function totalcourse which counts the number of total courses of the
coursedetails table.
Syntax:= CREATE OR REPLACE FUNCTION function_name [parameter_name[IN|OUT|IN
OUT] type [, ...]] RETURN return_datatype IS / AS
BEGIN
<function_body >
END [function_name];
Code:=

Output:=

C-Managing Subprograms

Output:=

D-Creating Packages:-
Que:=Create a package.
Code:=
1-Creating package:
Syntax:=
CREATE OR REPLACE PACKAGE package_name IS
sub_program and public element declaration
..
END package_name;
Code:=

2-Creating package body:


Syntax:= CREATE OR REPLACE PACKAGE BODY package_name IS
<global_declaration part>
<Private element definition>
<sub_program and public element definition>
.
<Package Initialization>
END package_name;
Code:=

3-Calling the package:


Syntax:=package_name.subprogram_name([parameter]);
Output:=

Pract 10 Creating Database Triggers.


Que:=Create a trigger that will invoke when any new course will be inserted or update query
encountered.
Syntax:= CREATE OR REPLACE TRIGGER trigger_name BEFORE / AFTER INSERT OR UPDATE
OR DELETE ON table_name
FOR EACH ROW
WHEN (condition)
DECLARE
Declaration-statements;
BEGIN
Executable-statements ;
END;
Code:=

Output:=

You might also like