Skip to main content

SQL Server Tutorial - Tables

In this article we will learn about some of the mostly used SQL Server queries every developer should know. 
These queries can be asked you as an Interview Question or they are handy for you in your day to day tasks.

1. Create Table:

In this SQL Server query we will learn How to create table in SQL Server.

1
2
3
4
5
6
CREATE TABLE TableName
(
   Id INT,
   Name Nvarchar(500),
   Age INT
)  

2. Create table with primary key:

In this query we will Create SQL Server table with primary key.
1
2
3
4
5
6
CREATE TABLE TableName
(
   Id INT PRIMARY KEY,
   Name Nvarchar(500),
   Age INT
)

3. Create table with primary key and Identity Column:

1
2
3
4
5
6
CREATE TABLE TableName
(
   Id INT  IDENTITY(1,1) PRIMARY KEY,
   Name Nvarchar(500),
   Age INT
)

4. Insert values into SQL Server table:

In this SQL Server query we will learn about How to insert values into Sql Server table.
1
INSERT INTO TableName (Name,Age) VALUES ('Max',30);

5. Insert multiple rows in a single query:

In this Sql query we will learn How to Insert Multiple Rows in a Single SQL Query. This query is very important and generally asked in .Net Interview questions.
1
2
INSERT INTO TableName (Name,Age)
VALUES ('Max',30),('John',28),('Jack',31);

6. Update query:

Update single record:

In this Sql query we will update single record from a table.
1
UPDATE TableName SET NAME='Max Payne' WHERE Id=1

Update all records:

In this Sql query we will update all records within a table.
1
UPDATE TableName SET AGE=31

7. Delete query:

Delete single record:

In this Sql query we will delete single record from a table.
1
DELETE FROM TableName WHERE Id=1

Delete multiple records:

In this Sql query we will delete all records from a table.
1
DELETE FROM TableName

8. Select:

Select all columns from a tables:

In this Sql query we will select all columns from a table.
1
SELECT * FROM TableName

Select specific columns:

In this Sql query we will select specific columns from a table.
1
SELECT columnName1,ColumnName2 from TableName

Comments

Popular posts from this blog

Scenario : Cloud Computing

ASP.NET Core - MVC Setup

Here we will set up the MVC framework in our FirstAppDemo application. We will proceed by building a web application on top of the ASP.NET Core, and more specifically, the ASP.NET Core MVC framework. We can technically build an entire application using only middleware, but ASP.NET Core MVC gives us the features that we can use to easily create HTML pages and HTTP-based APIs. To setup MVC framework in our empty project, follow these steps − Install the  Microsoft.AspNet.Mvc  package, which gives us access to the assemblies and classes provided by the framework. Once the package is installed, we need to register all of the services that ASP.NET MVC requires at runtime. We will do this inside the  ConfigureServices  method. Finally, we need to add middleware for ASP.NET MVC to receive requests. Essentially this piece of middleware takes an HTTP request and tries to direct that request to a C# class that we will write. Step 1  − Let us go to the NuGet package manager by ri

SQL Server Tutorial - Date functions

1. Get month names with month numbers: In this query we will learn about  How to get all month names with month number in SQL Server . I have used this query to bind my dropdownlist with month name and month number. 1 2 3 4 5 6 7 8 9 10 11 12 ;WITH months(MonthNumber) AS (      SELECT 0      UNION ALL      SELECT MonthNumber+1      FROM months      WHERE MonthNumber < 12 ) SELECT DATENAME(MONTH,DATEADD(MONTH,-MonthNumber,GETDATE())) AS [MonthName],Datepart(MONTH,DATEADD(MONTH,-MonthNumber,GETDATE())) AS MonthNumber FROM months ORDER BY Datepart(MONTH,DATEADD(MONTH,-MonthNumber,GETDATE())) ; 2. Get name of current month: In this query we will learn about  How to get name of current month in SQL Server . 1 select DATENAME(MONTH, GETDATE()) AS CurrentMonth 3. Get name of day: In this query we will learn about  How to get name of day in SQL Server . 1 select DATENAME(WEEKDAY, GETDATE()) AS TodayI