Unit 1 ADBMS

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Subject: Advance Data Base Management System

Unit-1
Structured Query Language:
SQL stands for Structured Query Language which is a standard language for
accessing and manipulating data bases. SQL was initially developed at IBM
(International Business Machine). It was design to manipulate & retrieve data in
relational database management system.
Elements of SQL:
1. Queries- It retrieves data based on particular or specific task. A query is a
database command that retrieve records.
2. Statements- It may control program flow transaction.
3. Expressions- It can produce tables which consisting of rows and columns
of data.
4. Clauses- Clauses are some optional component of queries & statement .
Working of SQL
SQL Request
Database
DBMS
Data

Features of SQL:
1. SQL is very flexible.
2. SQL based application are portable i.e. they can be run on any system.
3. SQL is a high level language.
4. SQL is a free format syntax which gives users the ability to structure SQL
statement on any of the stream
5. SQL can be embedded also in front end application code like visual
basics 6.0, java, and c++.
6. SQL itself is not a database where as it is just a language i.e. used to
create database and tables.
7. SQL is not case sensitive.
SQL Commands:
-SQL commands are instruction which is used to communicate with database.
-It is also used to perform specific task, function and queries of data.
Types of SQL commands:
SQL commands
DDL DML DCL TCL DQL

-create -Insert -Grant -Commit -Select


-Drop -Update -Revoke -Rollback
-Alter -Delete -Savepoint
-Truncate

1) Data Definition Language


Or
DDL Statements to create & manage tables using create and Alter
-DDL changes the structure of the table like creating a table, a table,
deleting a table, etc.
-ALL the commands of DDL are auto-committed that means it
permanently save all the changes in the database.
-Here are some commands that come under DDL
1) Create table command
We define SQL relation (table) by using the create table command
Rules for giving the table name
1] While naming a table the first table should be on alphabet.
2] SQL reserved words cannot be used to name a table.
3] Maximum length for a table name is up to 30 characters.
4] Two different table should not have the same name.
5] Underscore, numerical and letters are allowed but not blank space and single
quote.
1) Create: Create table command is used to create a new table in the database.
Syntax:-create table <table name> (column definition1,column definition
2,…);
Example:-create table Student (rollno number(3),name varchar(20),address
varchar(30);
If the above command executed successfully the message table created is
displayed.
2) Drop: Drop command is used to delete both the structure and record
stored in the table.
Syntax: - Drop table <table_name>;
Example:-Drop table student;
Drop command removes a table from the database all the table, rows, index,
privileges also be removed.
3) Alter: Alter command makes the changes in the structure of the table it is
used to add a new column in a table, modify the existing column table
and rename the table.
TYPES-
a) Alter table<table_name>add(column definition…);
b) Alter table<table_name>modify(column definition…);
c) Alter table<table_name>drop column (column_name);
d) Alter table<table_name>rename to (new table_name);
a) Adding a column in a table:
1) To add a column to an existing table the alter table.
Syntax: - Alter table<table_name>add (column name, column definition);
Example:-Alter table Student add DOB date;
This statement will add a new column DOB to the student table.
2) To add multiple columns to an existing table.
Syntax:-Alter table<table_name>add (column definition1,column
definition2,….column definition);
Example:-Alter table Student add (marks int(3),address varchar(30));
This will at two columns marks & address to the student table.
b) Modifying column in a table:
1) To modify column in an existing table
Syntax:-Alter table<table_name>modify(column name,column definition);
Example:-Alter table Student modify name varchar(40);
This will modify the column called name to be a datatype of varchar with 40
width.
2) To modify multiple columns in an existing table.
Syntax:-Alter table<table_name>modify(column definition1,column
definition2,..column definitionN);
Example:-Alter table Student modify (rollno int not null, name varchar(20);
This will modify both rollno & name columns of student table.
c) Drop:- Drop syntax in a table:-
To drop a column in an existing table the alter table syntax is
Syntax:-Alter table table_name drop column, column name;
Example:-Alter table student drop column DOB;
This will drop the column DOB from the table Student.
d) Rename:- Rename column in a table
Syntax:-Alter table table_name rename column old-name to new name;
Example:-Alter table Student rename column address to location;
4) Truncate: - Truncate command is used to remove all rows from a table.
-Truncate table command delete only the data inside the table.
Syntax:-Truncate table table_name;
Example:-Truncate table Student;
2) Data Manipulation language (DML)
Or
Manipulating data using insert, update, delete statement.
A data manipulation language is a language that enables users to access or
manipulate data as organized by the appropriate data model.
The following commands which are used to query & manipulate existing
objects like tables.
1] Insert
2] Update
3] Delete
1) Insert command
-Once the first step of creating table is completed the insert command is used to
add one or more rows to a table.
-While using this command the values are separated by ‘,’ and the data type
varchar and date are enclosed in a single quote ‘’. The values must be entered in
the same order as they are defined in the table.
Syntax:-insert into<table_name>values (val1,val2,..valn);
Example:-insert into Student values (101, ‘Deepak’, ‘Pune’);
The above command will add one record into student table.When you want to
insert more than one records using a single insert command then you have to
use insert command as follows:
Example:-insert into Student values(&roll no, ‘&name’, ‘&add’);
Syntax:-insert into <table_name>values(&val1,val2,…,&valn);
The above statement will now from PROMPT the user to enter values for all the
columns in the table as
Enter value for roll no: 101
Enter value for roll no: ‘Deepak’
Enter value for address: ‘Pune’
-Again if you would like to enter anthor record then use only ‘/’on the SQL
prompt.
-Again it will execute insert command again you will get prompt to entering
value.
2) Update command
-This command is used for making changes to the existing records in a table.
-With the update command we can update rows in the table.
-A single column or more than one column could be updated .We can also
update a specific rows depending on some condition.
-The update command consist of “SET CLAUSE” we can also use where clause
with update which is optional.
Syntax:-update <table_name>set field=value… [where condition];
-In the above syntax set clause is compulsory which will make the actual
changes in the records and where is optional.
-Update sets each field with the value that we supply, provided where condition
is satisfied.
Example:-update Student Set name= ‘Deepak’ where name = ‘Rohit’;
3) Delete command
-Delete command is used to rename one or more row from a table.
Syntax:-Delete from table_name[where condition];
Example:-Delete from Student where name= “Deepak”;
3) Data control Language (DCL)
-Data control language is used for providing privileges to different users. A
privilege is also called as “Authorization”.
-We may assign a user several forms of authorization on parts of the database
like authorization to read data, authorization to update data & authorization to
delete data.
-We may authorize the user all, none or a combination of this type of privileges
on specified parts of the database.
-The DCL commands are related to the security of database .They perform task
of assigning privileges so users can access certain objects in the databases.
-Privileges are of two types-
1) System: Creating a session tables are all types of system privilege.
2) Object: Any command or query to work on tables comes under object
privilege.
DCL includes following command-
1) Grant:-This command is used for granting privileges to other users.
-The privilege list allow the granting of several privilege in one command (like,
select, update)
-The relation name is nothing but the table name and user name means the
privilege where we have to apply for which user .If user creator, the user insert
authority over a table in case the user wants to share a table with others.
-The appropriate privilege can be granted on that particular table to others.
Syntax:-Grant <privilege list>on <relation name >to <user name>
Example:-Grant select, update on student to warehouse;
In the above example the user warehouse can only view the data from the table
student as wll as he can make changes into the table student.
2) Revoke Privilege command
-This command is used to with draw the privilege that has been granted to a
user. The revoke command removes user access rights or privileges to the
database object.
Syntax:-Revoke <privilege list >on <relation name>from <user name>
Example:-Revoke select, update on student from warehouse;
In the above example revoke command will with draw the privilege select
&update from the user warehouse for a table student.
4) Transaction Control Language (TCL)
A transaction is a logical unit of work this operation are automatically
committed in the database that’s why they cannot be used while creating tables
or dropping them following commands are TCL commands
1. Commit
2. Rollback
3. Savepoint
1) Commit:-Commit command used to permanently record all changes that
the user has made to the database since the last commit command was
issued or since the begging of database session .This command also
erases or save points in the transaction thus releasing the transaction
activity.
Syntax:-commit [work];
Example:-commit;
Work is an optional keyword that does not change the meaning of commit.
2) Rollback :- The rollback command is the transcational command used to
undo transcation that have not already saved to the database. A rollback
is used to in current transcation .We can either rollback the entire
transcation so that all the changes made by sql statement are rollback a
transcation.
Syntax:-Rollback;
Example:-insert into student values (7, ‘Amit’, ‘Pune’);
Rollback;
This command will cancel the effect of insert command.
3) Save point:-A save point is a pointed in a transaction when we can roll the
transaction back to the certain point without rolling back the entire transaction.
Syntax: - Save point save point name;
Example: - undo(ctrl+z)
5) DQL
OR
Retrieving data using SQL select:-
This command is used to retrieve the information stored in a table this is
generally referred to as retiring the table. The purpose of the ‘select’ statement
is to retrieve & display data from one or more database tables. Select command
is used to fetch the data from a database table. The sequence of processing in a
select statement is FROM, WHERE, Group by, Having,
Select, order by.
WHERE clause :
This clause is used to select some specific records which satisfies the
condition specified with the clause.To specify the condition we have
to use following operaters:
1. = : Equal
2. < : Less than
3. > : Greater than
4. <=: Less than or equal to
5. >=: Greater than or equal to
6. <>: Not equal
7. LIKE: Select item that are partial match

emp_id Name Job DOB Salary


101 Rohan Manager 20-Mar-78 5000
102 Mohan Clerk 21-Jan-79 4000
103 Pankaj Manager 15-Feb-80 3500
104 Roma Clerk 17-July-80 6500
105 Pinky Clerk 20-July-79 4500
106 Manish Manager 22-Dec-78 5000
107 Ritesh Manager 07-Feb-78 3100
108 Amar Clerk 18-Dec-79 4700

1.SELECT emp_id,Name From employee WHERE Salary is >=4500;


empid Name
101 Rohan
104 Roma
105 Pinky
106 Manish
108 Amar

1. select Name ,Job From employee WHERE Job = Manager;


Name Job
Rohan Manager
Pankaj
empid Manager Name
Manish
101 Manager Rohan
Ritesh
104 Manager Roma
107 Ritesh
2. SELECT emp_id,Name From employee WHERE Salary <>5000;
empid Name
102 Mohan
103 Pankaj
104 Roma
105 Pinky
107 Ritesh
108 Amar

LIKE Condition :
It allows you to use wild cards in the WHERE clause of an SQL statements. The
LIKE condition can be used in any valid SQL statement SELECT, INSERT,
UPDATE OR DELETE.
The patterns that you can choose are as follows:
1. Percentile (‘ % ’) –
Percentile allows you to match any string of any length.
2. Underscore (‘ _ ‘) –
Underscore allows you to match on a single character.
Example :
Suppose we want to display the name of employee starts with letter R then,
SELECT emp_id , Name from employee WHERE Name LIKE ‘R%’;

NOTE: Consider table employee.


Suppose we want to display the name of employees ends with letter h then,
SELECT emp_id, Name from employee WHERE name LIKE ’%h’;

SQL Logical Operator:


There are 3 logical operators:
1) AND
2) OR
empid Name 3) NOT
106 Manish These operators compare two
107 Ritesh conditions at a time to determine
a row can be selected for output.
1. AND Operator :
IF you want to select row, that must satisfy all the given conditions you
can use the logical operator AND.
EX. To find the name of all employees whose salary is between the ranges 3000
to 4500
The query would be,
SELECT Name, Salary from employee WHERE Salary > =3000 AND Salary
<=4500;

Name Salary
Mohan 4000
Pankaj 3500
Pinky 4500
Ritesh 3100

2. OR Operator:
If you want to select rows that satisfy at least one condition from the given
condition you can use the logical operator OR.
Example:
SEELECT emp_id, Name from employee WHERE Name = ‘Ritesh’OR Salary
= 4000;

empid Name
102 Mohan
107 Ritesh

3. NOT Operator :
If you want to find rows, that don’t satisfy condition you can use the logical
operator NOT.
Example :
SELECT emp_id , Name from employee WHERE NOT Job = ‘clerk’;
empid Name
101 Rohan
103 Pankaj
106 Manish
107 Ritesh

4. IN Operator :
This can be used to select the rows that match one of the values in the list .
Syntax :
SELECT column_name from<table_name>WHERE column_name IN
(value1,value2,…);
Example: SELECT Name ,Salary from employee WHERE Salary IN
(3000,4000,5000);
Name Salary
Rohan 3000
Mohan 4000
Manish 5000
5. BETWEEN Operator:
The BETWEEN operator is used in a WHERE clause to select a range of data
between two values the values can be numbers, text, dates.
Syntax: SELECT column_name from <table_name>WHERE column_name
BETWEEN value1 AND value2;
Example :
SELECT Name, Salary from employee WHERE Salary BETWEEN 3500 AND
6500;
Name Salary
Rohan 5000
Mohan 4000
Pankaj 3500
Roma 6500
Pinky 4500
Manish 5000
Amar 4700

Group by clause OR Aggregated data using group function:


This group by clause allows multiple columns to be grouped so aggregate
functions may be performed on multiple columns with one command .
A group by clause arranges data rows into a group according to the columns
specify. Following keywords are used by the aggregate function.
1) AVG : Get the average of specified column.
2) COUNT : Get the quantity of values in the column.
3) MAX : Return the maximum value in the specified column.
4) MIN: Return the maximum value in a specified column.
5) SUM: Return the sum of all numeric values in the specified column.

CUSTOMER

Sale date Cust name Product Qty Price

08-Dec-06 Mangesh Pen 1 30


08-Dec-06 Bharat Pencil 2 20
08-Dec-06 Magesh Eraser 2 60
10-Dec-06 Bharat Scale 2 40
10-Dec-06 Pavan Sharpner 1 15
10-Dec-06 Bharat Lead -pencil 2 20
10-Dec-06 Pavan Ink -pen 1 25

A group function returns a result based on group of rows, some of


these are purely mathematical functions as follows:

Suppose we want to calculate the total quantity of each customer in the


above table then use following query:

SELECT custname, sum(Qty)from customer group by custname;


Custname Sum(Qty)
Mangesh 3
Bharat 6
Pavan 2

Find out the total quantities of customer according to dates.


1. AVG: This function will return the AVG of values of the column
specified in the argument of the column.
Example:
1. SELECT saledate, Avg(price) from customer group by saledate ;

Saledate Avg(price)
08-Dec-06 36.66
10-Dec-06 25

2. SELECT custname, avg(price)from


Custname customer group by custname;
Avg(price)
Mangesh 45
Bharat 26.66
Pavan 20

2.COUNT function: Count function count the number of rows including the
no. of values present in the column without including NULLS.
SELECT custname, count (custname) from customer group by custname;
Custname Count(custname)
Mangesh 2
Bharat 3
Pavan 2

3.MAX() : To perform an operation which gives the maximum of a set of


values the max function can be used.
SELECT custname , MAX(price) from customer group by custname;
Custname MAX(price)
Mangesh 60
Bharat 40
Pavan 25

4. MIN() : This function will give the least of all values of the column present
in the argument.
SELECT custname , MIN(price) from customer group by custname;
Custname MIN(price)
Mangesh 30
Bharat 20
Pavan 15

5.SUM: The sum function can be used to obtain the sum of a range of values of
a record set.
SELECT custname, sum(Qty+price) as a total from customer group by
customer;
Custname Total
Mangesh 93
Bharat 86
Pavan 42

Displaying data from multiple tables:


OR
Joins in SQL:
Joins is one of the most important feature of SQL, to define relationship among
multiple tables and to retrieve data through these relationships, all with a single
command. The main purpose of a join is to combine the data spread across
tables. A join is actually performed by the WHERE Clause which combines the
specified rows of a table.
To join two tables we have to create references or objects of the table to
avoid the duplication.
SELECT columns from table1, table2 WHERE logical expressions;
There are basically three different types of joins:
 simple join (inner join)
 self join
 outer join
1. Simple join:
It is the most common type of join .It retrieves rows from two tables having a
common column and is further classified into two types:
A. Equi Join:
A join which is based on equalities is called as an equijoin. The equi join
combines rows that have equivalent values for the specified columns.
Item table
Icode Name DOP DOE Stock Max Price
level
101 Dove 15Jan97 15Jan99 40 100 20
102 Lifebuoy 20Jul98 20Jul99 50 100 25
103 Moti 21Aug99 21Aug2001 20 50 40
104 Lux 10Mar98 10Mar99 20 50 45
105 Cinthol 13Oct99 13Oct2001 125 100 22

Cust table
Cname Qty Icode
Rakesh 4 101
Mohan 10 103
Sumit 8 105

SELECT Cname, name, Qty ,price from item_table,cust_table;


Cname Name Qty Price
Rakesh Dove 4 20
Mohan Moti 10 40
Sumit Cinthol 8 22

Customers
Id Name Age Address Salary

1 Rajesh 32 Ahmedabad 2000


2 Ketan 25 Delhi 1500
3 Kaushal 23 Kota 2000
4 Chaitanya 25 Mumbai 6500
5 Harish 27 Bhopal 8500
6 Ketki 22 Mp 4500

Orders
Oid Date Customer id Amount

1 2016-10-08 3 3000
2 2016-10-08 3 1500
3 2016-11-20 2 1560
4 2015-05-20 4 2060

1.SELECT id, name , Name, Age, Amount from customers orders WHERE
customerid=orders.customer_id;

Id Name Age Amount


3 Kaushal 23 3000
3 Kaushal 23 1500
2 Ketan 25 1560
4 Chaitanya 25 2060

B. Inner join :
It is also called as simple / equi join which is use frequently on SQL for joining
tables. The inner join creates a new result table by combining column values of
two tables.
Syntax: SELECT table1.column1,table2.column2,… from table1 inner join
table 2 on table1.column field=table2.column field;
SELECT id, Name, amount, date from customers inner join orders on
customers.id =orders.customer_id;
Id Name Amount Date
3 Kaushal 3000 2016-10-08
3 Kaushal 1500 2016-10-08
2 Ketan 1560 2016-11-20
4 Chaitanya 2060 2015-05-20

2.LEFT join :
Left join returns all values from the left table plus matched values from the
right tab keyword return all rows from left table (table1) with the matching rows
in the right table (table2). The result is NULL in the right side when there is no
match.
Syntax : SELECT table1.column1,table2.column2 from table1 left join table 2
on table1.common field=table2.common field;
Example:
SELECT id, name, amount, date from customers left join orders on
customers.id=orders.customerid;
Id Name Amount Date
1 Rajesh NULL NULL
2 Ketan 1560 2016-11-20
3 Kaushal 3000 2016-10-08
3 Kaushal 1500 2016-10-08
4 Chaitanya 2060 2015-05-20
5 Harish NULL NULL
6 Ketki NULL NULL
3. RIGHT join :
A right join returns all values from right table plus matched values from the left
table.
In some databases right join is called as right join .
The right join keyword returns all rows from right table (table 2) with the
matching rows in the left table (table 1)
The result is NULL in the left side when there is no match.
Syntax: SELECT table1.column1, table2.column2 from table1 right join table 2
on table1.common field=table2.common field;
Example :
SELECT id, name, amount, date from customers right join order on
customer.id=orders.customersid;

Id Name Amount Date


3 Kaushal 3000 2016-10-08
3 Kaushal 1500 2016-10-08
2 Ketan 1560 2016-11-20
4 Chaitanya 2060 2015-05-20

Subqueries :
A subquery is a query within a query .As we know a query language is a
language in which a user requests information from the databases. The result of
the subquery is used by the DBMS to determine the result of higher level query
that contains a subquery. A subquery or inner query or nested query is query
within another query and embedded with WHERE clause. A subquery is use to
return data that will be used in main query. As a condition to further restrict the
data to be retrieve. Subquery can be used with the SELECT, INSERT,
UPDATE and DELETE Statements along with operators like =, <,>, <=, >=, IN
and BETWEEN etc.
Rules for the sub queries must follow:
1. Sub queries must be enclosed with parenthesis.
2. A sub query can have only one column in the SELECT clause, unless
multiple columns are in main query for the sub query to compare its
selected column.
3. An order by cannot be used in sub query. Although, the main query can
use an order by, the group by can be used to perform same function as the
order by in subquery.
4. A sub query that returns more than 1 row can only be used with multiple
value operator such that IN operator.
5. The SELECT list cannot include any references to values to a BLOB,
Array, CLOB or NCLOB .
6. A subquery cannot be immediately closed with a set function.
7. The BETWEEN operator cannot be used with a subquery. However the
BETWEEN operator used within subquery.

 Example of subqueries with the SEELECT statement :


Subqueries are most frequently used with the SELECT statement.
Syntax : SELECT column_name [column_name] FROM
table1[table2] WHERE column_name operator

CUSTOMER
ID Name Age Address Salary

1 Ramesh 35 Ahmedabad 2000


2 Kiran 25 Delhi 1500
3 Kunal 23 Kota 2000
4 Chetan 25 Mumbai 6500
5 Hardik 27 Bhopal 8500
6 Komal 22 MP 4500
7 Mukund 24 Indore 10000

SQL> SELECT *FROM customer WHERE ID IN(SELECT ID FROM


customer WHERE Salary>4500);
ID Name Age Address Salary
4 Chetan 25 Mumbai 6500
5 Hardik 27 Bhopal 8500
7 Mukund 24 Indore 10000

 Example of subqueries with the INSERT statement :


Syntax: insert into the table_name[(column1,column2)]
SELECT[*[column1,column2] FROM table1[table2]]

Consider a table customerdetails with similar structure as customer table.


Now to copy complete customer table into customerdetail.

SQL> insert into customer_details select *from customer WHERE ID


IN(select id from customer);
Customer_details
ID Name Age Address Salary

1 Ramesh 35 Ahmedabad 2000


2 Kiran 25 Delhi 1500
3 Kunal 23 Kota 2000
4 Chetan 25 Mumbai 6500
5 Hardik 27 Bhopal 8500
6 Komal 22 MP 4500
7 Mukund 24 Indore 10000

 Example of subqueries with the UPDATE statement :


Single or multiple columns in a table can be updated when using a
subquery with the update statement.
Syntax: UPDATE table
SET column_name=new_value
[WHERE OPERATOR [VALUE]]
(SELECT COLUMN_NAME FROM TABLE_NAME)
[WHERE]
In the previous table customer and customerdetail UPDATES salary by
0.25 times in customer table for all the customers whose age>= 27.
SQL> UPDATE CUSTOMER
SET SALARY=SALARY*0.25
WHERE AGAIN (SELECT AGE FROM CUSTOMER_DETAIL)
WHERE AGE>=27);

ID Name Age Address Salary

1 Ramesh 35 Ahmedabad 500


2 Kiran 25 Delhi 1500
3 Kunal 23 Kota 2000
4 Chetan 25 Mumbai 6500
5 Hardik 27 Bhopal 2125
6 Komal 22 MP 4500
7 Mukund 24 Indore 10000

 Example of subquery with DELETE statement:

The subquery can be used in conjunction with the DELETE statement


like with any other statement.

Syntax: DELETE FROM table_name


[WHERE OPERATOR VALUE]
(SELECT column_name FROM table_name)
[WHERE]

Following example deletes record from customer table from all the
customer whose age>= 27.
SQL> DELETE FROM CUSTOMER WHERE Age IN[SELECT Age
From customer detail WHERE Age>=27]

ID Name Age Address Salary

2 Kiran 25 Delhi 1500


3 Kunal 23 Kota 2000
4 Chetan 25 Mumbai 6500
6 Komal 22 MP 4500
7 Mukund 24 Indore 10000

SQL Set Operation

The SQL Set operation is used to combine the two or more SQL SELECT
statements.

Types of Set Operation

1. Union
2. UnionAll
3. Intersect
4. Minus

1. Union
o The SQL Union operation is used to combine the result of two or more
SQL SELECT queries.
o In the union operation, all the number of datatype and columns must be
same in both the tables on which UNION operation is being applied.
o The union operation eliminates the duplicate rows from its resultset.

Syntax

SELECT column_name FROM table1 UNION SELECT column_name FRO


M table2;

Example:

The First table

ID NAME

1 Jack
2 Harry
3 Jackson

The Second table

ID NAME

3 Jackson
4 Stephan
5 David

Union SQL query will be:

SELECT * FROM First UNION SELECT * FROM Second;

The resultset table will look like:

ID NAME

1 Jack
2 Harry
3 Jackson

4 Stephan
5 David

2. Union All

Union All operation is equal to the Union operation. It returns the set without
removing duplication and sorting the data.

Syntax:

SELECT column_name FROM table1


UNION ALL SELECT column_name FROM table2;

Example: Using the above First and Second table.

Union All query will be like:

1. SELECT * FROM First


2. UNION ALL
3. SELECT * FROM Second;

The resultset table will look like:

ID NAME

1 Jack
2 Harry
3 Jackson
3 Jackson
4 Stephan
5 David

3. Intersect

o It is used to combine two SELECT statements. The Intersect operation


returns the common rows from both the SELECT statements.
o In the Intersect operation, the number of datatype and columns must be
the same.
o It has no duplicates and it arranges the data in ascending order by default.

Syntax

SELECT column_name FROM table1 INTERSECT SELECT column_name F


ROM table2;

Example:

Using the above First and Second table.

Intersect query will be:

SELECT * FROM First INTERSECT SELECT * FROM Second;

The resultset table will look like:

ID NAME

3 Jackson

4. Minus

o It combines the result of two SELECT statements. Minus operator is used


to display the rows which are present in the first query but absent in the
second query.
o It has no duplicates and data arranged in ascending order by default.

Syntax:

SELECT column_name FROM table1 MINUS SELECT column_name FRO


M table2;

Example

Using the above First and Second table.

Minus query will be:

SELECT * FROM First MINUS SELECT * FROM Second;

The resultset table will look like:


ID NAME

1 Jack
2 Harry

Single row functions:


Single row functions can be character functions, numeric functions, date
functions, and conversion functions. Note that these functions are used to
manipulate data items. These functions require one or more input arguments and
operate on each row, thereby returning one output value for each row.
Argument can be a column, literal or an expression. Single row functions can be
used in SELECT statement, WHERE and ORDER BY clause. Single row
functions can be -
General functions - Usually contains NULL handling functions. The

functions under the category are NVL, NVL2, NULLIF, COALESCE,
CASE, DECODE.
 Case Conversion functions - Accepts character input and returns a
character value. Functions under the category are UPPER, LOWER and
INITCAP.
o UPPER function converts a string to upper case.
o LOWER function converts a string to lower case.
o INITCAP function converts only the initial alphabets of a string to
upper case.
Conversion Functions:
SQL Conversion functions are single row functions which are capable of
typecasting column value, literal or an expression . TO_CHAR, TO_NUMBER
and TO_DATE are the three functions which perform cross modification of data
types.
Conditional statements in SQL procedures

Conditional statements are used to define what logic is to be executed based on


the status of some condition being satisfied.

There are two types of conditional statements supported in SQL procedures:

 CASE
 IF
These statements are similar; however the CASE statements extends the IF
statement.

 CASE statement in SQL procedures


CASE statements can be used to conditionally enter into some logic
based on the status of a condition being satisfied.
 IF statement in SQL procedures
IF statements can be used to conditionally enter into some logic based on
the status of a condition being satisfied.

You might also like