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
| StateID | StateName |
|---|
| 1 | Arunachal Pradesh |
| 2 | Haryana |
| 3 | Punjab |
| 4 | Maharashtra |
| CityID | CityName | StateID |
|---|
| 1 | Itanagar | 1 |
| 2 | Gurgaon | 2 |
| 3 | Faridabad | 2 |
| 4 | Patiala | 3 |
| 5 | Ludhiana | 3 |
| 6 | Pune | 4 |
| 7 | Nasik | 4 |
| StateName | CityName |
|---|
| Arunachal Pradesh | Itanagar |
| Arunachal Pradesh | Other |
| Haryana | Faridabad |
| Haryana | Gurgaon |
| Haryana | Other |
| Punjab | Ludhiana |
| Punjab | Patiala |
| Punjab | Other |
| Maharashtra | Nasik |
| Maharashtra | Pune |
| Maharashtra | Other |
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
| PersonID | PersonName | DepartmentID |
|---|
| 1 | Karan | 1 |
| 2 | Gaurav | 1 |
| DepartmentID | DepartmentName |
|---|
| 1 | .Net |
| 2 | Testing |
select * from Person P Left Join Department D
On P.DepartmentID = D.DepartmentID
| PersonID | PersonName | DepartmentID | DepartmentID | DepartmentName |
|---|
| 1 | Karan | 1 | 1 | .Net |
| 2 | Gaurav | 1 | 1 | .Net |
select * from Person P Left Outer Join Department D
On P.DepartmentID = D.DepartmentID
| PersonID | PersonName | DepartmentID | DepartmentID | DepartmentName |
|---|
| 1 | Karan | 1 | 1 | .Net |
| 2 | Gaurav | 1 | 1 | .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
| EmployeeID | EmployeeName | Salary | Address | State |
|---|
| 1 | Jason | 20000 | House no 11 | State1 |
| 2 | Robert | 40000 | House no 33 | State2 |
| JobID | Title | AverageSalary | ProjectManager | Department | Address | State |
|---|
| 1 | Developer | 10000 | Smith | .Net | ABC Road | State4 |
| 2 | Tester | 12000 | Smith | Testing | ABC Road | State4 |
SELECT *
FROM Employee e CROSS JOIN job j
ORDER BY j.JobID
| EmployeeID | EmployeeName | Salary | Address | State | JobID | Title | AverageSalary | ProjectManager | Department | Address | State |
|---|
| 1 | Jason | 20000 | House no 11 | State1 | 1 | Developer | 10000 | Smith | .Net | ABC Road | State4 |
| 2 | Robert | 40000 | House no 33 | State2 | 1 | Developer | 10000 | Smith | .Net | ABC Road | State4 |
| 1 | Jason | 20000 | House no 11 | State1 | 2 | Tester | 12000 | Smith | Testing | ABC Road | State4 |
| 2 | Robert | 40000 | House no 33 | State2 | 2 | Tester | 12000 | Smith | Testing | ABC Road | State4 |
| ID | Name | Age |
|---|
| 2 | Name2 | 34 |
| 1 | Name1 | 34 |
| 1 | Name1 | 34 |
| 2 | Name2 | 34 |
| 3 | Name3 | 34 |
| 3 | Name3 | 34 |
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