0% found this document useful (0 votes)
2 views

Complex SQL Coding questions and Answers

The document provides SQL queries for various tasks related to managing employee salary data, including finding and deleting duplicate rows, calculating the second highest salary, creating tables, and fetching records based on specific conditions. It covers techniques such as using CTEs, ranking functions, and self-joins to achieve these tasks. Additionally, it includes methods for displaying records based on row numbers and fetching distinct records without using the DISTINCT keyword.

Uploaded by

renuka reddy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Complex SQL Coding questions and Answers

The document provides SQL queries for various tasks related to managing employee salary data, including finding and deleting duplicate rows, calculating the second highest salary, creating tables, and fetching records based on specific conditions. It covers techniques such as using CTEs, ranking functions, and self-joins to achieve these tasks. Additionally, it includes methods for displaying records based on row numbers and fetching distinct records without using the DISTINCT keyword.

Uploaded by

renuka reddy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.How to find duplicate rows from table and delete them?

ANS::
select * from EmployeeSalary;
 To See how many duplicate records are available
Select employeeID,JobTitle,Salary, count(*) as cnt
from EmployeeSalary group by employeeID having cnt>1;

 Create a table
Create Table EmployeeSalary
(SN int PRIMARY KEY,
EmployeeID int,
JobTitle varchar(50),
Salary int)
 Insert values into table
Insert Into EmployeeSalary
VALUES
(1,1001, 'Salesman', 45000),
(2,1002, 'Receptionist', 36000),
(3,1002, 'Receptionist', 36000),
(4,1002, 'Receptionist', 36000),
(5,1003, 'Salesman', 63000),
(6,1004, 'Accountant', 47000),
(7,1005, 'HR', 50000),
(8,1006, 'Regional Manager', 65000),
(9,1006, 'Regional Manager', 65000),
(10,1007, 'Supplier Relations', 41000),
(11,1008, 'Salesman', 48000),
(12,1009, 'Accountant', 42000),
(13,1009, 'Accountant', 42000)

 See all table values


select * from EmployeeSalary1
 To See count of duplicate records available in the table
Select *, count(*) as cnt
from EmployeeSalary1 group by employeeID having cnt>1;

 Using Max Function to select records with max SN


(select Max(SN)as MaxID from EmployeeSalary1
group by EmployeeID, JobTitle, Salary)
 Selecting only duplicate records
select * from EmployeeSalary1 where SN not in (select Max(SN)as MaxID from
EmployeeSalary1
group by EmployeeID, JobTitle, Salary)
 Deleting the selected duplicate records
Delete from EmployeeSalary1 where SN not in
(select Max(SN)as MaxID from EmployeeSalary1
group by EmployeeID, JobTitle, Salary)

 With CTE
with cte as(
select SN, EmployeeID,JobTitle, Salary,row_number()
over (partition by EmployeeID,JobTitle, Salary order by EmployeeID,JobTitle,
Salary)row_num
From EmployeeSalary1)
select * from CTE where row_num>1

Delete from CTE where row_num>1


Many editors may not detect the undated CTEs.

 With RANK()
Select E.EmployeeID,
E.JobTitle,
E.Salary,
T.srank from EmployeeSalary E
inner join
(Select *,
Rank() over (Partition By EmployeeID
order by SN,EmployeeID)srank
from EmployeeSalary) T on E.EmployeeID=T.EmployeeID;
 worked
with Temp as(
select SN, EmployeeID,JobTitle, Salary,
rank() over (partition by EmployeeID,JobTitle, Salary order by EmployeeID,SN)s_rank
From EmployeeSalary)
select * from temp where s_rank>=1

2. Find second highest Salary Of Employee? Nth highest salary?

 ANS:: Select Max(Amount) from Orders where Amount < ( Select Max(Amount) From
orders order by Amount desc);
 Nth highest Salary:
 Select Min(Amount) from (Select Distinct(Amount) From orders order by Amount desc limit
3 );
 Select Min(Price) From (Select Distinct Price
 From Products Order by Price Desc
 Limit 3);
 Using Self Join::
Select Distinct(Amount) from orders A where 3= (Select count(Distinct(Amount)) From
orders B Where A.Order_ID<=B.Order_ID )order by A.Amount Desc;
3. How to create the Orders_1 table, which is exact replica of Orders table?
Ans:: Create table Orders_1 as Select * From Orders;

4. How to fetch monthly salary of employee if annual salary is given?


Ans: Select EmployeeID, Salary/12 as MonthlySalary
From EmployeeSalary;

5. Write a query to fetch 1st record from the table?


Ans:
 MySql
Select * From EmployeeSalary Limit 1;
Select * From EmployeeSalary Where RowID = (Select MIN(RowID) From
EmployeeSalary )
 Oracle:
Select * From EmployeSalary where RowNum=1;
First 5 records
Select * From EmployeeSalary Where RowID <=5;

6. Write a query to fetch last record from the table?


ANS::
 Select * From EmployeeSalary where RowID =(Select MAX(RowID) From
EmployeeSalary);
 Select * From EmployeeSalary Order by SN DESC Limit 1;
 Last N Records:
 Select * From EmployeeSalary
WHERE
SN >( Select Count(*) From EmployeeSalary)-5;
 Select * From EmployeeSalary
WHERE
ROWID >( Select Count(*) From EmployeeSalary)-5;

7. Query the nth record from the table?


Ans::
 Select * From EmployeeSalary
WHERE ROWID =n;
 SELECT * FROM EmployeeSalary LIMIT N-1,1;

8. Fetch Last N records?


Ans::
 select distinct salary from employeeSalary a where 3 >= (select count(distinct salary)
from employeeSalary b where a.salary <= b.salary) order by a.salary desc
 Always >= Should be used in the where clause of main query.

9. Display all records with Even/odd row number?


ANS:
 odd rows:
 Select * from
(Select rowid as rno,E.* from Orders E)
where rno %2=1;
 Even rows:
Select * from
(Select rowid as rno,E.* from Orders E)
where rno %2=0;

10. create table with same structure as EmployeeSalary table?


Ans:
 Create table EmployeeSalary1 as
Select * From EmployeeSalary1 Where 1=2;

11.Display Top 50% records?


ANS: Top 50% records
 Select rowid, e.* From EmployeeSalary e Where rowid <= (Select count(*)/2 as cnt
From EmployeeSalary);
 Last 50% records
 Select rowid, e.* From EmployeeSalary e Where rowid > (Select count(*)/2 as cnt From
EmployeeSalary);
 First 20% records
Select rowid, e.* From EmployeeSalary e Where rowid <= (Select count(*)/5 as cnt
From EmployeeSalary);

For 40% use count(*)*(0.4)

12. Display common records between two tables?


ANS:
 Select * From EmployeeSalary
INTERSECT
Select * From EmployeeSalary1;

13. How to get distinct records from the table without using distinct keyword?
ANS:
 select * from EmployeeSalary a where rowid = (select max(rowid) from EmployeeSalary
b where a.EmployeeID=b.EmployeeID);

14. Select all records from Employee table whose name is not ‘John’
Ans:
 Select * From Customers Where First_Name NOT IN ('John');

You might also like