SlideShare a Scribd company logo
PreparedBy JAVATECH Search us in The World
Chapter DiscussionAbout Database Object, SubQuery, Joining ( 3 chapters inone class)
Database has few objects. They are
1. Table : Basic unit of storage which represented as rows and columns format.
2. View : It is logical subsets of data from more than one table.
3. Sequence : It is used to generate Unique integer number.
4. Synonym : It is an ulternate name for other database objects.
5. Index : Oracle server used index to faster access the rows using pointer.
View : It is logical subsets of data from more than one table. There are two types of view.
1. Simple view
2. Complex View
Why we use view
1. To restrict Data access : View doesnot allows to access all columns from table. It is created selected
columns only. Importants columns hides from other user.
2. Make complex query easy : You don’t need everytime specify join command. Only write once complex
commnd and make view. Then only access that view.
Simple View
The view which consists of only one tble is called simple view. The DML operation can perform with
this view. If you want restrict DML operation you can write WITH READ ONLY statement to avoid DML
operation
Complex View
The view which consists of more than one table is called Complex View. It restricts DML operation.
ROLL NAME DOB MARK
1 A 12-JAN-1990 500
2 B 16-JUN-1992 408
3 C 19-JUL-2000 557
4 D 30-MAY-1997 300
5 E 22-SEP-2000 490
Create View v1 as select roll,mark from student;
Above command create view name v1 consists of two column;
You can define your own column inside view
Create or replace view v1(MyRoll,MyMark) as select roll,mark from student;
Above command updated your view (because i write replace command) with MyRoll and MyMark column.
If you write select statement
Select * from v1;
It display like
MyRoll MyMark
1 500
2 408
3 557
4 300
5 490
You can update view
Update set mark=900 where roll=1;
After select from v1
MyRoll MyMark
1 900
2 408
3 557
4 300
5 490
Same also updated in Table
Select * from student;
ROLL NAME DOB MARK
1 A 12-JAN-1990 900
2 B 16-JUN-1992 408
3 C 19-JUL-2000 557
4 D 30-MAY-1997 300
5 E 22-SEP-2000 490
If you want to restrict DML operation in view then use WITH READ ONLY
E.g.
Create View v2 as select roll,mark from student where roll=2 WITH READ ONLY;
Then when you try to update view v2 whose roll is two it display arror.
Complex view
The view which consists of more than one table is called Complex table. Here DML command is
restriction
See the E.g
Create view v3(MyRoll,MyName,MyDept,Price) as Select stud1.roll,stud1.name,dept.name,dept.price from
stud,dept where stud.roll=dept.roll;
Here you can see view v3 is (MyRoll,MyName,MyDept,Price) is created. It is consists of 4 columns.
Sequence
Sequence is used to create to generate unique integer values. Declaraton of Synnym
Create sequence sequence_name : sequence name
Increment by n - sequence increment by n, specify the interval between sequence number
Strart with n – n is specified by the starting value of sequence
Max value n – n specified where sequence number will be start.
Min value n - n specify the minimum sequence no
Cycle / nocycle – after reach at max valueit will be recycled. Means again starts value from minimum no.
Cache n/ nocace – how many number oracle can allocate memory to store in memory. Numbers are faster
access from memory.
See how created ?
Create sequence s1 //sequence name is s1
Increment by 1 //sequence is incremented by 1
Start with 3 //sequence start value
MaxValue 30 //sequence will stop as it reached 30
MinValue 2 //After reached 30 next time it start from 2. Not again from start value. And minvalue
//should less than start value.
Cycle //cycle means value will be recycled after reach max value.
Error:ORA-04013 Number to Cache must be less than one cycle. So max value should specify 22 or above.
Default cache is 21.
Sequence Created.
To check sequence
Select s1.nextval from dual;
Select s1.currvalue from dual;
To verify user sequence from user_sequence data dictionary
SELECT sequence_name, min_value, max_value, increment_by, last_number FROM user_sequences;
You can also write
Insert into stud values(s1.nextval,’aa’,789);
To drop sequence
Drop sequence sequence_name;
Drop sequence s1;
Synonym
It is called alternate name for table.
E.g.
Create synonym sn for student_sce_dept;
Then instead of write big table name we use it through small name sn.
E.g. select * from sn; it display data from student_sce_dept.
To drop synonym
Drop synonym sn;
Index
Is used by the Oracle server to speed up the retrieval of rows by using a pointer
two types index
Automatically: when we declare primary key or unique key.
Manually: Users can create nonunique indexes on columns to speed up access to the rows.
CREATE INDEX emp_last_name_idx ON employees(last_name);
Index created.
SUB QUERY
It is called inner query. The query which is exists in another query is called inner query. It is also called nested
query.
Features
1. It is enclosed with parenthesis
2. It is written right side of outer query.
3. Inner query first executed then outer query executed.
4. Outer Query executed after result return from inner query.
Two types of inner query
Sometimes inner query returns two types result. First single result second multiple row result. Based upon this
It is two types.
1. Single row return inner query
2. Multi row return inner query
E.g.
Select name from student where mark<(select mark from student where name=’Ramesh’);
Right side is called inner query. First we should know what is mark of Ramesh. Then we select mark those are
below ramesh mark. In bold and underlined mark is called inner query.
Single row returns inner query operator
1. =
2. >
3. <
4. >=
5. <=
6. <>
Multiple rows returns inner queries
1. IN : If outer query matched any value from IN operator then it returns result.
2. ANY : <ANY means Less than the maximum. >ANY means more than the minimum.=ANY is equivalent
to IN
3. ALL : <ALL means less than the minimum and >ALL means more than the maximum.
Select name from student where mark in (300,700,800);
Or
Select name from student where mark in(select mark from student where mark<900);
JOIN command
Join command is used to return query more than one table.
Prior to Join Oracle8i or Prior
1. Cartesian Join
2. Equi-Join same as simple join same as inner join
3. Non-Equi Join
4. Outer Join
5. Self Join
Oracle 9i or Next
1. Cross Join
2. Natural Join
3. Using clause
4. ON clause
5. Full or two sided outer join
CartesianJoin
When we specify join command invalid or ommit then result of cartesian join will be produced
between two table.
E.g.
Selet roll,name,mark,branch from stud,dept;
Here stud has 5 rows and dept has 4 rows. Then total 5*4=20 rows will be ceated.
Equi Join:
It is based upon equality of two values of same column of two different tables. It use the (=) operator.
Select stud.roll,name,mark,branch from stud,dept where stud.roll=dept.roll;
It display roll,name from stud table mark branch from dept table those roll are same to both table. (=)
Table alias
Select s.roll,s.name ,d.mark,d.branch from stud s,dept d where s.roll=d.roll;
Above we specify alias name for both table s & d. S is alternate name for stud table and D is alternate
name for Dept table. Advantages No need to write tablename repeatedly. Only use the shortcut name.
For faster write the query.
Ambiguity inColumn.
Here roll is common column for stud and dept table. When we write query
select roll,name,mark,branch from stud,dept .............
Oracle server will be confuse to take about roll. Because it is common. So we specify which table roll
column we want to display. Modified query is
Select stud.roll , name, mark branch from stud,dept where stud.roll=dept.roll;
Non Equi Join
The operator which are used other than (=) is called Non Equi Join. Operators are used
1. >
2. <
3. <>
4. Between
Select s.name,d.mark from stud s,dept d where s.mark between d.high_mark and g.low_mark;
Left outer Join / Right Outer Join / Full Outer Join
Outer Join-It is an extension of inner join. It returns rows that are matched to both tables. And also returns
those are not matched to another table.
It returns the rows those are missed in join condition. Outer join symbol is (+) sign
e.roll(+)=d.roll //left outer join It returns the rows that are matched to both table except return rows those
that are not matched to the first table.
e.roll=d.roll(+) //right outer join it returns the rows that are matched to both table except returns the rows
that are not matched to the second table.
Full outer join: The join between two tables, that returns the result of an inner join as well as the result of left
outer join and return result of right join is a full outer join.
Full outer join
select name,mark from join7,join8 where join7.roll(+)=join8.roll union all select name,mark from join7,join8
where join7.roll=join8.roll(+)
Right Outer Join
select name,mark from join7,join8 where join7.roll=join8.roll(+);
Left Outer Join
select name,mark from join7,join8 where join7.roll(+)=join8.roll;
Self Join
From Stud d Natural Join Dept d; automatically choose same column name by oracle.
From stud s join dept d using (roll) // using clause is used to specify column name by user choice in join
condition
From stud s join dept d on(s.roll=d.my_roll) on clause is used to specify that have different column name.
Here roll & my_roll is not matched
Left outer join
select s.name,d.mark from join7 s left outer join join8 d on (s.roll=d.roll)
Right outer join
select s.name,d.mark from join7 s right outer join join8 d on (s.roll=d.roll)
Full outer join
select s.name,d.mark from join7 s full outer join join8 d on (s.roll=d.roll)
Prepared By JAVATECH Search us in The World
ASSIGNMENT ON DATABASE OBJECT,SUB-QUERY AND JOIN COMMAND
STUDENT
STU_ROLL NAME DOB MARK BRANCH SECTION
AMIT 13/05/2010 600 SCE IT-1
AJAY 25/09/2009 550 IT IT-2
ROHIT 15/12/2008 450 IT CS-1
MUKESH 02/11/2007 390 SCE CS-2
JOHN 30/03/2005 400 IT CS-3
FACULTY
FACULTY_ID FAC_NAME STU_ROLL FAC_SUBJECT CLASS_ROOM CLASS_DATE_TIME
JOSEPH JAVA C1
STEPHEN C++ C2
RICHARDSON DOTNET C3
JAMES PHP C4
AKS ORACLE C5
TIPS:- CLASS_DATE_TIME DATATYPE IS TIMESTAMP(2) - TIMESTAMP IS USED FOR TO SPECIFY TIME. 2
MEANS AFTER SECOND VALUE FRACTION VALUE WON’T DISPLAY. ONLY TWO DIGIT SECOND DISPLAY.
INSERT INTO TABLE VALUES(TO_DATE(’12-JAN-2001 13:34:56’,’DD-MON-YY HH24:MI:SS’));
1. Enter exact data into student table. But roll should insert using sequence, it should start from 1 and
increment by 1.[total 5 records]
2. Enter exact data into faculty table. But faculty id should insert using sequence, it should start from 1
and increment by 3[total 5 records]. E.g. 1, 4, 7, 10, 13 Also enter exact student roll into faculty table
from referencing student table by using sequence but it should be increment by 2. E.g. 1, 3, 5, 7, 9
[total 5 records]
3. Waq to display sequence name, minimum value, maximum value from all created sequences.
4. Waq to drop these two sequences.
5. Create synonym s55 for student table. And query all rows from synonym. Then drop it.
6. Create view stv1 from student table by selecting roll, name , mark & branch column.
7. Waq to update marks from all rows from stv1 view.
8. Waq to create view stv2 from student table selecting roll, name, dob & mark of rollno 3 which can’t be
deleted from view.
9. Drop these two view from database.
10. Waq to create view st_fa from student & faculty selecting stu_roll, stu name, faculty id, faculty name
where rollno of student table should same to student roll of faculty table.
11. Select and drop st_fa view.
12. Waq to display the student roll, name, dob, mark and faculty name, subject, classroom, from student,
faculty table those student rollno are common to student rollno of faculty table using equi join.
13. Waq to display the all distinct rollno of student table those student rollnos are less than from faculty
table rollno using non equi join.
14. Waq to join student & faculty table without using any join command. And check how many rows
created.
15. Waq to check from faculty table, which student’s rollno and faculty’s id are same. Then display their
faculty names only.
16. Waq to display student roll,name & facult id, fac name from student and faculty tables those are
common roll no by using natural join.
17. Waq to display student’s roll,name and faculty id, fac name from student,faculty table by using ‘using’
clause.
18. Waq to display student roll,name & faculty id, fac name from student, faculty where faculty id and
student roll are same.
19. Waq to display student roll, name, mark, fac id, fac name from student, faculty table those are
common rollno to both table except those rollno of student table are also not matched to faculty
table.
20. Waq to display student roll, name, mark, faculty id, fac name from student,faculty table those are
common rollno except display fac id, fac name of corresponding rows of stu rollno of faculty table
those are not matched to student table rollno.
LIKE OUR FACEBOOK PAGE “JAVATECH 123” TO GET UPDATED NOTE & VIDEOS
Prepared By JAVATECH Search us in The World
**************************BEST OF LUCK***********************
Next – PLSQL will Start
PL-PROGRAMMING LANGUAGE+SQL
PROGRAMMING LANGUAGED USED WITH SQL STATEMENTIS CALLED PLSQL

More Related Content

Similar to Database object, sub query, Join Commands & Lab Assignment (20)

PPTX
Day-2 SQL Theory_V1.pptx
uzmasulthana3
 
PPT
SQL subquery
Vikas Gupta
 
PPTX
View od dffmfmfmm,dm,f,dm,dfm,dddfdfsd,sd,sddf,df,ldf
talhahakeem295
 
PPTX
Module 3.1.pptx
ANSHVAJPAI
 
PPTX
Joins.pptxjjbmmmnnnnnjjjxrhjfluflurulrdudlu
c4x6vgk2n7
 
PPT
Joins.ppt
UmangThakkar26
 
PPTX
SQL Joins and View.pptx
pallavipatil634279
 
PPTX
MySQL Queries
mysql content
 
PPTX
MySql: Queries
DataminingTools Inc
 
PPTX
1. dml select statement reterive data
Amrit Kaur
 
PDF
SQL dabatase interveiw pdf for interveiw preparation
kumarvikesh2841998
 
PPTX
ADVANCE ITT BY PRASAD
PADYALAMAITHILINATHA
 
PPT
Ms sql server ii
Iblesoft
 
PDF
0808.pdf
ssuser0562f1
 
PPTX
Joins and Views.pptx
SangitaKabi
 
PPTX
askndmf,dskmlf,bvdmk,v nmdsvjkmc,dvjkcmsdcvjnkmd
talhahakeem295
 
PPTX
Joins
Mritunjay Sharma
 
PPT
Introduction to-sql
BG Java EE Course
 
PPTX
Data Retrival
Er. Nawaraj Bhandari
 
PPT
Chapter07 database system in computer.ppt
ubaidullah75790
 
Day-2 SQL Theory_V1.pptx
uzmasulthana3
 
SQL subquery
Vikas Gupta
 
View od dffmfmfmm,dm,f,dm,dfm,dddfdfsd,sd,sddf,df,ldf
talhahakeem295
 
Module 3.1.pptx
ANSHVAJPAI
 
Joins.pptxjjbmmmnnnnnjjjxrhjfluflurulrdudlu
c4x6vgk2n7
 
Joins.ppt
UmangThakkar26
 
SQL Joins and View.pptx
pallavipatil634279
 
MySQL Queries
mysql content
 
MySql: Queries
DataminingTools Inc
 
1. dml select statement reterive data
Amrit Kaur
 
SQL dabatase interveiw pdf for interveiw preparation
kumarvikesh2841998
 
ADVANCE ITT BY PRASAD
PADYALAMAITHILINATHA
 
Ms sql server ii
Iblesoft
 
0808.pdf
ssuser0562f1
 
Joins and Views.pptx
SangitaKabi
 
askndmf,dskmlf,bvdmk,v nmdsvjkmc,dvjkcmsdcvjnkmd
talhahakeem295
 
Introduction to-sql
BG Java EE Course
 
Data Retrival
Er. Nawaraj Bhandari
 
Chapter07 database system in computer.ppt
ubaidullah75790
 

More from Arun Sial (6)

PDF
Triggers in plsql
Arun Sial
 
PDF
PLSQL CURSOR
Arun Sial
 
PDF
Exception handling in plsql
Arun Sial
 
PDF
Procedure and Function in PLSQL
Arun Sial
 
PDF
PLSQL Note
Arun Sial
 
PDF
SQL BUILT-IN FUNCTION
Arun Sial
 
Triggers in plsql
Arun Sial
 
PLSQL CURSOR
Arun Sial
 
Exception handling in plsql
Arun Sial
 
Procedure and Function in PLSQL
Arun Sial
 
PLSQL Note
Arun Sial
 
SQL BUILT-IN FUNCTION
Arun Sial
 
Ad

Recently uploaded (20)

PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
PDF
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
Tally software_Introduction_Presentation
AditiBansal54083
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
Ad

Database object, sub query, Join Commands & Lab Assignment

  • 1. PreparedBy JAVATECH Search us in The World Chapter DiscussionAbout Database Object, SubQuery, Joining ( 3 chapters inone class) Database has few objects. They are 1. Table : Basic unit of storage which represented as rows and columns format. 2. View : It is logical subsets of data from more than one table. 3. Sequence : It is used to generate Unique integer number. 4. Synonym : It is an ulternate name for other database objects. 5. Index : Oracle server used index to faster access the rows using pointer. View : It is logical subsets of data from more than one table. There are two types of view. 1. Simple view 2. Complex View Why we use view 1. To restrict Data access : View doesnot allows to access all columns from table. It is created selected columns only. Importants columns hides from other user. 2. Make complex query easy : You don’t need everytime specify join command. Only write once complex commnd and make view. Then only access that view. Simple View The view which consists of only one tble is called simple view. The DML operation can perform with this view. If you want restrict DML operation you can write WITH READ ONLY statement to avoid DML operation Complex View The view which consists of more than one table is called Complex View. It restricts DML operation. ROLL NAME DOB MARK 1 A 12-JAN-1990 500 2 B 16-JUN-1992 408 3 C 19-JUL-2000 557 4 D 30-MAY-1997 300 5 E 22-SEP-2000 490 Create View v1 as select roll,mark from student; Above command create view name v1 consists of two column; You can define your own column inside view Create or replace view v1(MyRoll,MyMark) as select roll,mark from student;
  • 2. Above command updated your view (because i write replace command) with MyRoll and MyMark column. If you write select statement Select * from v1; It display like MyRoll MyMark 1 500 2 408 3 557 4 300 5 490 You can update view Update set mark=900 where roll=1; After select from v1 MyRoll MyMark 1 900 2 408 3 557 4 300 5 490 Same also updated in Table Select * from student; ROLL NAME DOB MARK 1 A 12-JAN-1990 900 2 B 16-JUN-1992 408 3 C 19-JUL-2000 557 4 D 30-MAY-1997 300 5 E 22-SEP-2000 490 If you want to restrict DML operation in view then use WITH READ ONLY E.g. Create View v2 as select roll,mark from student where roll=2 WITH READ ONLY; Then when you try to update view v2 whose roll is two it display arror.
  • 3. Complex view The view which consists of more than one table is called Complex table. Here DML command is restriction See the E.g Create view v3(MyRoll,MyName,MyDept,Price) as Select stud1.roll,stud1.name,dept.name,dept.price from stud,dept where stud.roll=dept.roll; Here you can see view v3 is (MyRoll,MyName,MyDept,Price) is created. It is consists of 4 columns. Sequence Sequence is used to create to generate unique integer values. Declaraton of Synnym Create sequence sequence_name : sequence name Increment by n - sequence increment by n, specify the interval between sequence number Strart with n – n is specified by the starting value of sequence Max value n – n specified where sequence number will be start. Min value n - n specify the minimum sequence no Cycle / nocycle – after reach at max valueit will be recycled. Means again starts value from minimum no. Cache n/ nocace – how many number oracle can allocate memory to store in memory. Numbers are faster access from memory. See how created ? Create sequence s1 //sequence name is s1 Increment by 1 //sequence is incremented by 1 Start with 3 //sequence start value MaxValue 30 //sequence will stop as it reached 30 MinValue 2 //After reached 30 next time it start from 2. Not again from start value. And minvalue //should less than start value. Cycle //cycle means value will be recycled after reach max value. Error:ORA-04013 Number to Cache must be less than one cycle. So max value should specify 22 or above. Default cache is 21. Sequence Created. To check sequence Select s1.nextval from dual; Select s1.currvalue from dual; To verify user sequence from user_sequence data dictionary SELECT sequence_name, min_value, max_value, increment_by, last_number FROM user_sequences;
  • 4. You can also write Insert into stud values(s1.nextval,’aa’,789); To drop sequence Drop sequence sequence_name; Drop sequence s1; Synonym It is called alternate name for table. E.g. Create synonym sn for student_sce_dept; Then instead of write big table name we use it through small name sn. E.g. select * from sn; it display data from student_sce_dept. To drop synonym Drop synonym sn; Index Is used by the Oracle server to speed up the retrieval of rows by using a pointer two types index Automatically: when we declare primary key or unique key. Manually: Users can create nonunique indexes on columns to speed up access to the rows. CREATE INDEX emp_last_name_idx ON employees(last_name); Index created. SUB QUERY It is called inner query. The query which is exists in another query is called inner query. It is also called nested query. Features 1. It is enclosed with parenthesis 2. It is written right side of outer query. 3. Inner query first executed then outer query executed.
  • 5. 4. Outer Query executed after result return from inner query. Two types of inner query Sometimes inner query returns two types result. First single result second multiple row result. Based upon this It is two types. 1. Single row return inner query 2. Multi row return inner query E.g. Select name from student where mark<(select mark from student where name=’Ramesh’); Right side is called inner query. First we should know what is mark of Ramesh. Then we select mark those are below ramesh mark. In bold and underlined mark is called inner query. Single row returns inner query operator 1. = 2. > 3. < 4. >= 5. <= 6. <> Multiple rows returns inner queries 1. IN : If outer query matched any value from IN operator then it returns result. 2. ANY : <ANY means Less than the maximum. >ANY means more than the minimum.=ANY is equivalent to IN 3. ALL : <ALL means less than the minimum and >ALL means more than the maximum. Select name from student where mark in (300,700,800); Or Select name from student where mark in(select mark from student where mark<900); JOIN command Join command is used to return query more than one table. Prior to Join Oracle8i or Prior 1. Cartesian Join 2. Equi-Join same as simple join same as inner join 3. Non-Equi Join 4. Outer Join 5. Self Join
  • 6. Oracle 9i or Next 1. Cross Join 2. Natural Join 3. Using clause 4. ON clause 5. Full or two sided outer join CartesianJoin When we specify join command invalid or ommit then result of cartesian join will be produced between two table. E.g. Selet roll,name,mark,branch from stud,dept; Here stud has 5 rows and dept has 4 rows. Then total 5*4=20 rows will be ceated. Equi Join: It is based upon equality of two values of same column of two different tables. It use the (=) operator. Select stud.roll,name,mark,branch from stud,dept where stud.roll=dept.roll; It display roll,name from stud table mark branch from dept table those roll are same to both table. (=) Table alias Select s.roll,s.name ,d.mark,d.branch from stud s,dept d where s.roll=d.roll; Above we specify alias name for both table s & d. S is alternate name for stud table and D is alternate name for Dept table. Advantages No need to write tablename repeatedly. Only use the shortcut name. For faster write the query. Ambiguity inColumn. Here roll is common column for stud and dept table. When we write query select roll,name,mark,branch from stud,dept ............. Oracle server will be confuse to take about roll. Because it is common. So we specify which table roll column we want to display. Modified query is Select stud.roll , name, mark branch from stud,dept where stud.roll=dept.roll;
  • 7. Non Equi Join The operator which are used other than (=) is called Non Equi Join. Operators are used 1. > 2. < 3. <> 4. Between Select s.name,d.mark from stud s,dept d where s.mark between d.high_mark and g.low_mark; Left outer Join / Right Outer Join / Full Outer Join Outer Join-It is an extension of inner join. It returns rows that are matched to both tables. And also returns those are not matched to another table. It returns the rows those are missed in join condition. Outer join symbol is (+) sign e.roll(+)=d.roll //left outer join It returns the rows that are matched to both table except return rows those that are not matched to the first table. e.roll=d.roll(+) //right outer join it returns the rows that are matched to both table except returns the rows that are not matched to the second table. Full outer join: The join between two tables, that returns the result of an inner join as well as the result of left outer join and return result of right join is a full outer join. Full outer join select name,mark from join7,join8 where join7.roll(+)=join8.roll union all select name,mark from join7,join8 where join7.roll=join8.roll(+) Right Outer Join select name,mark from join7,join8 where join7.roll=join8.roll(+); Left Outer Join select name,mark from join7,join8 where join7.roll(+)=join8.roll;
  • 8. Self Join From Stud d Natural Join Dept d; automatically choose same column name by oracle. From stud s join dept d using (roll) // using clause is used to specify column name by user choice in join condition From stud s join dept d on(s.roll=d.my_roll) on clause is used to specify that have different column name. Here roll & my_roll is not matched Left outer join select s.name,d.mark from join7 s left outer join join8 d on (s.roll=d.roll) Right outer join select s.name,d.mark from join7 s right outer join join8 d on (s.roll=d.roll) Full outer join select s.name,d.mark from join7 s full outer join join8 d on (s.roll=d.roll) Prepared By JAVATECH Search us in The World
  • 9. ASSIGNMENT ON DATABASE OBJECT,SUB-QUERY AND JOIN COMMAND STUDENT STU_ROLL NAME DOB MARK BRANCH SECTION AMIT 13/05/2010 600 SCE IT-1 AJAY 25/09/2009 550 IT IT-2 ROHIT 15/12/2008 450 IT CS-1 MUKESH 02/11/2007 390 SCE CS-2 JOHN 30/03/2005 400 IT CS-3 FACULTY FACULTY_ID FAC_NAME STU_ROLL FAC_SUBJECT CLASS_ROOM CLASS_DATE_TIME JOSEPH JAVA C1 STEPHEN C++ C2 RICHARDSON DOTNET C3 JAMES PHP C4 AKS ORACLE C5 TIPS:- CLASS_DATE_TIME DATATYPE IS TIMESTAMP(2) - TIMESTAMP IS USED FOR TO SPECIFY TIME. 2 MEANS AFTER SECOND VALUE FRACTION VALUE WON’T DISPLAY. ONLY TWO DIGIT SECOND DISPLAY. INSERT INTO TABLE VALUES(TO_DATE(’12-JAN-2001 13:34:56’,’DD-MON-YY HH24:MI:SS’)); 1. Enter exact data into student table. But roll should insert using sequence, it should start from 1 and increment by 1.[total 5 records] 2. Enter exact data into faculty table. But faculty id should insert using sequence, it should start from 1 and increment by 3[total 5 records]. E.g. 1, 4, 7, 10, 13 Also enter exact student roll into faculty table from referencing student table by using sequence but it should be increment by 2. E.g. 1, 3, 5, 7, 9 [total 5 records] 3. Waq to display sequence name, minimum value, maximum value from all created sequences. 4. Waq to drop these two sequences. 5. Create synonym s55 for student table. And query all rows from synonym. Then drop it. 6. Create view stv1 from student table by selecting roll, name , mark & branch column. 7. Waq to update marks from all rows from stv1 view. 8. Waq to create view stv2 from student table selecting roll, name, dob & mark of rollno 3 which can’t be deleted from view. 9. Drop these two view from database. 10. Waq to create view st_fa from student & faculty selecting stu_roll, stu name, faculty id, faculty name where rollno of student table should same to student roll of faculty table. 11. Select and drop st_fa view. 12. Waq to display the student roll, name, dob, mark and faculty name, subject, classroom, from student, faculty table those student rollno are common to student rollno of faculty table using equi join.
  • 10. 13. Waq to display the all distinct rollno of student table those student rollnos are less than from faculty table rollno using non equi join. 14. Waq to join student & faculty table without using any join command. And check how many rows created. 15. Waq to check from faculty table, which student’s rollno and faculty’s id are same. Then display their faculty names only. 16. Waq to display student roll,name & facult id, fac name from student and faculty tables those are common roll no by using natural join. 17. Waq to display student’s roll,name and faculty id, fac name from student,faculty table by using ‘using’ clause. 18. Waq to display student roll,name & faculty id, fac name from student, faculty where faculty id and student roll are same. 19. Waq to display student roll, name, mark, fac id, fac name from student, faculty table those are common rollno to both table except those rollno of student table are also not matched to faculty table. 20. Waq to display student roll, name, mark, faculty id, fac name from student,faculty table those are common rollno except display fac id, fac name of corresponding rows of stu rollno of faculty table those are not matched to student table rollno. LIKE OUR FACEBOOK PAGE “JAVATECH 123” TO GET UPDATED NOTE & VIDEOS Prepared By JAVATECH Search us in The World **************************BEST OF LUCK*********************** Next – PLSQL will Start PL-PROGRAMMING LANGUAGE+SQL PROGRAMMING LANGUAGED USED WITH SQL STATEMENTIS CALLED PLSQL