Practical-1: AIM: To Understand Basic Queries and Functions in SQL
Practical-1: AIM: To Understand Basic Queries and Functions in SQL
Practical-1: AIM: To Understand Basic Queries and Functions in SQL
PRACTICAL-1
AIM: To understand basic queries and functions in SQL.
1.Create:
Synatx:
CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);
Example:
2.Insert
Syntax :INSERT INTO table_name
VALUES (value1,value2,value3,...);
Example:
110110116018
3. Update :
Syntax:
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
4. Distinct :
Syntax :
Select Distinct column_name from table_name;
Example :
5. Where :
Syntax:
SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;
Example:
110110116018
6. OrderBy :
Syntax :
7. MAX() :
Syntax: SELECT MAX(column_name) FROM table_name;
Example :
8. MIN() :
Syntax: SELECT MIN(column_name) FROM table_name;
Example :
110110116018
9. AVG()
Syntax : SELECT AVG(column_name) FROM table_name
Example :
10. SUM() :
Syntax : SELECT SUM(column_name) FROM table_name;
Example :
11. GroupBy :
Syntax:
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;
Example :
110110116018
12. Drop :
Syntax : Drop Table table_name;
Example :
110110116018
PRACTICAL-2
AIM : Revision of joins.
Left Join:
The LEFT JOIN keyword returns all rows from the left table (table1), with the
matching rows in the right table (table2). The result is NULL in the right side when
there is no match.
Syntax:
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
Example:
110110116018
Right Join:
The RIGHT JOIN keyword returns all rows from the right table (table2), with the
matching rows in the left table (table1). The result is NULL in the left side when there
is no match.
Syntax:
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;
Example:
Inner Join:
The INNER JOIN keyword selects all rows from both tables as long as there is a
match between the columns in both tables.
Syntax:
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
Example:
110110116018
Full Join:
The FULL OUTER JOIN keyword returns all rows from the left table (table1) and
from the right table (table2).The FULL OUTER JOIN keyword combines the result of
both LEFT and RIGHT joins.
Syntax:
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;
Example:
110110116018
PRACTICAL-3
2) The Master and End section that also contains an Exception section (optionally).
The Declare Section: Code blocks starts with a declaration section in which memory
variables and other Oracle objects can be declared an initialized.
The Begin Section: It consists of a set of SQL and PL/SQL statements which
describe processes that have to be applied to table data. Data manipulation,
retrieval, looping and branching constructs can be applied in this section.
The Exception section: This section deals with handling of errors that arise during
execution of the data manipulation statements, which make up the PL/SQL code
block.
The End Section: This marks the end of a PL/SQL code block.
110110116018
PRACTICAL -4
10
110110116018
Aim: Write a PL/SQL code block that will accept anaccount number
from the user, check if theusersbalance is less than the minimum
balance, onlythen deduct Rs. 100/- from the balance. Theprocess is
fired on the ACCT_MSTR table.
Create atable ACC_MASTER
create table ACC_MASTER_46
(
acc_no number(5),
open_date date,
branch varchar(15),
amount number(10,2),
acc_type varchar(15)
);
Insert Data into table
insert into ACC_MASTER_46 values(00001,'06/Aug/2014','anand',5000,'saving');
insert into ACC_MASTER_46 values(00002,'15/Jul/2014','vadodara',1500,'current');
insert into ACC_MASTER_46 values(00003,'08/Apr/2014','valsad',5520,'saving');
insert into ACC_MASTER_46 values(00004,'07/Jan/2014','surat',5020,'saving');
insert into ACC_MASTER_46 values(00005,'23/Feb/2014','bardoli',5010,'saving');
insert into ACC_MASTER_46 values(00006,'20/Mar/2014','bharuch',5030,'loan');
Query
DECLARE
DISTIBUTED DATABASE APPLICATION
& SYSTEM
11
110110116018
accno number(5);
accnew number(5);
amtnew number(10,2);
BEGIN
accno:=&accno;
select acc_no,amount into accnew,amtnew from ACC_MASTER_46 where acc_no=
accno;
IF amtnew<2500
THEN
amtnew:=amtnew-100;
update ACC_MASTER_46 set amount =amtnew where acc_no=accno;
END IF;
END;
12