Wednesday, March 21, 2012

SQL Server interview questions

1. Difference Between Primary key and Unique key


A. Even though both the primary key and unique key are one or more columns that can uniquely identify a row in a table, they have some important differences. Most importantly, a table can have only a single primary key while it can have more than one unique key. Primary key can be considered as a special case of the unique key. Another difference is that primary keys have an implicit NOT NULL constraint while the unique key does not have that constraint. Therefore, unique key columns may or may not contain NULL values but primary key columns cannot contain NULL values.


2. Types of Joins in Microsoft SQL Server

A. Inner Join, Outer Join(Left, Right, Full) and Cross Join.

Inner join A join that displays only the rows that have a match in both joined tables. (This is the default type of join in the Query and View Designer.)

Outer join A join that includes rows even if they do not have related rows in the joined table. You can create three variations of an outer join to specify the unmatched rows to be included:

  • Left outer join All rows from the first-named table (the "left" table, which appears leftmost in the JOIN clause) are included. Unmatched rows in the right table do not appear.

    Right outer join All rows in the second-named table (the "right" table, which appears rightmost in the JOIN clause) are included. Unmatched rows in the left table are not included.

    Full outer join All rows in all joined tables are included, whether they are matched or not.

Cross join A join whose result set includes one row for each possible pairing of rows from the two tables.

No comments:

Post a Comment