Vous êtes sur la page 1sur 8

1. How to set an identity column?

Syntax:
IDENTITY [ (seed , increment ) ] seed: Is the value that is used for the very first row loaded into the table. increment: Is the incremental value that is added to the identity value of the previous row that was

loaded. You must specify both the seed and increment or neither. If neither is specified, the default is (1,1). Example: Case1: Seed = 2; Increment = 2

CREATE TABLE [dbo].[Biodata]( [FirstName] [nvarchar](50) NOT NULL, [LastName] [nvarchar](50) NOT NULL, [Id] [int] NOT NULL IDENTITY(2,2), [Age] [int] NULL, [Address] [nvarchar](50) NULL, CONSTRAINT [PK_Biodata] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
Case2: Default

CREATE TABLE [dbo].[Biodata]( [FirstName] [nvarchar](50) NOT NULL, [LastName] [nvarchar](50) NOT NULL, [Id] [int] NOT NULL IDENTITY, [Age] [int] NULL, [Address] [nvarchar](50) NULL, CONSTRAINT [PK_Biodata] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
NOTE: A Identity column can be set as the primary key of the table. In the above example the primary key is set on the table [dbo].[Biodata]on the column [Id] which is also an Identity Column.

2. How to drop a table using if exists statement in Sql?


IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Biodata]') AND type in (N'U')) DROP TABLE [dbo].[Biodata]

3. What are the different types of Joins in Sql?


The Different Types of Joins in SQL Server
1. Inner join or Equi join 2. Outer Join 3. Cross join

4. Explain Inner Join:


This type of join is also known as the Equi join. This join returns all the rows from both tables where there is a match. This type of join can be used in the situation where we need to select only those rows which have values common in the columns which are specified in the ON clause. For example:

--See the data present in tables Employee and Department select * from dbo.Employee select * from dbo.Department
Results: Employee Table:

Empid 1 2 3

EmpNumber A001 A002 A003

EmpFirstName Samir Amit Neha

EmpLastName Singh Kumar Sharma

EmpEmail samir@abc.com amit@abc.com neha@abc.com

Managerid 2 1 1

Departmentid 2 1 2

A004

Vivek

Kumar

vivek@abc.com

NULL

Deparment Table:
Departmenttid 1 2 3 4 DepartmentName Accounts Admin HR Technology

Now if the question is to fetch only the common rows from both the tables OR To fetch all the employees which belong to a particular department then use Inner join as below:

Query for Inner Join:

SELECT Emp.Empid, Emp.EmpFirstName, Emp.EmpLastName, Dept.DepartmentName FROM Employee Emp INNER JOIN Department dept ON Emp.Departmentid=Dept.Departmenttid

Results: Empid 1 2 3 EmpFirstName Samir Amit Neha EmpLastName Singh Kumar Sharma DepartmentName Admin Accounts Admin

5. Explain Self Join:


Sometime we need to join a table to itself. This type of join is called Self join. It is one of the type of inner join where both the columns belong to the same table. In this Join, we need to open two copies of a same table in the memory. Since the table name is the same for both instances, we use the table aliases to make identical copies of the same table to be open in different memory locations.

For example:

--See the data present in tables Employee select * from dbo.Employee


Empid 1 2 3 4 EmpNumber A001 A002 A003 A004 EmpFirstName Samir Amit Neha Vivek EmpLastName Singh Kumar Sharma Kumar EmpEmail samir@abc.com amit@abc.com neha@abc.com vivek@abc.com Managerid 2 1 1 1 Departmentid 2 1 2 NULL

Now if the question is to fetch the Manager Name using the table Employee then you will have to use the Self Join because Manager Name is nothing but the Employee Name which is obtained from the Employee table by using a Self Join as below:

Query for the Self Join:

SELECT Emp1.Empid, Emp1.EmpFirstName+' '+Emp1.EmpLastName as EmployeeName, Emp2.EmpFirstName+' '+Emp2.EmpLastName as ManagerName FROM Employee Emp1 INNER JOIN Employee Emp2 ON Emp1.Managerid=Emp2.Empid

Results: Empid 1 2 3 4 EmployeeName Samir Singh Amit Kumar Neha Sharma Vivek Kumar ManagerName Amit Kumar Samir Singh Samir Singh Samir Singh

6. Explain Outer Join and different types of Outer Join:

This type of join is needed when we need to select all the rows from the table on the left (or right or both) regardless of whether the other table has common values or not and it usually enter null values for the data which is missing. The Outer join can be of three types 1. Left Outer Join 2. Right Outer Join 3. Full Outer Join

7. Explain Left Outer Join and Right Outer Join and Full Outer Join:
Left Outer Join: If we want to get employee id, employee first name, employes last name and their department name for all the employees regardless of whether they belong to any department or not,then we can use the left outer join. In this case we keep the Employee table on the left side of the join clause. It will insert NULL values for the data which is missing in the right table. For example:

--See the data present in tables Employee and Department select * from dbo.Employee select * from dbo.Department
Results: Employee Table: Empid 1 2 3 4 EmpNumber A001 A002 A003 A004 EmpFirstName Samir Amit Neha Vivek EmpLastName Singh Kumar Sharma Kumar EmpEmail samir@abc.com amit@abc.com neha@abc.com vivek@abc.com Managerid 2 1 1 1 Departmentid 2 1 2 NULL

Deparment Table:

Departmenttid 1 2 3 4

DepartmentName Accounts Admin HR Technology

Query for Left Outer Join:

SELECT Emp.Empid, Emp.EmpFirstName, Emp.EmpLastName, Dept.DepartmentName FROM Employee Emp LEFT OUTER JOIN Department dept ON Emp.Departmentid=Dept.Departmenttid

Result: Empid 1 2 3 4
Right Outer Join: If we want to get all the departments name and employee id, employee first name, and employees last name of all the employees belonging to the department regardless of whether a department have employees or not, then we can use the right outer join. In this case we keep the Department table on the right side of the join clause. It will insert NULL values for the data which is missing in the left table (Employee).

EmpFirstName Samir Amit Neha Vivek

EmpLastName Singh Kumar Sharma Kumar

DepartmentName Admin Accounts Admin NULL

Query for Right Outer Join:

SELECT Dept.DepartmentName, Emp.Empid, Emp.EmpFirstName, Emp.EmpLastName FROM Employee Emp RIGHT OUTER JOIN Department dept ON Emp.Departmentid=Dept.Departmentid

Result:
Empid 2 1 3 NULL NULL EmpFirstName Amit Samir Neha NULL NULL EmpLastName Kumar Singh Sharma NULL NULL DepartmentName Accounts Admin Admin HR Technology

Full Outer Join: If we want to get all the departments name and the employee id, employee first name, employes last name of all the employees regardless of whether a department have employees or not, or whether a employee belong to a department or not, then we can use the full outer join. It will insert null values for the data which is missing in both the tables.

Query for Full Outer Join:

SELECT Emp.Empid, Emp.EmpFirstName, Emp.EmpLastName, Dept.DepartmentName FROM Employee Emp FULL OUTER JOIN Department dept ON Emp.Departmentid=Dept.Departmenttid

Empid 1 2 3 4 NULL NULL

EmpFirstName Samir Amit Neha Vivek NULL NULL

EmpLastName Singh Kumar Sharma Kumar NULL NULL

DepartmentName Admin Accounts Admin NULL HR Technology

8. Explain Cross Join:


This join combines all the rows from the left table with every row from the right table. This type of join is needed when we need to select all the possible combinations of rows and columns from both the tables. This type of join is generally not preferred as it takes lot of time and gives a huge result that is not often useful.

Query for the Cross Join:


Collapse

SELECT Emp.Empid, Emp.EmpFirstName, Emp.EmpLastName, Dept.DepartmentName FROM Employee Emp CROSS JOIN Department dept

Result: Empid 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 EmpFirstName Samir Amit Neha Vivek Samir Amit Neha Vivek Samir Amit Neha Vivek Samir Amit Neha Vivek EmpLastName Singh Kumar Sharma Kumar Singh Kumar Sharma Kumar Singh Kumar Sharma Kumar Singh Kumar Sharma Kumar DepartmentName Accounts Accounts Accounts Accounts Admin Admin Admin Admin HR HR HR HR Technology Technology Technology Technology

9. Explain Normalization:
Normalization is the process of efficiently organizing data in a database. There are two goals of the normalization process: i. eliminating redundant data (for example, storing the same data in more than one table) ii. ensuring data dependencies make sense (only storing related data in a table).

Both of these are worthy goals as they reduce the amount of space a database consumes and ensure that data is logically stored.

Vous aimerez peut-être aussi