Saturday 5 September 2015

Delete multiple duplicate rows ? OR How to Delete Duplicate Records in Oracle ?

What is the difference between UNION and UNION ALL?

This deals with SQL. UNION only selects distinct values, UNION ALL selects all values.

What is a Left outer join?

This deals with SQL. Left outer join preserves the unmatched rows from the first (left) table, joining them with a NULL row in the shape of the second (right) table.

What is SQL?

SQL is a Structured Query Language that allows you to 
communicate with a database and handle the data it 
contains in all kinds of ways. It consists of Data 
Definition Language commands, such as Create, Alter, 
Truncate and Drop, and Data Manipulation Language 
commands, such as Select, Insert, Update and Delete. 
The most common databases that use SQL as their query 
language are Oracle, SQL Server, DB2, and MySQL.

How is the primary key different from a unique key?

Both the primary and unique keys uniquely identify a record in a database table. One difference is that 
you can have more than one unique key per table, but only one primary key. Also, the primary key does not 
allow any null value, while the unique key allows one.

Write a SQL query to get the second highest salary from the table above. Also write a query to find the nth highest salary in SQL, where n can be any number.

Solution to finding the 2nd highest salary in SQL
SELECT MAX(Salary) FROM Employee
WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employee )

Finding nth highest salary example
SELECT *
FROM Employee Emp1
WHERE (1) = (
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)

Find the nth highest salary in Oracle using rownum
select * from (
  select Emp.*, 
row_number() over (order by Salary DESC) rownumb 
from Employee Emp)
where rownumb = n;  /*n is nth highest salary*/

Reference:-


SQL Links ?