Sei sulla pagina 1di 60

INTRODUCTION TO SQL SQL: Structured Query Language.

There are number of versions of SQL. Now version of SQL is 92.The original version of SQL was developed at IBMs Sanjose Research laboratory. This language originally called sequel, as part of the system R project in the early 1970s. Its name has changed to SQL. SQL is a non-procedure query language. SQL has clearly established itself as the standard relational d211ase language. It is used to formulate operations and relations.

Characteristics:
SQL is a non-procedure query language. SQL is a common language for most RDBMS.

Components:
SQL language has several components.
1) 2) 3) 4)

DDL Data Definition Language. DML Data Manipulation Language. DCL Data Control Language. DQL Data Query Language.

Data Definition Language:


To define definitions of data. It supports commands such as Create: It is used to create table. Syntax: SQL> create table table_name(column name data type(size)); Eg: SQL> create table std(Sid number(10),Sname varchar2(10)); Alter: It can be used to add or modify data. Syntax: SQL> alter table table_name Add/modify column _name datatype(size); Eg: SQL> alter table std add marks number(20); SQL> alter table std modify email number(30); Drop: It can be used to delete the entire table.

Syntax: SQL> drop table table_name; Eg: SQL> drop table std; Data Manipulation Language: To modify data it supports some commands such as Insert: It Is used to insert one or more records into already existing table through data definition language. Syntax: SQL> insert into table_name values(expression); Eg: SQL> insert into std values(&Sid,&Sname); Delete: It is used to delete a record from table. Syntax: SQL> delete from table_name Where <condition>; Eg: SQL> delete from std where sid=12; Upate: It is used modify existing data. Syntax: SQL> update table_name Set old col.name=new col.name Where<condition>; Eg: SQL> update std Set marks=95 Where marks<95; Data Control Language: To control data it supports some commands such grant , revoke. Grant: It is a command used to provide privileges to the user on the table. Syntax: Grant <privilege> On<table_name> To <user_name>;

Revoke: It is a command used to with draw the privileges from the user. Syntax: Revoke<privilege> On <table_name> From<user_name>; Trasaction Control Language: Commit: It is a command used to make permanent all changes done in a table. Syntax: COMMIT; Roll Back: It is used for undo work done on the table. Syntax: ROLL BACK; Data Query Language: To provide queries to data in database. It provides commands like select. Select: It is used for performing queries in database table. Syntax: SQL> select <attribute_list> From table_name Where<condition> Group by<grouping criteria> Order by <list> Having<condition>; Eg: SQL>select Sname From std Where marks=20; Creation of Employee schema Create table for the attributes eno,ename ,job,mgr,doj,sal,deptno. Eno 7369 7499 7521 Ename Job Smith clerk Wallen Salesman Ward Clerk Mgr Doj 7902 01/mar/99 7698 07/sep/98 7782 03/jul/97 Sal 500 800 1600 deptno 10 20 30

7566 7654 7698 7782 Syntax:

Allen Analyst Blake Manager Clark Salesman Scott Clerk

7369 01/dec/97 7521 25/jun/95 7521 03/feb/95 7466 23/sep/92

1200 8000 8000 15000

20 30 10 20

SQL> create table table_name(edumn name datatype(size)); Eg: SQL>create table emp(eno number(5),ename varchar2(10),job varchar2(10),mgr number(5),doj varchar2(12),sal number(5),deptno number(2)); Output: Table created. Syntax: SQL>Insert into table name values(expression); Eg: SQL>insert into Emp values(&eno,&ename,&job,&mgr,&doj,&sal,&deptno); Output: Enter value for eno:7369 Enter value for ename:Smith Enter value for job:Clerk Enter value for mgr:7902 Enter value for doj:01/mar/99 Enter value for sal:500 Enter value for deptno:10 Old 1: insert into Emp values(&eno,&ename,&job,&mgr,&doj,&sal,&deptno): New1: insert into Emp values(7369,smith,clerk,7902,01/mar/99,500,10); Output: 1 row created. Eg: SQL>/ Output: Enter value for eno: 7499 Enter value for ename: Wallen Enter value for job: Salesman Enter value for mgr: 7698 Enter value for doj: 07/sep/98 Enter value for sal:800 Enter value for deptno:20

Old 1: insert into Emp values (&eno,&ename,&job,&mgr,&doj,&sal,&deptno); New1: insert into Emp values(7499,Wallen,Salesman,7698,07/sep/98,800,20); Ouptput: 1 row created. Eg: SQL>/ Output: Enter value for eno: 7521 Enter value for ename: Ward Enter value for job: Clerk Enter value for mgr: 7782 Enter value for doj: 03/jul/97 Enter value for sal: 1600 Enter value for deptno:30 Old 1: Insert into Emp values values(&eno,&ename,&job,&mgr,&doj,&sal,&deptno); New 1: Insert into Emp values(7521,word,clerk,7782,03/jul/97,1600,30); Output: 1 row created. Eg: SQL>/ Output: Enter value for eno: 7566 Enter value for ename: Allen Enter value for job: Analyst Enter value for mgr: 7369 Enter value for doj: 01/dec/97 Enter value for sal: 1200 Enter value for deptno: 20 Old 1: Insert into Emp values(&eno,&ename,&job,&mgr,&doj,&sal,&deptno); New 1: Insert into emp values(7566,Allen,Analyst,7369,01/dec/97,1200,20); Output: 1 row created. Eg: SQL>/ Output: Enter value for eno:7654 Enter value for ename: Blake Enter value for job: Manager

Enter value for mgr: 7521 Enter value for doj: 25/jun/95 Enter value for sal:8000 Enter value for deptno:30 Old 1: Insert into emp values (&eno,&ename,&job,&mgr,&doj,&sal,&deptno); New 1: Insert into emp values (7654,blake,manager,7521,25/jun/95,8000,30); Output: 1 row created. Eg: SQL>/ Output: Enter value for eno: 7698 Enter value for ename:Clark Enter value for job: Salesman Enter value for mgr: 7521 Enter value for doj: 03/feb/95 Enter value for sal: 8000 Enter value for deptno:10 Old 1: Insert into emp values (&eno,&ename,&job,&mgr,&doj,&sal,&deptno); New 1: insert into emp values (7698,dark,salesman,7521,03/feb/95,8000,10); 1 row created. Eg: SQL>/ Output: Enter value for eno: 7782 Enter value for ename: Scott Enter value for job: Clerk Enter value for mgr: 7466 Enter value for doj: 23/sep/92 Enter value for sal: 1500 Enter value for deptno: 20 Old 1: Insert into emp values(&eno,&ename,&job,&mgr,&doj,&sal,&deptno); New 1: Insert into emp values(7782,scott,clerk,7466,23/sep/92,1500,20); Output: 1 row created. 1. To display all attributes of emp details

Syntax: Select * FROM tablename; Eg: SQL> select * FROM emp; Output: Eno 7369 7499 7521 7566 7654 7698 7782 Ename Job Smith clerk Wallen Salesman Ward Clerk Allen Analyst Blake Manager Clark Salesman Scott Clerk Mgr Doj 7902 01/mar/99 7698 07/sep/98 7782 03/jul/97 7369 01/dec/97 7521 25/jun/95 7521 03/feb/95 7466 23/sep/92 Sal 500 800 1600 1200 8000 8000 1500 deptno 10 20 30 20 30 10 20

7 rows selected. 2. To display eno whose salary is grater than 1000 Syntax: Select eno from tablename Where <condiotion>; Eg: SQL>select eno from emp Where SQL>1000; Output: Eno 7521 7566 7654 7698 7782 3.To display emp details whose salary between 800 and 1000. Syntax: Select * from table Where <condition>; Eg: SQL> Select * from Emp Where sal between 800 and 1000; Output: Eno Ename Job Mgr Doj 7499 Wallen Salesman 7698 07/sep/9 8

Sal 800

deptno 20

4. To count the no.of tuples present in relation emp. Syntax: Select count(column_name) From table_name; Eg: SQL>select count(*) From emp; Output: Count(*) 7 5. To find max salary of emp Syntax: Select max(column name) From table_name; Eg: SQL>select max(sal) From emp; Output: Max(sal) 1500 6. To find min salary of emp. Syntax: Select min(column_name) From table_name; Eg: SQL> Select min(sal) from emp; Output: Min (Sal) 500 7. To find sum of salary of emp. Syntax: Select sum(column name) From table name; Eg: SQL >Select sum(sal) From emp; Sum (sal) 35100

8. To find avg of salary of emp relation Syntax : Select avg(column name) From table name; Eg: SQL > select avg(sal) From emp; Output : Avg (sal) 5014.28571 9. To display Eno, Ename whose job is salesman Syntax : Select colomn name From table_name Where< condition>; Eg: Select Eno,Ename From Emp Where job= salesman; Output : Eno Ename 7499 Wallen 7698 Clerk 10. To display all attributes of Emp whose deptno is 20 Syntax : Select * From table_name Where < condition>; Eg: SQL > Select * From Emp Where deptno=20; Output : Eno 7499 7566 7782 Ename Job Wallen Salesman Allen Analyst Scott Clerk Mgr Doj 7698 7/sep/98 7369 1/dec/97 7466 22/sep/92 Sal 800 1200 1500 Dept_no 20 20 20

11. To display Emp details whose Ename is starting with s. Syntax : Select * From table_name Where Column_name likei%; Eg : SQL > Select * From Emp Where Ename like s%; Output: Eno Ename Job Mgr Doj 7369 Smith Clerk 7902 1/mar/99 7782 Scott Clerk 7466 23/sep/92 12. To display Ename whose name starts with c. Syntax : Select Column_name From table_name Where column_name like i%; Eg : SQL > Select Ename From Emp Where Ename like c%; Output : Ename Clerk 13. To display Ename ends with R; Syntax : Select column_name From table_name Where column_name like %i; Eg : SQL > Select Ename From Emp Where Ename like % R; Output : No rows selected. 14.To dispay length of Ename from Emp. Syntax: Select length(column_name) From table_name;

Sal 500 1500

Dept_no 10 20

Eg: SQL> Select length(Ename) From Emp; Output: Length(Ename) 5 6 4 5 5 5 5 7 rows selected. 15. To display Enames in lowercase. Syntax: Select lower(column_name) From table_name; Eg: SQL> Select lower(Ename) From Emp; Output: Lower(Ename) Smith Wallen Ward Queen Blake Clark Scott 7 rows selected. 16. To display job in upper case. Syntax: Select upper(column_name) From table_name; Eg: SQL> Select upper(job) From Emp; Output: Upper(job) Clerk Salesmen

Clerk Analyst Manager Salesmen Clerk 7 rows selected. 17. To describe Emp table. Syntax: Desc table_name; Eg: SQL> desc Emp; Output: Name Eno Ename Job Mgr Doj Sal Dept_no

Type number(5) varchar2(10) varchar2(10) number(5) varchar2(12) number(5) number(2)

18. To delete Emp where deptno=10. Syntax: Delete from table_name Where <condition>; Eg: SQL> delete from Emp Where deptno=10; Output: 2 rows deleted. 19.To update Salary 10% whose getting more than 2000. Syntax: Update table_name Set oldcolumn_name=newcolumn_name Where <condition>; Eg: SQL> update Emp set Sal=Sal+(Sal*0.1) where Sal<=2000; Output: 3 rows updated.

20. To delete Emp details who Salary is between 800 & 1000. Syntax: Delete from table_name Where <condition>; Eg: SQL> delete from Emp Where Sal between 800 and 1000; Output: 1 row deleted. 21. To drop an Emp table. Syntax: Drop table table_name; Eg: SQL> drop table Emp; Output: Table droped. Student Schema Query: Create Std table which consists of roll no, branch, marks, addres. Roll No. Name Branch Marks 1 Ravi IT 45 2 Sravan IT 50 3 Pushpa CSE 50 4 David ECE 45 5 Raghu ECE 55 6 Ramya IT 55 7 Latha CSE 50 8 Mukesh IT 60 9 Rajesh CSE 55 10 Sai ECE 60 Syntax : <SQL> Create table table_name(column_name datatype[size]); Eg: <SQL> Create table Std(Sno numbe[2],Sname vachar2[10],banch varchar2[5],marks number[2],address varchar2[5]); Output: Table created. Syntax: SQL> insert into table_name values(&Column); address HYD HYD HYD MBR MBR MBR MBR HYD HYD HYD

Eg: SQL> insert into Std values(&Sno,&name,&branch,&marks,&address); Output: Enter value for Sno : 1 Enter value for Sname : Ravi Enter value for branch : IT Enter value for marks : 45 Enter value for address : HYD. 1 row created. Eg: SQL> / Output: Enter value for Sno : 2 Enter value for Sname : Sravan Enter value for branch : IT Enter value for marks : 50 Enter value for address : HYD. 1 row created. Eg: SQL> / Output: Enter value for Sno : 3 Enter value for Sname : Pushpa Enter value for branch : CSE Enter value for marks : 50 Enter value for address : HYD. 1 row created. Eg: SQL> / Output: Enter value for Sno : 4 Enter value for Sname : David Enter value for branch : ECE Enter value for marks : 45 Enter value for address : MBR. 1 row created. Eg: SQL> / Output: Enter value for Sno : 5

Enter value for Sname : Raghu Enter value for branch : ECE Enter value for marks : 55 Enter value for address : MBR. 1 row created. Eg: SQL> / Output: Enter value for Sno : 6 Enter value for Sname : Ramya Enter value for branch : IT Enter value for marks : 55 Enter value for address : MBR. 1 row created. Eg: SQL> / Output: Enter value for Sno : 7 Enter value for Sname : Latha Enter value for branch : CSE Enter value for marks : 50 Enter value for address : MBR. 1 row created. Eg: SQL> / Output: Enter value for Sno : 8 Enter value for Sname : Mukesh Enter value for branch : IT Enter value for marks : 60 Enter value for address : HYD. 1 row created. Eg: SQL> / Output: Enter value for Sno : 9 Enter value for Sname : Rajesh Enter value for branch : CSE Enter value for marks : 55 Enter value for address : HYD. 1 row created. Eg:

SQL> / Output: Enter value for Sno : 10 Enter value for Sname : Sai Enter value for branch : ECE Enter value for marks : 60 Enter value for address : HYD. 1 row created. 1. To display Snames where brach=IT. Syntax: SQL> select col_name From table_name Where <condition>; Eg: SQL> select Sname From Std Where brach=IT; Output: Sname Ravi Sravan Ramya Mukesh 2. To arrange the names in lower case. Syntax: Select lower(col_name) From table_name; Eg: SQL> select lower(Sname) From Std; Output: Lower(Sname) ravi sravan puspha david raghu latha mukesh rjesh sai 10 rows selected.

3. To arrange branch in uppercase. Syntax: Select upper(col_name) From table_name; Eg: SQL> select upper(branch) From Std; Output: Upper(branch) IT IT CSE ECE ECE IT CSE IT CSE ECE 10 rows selected. 4. To display the sum of all the student marks. Syntax: Select sum(col_name) From table_name; Eg: SQL> select(marks) From Std; Output: Sum(marks) 525 5. To display the max marks. Syntax: Select max(col_name) From table_name; Eg: SQL> select max(marks) From std; Output: Max(marks) 60

6. To display min marks. Syntax: Select min(col_name) From table_name; Eg: SQL> select min(marks) From std; Output: Min(marks) 45 7. To display the avg marks from Std. Syntax: Select avg(col_name) From table_name; Eg: SQL> select avg(marks) From Std; Output: Avg(marks) 52.5 8. To display Sname,marks of a Std who is getting more than 50. Syntax: Select col_name From table_name Where <condition>; Eg: SQL> select Sname,marks From Std Where marks>50; Output: Sname marks Raghu 55 Ramya 55 Mukesh 60 Rajesh 55 Sai 60 9. To display Snames in assending order. Syntax: Select col_name From table_name Order by col_name asc;

Eg: SQL> Select Sname From Std Order by Sname asc; Output: Sname David Latha Mukesh Pushpa Raghu Rajesh Sai 10.To display address in descending order. Syntax: Select col_name From table_name Order by col_name desc; Eg: SQL> select address From Std Order by address desc; Output: MBR MBR MBR MBR HYD HYD HYD HYD HYD HYD 10 are selected. 11. To display Sname whose name starts with s; Syntax: Select col_name From table_name Where col_name like char%; Eg: SQL> select Sname

From Std Where Sname like S%; Output: Sname Sravan 12.To display Sname whose name ends with a. Syntax: Select col_name From table_name Where col_name like %char; Eg: SQL> Select Sname From Std Where Sname like %a; Output: Sname Puspha Ramya Latha 13.To count total number of records in the Sdt relation. Syntax: Select count(col_name) From table_name; Eg: SQL> select count(*) From Std; Output: Count(*) 10 14. To display tuples whose marks between 50 & 60. Syntax: Select * from table_name where col_name between condition; Eg: SQL> select * From Std Where marks between 50 and 60; Output: Roll No. 2 Name Sravan Branch IT Marks 50 address HYD

3 5 6 7 8 9 10

Pushpa Raghu Ramya Latha Mukesh Rajesh Sai

CSE ECE IT CSE IT CSE ECE

50 55 55 50 60 55 60

HYD MBR MBR MBR HYD HYD HYD

8 rows selected. 15. update Sname who is getting marks<50. Syntax: Update table_name Set old_name=ne_name Where <codetion>; Eg: SQL> update Std Set Sname=pavan Where marks<50; Output: 2 rows are updated. 16.update branch CSE where marks<50. Syntax: Update Std Set branch=Mech Where marks<50. Outut: 2 rows updated. 17.Display marks from Std without duplicate. Syntax: Select distinct(col_name) From table_name; Eg: SQL> select distinct(marks) From Std; Output: Marks 45 50 55 60 18.Delete Std where Sno=1. Syntax:

Delete from where condition; Eg: SQL> delete from Std where Sno=1; Output: 1 row created. 19. To find length of Sname from Std. Syntax: Select length(col_name) From table_name; Eg: Length(Sname) 6 6 5 5 5 5 6 6 3 9 rows selected. 20. To display all tuples from Std where marks is 50. Syntax: Select * From table_name Where condition; Eg: SQL> select * From Std Where marks=50; Output: Roll No. 2 3 7 Name Sravan Pushpa Latha Branch IT CSE CSE Marks 50 50 50 address HYD HYD MBR

21.To delete from Std. Syntax: Alter table table_name Drop column col_name;

Eg: SQL> alter table Std Drop column branch; Output: Table altered. 22. To add column to the Std relation. Syntax: Alter table table_name Add column datatype; Eg: SQL> alter table Std Add Email varchar(20); Output: Table alter. 23. To rename std relation. Syntax: Rename <old_name>to<new_name>; Eg: SQL> rename<Std>to<Stud>; Output: Table renamed. 24. To describe Std. Syntax: Desc table_name; Eg: SQL> desc Std; Output: Name Sno Sname Branch Marks address

Type number(2) varchar2(10) varchar2(5) number(5) varchar2(5)

Introduction: One of the most powerful features of SQL is nested Queries. A nested Queries is a Query that has another Query embedded with in it. The embedded Query is called sub Query. The embedded quarry can of course be a nested query itself. The statement counting a sub query is called a parent statement. CREATION OF SAILORS TABLE: Create sailors table and insert the values as shown below. SID 22 29 31 32 58 64 71 74 95 SNAME Dustin Brutus Lubber Andy Rusty Horatio Zorba Art Bub RATING 7 1 8 8 10 7 10 3 3 AGE 45 33 55 25 35 35 16 25 63

Syntax:create table tablename(col_name datatype(size)); SQL>create table sailors(sid number(10),sname varchar2(10),rating number(10),age varchar2(10)); table created. Syntax:Insert into tablename values (&col.name); SQL>insert into sailors values (&sid,&sname,&rating,&age); Enter the for sid:22 Enter value for sname:Dustin Enter value for rating:7 Enter value for age:45 SQL>/

Enter value for sid :29 Enter value for sname : Brutus Enter value for rating :1 Enter value for age :33 SQL>/ Enter value for sid :31 Enter value for sname : lubber Enter value for rating :8 Enter value for age :55 SQL>/ Enter value for sid :32 Enter value for sname : A ndy Enter value for rating :8 Enter value for age :25 SQL>/ Enter value for sid :58 Enter value for sname : Rusty Enter value for rating :10 Enter value for age :35 SQL>/ Enter value for sid :64 Enter value for sname : Horatio Enter value for rating :7 Enter value for age :35 SQL>/ Enter value for sid :71 Enter value for sname : Zorba Enter value for rating :10 Enter value for age :16 SQL>/ Enter value for sid :85

Enter value for sname : Art Enter value for rating :3 Enter value for age :25 SQL>/ Enter value for sid :95 Enter value for sname : Bub Enter value for rating :3 Enter value for age :63 Create Reserves table and insert the values SID 22 22 22 22 31 31 31 64 64 74 BID 101 102 103 104 102 103 104 101 102 103 DAY 10/10/98 10/10/98 10/8/98 10/7/98 11/10/98 11/10/98 11/12/98 10/5/98 9/8/98 9/8/98

Systax:SQL> Create table tablename(colname datatype(size)); SQL>Create table reserves(sid number(10) , bid number(10), Varchar2(10)); Table created Syntax:SQL> insert into tablename values(&col.name); SQL> insert into reserves values(&sid,&bid,&day); Enter value for sid :22

Enter value for bid :101 Enter value for day :10/10/98 OLD\: insert into reserves values(&sid,&bid,&day); NEW\: insert into reserves values(22,101,10/10/98); 1 row created SQL>/ Enter value for sid :22 Enter value for bid :102 Enter value for day :10/10/98 OLD\: insert into reserves values(&sid,&bid,&day); NEW\: insert into reserves values(22,102,10/10/98); 1 row created SQL>/ Enter value for sid :22 Enter value for bid :103 Enter value for day :10/08/98 SQL>/ Enter value for sid :22 Enter value for bid :104 Enter value for day :10/07/98 OLD\: insert into reserves values(&sid,&bid,&day); NEW\: insert into reserves values(22,104,10/07/98); 1 row created SQL>/ Enter value for sid :31 Enter value for bid :102 Enter value for day :11/10/98

OLD\: insert into reserves values(&sid,&bid,&day); NEW\: insert into reserves values(31,102,11/10/98); 1 row created SQL>1 Enter value for sid :31 Enter value for bid :103 Enter value for day :11/06/98 OLD\: insert into reserves values(&sid,&bid,&day); NEW\: insert into reserves values(31,103,11/6/98); SQL>/ Enter value for sid :31 Enter value for bid :104 Enter value for day :11/12/98 OLD\: insert into reserves values(&sid,&bid,&day); NEW\: insert into reserves values(31,104,11/12/98); SQL>/ Enter value for sid :64 Enter value for bid :101 Enter value for day :10/05/98 SQL>/ Enter value for sid :64 Enter value for bid :102 Enter value for day :09/08/98 OLD\: insert into reserves values(&sid,&bid,&day); NEW\: insert into reserves values(64,102,09/08/98); SQL>/ Enter value for sid :74 Enter value for bid :103 Enter value for day :09/08/98

Create boat table by inserting the values BID BNAME COLOR 101 Interlake Blue 102 Interlake Red 103 Clipper Green 104 Marine red Systax:Create table tablename(col.name datatype(size)); SQL>Create table boat(bid number(10) , bname varchar2(10) , color varchar2(10)); Table created Syntax:Insert into tablename values(&colname); SQL>insert into boat values(&bid,&bname,&color); Enter value for bid :101 Enter value for bname :interlake Enter value for color :blue SQL>/ Enter value for bid :102 Enter value for bname :interlake Enter value for color :red SQL>/ Enter value for bid :103 Enter value for bname :Clipper Enter value for color :Green SQL>/ Enter value for bid :104 Enter value for bname :marine Enter value for color :red

1. Find the names of sailors who have reserved baot 103. Syntax:SQL>select s.sname from sailors s Where s.sid in(select R.sid , from reserver R, where R.sid=103); Output:- sname Dustin Lubber Horatio 2.Find the names of sailors who have reserved red boat Syntax:SQL>select S.sname from sailors s where s.sid in(select R.sid from reserves R where R.bid in(select B.bid from boat B where aaab.color=red)); Output:Dustin Lubber Horatio 3. Find the names of sailors who have not reserved boat 103. Syntax:SQL>select S.sname from sailors s where s.sid not in(select R.sid from reserver R where R.bid=103); Output:- Sname Brutus Andy Rusty Horatio Zorba Art bub 4. Find the names of sailors who have not reserved greenboat.

Syntax:SQL>select S.sname from sailors s where s.sid not in(select R.sid from reserver R where R.bid in(select b.bid from boat B where B.color=green)); Output:- sname Brutus Rusty Horatio Zorba Art Bub 5.Find the oldest age of sailors Syntax:SQL>select max(s.age)from sailors s; Output:- 63 6.Find the youngest age of sailors Syntax:SQL>select min(s.age)from sailors s; Output:- 16 7.Count no of tuples present in sailors Syntax:SQL>select count (*) from sailors s; Output:- count(*) 10 8.Find the avg age of sailors Syntax:SQL>select avg(s.age) from sailors s;

Output:- Avg(s.age) 37.3 9.Find the sum of ages of all sailors Syntax:SQL>select sum(s.age) from sailors s; Output:-sum(s.age) 373 10.Set comparision:Find the names of sailors who have reserved baot 103. Syntax:Sql>select S.sname from sailors s,reserve R Where S.sid=R.sid and R.bid=103; Output:-sname Dustin Lubber Horatio 11.Find the names of sailors who had reserved green boat Syntax:SQL>select S.sname from sailors s,reserves R,boats B Where S.sid=R.sid and R.Bid=B.Bid and B.color=green;

Output:-sname Dustin Lubber Horatio

12.write the sailors name in uppercase letters Syntax:SQL>select upper(S.Sname) from sailors s; Output:-sname Brutus Dustin Lubber Andy Rusty Horatio Zorba Art Bub 13.write the sailors names in lowercase letters Syntax:SQL>select lower(S.sname) from sailors s; Output:-sname Dustin Lubber Andy Rusty Horatio Zorba Art Bub 14.Find the color of boat who have reserved by lubber. Syntax:SQL>select B.color from boat B,reserver R,sauilors s Where B.bid=R.bid and R.sid=S.sid and S.sname=lubber Output:-color

Red Green Red 15Find the names of sailors who have reserved atleast one boat Syntax:SQL>select S.sname from sailos s,reseves R,boat B where S.sid=R.sid; output:- Dustin Dustin Dustin Dustin Lubber Lubber Lubber Horatio Horatio Horatio 16.Find the names of ailors who have reserved red or green boat Syntax:SQL>select S.sname from sailors s,reserves R,boat B where S.sid=R.sid and R.sid=B.bid and B.color=red Union(select S.sname from sailors s,reserves R,boat B where S.sid=R.sid and R.sid=B.bid and B.color=green); Output:Dustin Horatio Lubber 17.Find the names of sailors who have rating to 10 or who have reserved boat 104

Syntax:SQL>select S.sname from sailors s where S.rating=10 Union(select S.sname from sailors s,reserves R,boats B where S.sid=R.sid and R.bid=104); Output:Dustin Lubber Rusty Zorba 18.Find the sid of sailors whose rating is 10 or have reserved boat 102 Syntax;SQL>select S.sid from sailors s where S.rating=10 Union(select R.sid from reservevs R where R.sid=102); Output:-SID 58 71

Co-Related Nested Queries:19.Find the names of sailors who have reserved boat 103 Syntax:SQL>select S.sname from sailors s where exits(select * from reserves R where R.bid=104 and S.sid=R.sid);

20.Find the names of sailors who have not reserved boat 104

Syntax:SQL>select S.sname from sailors s where not exits(select * from reserves R where R.bid=104 and S.sid=R.sid); Output:Brutus Andy Rusty Horatio Zorba Art Bub

VIEWS

To reduce redundant data to minimum possible, Oracle allows the creation of an object called a view. A view is mapped to select sentence. Table on which the view is based is described in the FROM clause of the select command. The Oracle engineer treats a view just as through it was a based table. How ever a query fixed on a view will run slower than a query fired on a base table. Some views are used for looking at data table. Other views can be used to only look at table data and nothing else the view is called Read _only view. A view can be created as Syntax: Create view view_name as select column_name1,column_name2,..column_name n From table_name Where column_name = expression list Group by grouping criteria having condition; Eg: Create view view_emp1 as select * From emp1; Output: View created. Selecting data: Views are used to create temporary tables by selecting some of tables or attributes from existing tables. Once a view has been created it can be queried exactly like a base table. Syntax: Select column_name1, column_name2...column_name n From view_name Data retrieval from views: Once a view is defined the data is retrieved from view same as that real table using select commands. Insertion Deletion Update are tedious pro cessor in views. View on which table is created will have a subset of the actual columns of the table from which it will is built. 1. updatable views 2. non_updatable views Updatable views:

Views on which data manipulation can be done are called updatable views. Non_updatable views: Views on which data manipulation cannot be done are called non_updatable views. Inserting values into views: Syntax: Insert into view_name values(Expression list); Eg: Insert into view_emp1 values(eno,&ename,&job); Insert into view_emp1 values(2,Dustin,Cieck); Output: 1 row created. To drop view: Syntax: Drop view view_name; Eg: Drop view view_emp1; Output: Table dropped. View dropped. Query: Create view table on emp relation. Eno Ename Job Mgr Doj Sal Deptno 7369 Smith Clerk 7902 01-03-99 500 10 7499 Wallen Salesman 7698 07-09-98 800 20 7521 Ward Clerk 7782 03-07-97 1600 30 7566 Allen Analyst 7369 01-12-97 1200 20 7654 Blake Manager 7521 25-06-95 8000 30 7698 Clerk Salesman 7521 03-02-95 8000 10 7782 scott clerk 7466 23-09-92 15000 20 Syntax: SQL> create view view-name as select columnname1,columnname2,columnnamee3, from tablename; Eg: SQL> create view view-empl as select * from empl; Output: View created. Inserting 7,views in the view. Syntax: SQL> insert into view name values(expressions-list);

Eg: SQL> insert into view-empl values (&eno,&ename,&job,&mgr,&doj,&sal,&deptno); Enter value for eno : 7369 Enter value for ename : smith Enter value for job : clerk Enter value for mgr : 7902 Enter value for doj : 01-03-99 Enter value sal : 500 Enter value for deptno: 10 Old 1: Insert into view_emp1 values(&eno,&ename,&job,&mgr,&sal,&deptno); New 1: Insert into view_emp1 values(7369,Smith,Clerk,7902,01/03/99,500,10); Output: 1 row created. Eg: SQL>/ Enter value for eno : 7499 Enter value for ename : Wallen Enter value for job : Salesman Enter value for mgr : 7698 Enter value for doj : 07-09-98 Enter value sal : 800 Enter value for deptno: 20 Old 1: Insert into view_emp1 values(&eno,&ename,&job,&mgr,&sal,&deptno); New 1: Insert into view_emp1 values(7499,Wallen,Salesman,7698,07-0998,800,20); Output: 1 row created. Eg: SQL>/ Enter value for eno : 7521 Enter value for ename : Ward Enter value for job : clerk Enter value for mgr : 7782 Enter value for doj : 03-07-97 Enter value sal : 1600 Enter value for deptno: 30

Old 1: Insert into view_emp1 values(&eno,&ename,&job,&mgr,&sal,&deptno); New 1:Insert into view_emp1 values(7521,Ward,Clerk,7782,03/07/97,1600,30); Output: 1 row created. Eg: SQL>/ Enter value for eno : 7566 Enter value for ename : Allen Enter value for job : Analyst Enter value for mgr : 7369 Enter value for doj : 01-12-97 Enter value sal : 1200 Enter value for deptno: 20 Old 1: Insert into view_emp1 values(&eno,&ename,&job,&mgr,&sal,&deptno); New 1:Insert into view_emp1 values(7566,Aiien,Analyst,7369,01/12/97,1200,20); Output: 1 row created. Eg: SQL>/ Enter value for eno : 7654 Enter value for ename : Blake Enter value for job : Manager Enter value for mgr : 7521 Enter value for doj : 25-06-95 Enter value sal : 8000 Enter value for deptno: 30 Old 1: Insert into view_emp1 values(&eno,&ename,&job,&mgr,&sal,&deptno); New 1:Insert into view_emp1 values(7654,Blake,Manager,7521,25/06/95,8000,30); Output: 1 row created. Eg: SQL>/ Enter value for eno : 7698 Enter value for ename : Clark Enter value for job : Salesman

Enter value for mgr : 7521 Enter value for doj : 03-02-95 Enter value sal : 8000 Enter value for deptno: 10 Old 1: Insert into view_emp1 values(&eno,&ename,&job,&mgr,&sal,&deptno); New 1:Insert into view_emp1 values(7698,Clark,Salesman,7521,03/02/95,8000,10); Output: 1 row created. Eg: SQL>/ Enter value for eno : 7782 Enter value for ename : Scott Enter value for job : Clerk Enter value for mgr : 7466 Enter value for doj : 23-09-92 Enter value sal : 1500 Enter value for deptno: 20 Old 1: Insert into view_emp1 values(&eno,&ename,&job,&mgr,&sal,&deptno); New 1:Insert into view_emp1 values(7782,Scott,Clerk,7466,23/09/92,1500,20); Output: 1 row created. 1.Retrieve view details. Eg: SQL> Select * from view_emp1; Output: Eno Ename Job Mgr Doj 7369 Smith Clerk 7902 01-03-99 7499 Wallen Salesman 7698 07-09-98 7521 Ward Clerk 7782 03-07-97 7566 Allen Analyst 7369 01-12-97 7654 Blake Manager 7521 25-06-95 7698 Clerk Salesman 7521 03-02-95 7782 scott clerk 7466 23-09-92 2. Display emp details where salary is<1000. Eg: SQL> Select * from view_emp1 where sal<1000. Output:

Sal 500 800 1600 1200 8000 8000 15000

Deptno 10 20 30 20 30 10 20

Eno Ename Job Mgr Doj Sal 7369 Smith Clerk 7902 01-03-99 500 7499 Wallen Salesman 7698 07-09-98 800 3. Display emp details where eno is 7654. Eg: SQL> Select * from view_emp1 where eno=7654; Output: Eno 7654 Ename Blake Job Manager Mgr 7521 Doj Sal 25-06-95 8000

Deptno 10 20

Deptno 30

4. Delete emp details where eno is 7654. Eg: SQL> Delete from view_emp1 where eno=7654; Output: 1 row delete. 5. Update emp details. Eg: SQL> Update view_emp1 Set job=Work Where job=Clerk; Output: 3 rows updated. 6. Update salary whose is getting < 1000. Eg: SQL> Update view_emp1 Set sal=sal+1000 Where sal<1000; Output: 2 rows updated.

Duals Duals means with in junction and it is the mathematical function. 1. Find the logarithmic value of 5 Syntax : SQL > Select ln (5) from dual; Output: 1.60943791 2.Find the sign of the number -5 Syntax : SQL > Select sign (-5) from dual; Output : -1 3. Find the size of the word Hello Syntax : SQL > Select Vsize (hello) from dual; Output : 5 4.Find mod of any two nos Syntax : SQL > Select mod (4,7) from dual; Output : 4 5. Find sin value for 90 Syntax : SQL >Select sin(90) from dual; Output : .893996664 6. Find square root of 36 Syntax : SQL >Select sqrt(36) from dual; Output : 6 7. Find power value of 3,2 Syntax : SQL > Select power(3,2) from dual; Output : 9 8. Write smith in upper case letters Syntax : SQL > Select upper(smith) from dual;

Output : Smith 9. Write down in the form of in it cap Syntax : SQL >select initcap(wallen) from dual; Output : Wallen 10. Write L pad for the empname smith Syntax: SQL > Select Lpad (smith,(10),*) from dual; Output : *****smith 11. Write Rpad for the empname smith Syntax : SQL > Select Rpad (smith,(10), $) from dual; Output : Smith$$$$$ 12. Write empname smith in reverse order Syntax : SQL > Select reverse (smith) from dual; Output : Htims 13. Find absolute value for -25 Syntax : SQL > Select abs(-25) from dual; Output : 25 14. Translate the letters th in smith by lk Syntax: SQL > Select translate (smith, th, lk) from dual; Output: Smilk 15. Replace the letters th in smith by lk. Syntax: SQL > Select replace (smith, th, lk) from dual; Output: Smilk 16. Write LTRIM for HHHSHADAAH Syntax: SQL > Select LTRIM (HHHSHADASH, H) from dual;

Output: Shadash 17. Write RTRIM for HHHSHADASH Syntax: SQL > Select RTRIM (HHHSHSDASH, H) from dual; Output: hhhshadas. 18. Display the system date Syntax: SQL > Select sysdate from dual; Output: SYSDATE --------11-MAR-09 19. Display $after the salary at one place only. Syntax: SQL > Select rpad (sal,length(sal)+1, $) from emp; Output: 800$ 1600$ 1250$ 2975$ 2850$ 2450$ 3000$ 5000$ 20. Write RANI in lower case letters Syntax : SQL > Select lower(RANI) from dual; Output : rani 21. Concatenate of two strings. Syntax: Select concat('suda', 'rani') from dual; OUTPUT: CONCAT(' -------sudarani

22. Find the length of sting; Syntax: Select length('Drkvsrit') from dual; OUTPUT: LENGTH('DRKVSRIT') -----------------8 23. Print a substring from required position. Syntax: SQL> Select substr('suda rani',5) from dual; OUTPUT SUBST ----rani 24. Print a substring position where it occur in source string. Syntax: SQL> Select instr('suda rani','rani') from dual; INSTR('SUDARANI','RANI') -----------------------6

PL/SQL PL/SQL: PL/SQL is a combination of SQL with procedural language. PL/SQL is a back structured language. PL/SQL combines basic components Called blocks like precedence (s), functions. PL/SQL allows all SQL statements except DDL statements. PL/SQL allows DML commands such as insert, delete, update, select. Structure of PL/SQL: Every PL/SQL program consists of 3 parts. 1) A declare section for variable declaration. 2) A selection for execution statements. 3) A selection for exception hadiling. Syntax: commands Declare declaration Begin executable SQL statements [Exception] . Optional End; Declare: This section is the start of the block consisting of variables, constants. Begin: This section start the block of executable parts consists of all Executable statements. End: It signals the end of the block. Exception: The exception block consists information about Exception handling. This is a optional block. 1. Write PL/SQL program for area of circle SQL > Set serveroutput on SQL> Create table area (r number(5), area varchar2(10)); Table created; SQL > ed area1.sql SQL > declare

R number:=&R; Area varchar2(10); Begin Area :=3.14*r*r; Insert into area value (R,area); End; SQL>@area1.sql Enter value for r:2 Old 2 : R number: = &R New 2: R number : = 2 PL/SQL procedure successfully completed SQL> Select * from area; Output: R 2 Area 12.56

2.Write PL/SQL pgm for area of triangle. SQL>create table tri(b number(5),h number(5),area varchar2(10)); Table Created SQL>ed tril.sql; SQL>declare B number:=&B; H number:=&H; area varchar2(10); begin area:=0.5*B*H; insert into tri values(B,H,area); end; / enter value for b=2 old2:B number:=&B; new2:B number:=2; enter value for H=3 old2:H number:&H; new2:B number:=3; PL/SQ/l procedure successfully completed. SQL>select * from tri;

B H AREA 2 3 3 3.Write PL/SQL program for factorial of a number. SQL>create table fact1(n number(5),fact number(5)); Table created. SQL>declare N number:=&N; Fact number:=1; begin for i in 1..n loop fact:=fact*i; end loop; insert into fact1 values(N,fact); end; / SQL>@ fact1.sql enter value for n=3 old2: N number:=&N; new2: N number:=3; PL/SQL procedure successfully completed SQL>select * from fact1; N fact 3 6 4.Write PL/sQL program to find the fibonacci series. SQL>Create table fib(n number(4),f1 number(4),f2 number(4),f3 number(4),i number(4)); Table Created. SQL>declare n number(4); f1 number(4);

f2 number(4); f3 number(4); i number(4); Begin n=&n; i==2; f1=0; f2=1; DBMS_OUTPuT.PUT_LINE(f1); DBMS_OUTPuT.PUT_LINE(f2); while(i<=n) loop f3=f1+f2; DBMS_OUTPuT.PUT_LINE(f3); f1=f2; f2=f3; i=i+1; end loop; end; / OUTPUT: Enter value for n=3; old 8 :n:=&n; new 8 :n:=3; 0 1 1 2 PL/SQL procedure successfully completed SQL>\ Enter value for n=5 old 8: n:=&n; new 8: n:=5; 0 1

1 2 3 5 PLSQL procedure successfully completed. 5.Write a PL/SQL program for reverse of a string. SQL>create table rev(len number(10),rstr varchar2(20),str varchar2(10)); Table created SQL>declare len number(10); rstr varchar2(20); str varchar2(10)= '& string'; begin len:=length(Str); for i in reverse 1..len loop rstr:=rstr||substr(str,i,1); endloop; DBMS_OUTPuT.PUT_LINE(rstr); end;

OUTPUT: enter valule for string:naveen old4=str varchar2(10)=='&string'; new4=str varchar2(20)=='naveen'; neevan PL/SQL procedure successfully completed. 6.write a program for biggest number.

SQL>create table big1(n1 number(2),n2 number(2),n3 number(3),big number(2));

Table created SQL>declare n1 number(2):=&n1; n2 number(2):=&n2; n3 number(2):=&n3; big number(2); begin big:=n1; if big<n2 then big:=n2; end if; if big<n3 then big:=n3; end if; insert into big1 values(n1,n2,n3,big); DBMS_OUTPuT.PUT_LINE(big); end; / OUTPUT: enter value for n1=45 old 2=n1 number(2):=&n1; new 2=n1 number(2):=45; enter value for n2=38; enter value for n3=64; 64 PL/SQL procedure successfully completed. 7.write a PL/SQL program to find a number whether it is palindrome or not. SQL>create table pal(n number(10),temp number(10),rev number(10),rev1 number(10),rem number(10)); SQL>declare n number(10):=&n; temp number(10); rev number(10):=0; rev1 number(10); rem number(10);

begin temp:=n; while temp>0 loop rem:=mod(temp,10); rev1:=rev*10; rev:=rev1+rem; temp:=temp/10; end loop; if(rev=n) then DBMS_OUTPuT.PUT_LINE('palindrome'); else DBMS_OUTPuT.PUT_LINE('not palindrome'); end if; end; / OUTPUT: enter value for n 1221 palindrome PL/SQL procedure successfully completed. 8.write a PL/SQL program for sum and product of three numbers. SQL>create table sap(a number(5),b number(5),c number(5),sum number(7),avg number(7),product number(7)); SQL>ed sap1.sql; SQL>declare a number:=&a; b number:=&b; c number:=&c; sum number; product number; avg number; begin sum:=a+b+c;

avg:=(a+b+c)/3; product:=a*b*c; insert into sap values(a,b,c,sum,avg,product); end; / OUTPUT:SQL> SQL>@sap1.sql; enter values for a=2 old 2:a number:=&a new 2;a number:=2 enter values for b=3 old 3:b number:=&b new 3:b number:=3 enter value for c:1 old 4:c number:=&c new 4:c number:=1 PL/SQL procedure successfully completed select * from sap; A B C SUM AVG PRODUCT 2 3 1 6 2 6

9.Write PL/SQL program to perimeter and area of a square. SQL>create table pa(a number(5),p number(15),area number(5)); SQL>ed pa1.sql SQL>declare a number:=&a; p number(5); area number(5); begin p:=4*a; area:=a*a; insert into pa values(a,p,area); end;

/ OUTPUT: enter value for a: 2 old 2:a number:=&a; new 2:a number:=2; PL/SQL procedure successfully completed SQL>select * from pa; A P AREA 2 8 4 ------------------------------------------------------------------------------------------------10.Write PL/SQL program to find the given number is even or odd SQL>create table even(n number(10)); Table created SQL>create table odd(n number(10)); Table created SQL>declare n number(2):=&n; begin if(mod(n,2)=0) then insert into even values(n); else insert into odd values(n); end if; end; / OUTPUT: enter values for n:3 old 2: n number(2):=&n;

new 2: n number(2):=3; PL/SQL procedure successfully completed SQL>select * from event; no rows selected. SQL>select * from odd; N 3

TRIGGERS A trigger is a statement (action) that is executed automatically by the system in a side effect of the modification in database. A trigger is a procedure that is automatically executed whenever an event occurs in database. A trigger describes three parts event.A change in database that activates trigger. Condition: It is a procedure that is executed when trigger is activated,its condition is true. A tree is an useful mechanism for altering or performing certain events automatically in database and some conditions are met.A trigger may be in the form of fig events. 1.insert 2.delete 3.update Syntax: create/replace trigger<trigger name> before insert on <table name> [update] [after][Delete] for each row declare begin

DBMS_OUTPuT.PUT_LINE('trigger message'); <SQL> statement> end; / TRIGGERS 1.create triggers on dept before inserting values SQL>ed t1.sql; create or replace trigger t1 before insert ion dept for each row begin DBMS_OUTPUT.PUT_LINE('insert'); end; / SQL>@ t1.sql; trigger created; SQL>insert into dept values(&deptno,'&dname','&loc'); enter value for deptno=10 enter value for dname=accounting enter value for loc=newyork 1 row created. SQL> enter value for deptno=20 enter value for dname=research enter value for loc=dallar 1 row created. SQL>select * from dept; deptno dname 10 20 loc newyork dallar

accouting research

2.create trigger on dept after inserting values. SQL>set serveroutput on SQL>ed t1.sql; create or replace trigger t1

after insert on dept for each row begin DBMS_OUTPuT.PUT_LINE('insert'); end; / SQL>@ t1.sql; trigger created. SQL> insert into dept values(& deptno,'&deptname',&loc'); enter value for deptno:45 enter value for dname:machines enter value for loc:Delhi old 1:insert into dept values(&deptno,'&dname','&loc'); new 1:insert into dept values(45,'machines','Delhi'); 1 row created. SQL>select * from dept; deptno 10 20 45 dname Acounting newyork Research dallar machines delhi loc

3.create trigger on hospital before delete values SQL>ed s2.sql; create or replace trigger s1 before delete on hospital for each row begin DBMS_OUTPuT.PUT_LINE('delete'); end; / SQL>@ s2.sql; Trigger created; SQL>delete form hospital where docname='abhi'; 1 row deleted. SQL>select * from hospital;

docname

dno

pname 2

pno

raghu 1 balu ravi 3 siva 2 sankar 3 prasad 5 raghu 3 ravi 4 4 rows selected.

4.create trigger on hospital after delete the values. SQL>ed s2.sql; create or replace trigger s1 after delete on hospital for each row begin DBMS_OUTPuT.PUT_LINE('delete'); end; / SQL>@ s2.sql; Trigger created SQL>delete from hospital where dname='ravi'; 1 row deleted. SQL>select * from hospital; Dname dno pname 2 pno

raghu 1 balu sankar 3 prasad 5 raghu 3 ravi 4 3 rows selected.

5.create trigger on hospital before update values SQL>ed s1.sql; create or replace trigger s1 before update on hospital for eachrow

begin DBMS_OUTPuT.PUT_LINE('update'); end; / SQL>@ s1.sql; Trigger created. SQL>update hospital set pno=pno*2; where pno<3; 2 rows updated SQL>select * from hospital; dname raghu ravi snakar 3 raghu 3 dno pname pno 4 4

1 balu 3 siva prasad 5 ravi 4

4 rows selected.

Potrebbero piacerti anche