SQL Query to find Nth maximum of Nth row?

1. Queries for Nth maximum Nth row?2. In One table there are 5 fields. empno, ename, deptcd, managr_id, salary.Select the departments whose sum of the salary greater than the sum of salaries of any department?3. When index will be usd in the Query?

Showing Answers 1 - 35 of 35 Answers

Ron

  • Jul 21st, 2006
 

1. Queris for Nth maximum Nth row?

Select * from (select column_name1, column_name1 from table_name order by column_name) where rownum <= N


2. In One table there are 5 fields. empno, ename, deptcd, managr_id, salary.
Select the departments whoose sum of the salary greater than the sum of salaries of any department?

Select deptcd, sum(salary) from table_name group by deptcd orderby salary desc

3. When index will be usd in the Query?
Index is used by the Optimizer when a query is fired in Oracle.

pavithra T.M

  • Sep 18th, 2006
 

how to find starting letters in names in sql query

e.g sachin tendulkar

i want to display S & T in this name

  Was this answer useful?  Yes

pavi

  • Sep 18th, 2006
 

write the query hike the sal of employee

1 year experence 10%

less than one year experence 5%

  Was this answer useful?  Yes

Jagan

  • Oct 12th, 2006
 

select salary

from (select rownum rank, salary

        from ( select salary f

 rom emp order by salary desc))where rank=&N
/

  Was this answer useful?  Yes

Jagan

  • Oct 12th, 2006
 

select salary

     from(select rownum rank, salary

            from ( select salary

                  from emp order by salary desc))

                           where rank=&N
/

this should do the job . . . exactly what you are asking

  Was this answer useful?  Yes

Ratheesh

  • Jun 1st, 2007
 

select a.dept from emp a ,emp b
having sum(a.salary)>sum(b.salary)
group by a.dept

  Was this answer useful?  Yes

dj_dj_dj

  • Aug 13th, 2009
 

RE: 1. Queris for Nth maximum Nth row?2. In One table there are 5 fields. empno, ename, deptcd, managr_id, salary.Select the departments whoose sum of the salary greater than the sum of salaries of any department?3. When index will be usd in the Query?*))select department, max(sum(salary)) from table_name group by department *) Function based index will be used for the above querry.

aswini kumar das

  • Nov 16th, 2011
 

select * from hr.employees e
where n-1 = (select count(salary) from hr.employees where e.salary < salary)

  Was this answer useful?  Yes

Verdict

  • Jan 20th, 2012
 

Code
  1. SELECT emplid, Sal,

  2. Case when Total_experience = 1 then (Sal+Sal*0.10)

  3. end AS Salary

  4. FROM Employee

  5.  

  6. SELECT emplid, Sal,

  7. Case when Total_experience < 1 then (Sal+Sal*0.05)

  8. end AS Salary

  9. FROM Employee



  Was this answer useful?  Yes

nehu

  • Feb 14th, 2012
 

The query to your second question will return all the departments even that department whose total salary is not greater than any department. I guess if the question is to find all departments whose total is greater than any department then the query should be

Code
  1.  

  2. declare

  3. cursor c1 IS SELECT department,sum(salary) total FROM emp GROUP BY department;

  4. cursor c2 IS SELECT department,sum(salary) total FROM emp GROUP BY department;

  5. /* hv declared the same cursor bcoz we cannt access the already open cursor in second loop*/

  6. begin

  7. FOR cur IN c1

  8. loop

  9. FOR cur1 IN c2

  10. loop

  11. IF cur.total>cur1.total

  12. then

  13. INSERT INTO result VALUES(cur.department);

  14. /* result is a table which store values of desired departments*/

  15. end IF;

  16. end loop;

  17. end loop;

  18. end;

  19. /

  20.  

  Was this answer useful?  Yes

nehu

  • Feb 15th, 2012
 

The query to hike the sal of employee

1 year experience 10%

less than one year experience 5%

:-

Code
  1. declare

  2. cursor c1 IS SELECT experience,empno FROM emp;/* empno is a unique key in table emp*/

  3. begin

  4. FOR cur IN c1

  5. loop

  6. IF(cur.experience<1) then

  7.  UPDATE emp SET salary=salary+0.05*salary WHERE empno=cur.empno;/* it is necessary to put where clause here otherwise all salaries in table emp will be updated*/

  8. else

  9. UPDATE emp SET salary=salary+0.1*salary WHERE empno=cur.empno;

  10. end IF;

  11. end loop;

  12. end;

  13. /

  Was this answer useful?  Yes

KALYAN KUMAR

  • Feb 23rd, 2012
 

SELECT*FROM EMP WHERE ENAME LIKE S%T;

  Was this answer useful?  Yes

mksakeesh

  • Mar 6th, 2012
 

Window function

Code
  1. SELECT* FROM (SELECT rank() over(ORDER BY price DESC) odrd,products.* FROM products) WHERE odrd =5;

  Was this answer useful?  Yes

priyank

  • May 10th, 2012
 

Use Case in update statement

  Was this answer useful?  Yes

mahesh

  • Aug 12th, 2013
 


Code
  1. SELECT* FROM (SELECT RANK()

  2. over(ORDER BY price DESC)

  3. odrd,products.* FROM products)

  4. WHERE odrd =5;

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions