0% found this document useful (0 votes)
231 views5 pages

Select From Employee Where Rowid Select Max (Rowid) From Employee

The document discusses various SQL queries to retrieve data from database tables in different ways such as: - Finding distinct records without using distinct keyword - Retrieving a specific record by number - Fetching the last or highest salary record - Finding duplicate rows - Calculating monthly salary from annual salary - Retrieving the first, last, or specific numbered records - Getting the highest 3 salaries - Displaying odd or even numbered rows - Using rank function to find the 3rd highest salary - Retrieving the first or last 50% of records - Finding employees not assigned to a department - Inserting names with single quotes - Filtering employees joined in a specific month - Calculating

Uploaded by

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

Select From Employee Where Rowid Select Max (Rowid) From Employee

The document discusses various SQL queries to retrieve data from database tables in different ways such as: - Finding distinct records without using distinct keyword - Retrieving a specific record by number - Fetching the last or highest salary record - Finding duplicate rows - Calculating monthly salary from annual salary - Retrieving the first, last, or specific numbered records - Getting the highest 3 salaries - Displaying odd or even numbered rows - Using rank function to find the 3rd highest salary - Retrieving the first or last 50% of records - Finding employees not assigned to a department - Inserting names with single quotes - Filtering employees joined in a specific month - Calculating

Uploaded by

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

What is query to find distinct records without using distinct keyword?

select * from Employee a where  rowid = (select max(rowid) from Employee b where 
a.Employee_no=b.Employee_no);
What is query to find the record no 15 from database?

Select * from ( Select Employee.*, rownum Rn from Employee) Where Rn=15;

What is Query to Fetch last record from the table?

Select * from Employee where Rowid= select max(Rowid) from Employee;


Query to find Second Highest Salary of Employee?

Select distinct Salary from Employee e1 where 2=Select count(distinct Salary) from
Employee e2 where e1.salary<=e2.salary;
Query to find duplicate rows in table?(click here for explaination)

Select * from Employee a where row_id != select max(row_id) for Employee b where
a.Employee_num=b.Employee_num;
Using Simple delete statement you can remove the duplicate records from the table.

Delete from Employee a where rowid != 


         (select max(rowid) from Employee b where  a.Employee_num =b.Employee_num;
3.How to fetch  monthly Salary of Employee if annual salary is given?(click here for
Explaination)

Select Employee_name,Salary/12 as ‘Monthly Salary’ from employee;


4.What is the Query to fetch first record from Employee table?

 Select * from Employee where Rownum =1;


6.What is Query to display first 5 Records from Employee table?

Select * from Employee where Rownum <= 5;

What is Query to display last 5 Records from Employee table?

 Answer:

Select * from Employee e where rownum <=5

union

select * from (Select * from Employee e order by rowid desc) where rownum <=5;

7.What is Query to display Nth Record from Employee table?

Select * from Employee  where rownum = &n;


8.How to get 3 Highest salaries records from Employee table?

select distinct salary from employee a where 3 >= (select count(distinct salary) from emp loyee
b where a.salary <= b.salary) order by a.salary desc;

9.How to Display Odd rows in Employee table?

Select * from(Select rownum as rno,E.* from Employee E) where Mod(rno,2)=1;

10.How to Display Even rows in Employee table?

Select * from(Select rownum as rno,E.* from Employee) where Mod(rno,2)=0;

11.How to fetch 3rd highest salary using Rank Function?

select * from (Select Dense_Rank() over ( order by  salary desc) as Rnk,E.* from Employee E)
where Rnk=3;

13.Display first 50% records from Employee table?

Select rownum,E.* from Employee E where rownum<=(Select count(*/2) from Employee);

14.Display last 50% records from Employee table?

Select rownum,E.* from Employee E


minus
Select rownum,E.* from Employee E where rownum<=(Select count(*/2) from Employee);

17.Find Query to get information of Employee where Employee is not assigned to the department.

Select * from Employee where Dept_no Not in(Select Department_no from Employee);
14.How to insert name with single Quote in Employee table?
Answer:

We need to use  another single Quote before special character.

Insert into Employee (Name) values(‘Amit”’);


15.Fetch the employees who has joined in January month?
Answer:

Select * from Employee where To_Char(Joining_Date,’MM’)=’01’;

Select * from Employee where To_Char(Joining_Date,’MON’)=’JAN’;


17.Select 20% salary of Amit and 10% salary of Pradnya from Employee Table?
Answer:
Select Name,

Case when name=’Amit’ then 0.2*salary end as ‘Amit Salary’,

case when name=’Pradnya’ then 0.1*salary  end as ‘Pradnya Salary’ from Employee;

Database Normalization is nothing but technique of designing the database in structured


way to reduce redundancy and improve data integrity.
1. To Eliminate the redundant or useless data
2. To Reduce the complexity of the data
3. To Ensure the relationship between tables as well as data in the tables
4. To Ensure data dependencies and data is logically stored.

The first normal form is the normal form of database where data must not contain repeating
groups.The database is in First normal form If,

1.It contains only automic values.

Automic values:- The Single cell have only single value

2.Each Record needs to be unique and there are no repeating groups.

Repeating Groups:- Repeating group means a table contains 2 or more values of columns that
are closely related.

2.Second Normal Form/2nd Normal Form:


The data is said to be in second normalized form If,

1.It is in First normal form

2.There should not be any partial dependency of any column on primary key.Means the table have
concatanated primary key and each attribute in table depends on that concatanated primary key.

3.All Non-key attributes are fully functionally dependent on primary key.If primary is is not composite
key then all non key attributes are fully functionally dependent on primary key.

Third Normal Form/3rd Normal Form:


The database is in Third normal form if it satisfies following conditions:

1.It is in Second normal form

2.There is no transitive functional dependency


Transitive Dependency:

When table 1 is Functionally dependent on table 2. and table 2 is functionally dependent on


table 3 then.table 3 is transitively dependent on table 1 via table 2.

Consider following table:

Employee No Salary Slip No Employee Name Salary


1 0001 Amit 50000
2 0002 Divya 40000
3 0003 Rama 57000

In above table Employee No determines the Salary Slip No.And Salary Slip no Determines
Employee name.Therefore Employee No determines Employee Name.We have transitive functional
d4.BCNF(Boyce-Codd Normal Form)

BCNF Normal form is higher version of third normal form.This form is used to handle analomies
which are not handled in third normal form.BCNF does not allow dependencies between attributes
that belongs to candidate keys.It drops restriction of the non key attributes from third normal form.

Third normal form and BCNF are not same if following conditions are true:

1.The table has 2 or more candidate keys

2.At least two of candidate keys are composed of more than 1 attribute

3.The keys are not disjoint.

Example:

Address-> {City,Street,Zip}

Key 1-> {City,Zip}

Key 2->{City,Street}

No non key attribute hence this example is of 3 NF.

{City,Street}->{zip}

{Zip}->{City}

There is dependency between attributes belonging to key.Hence this is BCNF.


dependency so that this structure not satisfying Third Normal Form.

You might also like