Here we will see examples of SQL Queries. If you have just started working on SQL Server, you must know writing SQL Queries. Here we will see some important SQL Queries.

SQL Queries Example:

Before moving ahead I am creating an Employee Table and inserting some demo data into it.

Create Table:

CREATE TABLE [dbo].[Employee](
 [EmployeeID] [bigint] IDENTITY(1,1) NOT NULL,
 [EmployeeName] [varchar](50) NULL,
 [Salary] [float] NULL,
 [Gender] [varchar](10) NULL,
 [Address] [varchar](50) NULL,
 [State] [varchar](50) NULL,
 [City] [varchar](50) NULL,
 [Status] [varchar](20) NULL,
 CONSTRAINT [PK__Employee__3214EC274AA9A371] PRIMARY KEY CLUSTERED
 (
 [EmployeeID] ASC
 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
 ) ON [PRIMARY]

GO
 SET ANSI_PADDING OFF
 GO

Insert Records:

INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Salary], [Gender], [Address], [State], [City], [Status]) VALUES (1, N'Vinod', 50000, N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
 GO
 INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Salary], [Gender], [Address], [State], [City], [Status]) VALUES (2, N'Manoj', 60000, N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
 GO
 INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Salary], [Gender], [Address], [State], [City], [Status]) VALUES (3, N'Ansh', 30000, N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
 GO
 INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Salary], [Gender], [Address], [State], [City], [Status]) VALUES (4, N'Ritesh', 2000, N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
 GO
 INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Salary], [Gender], [Address], [State], [City], [Status]) VALUES (5, N'Vishwas', 15000, N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
 GO
 INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Salary], [Gender], [Address], [State], [City], [Status]) VALUES (6, N'Ansh', 50000, N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
 GO
 INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Salary], [Gender], [Address], [State], [City], [Status]) VALUES (7, N'Varali', 50000, N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
 GO
 INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Salary], [Gender], [Address], [State], [City], [Status]) VALUES (8, N'Nitin', 40000, N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
 GO
 INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Salary], [Gender], [Address], [State], [City], [Status]) VALUES (9, N'Vikas', 3000, N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
 GO
 INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Salary], [Gender], [Address], [State], [City], [Status]) VALUES (10, N'Ritwik', 50000, N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
 GO
 INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Salary], [Gender], [Address], [State], [City], [Status]) VALUES (11, N'Arohi', 3000, N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
 GO
 INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Salary], [Gender], [Address], [State], [City], [Status]) VALUES (12, N'Ketan', 20000, N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
 GO
 SET IDENTITY_INSERT [dbo].[Employee] OFF
 GO

Example:

Fetch All Employee Record:

SELECT * FROM [dbo].[Employee]

Fetch EmployeeName In Upper Case

SELECT UPPER([EmployeeName ]) FROM [dbo].[Employee]

Fetch EmployeeName In Lower Case

SELECT LOWER([EmployeeName ]) FROM [dbo].[Employee]

Fetch State And City As together.

SELECT [State]+' '+ [City] AS 'Address' FROM [dbo].[Employee]

Fetch EmployeeName After removing white spaces from the right side.

SELECT RTRIM([EmployeeName ]) FROM [dbo].[Employee]

Fetch EmployeeName After removing white spaces from the left side.

SELECT LTRIM([EmployeeName ]) FROM [dbo].[Employee]

Fetch Unique City

SELECT DISTINCT [City ] FROM [dbo].[Employee]

Fetch EmployeeName  With Alias Name Employee Name.

SELECT [EmployeeName  ] AS 'Employee Name' FROM [dbo].[Employee]

Fetch Position Of “V” In Name “Vinod”

SELECT CHARINDEX('V',[EmployeeName],0) from [Employee] where [EmployeeName]='Vinod'

Fetch Number of Characters in EmployeeName FROM Employee.

SELECT LEN([EmployeeName]) FROM [dbo].[Employee]

Fetch EmployeeNameIn Ascending Order

SELECT [EmployeeName]  FROM [dbo].[Employee] ORDER BY [EmployeeName] ASC

Fetch EmployeeName In Descending Order FROM Employee.

SELECT [EmployeeName ] FROM [dbo].[Employee] ORDER BY [EmployeeName ] DESC

Fetch First 3 Characters of EmployeeName FROM Employee.

SELECT SUBSTRING([EmployeeName ],1,3) AS Name FROM [dbo].[Employee]

Fetch Maximum salary of Employee FROM Employee.

SELECT MAX([Salary]) AS Salary FROM [dbo].[Employee]

Fetch Minimum salary of Employee FROM Employee.

SELECT MIN([Salary]) AS Salary FROM [dbo].[Employee]

Fetch Sum Of salary of all Employee FROM Employee.

SELECT SUM([Salary]) AS Salary FROM [dbo].[Employee]

Count all Employee FROM Employee.

SELECT COUNT(*) FROM [dbo].[Employee]

View More:

Conclusion:

I hope this is a useful tutorial for you if you have just started working with SQL Server. Please don’t hesitate to comment for any technical help. Your feedback and suggestions would be appreciated.

14 Comments

  • Hi, i think that i saw you visited my site so i came to “return the favor”.I am attempting to find things to improve my web site!I suppose its ok to use some of your ideas!!|

  • Hello everybody, here every one is sharing these know-how, thus it’s pleasant to read this website, and I used to pay a visit this weblog everyday.|

  • What’s up mates, its enormous article on the topic of educationand completely explained, keep it up all the time.|

  • Howdy just wanted to give you a brief heads up and let you know a few of the pictures aren’t loading correctly. I’m not sure why but I think its a linking issue. I’ve tried it in two different internet browsers and both show the same outcome.|

  • Great site. A lot of useful information here. I’m sending it to a few pals ans also sharing in delicious. And naturally, thanks on your sweat!|

  • My brother suggested I might like this blog. He was totally right. This post truly made my day. You can not imagine just how much time I had spent for this information! Thanks!|

  • What i do not realize is actually how you’re not actually much more well-liked than you might be right now. You’re so intelligent. You realize thus significantly relating to this subject, made me personally consider it from numerous varied angles. Its like women and men aren’t fascinated unless it is one thing to accomplish with Lady gaga! Your own stuffs outstanding. Always maintain it up!

  • Their assignment writers across the globe are well trained in their chosen field of study. Do My Work which reflects in multiple offer. It is important to choose the best one among the many.

  • Hi! I’ve been following your blog for some time now and finally got the courage to go ahead and give you a shout out from Humble Texas! Just wanted to say keep up the good work!|