Skip to content

Instantly share code, notes, and snippets.

@A-Programmer
Created May 4, 2018 15:51
Show Gist options
  • Save A-Programmer/c925de16348626d81c833b2d43bb51d0 to your computer and use it in GitHub Desktop.
Save A-Programmer/c925de16348626d81c833b2d43bb51d0 to your computer and use it in GitHub Desktop.
75 SQL queries that makes a programmer's life easier. Part 1

75 SQL queries that makes a programmer's life easier. Part 1

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.

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.

CREATE TABLE TableName ( Id INT PRIMARY KEY, Name Nvarchar(500), Age INT )

3. Create table with primary key and Identity Column:

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.

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.

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.

UPDATE TableName SET NAME=``'Max Payne' WHERE Id=1

Update all records:

In this Sql query we will update all records within a table.

UPDATE TableName SET AGE=31

7. Delete query:

Delete single record:

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

Delete multiple records:

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

8. Select:

Select all columns from a tables:

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

Select specific columns:

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

9. Create View:

A view is a virtual table created based on the result generated by SQL statement. Fields in a view are directly related to one of more tables from database. CREATE VIEW view_name AS SELECT Id,Name,Age FROM TableName

Usage:

select * From view_name

10. Create Stored procedure:

In this query we will learn about How to create stored procedure in Sql Server. CREATE PROCEDURE getInfoFromTable AS SELECT * FROM TableName

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment