Sunday, August 3, 2014

Second highest salary------



Second highest salary------
SELECT*
FROM EmployeeDetails e1
WHERE (1)=(
SELECTCOUNT(DISTINCT(e2.Salary))
FROM EmployeeDetails e2
WHERE e2.Salary > e1.Salary)

selecttop 1 salary from
selectdistincttop 2 salary from EMPLOYEE orderby salary desc)
as salary orderby salary asc

Weekend days SQL



Weekend days
declare @DateFrom DateTime=CONVERT(DATETIME,'01/01/2014', 103)
declare  @DateTo DateTime=CONVERT(DATETIME,'01/03/2014', 103)
;WITH CTE(dt)
AS
(
Select @DateFrom
UnionAll
SelectDATEADD(d,1,dt)FROM CTE
Where dt<@DateTo
)
selectDayName,CONVERT(varchar(50), dt, 103)asDatefrom (select'Saturday'asDayName,dt from CTE
whereDATENAME(dw,dt)In('Saturday')groupby dt
Union
select'Sunday'asDayName,dt from CTE
whereDATENAME(dw,dt)In('Sunday'))as result orderby dt

Rename Colum Name Sql



Createtablesample
(
ID int,
F_Name nvarchar(20),
L_Name nvarchar(20)
)
insertintosample(ID,F_Name,L_Name)values(1,'Ajay','Pravin')
insertintosample(ID,F_Name,L_Name)values(2,'udhaya','Kumar')
insertintosample(ID,F_Name,L_Name)values(1,'Ravi','Kumar')

Select ID,F_Name as First_Name, L_Name as Last_Name fromsample

Cross Join SQL



CREATETABLE [dbo].[EmDetails]
(
                [EmpId] [int] IDENTITY(1,1)NOTNULL,
                [Name] [varchar](50)NULL,
                [Address] [varchar](200)NULL,
                [City] [varchar](50)NULL
)
INSERTINTO EmDetails(Name,Address,City)VALUES ('Lalit','#1234','Chandigarh')
INSERTINTO EmDetails(Name,Address,City)VALUES ('Raman','#4567','Panchkula')
INSERTINTO EmDetails(Name,Address,City)VALUES ('Arjun','#111','Noida')
INSERTINTO EmDetails(Name,Address,City)VALUES ('Rohan','#321','Delhi')
INSERTINTO EmDetails(Name,Address,City)VALUES ('Raghav','#675','Noida')

CREATETABLE [dbo].[Tb_Dept]
(
                [DEPT_ID] [bigint] IDENTITY(1,1)NOTNULL,
                [DEPT_NAME] [varchar](50)NULL
)
INSERTINTO Tb_Dept(DEPT_NAME)VALUES ('HR')
INSERTINTO Tb_Dept(DEPT_NAME)VALUES ('IT')
INSERTINTO Tb_Dept(DEPT_NAME)VALUES ('SALES')

SELECT*FROM EmDetails CROSSJOIN Tb_Dept

SELECT*FROM EmDetails , Tb_Dept