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.

Leave a Comment