0% found this document useful (0 votes)
25 views115 pages

Xpresdata SQL Material-1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views115 pages

Xpresdata SQL Material-1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 115

SQL MATERIAL

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


DATABASE MANAGEMENT SYSTEM

A DBMS which is based on relational theory


is called as relational database management
system.

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Database

It is collection of meaningful data

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Management System
It is a software it helps to manage the database management system should able to perform the
following activities very easily.

1. Inserting the new data.

2. Updating the existing data.

3. Deleting unnecessary data.

4. Retrieving the require data.

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Examples of RDBMS
1. ORACLE

2. SQL SERVER

3. DB2

4. MYSQL

5. SYBASE

6. TERA DATA

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


SQL
Structured query language pronounced as (SEQUEL). This
language is used to communicate to oracle database.

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


SQL Commands
1. DDL (Data Definition Language)

2. DML (Data Manipulation Language)

3. DRL/DQL (Retrieval/query)

4. TCL (Transaction Control Language)

5. DCL (Data Control Language)

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


DDL
This language is used to manage database objects. It is collection of five commands.

 CREATE

 ALTER

 DROP

 TRUNCATE

 RENAME

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


DML
This language is used to manipulate the data you have stored. It is collection of four commands.

 INSERT

 UPDATE

 DELETE

 MERGE

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


What is Flashback Query ?

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


DRL
This language is used to retrieve the data from the database. It is collection of only one command.

 SELECT

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


TCL
It is used to maintain the transaction of Oracle database. It is collection of three commands.

 COMMIT

 ROLLBACK

 SAVEPOINT

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


DCL
This language is used to control the axis of the data to the users it is collection of two commands.

 GRANT

 REVOKE

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


DATA TYPES
 Char

 Varchar2

 Number(P,S)

 Date

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Char & Varchar2

Black Padded Mechanism

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


What is an Operator ?

SQL Operator is a special symbol in database which can be used for Mathematical and Logical
Operations.

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Operators in SQL
o Arithmetic Operators

o Comparison Operators

o Logical Operators

o Special Operators

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Arithmetic Operators
+ Add

- Subtract

* Multiply

/ Divide

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Comparison Operators

Operator Description
= Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
<> Not equal to

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Logical Operators
 AND

 OR

 NOT

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Special Operators
 IN NOT IN

 BETWEEN NOT BETWEEN

 LIKE NOT LIKE

 IS NULL IS NOT NULL

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


SELECT QUERY FLOW
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY
Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com
NVL Function

NVL(expr1, expr2)

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Data Model

Data models define how the


logical structure of a database

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


ER Model
ER Model is best used for the conceptual design of a
database.
ER Model is based on −
•Entities and their attributes.
•Relationships among entities.
Entity = Attribute

Relationship = Mapping Columns

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


ER Relationships
• one to one
• one to many
• many to one
• many to many

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com
What is Schema ?

A Schema is the relational structure of database


objects

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


About Schema ?
 The schema contains tables and joins.

 It defines the logical relationship of the tables and is within the database

 The tables contain columns that you eventually map to objects that end users use
to create reports.

 The joins link the tables so that the correct data is returned for queries that are run
on more than one table.

 A schema is the set of metadata (data dictionary) used by the database, typically
generated using DDL.

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Types of Schema

1. Star Schema
2. Snow Flake Schema
3. Galaxy Schema

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Fact Tables

The fact table contains business facts.


KPI – Facts (Number data)

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Dimension Tables

Dimension Tables has description of data.

NKPI – Non Facts (Text Information)

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


1. Star Schema

Star schema is a simplest form of dimensional data


model where the data is organized into facts and
dimensions.
One fact table connects to several dimensions.

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


2. Snow Flake Schema
A Snowflake Schema is an extension of a
Star Schema, and it adds additional dimensions.
It is called snowflake because its diagram resembles
a Snowflake.
The dimension tables are normalized which splits
data into additional tables.

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


3. Galaxy Schema
Galaxy Schema is also known as fact constellation schema
as the name suggests, appears like a galaxy in the space.

The design involves more than one fact table, which are
connected further with multiple dimension tables that are
completely normalized.

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


SQL Functions
1. Number Functions
2. Character Functions
3. Date Functions
4. Group Functions

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


1.Number Functions
oABS -> abs(value)

oMOD -> mod(value1,value2)

oROUND -> round(value), round(value1,value2)

oTRUNC -> trunc(value), trunc(value1,value2)

oGREATEST -> greatest(value1,value2,value3,…)

oCIEL -> ceil(value)

oFLOOR -> floor(value)

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


2. Character Functions

LOWER -> lower(ENAME)

UPPER -> upper(ENAME)

INITCAP -> initcap(ENAME)

CONCAT -> concat(exp1,exp2)

LENGTH -> length(ENAME)

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


2. Character Functions
SUBSTR -> substr(exp1,exp2) / substr(exp1,exp2,exp3)
INSTR -> instr(exp1,exp2)
LPAD -> lpad(exp1,exp2,exp3)
RPAD -> rpad(exp1,exp2,exp3)
LTRIM -> ltrim(exp1,exp2)
RTRIM -> rtrim(exp1,epx2)
REPLACE -> replace(exp1,exp2,exp3)
TRANSLATE -> translate(exp1,exp2,exp3)

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


3.Date Functions

o SYSDATE

o ADD_MONTHS

o LAST_DAY

o NEXT_DAY

o MONTHS_BETWEEN

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


4. Group Functions
o MAX

o MIN

o AVG

o SUM

o COUNT(*)

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


USING GROUP BY

 ROLLUP

 CUBE

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


IMPORTANT FUNCTIONS

 TO_DATE

 TO_CHAR

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


SQL Joins

Joins are used to get the data from more than one table.

If you we have ‘n’ tables then we need to use ‘n-1’ join conditions.

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


8i Joins & 9i Joins

8i Joins
 Equi Join
 Non Equi Join
 Self Join
 Outer Join

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


9i Joins

9i Joins
 Inner Join
 Left Outer Join
 Right Outer Join
 Natural Join

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Equi Join (Inner Join)

We use equality operator (=) in where condition to retrieve the data from
multiple tables using join condition.

We can fetch the data from common columns using ‘joins’.

We get only matching rows for inner join.

Note : same datatype is required for joining columns.

ORA-01722: invalid number

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Syntax for Inner Join

Select column1,column2,…….
From Table1,Table2
Where Table1.Commoncolumn=Table2.Commoncolumn ;

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


How to avoid ambiguity?

We need to use alias names in the query to avoid ambiguity.

ORA-00918: column ambiguously defined

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Example :

Select e.ename, e.sal, d.deptno, d.loc


From emp e, dept d
Where e.deptno=d.deptno;

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Example :

Write a query to get maximum, minimum, and sum salary from the location
wise?

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Non Equi Join

We use Non-Equi Join to fetch the data from the tables

except equality conditions (>,<,>=,<=).

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Self Join

 Joining a table itself is called ‘Self Join’.

 Both columns must be same datatype.

 We need to use column alias for self join.

We use self join for two conditions:

 We can compare column to column in the table

 We can compare value to value in the column.

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com
Self Join

1. Write a query to display employee names and their manager names


from emp table using self join?
select e1.ename “employees”, e2.ename “managers”
from emp e1, emp e2
where e1.mgr=e2.empno ;

Hierarchical Relationship

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Hierarchical Relationship

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Hierarchical Relationship

SELECT ename, empno, mgr


FROM emp
START WITH mgr IS NULL
CONNECT BY PRIOR empno = mgr;

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Outer Join

We can fetch the all rows from one table and matching rows from another table
using Outer Join.

For Outer Join, We use Join Operator (+).

This Join operator is used only one side in the joining condition.

(+)  matching rows

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Outer Join Example
Select ename, sal, d.deptno,dname

From emp e, dept d 14 ROWS

Where e.deptno = d.deptno(+);

Select ename, sal, d.deptno,dname

From emp e, dept d 15 ROWS


Where e.deptno(+) = d.deptno;

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Practice Join Query :
1. Write a query to display the employees who are working in ‘CHICAGO’
location ?

2. Write a query to display the sum salary by department name wise?

3. Find out total employees in location wise?

4. Write a query to get sum salary in department name wise along with total

Salary amount using rollup?

5. Display the employees from each department wise and also display the
department names which has more than 5 employees?

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


9i Joins & ANSI Joins

9i Joins
1. Inner Join

2. Left Outer Join

3. Right Outer Join

4. Full Outer Join

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Inner Join

It returns matching rows only.

Joining columns must be same datatype.

We use these joins in all databases because of ANSI Joins.

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


9i Inner Join Syntax :

Select column1,column2,….
From TableName1 join TableName2
On TableName1.Commoncolumn=TableName2.Commoncolumn ;

Select e.ename, d.dname


From emp e join dept d 14
Rows
On e.deptno=d.deptno;

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Inner Join

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


9i - Inner Join Example

Display the employees who are working in ‘CHICAGO’ Location?

Select e.ename, d.loc from emp e join dept d

on e.deptno=d.deptno

Where loc=‘CHICAGO’;

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Practice Query :

Display the employee names who is working in ‘SALES’ Department?

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


9i Join – Left Outer Join

Returns all rows from left side table and matching rows from right side table.

And also returns ‘NULL’ values in the place of non-matching rows.

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Left Outer Join

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Left Outer Join Example

Select e.ename, d.loc 14


from emp e left join dept d Rows
on e.deptno= d.deptno;

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


9i Join – Right Outer Join

Returns all rows from right side table and matching rows from left side table.

And also returns ‘NULL’ values in the place of non-matching rows in another
table.

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


9i Join – Right Outer Join

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Right Outer Join Example

Select e.ename, d.loc


15
from emp e right join dept d Rows
on e.deptno= d.deptno;

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


9i Join – Full Outer Join

Return all rows from all tables (left & right).

It will return matching and non-matching rows.

But ‘NULL’ values return in the place of non-matching rows.

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


9i Join – Full Outer Join

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


9i Join – Full Outer Join

Select e.ename, d.loc 15


from emp e full join dept d Rows

on e.deptno= d.deptno;

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Natural Join

We are using this Natural Join when we do not required joining condition
between the tables.

But must contain common column.

Note : Only we can use for two tables.

Example:

select * from table1 natural join table2;

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Cross Join

Cross join is also referred to as Cartesian Product.

For every row in the LEFT Table of the CROSS JOIN


all the rows from the RIGHT table are returned and
Vice-Versa.

(i.e. result will have the Cartesian product of the rows


from join tables).

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Cross Join

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Cross Join Example

Syntax:

Select * from Table1 cross join Table2 ;

Example:

Select * from emp cross join dept ;

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Cross Join

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Cross Join

No.of Rows in the Result of Cross Join = (No. of Rows in LEFT Table) * (No. of Rows in RIGHT Table)

CROSS JOIN = EMP (14 ROWS) * DEPT (4 ROWS)

56 ROWS

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


SQL CONSTRAINTS

 Constraints are used to stop invalid data entry into tables.

 We use constraints for columns in the table.

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Types of Constraints

1. Not Null

2. Unique

3. Primary Key

4. Foreign Key

5. Check

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


1. Not Null

Not Null doesn’t support null values but it will accept duplicate values.
Note: This Constraint is not supported for Table Level.
Example :
create table xpresdata (sno number(10) not null,name varchar2(100));
insert into xpresdata values (10,’BCD’);
insert into xpresdata values (10,’DFG’);
insert into xpresdata values (null,'ABC’); - Error

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


2.Unique
Unique Constraint doesn’t accept duplicate values but it accept null values.
This is defined at column level & at Table level.
Example:
create table xpresdata1(sno number(10) unique,name varchar2(100));
insert into xpresdata values (null,'ABC’);
insert into xpresdata values (10,’BCD’);
insert into xpresdata values (10,’DFG’); - Error
create table xpresdata2(sno number(36),name varchar2(100) ,unique(sno,name));

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


3.Primary Key

 Primary Key doesn’t accept duplicate values and null values.


 This key used to identify the unique record from the table.
 We use Primary key for relationship with another tables.
 Primary Key table is Parent Table.
 This is also called as Master Table.
Note : Only one Primary Key for One Table.
create table xpresdata4 (sno number(10) primary key ,name varchar2(10),primary
key(sno,name)); -> Error

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Primary Key Example
Example :
create table xpresdata3 (sno number(10) primary key ,name varchar2(10));
insert into xpresdata3 values (10,'ARJUN’);
insert into xpresdata3 values (20,'RAM’);
insert into xpresdata3 values (20,'SIVA’); -> Error

Composite Primary Key :


create table xpresdata4 (sno number(10) ,name varchar2(10),primary key(sno,name));

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


4.Foreign Key
We use Foreign Key to maintain the relationship with the tables.

We can call it as ‘Referential Integrity Constraints’ (Foreign Key).

Note:

This Foreign Key always with connection of ‘Primary Key Columns’.

Primary Key Column & Foreign Key Column must be same datatype.

Foreign Key Column will accept duplicates and null values.

 This is called CHILD Table.

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Foreign Key Example

Example :

create table xpresdata5(sno number(10) references xpresdata3(sno),loc


varchar2(100));

Note:

 All Constraints can be used for column level to avoid ambiguity.

 Only Primary key data will be inserted into Foreign key columns in any
tables.

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


5.Check

Check Constraint is used to stop invalid entry to the columns in the tables.

Example 01:

create table xpresdata6(sno number(10),name varchar2(100),sal number(5)


check(sal>5000));

insert into xpresdata6 values (1,'ARJUN',6000);

insert into xpresdata6 values (2,'RAJ',7000);

insert into xpresdata6 values (3,'KIRAN',3000); -> Error

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Check

Example 02 :
create table xpresdata7(sno number(10),name varchar2(100)
check(name=upper(name)),sal number(5));
insert into xpresdata7 values (1,'ARJUN',8000);
insert into xpresdata7 values (2,'Uma',9000); -> Error
Important Note:
Select search_condition,table_name,constraint_type from user_constraints where
table_name=‘XPRESDATA7’;

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


How to see the constraints
We can see the constraints for columns using ‘Data Dictionaries ‘ (Real Only
Tables from Database).

For Constraints we use USER_CONSTRAINTS table to verify.

Step-01:

desc user_constraints;

Step-02 :

Select constraint_name,constraint_type,table_name from user_constraints


where table_name like '%XPRES%';

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


How to create a table from
another table?
Example 01 :
Table Creation without data from another table.
Create Table EMP_NEW as select * from emp where 1=2;
Example 02 :
Table Creation with data from another table.
Create Table EMP_NEW as select * from emp where 1=1;
Or
Create Table EMP_NEW as select * from emp;

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


SET Operators
 Set Operators are used to fetch the data from tables.

 Whenever we use set Operators, then expressions must be same data


type.

 Always set Operators returns first query column names.

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


SET Operators
We have 4 Set Operators in SQL.

They are :

 Union

 Union All

 Intersect

 Minus

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Union
 It return unique values.

 It has automatic sorting operations.

 It has sorting operations on first column.

Syntax :

Select Column1,Column2,Column3,….from Table1

UNION

Select Column1,Column2,Column3,….from Table2 ;

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Union Example
Example : 01
select * from emp
Union
Select * from emp;
Example : 02
select ename, sal from emp
Union
select ename, sal as total from emp_new;

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Union Example
Example : 03
select ename, sal from emp
Union
Select empno, sal from emp_new;  Error
Example : 04
select ename, sal from emp
Union
Select ename as empname, sal from emp_new;

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Union All
 It will accept duplicate values.

 There is no sorting in Union All.

Syntax :

Select Column1,Column2,… from Table1

Union All

Select Colum1,Column2,… from Table2;

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Union All Example
Example: 01
Select * from emp
Union all
Select * from emp_new;
Example: 02
Select ename,sal from emp
Union all
Select ename,sal from emp_new;

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Intersect
It returns common values from the tables.

Syntax :

Select Column1,Column2,… from Table1

intersect

Select Colum1,Column2,… from Table2;

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Intersect Example
Example 01 :
Select * from emp
Intersect
Select * from emp_new;
Example 02 :
Select ename,sal from emp
Intersect
Select ename,sal from emp_new;

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Minus
It will returns first query rows those values are not in second query.

Syntax :

Select Column1,Column2,… from Table1

intersect

Select Colum1,Column2,… from Table2;

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Minus Example
Example 01:
Select * from emp
Minus
Select * from emp_new;
Example 02:
Select empno,ename,sal from emp
Minus
Select empno,ename,sal from emp_new;

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Union Vs Union All

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Set Operators Task
STAGE1 STAGE2
ID BANK ID BANK
234 HDFC 298 BANDAN
278 SBI 245 CITI
298 YES Union - ? 756 DBS
245 CITI Union All - ? 987 IDFC
756 DBS 234 HDFC
Intersect - ?

Minus - ?

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Analytical Functions

 We use Analytical Functions to assign ‘ranks’ to each row in


a table either group wise or row wise in a table.

 It is similar to ‘Group By’ Only.

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Analytical Functions

1. Row_Number()
2. Rank()
3. Dense_Rank()

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Analytical Functions Syntax
Very Important ****

For Group Wise Rows:

Select Column1,Column2,… Analytical Function Name

Over(Partition By Column Name Order by column name[asc/desc) from Table


Name;

For Row Wise Rows:

Select Column1,Column2,… Analytical Function Name

Over(Order by column name) from Table Name;

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Analytical Functions
Row_Number() will assign different rank numbers when values are same.

Rank() Function will assign same ranks when values are same but rank() will
skip next consecutive rank numbers.

Dense_Rank() Function will assign same ranks when values are same but
Dense_Rank() doesn’t skip any consecutive rank numbers.

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Analytical Functions Example
DEPTNO ENAME SAL ROW_NUMBER()
20 SCOTT 3800 1
20 FORD 3800 2
20 JONES 3475 3
DEPTNO ENAME SAL RANK()
20 SCOTT 3800 1 Rank is
20 FORD 3800 1 Skipped
20 JONES 3475 3
DEPTNO ENAME SAL DENSE_RANK()
20 SCOTT 3800 1
20 FORD 3800 1
20 JONES 3475 2
Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com
Example
Example :
Write a query to get Nth highest salary using emp table.
Select * from (select deptno,ename,sal,dense_rank()
Over(order by sal desc ) r from emp ) where r=&n;
Example:
Write a query to get Nth highest salary in department wise using emp table.
Select * from (select deptno,ename,sal,dense_rank()
Over(partition by deptno order by sal desc ) r from emp ) where r=&n;

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


Example Task
ID ENAME SAL ROW_NUMER() RANK() DENSE_RANK()

191 SIVA 2700

989 RAM 3400

154 GANI 3400

765 SEEMA 3400

787 YOGI 6790

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com


To be Continued…..!

Phone : +91-9741584579 Mail : [email protected] Website: www.xpresdata.com

You might also like