Oracle SQL 1
Oracle SQL 1
-Describe Statement-
It is used to display the database object structure like column & data-type.
Describe table_name;
To Modify a column- If you want to change the data type, strength & default
value of a column then you can use modify statement under alter statement
for that.
Alter table table_name modify (column_name change_datatype
(change_strength) change_default value );
To Rename a Column- If you want to change the name of a column then you
can use rename statement under alter statement for that.
Alter table table_name rename column column_name to
new_coulmn_name;
To Drop a Column- if you want to delete a column then you can use drop
statement under alter statement for that like this
Alter table table_name drop column column_name;
Note- You can only delete one column at a time.
To Set unused a Column- It is used to hide a column that is unused
but we cannot use it again. If once a column sets unusable then it will
have to be dropped because this statement is used to drop a column
in the future.
Alter table table_name set unused column column_name;
A data dictionary is used to find the unused column which is as follows.
Select * from user_unused_col_tabs;
5-Truncate Statement- it is used to remove all the data records at once but it
retains data base structure.
Truncate table table_name;
To show dropped data- To show dropped data we use the statement as like
show recyclebin;
There is also a data dictionary to show the recyclebin i.e., like this
select * from recyclebin;
6-Flashback Statement- It used to retrieve dropped database object.
Flashback table table_name to before drop;
Note- If we have two database objects of the same name in the recyclebin
then two database objects of the same name cannot be flashbacked
simultaneously, the latest database object will be flashback only. If you want to
flashback the previously dropped same name object first, then you have to use
object name for that instead of table name which you will get from the data
dictionary of recyclebin.
select * from recyclebin;
Flashback table “object name” to before drop;
Constraints
Constraints is a restriction to a row while executing DML statements on it,
actually it is used to specify the rules concerning data in the table, it can be
applied for single & multiple fields during the creation of the table or after
creating using the alter table commands.
There are some types of constraint like-
Primary Key, Unique, not null, Check, Foreign Key etc.
We can use two types of constraint while creating tables. Which are as follows.
1)Table Level Constraint
Create table table_name (Column_name1 datatype (length)
primary key,
Column_name2 datatype (length)
unique key);
2)Column Level Constraint
Create table table_name (column_name1 datatype (length),
column_name2 datatype (length),
Constraint table_column_pk primary key
(Column_name1),
Constraint table_column_pk primary key
(Column_name2));
1-Primary Key-
Each table has a column in which only unique data is taken, that makes it
different from other column.
For Example-If we take the table of a school in which the name of student’s
column, marks & their Roll Number column are mentioned, so Roll Number
column is the most unique in this type table because the name & marks of a
student may be same but their roll number may not be same.
So, we put a restriction on the column named Roll number so that duplicate
data cannot be inserted in it. For that we use primary key.
Primary Key doesn’t allow duplicate values as well as null values, we can
create one primary on a single table.
Create table table_name (Column_name datatype (strength)
Constraint table_columname_pk primary key(column_name));
Note: You can also talk about the problem of duplicate data in your
interview.
A common challenge we faced last month was when data was not getting
inserted in our database object, we noticed an error in logfiles, we found out
that it was duplicate data because it was showing the unique constraint
violated in the log files which means the data was already inserted. I read it &
asked my data quality team to correct and that’s how the problem was
resolved.
composite Primary key-
As we know that we can create one primary key on a single table but we can
create primary key on more than one column as composite Primary key.
Create table table_name (column_name datatype (length),
column_name datatype (length),
Constraint table_column_pk primary key (Column_name, Column_name));
2-Unique-
Unique key doesn’t allow duplicate values but allow null values, we can create
more than one unique on a single table.
Create table table_name (Column_name datatype (strength)
Constraint table_columname_uk unique (column_name));
Note: As we know that Unique Constraint allow null values but if under unique
constraint you have given ‘Null value’ before, then it will accept that Null value
when you give again, you can insert any number of null values in a unique
constraint because null just means empty so how will the duplicates ones
compare.
Interview Question- How many null values can be given in unique constraint?
→Answer- ‘N’ number of null values can be given in the unique constraint
because the meaning of null is blank which cannot be compared with last given
null value.
3-Not null-
Not null doesn’t allow only null values, null mean blank or empty it doesn’t
contain any space.
Create table table_name (Column_name datatype (strength)
constraint table_columname_null not null);
Note1: ‘Not null’ actually belongs to Check Constraint.
Note2: Not Null Constraint is always considered as column level constraint.
Primary Key=Unique+Not Null
Because we can use one primary key on a single table but in some special
circumstances, we can also use multiple primary key in this way mean we can
use ‘Unique’ and ‘Not Null’ constraint together to make the primary key
constraint.
4-Check-
It restricts data records based on condition.
Create table table_name (Column_name datatype (strength)
constraint table_columname_check check (Condition) );
For example, if we want to apply check constraint on date column then syntax
could be like this –
Create table table_name (Column_date date
constraint table_date_uk unique (‘column_date’>‘date’) );
5-Default Constraint- It is used to set a default value to a column. If you want
to set a default value to a column then syntax could be like this.
Create table table_name (column_name Data-Type (Length) default
0/sysdate );
6-Foreign Key-
Foreign Key is used to create a connection between two or more tables.
Create Table table_name (column name datatype (length)
Constraint table_culumnamne_fk foreign key (column_name)
references table_name (column_name);
In order to apply a foreign key, there must be two tables and they must also
have a relationship between them, similar to the relationship between a
parent and a child, hence these tables are referred as parents table & child
table.
The Relationship between them something like this.
1)One to One
2)Many to One
3)Many to Many
The Primary key is always applied to the parent table while the foreign key is
always applied to the child table.
You can understand this relationship by the following tables which establish
relationship between a parent & a foster child.
You can divide these above tables into three parts, like this
Parent Table Child-Parent Table Child Table
Customers Orders Orderitems
Author Books BookAuthor
Publisher
Note: Here the table of Promotion is no way related to Parent and Child, mean
promotion is not referenced.
You have to build the table in this order.
Now you have to make the tables in the Oracle-SQL on the basis of their
relations.
Here the table named customer is related to the table named order as father
as child. Similarly, we can understand the relation of other tables as follows.
Note: You can’t put a ‘not null’ key & ‘default value’ by alter statement
because that is column level constraint key, when we modify a column in a
table, we always modify the column for that, so to add ‘not-null’ as well we
have to modify it.
Alter table table_name modify column_name not null;
2)To remove a Constraint- if you want to remove the constraint, then for this
you can use ‘Alter Drop Statement’ which syntax will be like this.
Alter table table_name drop constraint constraint_name;
We will also use same syntax for the ‘not null’ constraint but we will have to
give the name of not null constraint.
But to remove the primary key we have to remove the foreign key which is
referenced to the primary key in another table, for this we can use the
following syntax-
Alter table table_name drop primary key;
Since there is only one primary key in a table, we will write here the primary
key directly instead of constraint name.
But if this primary key is related to another table’s foreign key, then it will give
error like this.
“This Unique /Primary key is Referenced by some foreign key”
For this you have to remove the foreign key from another table.
Firstly
Alter table table_name drop foreign_constarint_name;
then
Alter table table_name drop primary key;
Note: Alias’s data is not filtered in Where clause. To filter any data, you need to
enter the same condition that you entered while inserting the data in the
table.
For Example, Character, Data Type and Length should be the same as you used
while creating the table & inserting the data.
Is [not] null- It is used to filter the specific data record from a table based on
null condition. Null is not a value so it can’t be used to compare against a value
hence it can’t be used with any operator.
So, we use ‘Is null or not null’ to filter it.
Select column_name from table_name
where column_name is not null;
Select column_name from table_name
where column_name is null;
[Not] Between operator- It is used to filter the specific data record from a
table based on Range condition.
Select column_name from table_name
where column_name between first_interval and second_interval;
For example, we have to filter the income between 10,000 to 15,000.
Select income from employees
where income between 10000 and 15000;
[Not] in operator- It is used to filter the specific data record from a table based
on two or three data record condition.
Select column_name from table_name
where column_name in (‘first_column’ ,‘second_column’);
For example, we have to filter the two countries name from customer name
table.
Select name, country from customers
where country in (‘India’, ‘Pakistan’);
[Not] like operator- It is used to filter the specific data record from a table
based on pattern with meta characters.
Select column_name from table_name
where column_name like ‘%_a’;
For example, we have to filter the pattern of “Himanshu’’ from customer name
table.
Select name, country from customers
where name like ‘H__%’;
Note: Here % is used to any number of characters while underscore (_) is used
to represent exactly one character in the indicated position.
Logical Operators- It is used to filter the specific data record from a table
based on two or more conditions.
There are two logical operators that are used to filter the specific data record
from a table based on two or more conditions.
And Operators- It is used to filter the specific data record from a table to
combine the two conditions together.
select column_name from table_name
where condition1 and condition2;
For example, List the title and publish date of any computer book published in
2005.
select TITLE, PUBDATE, CATEGORY from books
where CATEGORY='COMPUTER' and pubdate like '%_%_05';
Or Operators- It is used to filter the specific data record from a table to search
the only one conditions between two or more conditions.
select column_name from table_name
where condition1 or condition2;
For Example, list the customers live in Georgia or New Jersey.
select customer#, lastname, state from customers
where state='GA' or State='NJ'
2)To update multiple column data records- If you want to update multiple
column data records then you have to used comma as a separator between
them.
Update table_name set column_name=‘Exact Data’,
column_name=‘Exact Data’
Where unique condition;
Note: if there are any constraints on any column of a table then the data
records of that column cannot be updated.
Why we use Where Clause with Update statement?
If we don’t use where clause with update statement then it will update entire
column of table with same data records which is set during the query.
To avoid this type of problem, always run the select statement before the
update statement and then copy it and use it with update statement by paste.
3)To update column data records based on mathematical expression-
For example, we have an employee’s name table in which we have to increase
their salary column data records based on their increment.
Update employees set salary=salary+increment;
update employees set salary=salary+200
where salary > 15000;
Rollback Statement: It is used to undo the wrong statements. If you have run
the commit statement before this it will not rollback.
rollback; or shortcut key (fn + f12)
Whenever you start executing a DML statement on a database object, a
transaction is started which ends with the commit or rollback statement.
Transaction: Set of Multiple DML Statement.
If you don’t commit then this transaction will not be saved i.e., changes made
by you like insert, update, delete etc. will not be saved permanently. If you are
doing this work on a server then these changes will be limited to you only, it
will not be saved for any other user until you commit or rollback your
transaction, once you commit or rollback it will be saved for other users.
There are two types of Commit Statement.
1) Explicit commit statement: commit that is done by manually is called
explicit commit.
2) Implicit Commit Statement: Commit that happen automatically are
called implicit commits. Whenever you run efficient or wrong DDL or
DCL statement during transaction it gets automatically committed.
Note: DDL & DCL is preceded by a commit and followed by another
commit.
Commit;
DDL or DCL Statement
Commit;
For example,
select * from test;
update test set name='Himansh',Surname='Gaur'
where phno='55456';
alter table abc add xyz varchar(12); (wrong ddl statement)
Save Point Statement: It is used to partial rollback.
For example, we have a table named test,
insert into test values ('Abhishek','Parjapati',785645);
Cache: means that it already generates the 20 values of the next sequence in
order to maintain the quickness of the select statement. It is generally used
only with cycle to maintain next 20 upcoming numbers.
create sequence ATMST maxvalue 21 minvalue 1 cycle; select
* from user_sequences;
SEQUENCE_NME MIN_VALUE MAX_VALUE INCREMENT_BY CYCLE_FLAG ORDER_FLAG CACHE_SIZ LAST_NUMBER
E
ATMST 1 21 1 Y N 20 1
How to drop sequence: The way we drop the table. Similarly, we can also drop
the sequence. Sequence cannot be retrieved after it is dropped but after
dropping the table, we can flashback from it.
Drop sequence sequence_name;
How to alter sequence: if you want to change the cycle, increment value,
maximum and minimum value of the sequence then you can use alter
statement for this.
Alter sequence sequence_name minvalue 2;
Interview Questions:
What are the per pseudo columns of sequence objects?
-nextval and currval
Index
An index is a pointer to the data records in a table, it is used to retrieve the
data records faster, it means index is used in select statement to fetch the data
records faster, it is created on a particular column of a table, basically we
create index on that column of a table on which SELECT statement is used
most frequently with WHERE clause, an index in a database is very similar to an
index in the back of the book.
On which column of table should we create an Index?
→ On which SELECT statement is used most frequently with WHERE clause.
→ In which the number of null values is less.
How to create an index?
There are some types of indexes like Btree index, Bitmap index, function-based
index.
1)Btree Index: we create B Tree index on high selectivity of data mean where
data records are mostly unique. It creates leaves.
Create index index_name on table_name (column_name);
2)Bitmap Index: we create Bitmap Index on low selectivity of data mean where
data records are mostly duplicate like gender, age, region etc. it creates binary
values (1, 0).
Create bitmap index index_name on table_name (column_name);
Interview Question-
What happens to the constraints and indexes when you drop a table?
→ Whenever we drop a table, the constraints and indexes on it drop along
with it.
Dropping an Index: You can drop any index expect the Unique index because
unique index is related to a column of a table on which Primary key or unique
constraint is applied. whenever we put a primary or unique key constraint on a
table, it creates a unique index.
Synonym
It is used to create the synonym for a table & it does not contain any data,
actually it is fetched the data from the original table. We can create multiple
synonyms of a table.
Whenever you insert data into a synonym table it inserts the same data into
the original table as well because it’s just another name for the original table.
create synonym synonym_name for table_name;
There are two types of synonyms.
1)Private Synonym: By default, private synonym creates itself.
2)Public Synonym: It is created for all the users present in the database, it is
made by the following query.
Create Public synonym synonym_name for table_name;
Suppose you have 3 data records in table named employee and also 3 data
records in another table named department, if we join these two tables
together without join condition then the oracle returns their 9 data records as
a cartesian product or cross join table.
Select name, dept from employee, department;
Emp Dept.
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
Note: Whenever the names of any two columns in any two tables are same,
then while writing those columns in the query '[Link]' has
to indicate which column belongs to which table. For this we can also create
table alias. We can write this query as follows.
Select [Link], [Link] from table1 a , table2 b;
Select [Link], [Link] from table1 a cross join table2 b;
2)Equi Join: It is used to retrieve all the data records from two or multiple
tables, where we use join condition with Equal Operators. It is also called inner
join or natural join.
In Equi Join, the data of any two tables must be common though the column
names may or may not be common. Thus, we can say that the data of any two
columns of two tables must be same in equi join.
1) Traditional Method
select column1, column2 from table1 alias1, table2 alias2
where alias1.common_data_column=alias2.common_data_column;
2) Join Method
select column1, column2 from table1 alias1 join table2 alias2
on alias1.common_data_column=alias2.common_data_column;
If all the data in both the table is common & sure i.e., common column, then
you can use the ‘using clause’ in place of ‘on clause’, under which we will not
use equal operator.
select column1, column2 from table1 join table2
using (common_data_column);
EmpID EmpID
A538 A538
A539 A539
Join Method:
Select column1, column2
from table1 alias1 Join table2 alias2 on
(alias1.common_column=alias2.common_column)
join table3 alias3 on (alias2.common_column= alias3.common_column);
Select column1, column2
from table1 join table2 using (common_column)
join table3 using (common_column);
On the basis of above tables, we have to extract the names of the customers
who have purchased some category title book of the different writers.
Traditional Method:
select firstname,lastname,title,lname,fname,
firstname || lastname || ' is ordered ' || title || ' which is written by ' ||
fname || lname as quote
from customers c,orders o,orderitems oi,books b,bookauthor ba,author a
where [Link]#=[Link]#
and [Link]#=[Link]#
and [Link]=[Link]
and [Link]=[Link] and [Link]=[Link];
Join Method:
select firstname,lastname,title,fname,lname,
firstname || lastname || ' is ordered '
|| title || ' which is written by ' || fname || lname as quote
from customers c join orders o on ([Link]#=[Link]#)
join orderitems oi on ([Link]#=[Link]#)
join books b on ([Link]=[Link])
join bookauthor ba on ([Link]=[Link])
join author a on ([Link]=[Link]);
select firstname,lastname,category,title,fname,lname,
firstname || lastname || ' is ordered ' || category ||
' category book named ' || title || ' which is written by '
|| fname || lname as quote
from customers join orders using (customer#)
join orderitems using (order#)
join books using (isbn)
join bookauthor using (isbn)
join author using (authorid)
where Category='COMPUTER';
Interview Question:
If you are joining 5 tables using equi-join then how many conditions will you
have to give in that?
→(T-1)=C so 5-1=4 conditions
Non-Equi Join: It is used to retrieve all data records from two or more tables
where we do not use join condition with equal operator but it can be any other
operator instead of equal operator condition.
1) Traditional Method
select column1, column2 from table1 , table2
where column3<> column4;
2)Join Method
select column1, column2 from table1 alias1 join table2 alias2
on column3<>column4;
Empid Empid
A540 A537
A540 A535
A540 A534
A536 A537
A536 A535
A536 A534
null A534
Example: Suppose we have two tables named Books table and Promotions
table, if we want to extract data of gifts between maximum and minimum
retail price, while maximum and minimum retail price column come under
promotion table and retail price column comes under Books table. Then we
can extract the data by Non Equi Join Query like this.
select title, retail, gift
from books join promotion
on retail between minretail and maxretail;
Self-Join: When a table is joined by itself then it’s called Self-Join that’s mean
when we join a table with the same table it is called self-join. In self-join we are
required to give alias to the table, because the same column of the same table
can also be used under this.
1) Traditional Method
select [Link], [Link] from table
alias1 table alias2
where [Link]=[Link];
2)Join Method
select [Link], [Link] from table alias1 join
table alias2 on [Link]=[Link];
2) Join Method
select column1, column2 from table1 alias1 left outer join table2 alias2
on alias1.common_data_column=alias2.common_data_column;
select column1, column2 from table1 alias1 left outer join table2 alias2
using (common_data_column);
EmpID EmpID
A534 Null
A536 Null
A538 A538
A540 A540
EmpID EmpID
Null A532
Null A533
A538 A538
A540 A540
EmpID EmpID
A532 Null
A533 Null
A538 A538
A540 A540
Null A534
Null A536
Let suppose we have two table named customers and orders, if we want to
extract the data of those customers who ordered some items, then we can
extract the data by left outer join or right outer join.
select c. customer#, Firstname,lastname,[Link]#
from customers c,orders o
where [Link]#(+)=[Link]#;
Set Operators
Set Operators are used to join the result of two or more select statements.
There are some types of set-operators like Union, Union-All, Minus & Intersect
etc.
Union: It will give unique shorted data records from the result set of two or
more select statements.
select column_name from table_name1
union
select column_name from table_name2;
A = (1,2,3,4) B= (1,2,5,6)
(A Union B) = (1,2,3,4,5,6)
Union All: It will give all the data records from the result set of two or more
select statements.
select column_name from table_name1
union all
select column_name from table_name2;
A = (1,2,3,4) B = (1,2,5,6)
(A Union All B) = (1,2,3,4,1,2,5,6)
Intersect: It will give common shorted data records from the result set of two
or more select statement.
select column_name from table_name1
intersect
select column_name from table_name2;
A = (1,2,3,4) B = (1,2,5,6)
(A Intersect B) = (1,2)
For Example, Let suppose we have two tables named Customers and Orders, if
we want to extract the data of customers who have placed an order for an
item, we can use the Intersect set operators for that.
Minus: It will give the data records from first select statement which is not
present in second select statement.
select column_name from table_name1
minus
select column_name from table_name2;
A = (1,2,3,4) B = (1,2,5,6)
(A Minus B) = (3,4)
For Example, Let suppose we have two tables named Customers and Orders, if
we want to extract the data of customers who have not placed an order for an
item, we can use the minus set operators for that.
Example: If we have two tables named authors and books, if we want to
extract the data of authors who have written books of both categories of
children and family life, then we can use union set operators like this.
select fname || ' ' || Lname as Writer,title,Category
from books join bookauthor using(isbn)join author using (authorid)
where category='FAMILY LIFE'
Union
select fname || ' ' || Lname as Writer,title,Category
from books join bookauthor using(isbn)join author using (authorid)
where category='CHILDREN';
You can extract the data by another way like this,
select distinct fname || ' ' || Lname as Writer,title,Category
from books join bookauthor using(isbn)join author using (authorid)
where category in ('FAMILY LIFE','CHILDREN');
For Example,
select fname || ' ' || Lname as Writer,title,Category
from books join bookauthor using(isbn)join author using (authorid)
where category='FAMILY LIFE'
Union
select fname || ' ' || Lname as Writer,title,Category
from books join bookauthor using(isbn)join author using (authorid)
where category='CHILDREN'
Union
select fname || ' ' || Lname as Writer,title,Category
from books join bookauthor using(isbn)join author using (authorid)
where category='COMPUTER';
Aggregate Functions
Aggregate is a function where the values of multiple data records are grouped
together to form a single summary value. It is also called group function.
There are some Aggregate functions like SUM, COUNT, MAX, MIN, AVG et
cetera.
Select sum(column_name) from table_name;
Select Max(column_name) from table_name;
Select Min(column_name) from table_name;
Select Avg(column_name) from table_name;
Select count(*) from table_name;
For example, suppose we have a table named Student under which there are
some columns like roll_no, name, subject, marks. If we want to extract the
total marks of the student whose roll number is 1, then aggregate function will
be used like this.
select sum(marks) from student where roll_no 1;
Note: Aggregate Function always skip the null values.
45+55+50=150 SUM Marks
45+55+null+50/3=50 AVG 45
55
45, 55, Null, 50=4 Entry Count null
Max value=55 Min Value=45 50
For example, we have a table named employees under which there are many
columns, HIRE_DATE is one of them column, if we want to extract the data of
employees who joined earlier in the organization, for that we will use
aggregate function like this.
Select min(hire_date) from employees;
Interview Question:
→ How to check how many data records are included in a table?
Select count(*) from table_name;
To Filter The Specific Data Records:
Select simple_column, sum(column_name) from table_name
where unique_condition group by simple_column;
→ GROUP BY clause is always used on the column which contains duplicate
data.
Student_id Marks
A9554 65 Student_id Total
A9554 76 A9554 206
A9553 98
A9554 65
Select student_id, sum(marks) as total
from student
where student_id= ‘A9554’
group by student_id;
having clause: Like where clause, having clause is also used to filtered the data
records but from the groups based on specified condition. It is mainly used
with select statement, it is used after the group by clause & it can’t be used
without group by clause.
For example, if we have three tables named books, authourid and author
under which there are some columns like title, authorid, fname, lname then we
want to extract the data of those author who has written n numbers books, for
that having clause will be used like this.
select fname || ' ' || lname as Author, count (*) as "Written BooK"
from books join bookauthor using (isbn)
join author using (authorid)
group by fname,lname;
select * from books;
select category,max(retail-cost) as "Profit"
from books group by category
having max(retail-cost) = (select max(retail-cost) from books);
Interview Questions:
→How to extract duplicate data from a table?
Select column_name, count (*)
from table_name group by column_name (on which we want to see duplicity)
having count (*) >1;
→how to work a query?from→where→group by→having→select→order by
Case Conversion Functions
Lower Function: It is used to convert all inserted data records characters from
uppercase to lowercase.
Select lower (column_name) from table_name;
Upper Function: It is used to convert all inserted data records characters from
lowercase to uppercase.
Select upper (column_name) from table_name;
Initcap Function: It is used to convert the initial character of all inserted data
records to uppercase.
Select initcap (column_name) from table_name;
Note→ Case conversion functions only apply to the varchar data type.
Case conversion Functions with multiple column: case conversion functions
can be used with multiple functions with some restriction.
→Can’t use with multiple arguments: The case conversion function cannot be
used with integrating multiple columns.
Select initcap (column_name1, column_name2) from table_name;
It will give the error like this ORA-00909: invalid number of arguments.
You have to give separate case conversion for each such column.
Select initcap (column_name1) ,Upper (column_name2) from table_name;
→Can use with concatenation: The case conversion function can be used with
concatenating multiple columns.
Select initcap (column_name1 || ‘ ‘ || column_name2) as alias from
table_name;
Application of case conversion Functions: Mainly we use case conversion
functions while extracting the data records from a table via ‘where clause’,
when we extract data records these are used to deny uppercase and lowercase
characters.
select column_name from table_name
where lower(column_name) = ‘lowercharacter’
Note: It is necessary to write the query in the same case the function we are
using to extract the data.
select column_name from table_name
where upper(column_name) = ‘uppercharacter’
Substr: It is used to extract a particular cut part of the data records inserted.
Select substr(column_name,cutting_postion,Number_of_arguments);
For Example, select substr(first_name,1,3) from employees;
We can practice on dummy table also; dummy table is one which does not
have columns it is managed by oracle database.
Positive Cutting: It is used to extract a particular cut off part of the data record
from the beginning.
→By Passing Three Arguments:
select substr('Himanshu Gaud',1,7) from dual;
Output:Himansh
→By Passing Two Arguments: Substrs work like this when we don't pass a
third number argument.
select substr('Himanshu Gaud',7) from dual;
Output: hu gaud
Note: It is mandatory to pass two arguments during substr.
Negative cutting: It is used to extract a particular cut off part of the data
record from the end.
→By Passing Two Arguments
select substr('Himanshu Gaud',-4) from dual;
Output: hu gaud
→By Passing Three Arguments
select substr('Himanshu Gaud',-4,3) from dual;
Output: hu gau
Note: Cutting is always in forward form.
Note: During negative cutting, the value of third argument i.e., number of
characters should always be smaller than the second argument i.e., position of
character.
Instr: it is used to determine the position of a character.
→By Passing Two Arguments
Select instr(column_name, ‘Character’) from table_name;
For Example,select instr('Himanshu vashu Gaud','a') from dual;
Output:4
→By Passing Three Arguments
Select instr(column_name, ‘Character’, cutting_postion)
from table_name;
For Example,select instr('Himanshu vashu Gaud','a',5) from dual;
Output:11
→By Passing Four Arguments
Select instr(column_name, ‘Character’, cutting_postion,
Occurrence_of_character) from table_name;
For Example,select instr('Himanshu vashu Gaud','a',5,3) from dual;
Output:0
Because after the fifth character a does not appear for the third time.
So it can be like this, select instr('Himanshu vashu Gaud','a',5,2) from dual;
Output:17
→By Passing Negative Position Argument:
Select instr(column_name, ‘Character’, -cutting_postion,
Occurrence_of_character) from table_name;
For Example, select instr('Himanshu vashu Gaud','a',-2,3) from dual;
Output: 4
Co-related Subquery
In which inner query is related with outer query, it is called correlated
subquery.
For Example, we have two tables named books & orderitems, if we want to
extract the data of
select quantity*paideach from orderitems
where isbn in (select isbn from books
where [Link]=[Link]);
select category, (select count (*) from books a where [Link]=[Link])
total from books b;