Data Warehousing Community Forum
February 08, 2012, 10:57:31 am

Pages: [1]   Go Down
  Print  
Author Topic: Some typical Queries ??  (Read 840 times)
Arvind
DW Apprentice
**

Reputation: +10/-0
Offline Offline

Posts: 78


View Profile
« on: November 14, 2009, 03:23:40 pm »

Struture of EMP table -

Code:
EMP#
EMP_NAME
MGR#
SALARY

Queries -
  • How to get Third maximum salary from employee table ??
  • List the EMP details with their Supervisor Info ? And If no Supervisor, then keep them blank ?
Logged

regards,
Arvind
Whoever
Administrator
*****

Reputation: +34/-0
Offline Offline

Posts: 124



View Profile WWW
« Reply #1 on: November 19, 2009, 09:00:33 pm »

This would work -
Nth maximum salary
Code:
SELECT TOP 1 A.* FROM
(SELECT TOP n * FROM [EMP]
ORDER BY [SALARY] ASC) A
ORDER BY [SALARY] DESC

Emp with Supervisor, blank for no Supervisor -
Code:
SELECT
A.[EMP#] AS EmployeeNumber,
A.[EMP_NAME] AS EmployeeName,
A.[MGR#] AS ManagerNumber, /* Should be NULL when Employee has no manager */
NVL(B.[EMP_NAME],'') AS ManagerName, /* Should be an empty string when Employee has no manager */
A.[SALARY] AS EmployeeSalary
FROM [EMP] A
LEFT OUTER JOIN [EMP] B
ON A.[MGR#] = B.[EMP#]

p.s. - I'm assuming that's the correct usage of NVL(), I'm more of a T-SQL person. Wink
Logged

If most people said what’s on their minds, they’d be speechless.
Arvind
DW Apprentice
**

Reputation: +10/-0
Offline Offline

Posts: 78


View Profile
« Reply #2 on: November 21, 2009, 11:10:08 pm »

Nth maximum salary
Code:
SELECT TOP 1 A.* FROM
(SELECT TOP n * FROM [EMP]
ORDER BY [SALARY] ASC) A
ORDER BY [SALARY] DESC

Can you rephrase this query with Ranking function ??
Logged

regards,
Arvind
Pages: [1]   Go Up
  Print  
 
Jump to: