Top 100 sql server queries interview questions

SQL Server Advanced Questions - 'Tricky Query'


71) How to select random record form a tblstudent.
1
Select top 1* from tblStudent order by NEWID()
72) Write a query to create a clone of existing table without using Create Command.
1
Select Top 6 * INTO tblStudent_clone From tblstudent

note- 'tblStudent_clone' will be create in sample database

73) Write a query to calculate number of T in string 'TECHSTUDY'.
1
Select LEN('TECHSTUDY') - LEN (REPLACE('TECHSTUDY', 'T', ''))
     

74) What will be the result of the query below?
1
select case when null = null then 'True' else 'False' end as Result;

Answer : False ! The reason is the proper way to compare a value in SQL server is using 'IS' operator and not using '='.


75) What would be the output of the follwing query?
1
2
3
Select Case when 10 = 10 then 'Tech Study'
when 20 = 20  then 'techstudy.org'
else 'techstudy the complete debuggin solution' end as name

Answer: Tech Study

76) Write down the query to print first letter of a Name in Upper Case and all other letter in Lower Case.
1
2
Select UPPER(SUBSTRING(Firstname, 1,1))+ 
LOWER(SUBSTRING(Firstname, 2, Len(FirstName)-1))As Firstname from tblStudent
77) Write down the query to display all student name in one cell seprated by ',' example:-"Sara, David, Dora, Jack, Vikram, Ross".
1
2
3
Declare @name varchar(MAX) = ''
Select @name = @name + FirstName + ', ' from [tblStudent]
Select SUBSTRING(@name,1, LEN(@name)) As StudentList

SQL Server Advanced Questions - 'SQL DDL Questions'


78) Write down the query to create tblstudent table with primary key (studentId)
1
2
3
4
5
6
7
8
Create table tblstudent(
StudnetId int identity(1,1) Not null primary key, 
Firstname varchar(50), 
LastName varchar(50), 
Admission_fee int, 
Admission_date date, 
Branch date
)
79) How to set Primary key(PK) using Alter command
1
Alter table tblstudent3 add primary key(StudnetId)
80) How to drop Primary key(PK) using Alter command
1
2
ALTER TABLE tblstudent
DROP CONSTRAINT PK__tblstudentid
81) Write a query to add new column in tblstudent
1
2
ALTER TABLE tblstudent
ADD Address varchar(50)
82) Write a query to drop Address column in tblstudent
1
2
ALTER TABLE tblstudent
DROP Column Address 


« 1 2 3 4 5 6 »  

Responses




Popular Posts

Facebook

Founded in 2016, Tech Study passionately delivers stylish and dynamic innovative information to programmer and technology lovers. http://www.techstudy.org/.