Top 100 sql server queries interview questions

Introduction

In this article, you will find basic to most puzzled interview queries questions with an example. This article is very useful for those who are preparing for an interview in IT company. Whether you are experienced or fresher, this article will cover all Microsoft SQL Server query questions from basic to advanced level.


So, before starting let's create and populate tblstudent and tblScholarship table using following script.


1
2
3
4
5
6
7
8
9
Create table tblStudent
(
	StudentId int,
	FirstName varchar(50),
	LastName varchar(50),
	Admission_fee int,
	Admission_date datetime,
	Branch varchar(50),
)

1
2
3
4
5
6
Create table tblScholarship
(
	Student_ref_id int,
	Scholarship_Date date,
	Scholarship_Amount int
)

SQL Server Basic Questions - 'Select', 'Where', 'Order By', 'Wild Card Search'.


1) Get all students details from the tblstudent table.
1
Select * from tblstudent
2) Get first name from the tblstudent using alias name "StudentName”.
1
Select FirstName as StudentName from tblstudent 
     

3) Get first name, last name from the tblstudent table.
1
Select FirstName, LastName from tblstudent
4) Select student details whose name is "David" from tblstudent.
1
Select * from tblstudent where FirstName = 'David'
5) Get first name from tblstudent in upper case.
1
 Select upper(FirstName) from tblstudent 
6) Get last name from tblstudent in lower case.
1
 Select lower(FirstName) from tblstudent 
7) Get unique branch from tblstudent table.
1
Select distinct(Branch) from tblstudent
8) Write a query to combine FirstName and LastName and display it as "Full Name".
1
Select FirstName +' '+ LastName As "Full Name" from tblstudent 
9) Get all student details from tblstudent whose "FirstName" contains 'a'.
1
Select * from tblstudent where FirstName like '%A%'
10) Get all student details from tblstudent whose "FirstName" start with latter 'd'.
1
Select * from tblstudent where FirstName like 'D%'
11) Get all student details from tblstudent whose "FirstName" end with 'a'.
1
Select * from tblstudent where FirstName like '%a'
     


12) List all students whose first name start with 'Ma' or 'Da'.
1
SELECT FirstName, LastName, Branch FROM tblStudent WHERE firstname LIKE '%Ma%' OR firstname LIKE '%Da%'
13) Get all students details from the tblStudent table order by FirstName Ascending.
1
Select * from tblStudent order by FirstName asc
14) Get all students details from the tblStudent table order by FirstName Descending
1
Select * from tblStudent order by FirstName desc
15) Get all students details from the tblStudent table order by LastName Ascending and Admission fees descending.
1
Select * from tblStudent order by LastName asc, Admission_Fee desc
16) Get position of 'v' in name 'David' from tblstudent.
1
Select CHARINDEX('v', FirstName,0) from tblstudent where FirstName = 'David'
17) Select firstname from tblstudent with 'Hello' prefix.
1
select 'Hello ' + firstname from tblStudent
18) Get FirstName from tblstudent after removing white spaces from right side.
1
Select RTRIM(FirstName) from tblstudent
19) Get FirstName from tblstudent after removing white spaces from left side.
1
Select LTRIM(FirstName) from tblstudent
20) Get length of FirstName from tblstudent.
1
Select len(FirstName) from tblstudent
21) Get FristName from tblStudent table after replacing 'a' with '$'.
1
Select REPLACE(FirstName,'a','$') from tblStudent
22) Get all students details from tblStudent whose first name starts with 'm' and name contains 4 letters.
1
Select * from tblStudent where FirstName like 'm___'
23) Get all students details from tblStudent whose first name ends with 'a' and name contains 4 letters.
1
Select * from tblStudent where FirstName like '___a' 
24) Get fristname from tblstudent not start with any single character between a-p.
1
select * from tblStudent where FirstName like '[^a-p]%'
« 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/.