C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Sql Queries


Posted Date: 15 Sep 2008    Resource Type: Articles    Category: Databases

Posted By: Gitolekha Ray       Member Level: Silver
Rating:     Points: 10



Q1.Table Structure: EMPLOYEE(EmpName, DeptName, Salary)
1. Write a SQL query to get highest salary in each Department.
2. Write a sql to get the number of employees in each Department.
3. Write a sql to split employee in two columns:
a) Employee above 5000(employee getting salary more than 5000)
b) Employee below 5000(employee getting salary below 5000)
No need to show the employee getting salary = 5000

A.
1. SELECT DeptName, MAX(salary) as "Highest salary"
FROM employee
GROUP BY DeptName

2. select DeptName, count(EmpName)
from Employee
group by DeptName

3a. select Salary, EmpName,
case when salary < 5000 then
'Less than 5000'
else
'Greater than 5000'
end
from employee

OR, alternative way,

3a. select case when salary < 5000 then
EmpName+' '+CAST(salary As Varchar(100))
else
NULL
end 'Less than 5000',
case when salary > 5000 then
EmpName+' '+CAST(salary As Varchar(100))
else
NULL
end 'Greater than 5000'
from employee

3b. select EMPNAME = EmpName,
EMPSAL =
case
when salary < 5000
then CAST(Salary as varchar(100))+ ' '+'Less than 5000'
when salary > 5000
then CAST(salary as varchar(100))+' ' +'Greater than 5000'
when salary = 5000
then CAST(salary as varchar(100))+' ' +'Equal to 5000'
end
from employee1


Q2. Write a sql to get the department name having highest number of employees.

A. Select DeptName From Employee1
Group by DeptName
Having count(EmpName) in(
Select Max(A.COUNTEMP)
From (select DeptName, count(EmpName)as COUNTEMP
from Employee1
group by DeptName) A)




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
SQL queries  .  Solutions  .  Queries  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Creating user defined function in sqlserver database
Previous Resource: What is transaction and ACID properties?
Return to Discussion Resource Index
Post New Resource
Category: Databases


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

Help Desk

Contact Us    Privacy Policy    Terms Of Use