Interview Questions → SQL SERVER → SQL Server Questionnaire II

SQL Server Questionnaire II











Create table tbl_Test 
(
    PK_ID int identity(1,1),
    sName Varchar(200),
    sAddress Varchar(200)
)




insert into tbl_Test values('Name1','Gurgaon')
insert into tbl_Test values('Name2','Gurgaon')
insert into tbl_Test values('Name3','Delhi')
insert into tbl_Test values('Name4','Delhi')






select * from tbl_Test where sAddress like 'Gurgaon'




select * from 
--    derived table
(select * from tbl_Test where sAddress like 'Gurgaon') as DerTable










StateIDStateName
1Arunachal Pradesh
2Haryana
3Punjab
4Maharashtra




CityIDCityNameStateID
1Itanagar1
2Gurgaon2
3Faridabad2
4Patiala3
5Ludhiana3
6Pune4
7Nasik4



StateNameCityName
Arunachal PradeshItanagar
Arunachal PradeshOther
HaryanaFaridabad
HaryanaGurgaon
HaryanaOther
PunjabLudhiana
PunjabPatiala
PunjabOther
MaharashtraNasik
MaharashtraPune
MaharashtraOther



select statename,CityName from
(
Select a.stateid,statename,cityname,1 CityOrder
From state a inner join city b
On a.stateid=b.stateid
Union
Select stateid,statename,'Other',2 City
From state
) D
Order by stateid,CityOrder,cityName


























PersonIDPersonNameDepartmentID
1Karan1
2Gaurav1




DepartmentIDDepartmentName
1.Net
2Testing




select * from Person P Left Join Department D
On P.DepartmentID = D.DepartmentID




PersonIDPersonNameDepartmentIDDepartmentIDDepartmentName
1Karan11.Net
2Gaurav11.Net




select * from Person P Left Outer Join Department D
On P.DepartmentID = D.DepartmentID


PersonIDPersonNameDepartmentIDDepartmentIDDepartmentName
1Karan11.Net
2Gaurav11.Net












      BEGIN TRANSACTION
      INSERT INTO SalesHistory
      (Product, SaleDate, SalePrice)          
      VALUES            
      ('PoolTable', GETDATE(), 500)




      Select * from SalesHistory With(NoLock)








      BEGIN TRANSACTION
      UPDATE TOP(1) SalesHistory
      SET SalePrice = SalePrice + 1








EmployeeIDEmployeeNameSalaryAddressState
1Jason20000House no 11State1
2Robert40000House no 33State2




JobIDTitleAverageSalaryProjectManagerDepartmentAddressState
1Developer10000Smith.NetABC RoadState4
2Tester12000SmithTestingABC RoadState4




      SELECT *
      FROM Employee e CROSS JOIN job j
      ORDER BY j.JobID




EmployeeIDEmployeeNameSalaryAddressStateJobIDTitleAverageSalaryProjectManagerDepartmentAddressState
1Jason20000House no 11State11Developer10000Smith.NetABC RoadState4
2Robert40000House no 33State21Developer10000Smith.NetABC RoadState4
1Jason20000House no 11State12Tester12000SmithTestingABC RoadState4
2Robert40000House no 33State22Tester12000SmithTestingABC RoadState4








IDNameAge
2Name234
1Name134
1Name134
2Name234
3Name334
3Name334




      WITH CTE (Id,Name,Age, DuplicateCount)
      AS
      (
        SELECT Id,Name,Age,
        ROW_NUMBER() OVER(PARTITION BY ID,Name,Age ORDER BY ID) AS DuplicateCount
        FROM #DuplicateRcordTable
      )
      delete from CTE where DuplicateCount >1
















Posted By  -  Karan Gupta


Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250