Sql Server Interview Questions and Answers (1758) - Page 23

What do you meant by an UnPivot element in sql server?

As the name implies, an UnPivot element is absolutely opposite to Pivot operation.Generally when we invoke an UnPivot operation it would turn the pivoted elements into rows. That means one row of data for every column is to be unpivoted.

It could be well explained by using a simple example.

Let us create a table first :

 CREATE TABLE EMP(

EID INT,
ENAME VARCHAR(50),
JOB VARCHAR(50),
SAL INT
)


Insert values into it :

INSERT INTO EMP1 

VALUES(1,'NARENDRA','MANAGER',10000)
INSERT INTO EMP1
VALUES(2,'SRIDHAR','ANYLYST',12000)
INSERT INTO EMP1
VALUES(3,'NAREN','CLERK',14000)
INSERT INTO EMP1
VALUES(4,'NARENDRA','MANAGER',11000)
INSERT INTO EMP1
VALUES(5,'SRIDHAR','ANYLYST',13000)
INSERT INTO EMPTABLE1
VALUES(6,'NAREN','CLERK',15000)
INSERT INTO EMP1
VALUES(7,'NARENDRA','MANAGER',12000)
INSERT INTO EMP1
VALUES(8,'SRIDHAR','ANYLYST',14000)
INSERT INTO EMP1
VALUES(9,'NAREN','CLERK',16000)
INSERT INTO EMP1
VALUES(10,'NARENDRA','MANAGER',13000)
INSERT INTO EMP1
VALUES(11,'ARAVIND','MANAGER',15000)


Now apply UnPivot property :

SELECT     EID , Property, Value

FROM (SELECT EID,
CONVERT(sql_variant,EName) AS EName,
CONVERT(sql_variant,JOB) AS JOB,
CONVERT(sql_variant,SAL) AS SAL
FROM EMP1) EMP1
UNPIVOT (Value For Property In (EName, JOB, SAL)) as UPT


Now the final output will be in this format :

------------------------------------------------
EID || Property || Value
-----------------------------------------------
1 || EName || NARENDRA
1 || JOB || MANAGER
1 || SAL || 10000
.
.
.
.

In this way it goes and seperates all the records in the table.
Limitations of using AWE Memory in SQL Server

1. SQL Server uses AWE(Address Windowing Memory) to use Physical Memory over 4GB on 32 bit Operating system.
2. AWE option supported in SQL Server Enterprise, Standard & Developer editions only.
3. AWE option allowed only in 32 bit operating system.
4. But this option is available in 64 bit operating system also, But its ignored, In futured version of service pack, The option will be removed.
5. SSAS (Analysis Services) can not take advantage of AWE memory.
Difference between PAD_Index and FillFactor ?

PAD_Index : When we create or modify an index, The percentage of free space allocated in the INTERMEDIATE LEVEL pages during the operation.

FillFactor :When we create or modify an index, The percentage of free space allocated in the LEAF LEVEL of each index page during the operation.
What are the Advantages of using SQL Triggers ?

Different Advantages of Using SQL Triggers :

. It can catch the errors in business logic at the database level.

. It provides an alternative way to run scheduled tasks.

. It is very much useful when we use it to audit the change the data in a database table.

. Using SQL trigger,we don’t have to wait to run the scheduled tasks. we can handle
those tasks before or after changes being made to database tables.


. It provides an alternative way to check integrity.
What are the Disadvantages of using SQL Triggers ?

Disadvantages of Using SQL Triggers :

. Trigger can provide extended validation and it cannot be replaced with all the validations.

. Simple validations can be done at the application level itself .

. Triggers executes invisibly from client-application which connects to the database server.So it is difficult to figure out what happens at the database layer.

. Triggers runs on every update made to the table therefore it adds more load to the database and cause the system to run slow.
What is UNIQUE KEY constraint

? It enforces the uniqueness of the values in a set of columns, so no duplicate values are entered.
? The unique key constraints are used to enforce entity integrity as the primary key constraints.
What are cross joins?

? A cross join that does not have a WHERE clause produces the Cartesian product of the tables involved in the join.
? The size of a Cartesian product result set is the number of rows in the first table multiplied by the number of rows in the second table.
? Example: is when company wants to combine each product with a pricing table to analyze each product at each price.
What is a view?

? It can be thought of as a subset of a table.
? It can be used for retrieving data, as well as updating or deleting rows.
? Rows updated or deleted in the view are updated or deleted in the table the view was created.
? It should also be noted that as data in the original table changes, so does data in the view, as views are the way to look at part of the original table.
? The results of using a view are not permanently stored in the database.
What are the index combination's a table can have?

? Indexes
? A clustered index
? A clustered index and many nonclustered indexes
? A nonclustered index
? Many nonclustered indexes
What is an index?

? An index is a physical structure containing pointers to the data.
? Indices are created in an existing table to locate rows more quickly and efficiently.
? It's possible to create index on one or more columns of a table, and each index is given a name.
? The users cannot see the indexes; they are just used to speed up queries.
? Effective indexes are one of the best ways to improve performance in a database application.
? A table scan happens when there is no index available to help a query. In a table scan SQL Server examines every row in the table to satisfy the query results.
? Table scans are sometimes unavoidable, but on large tables, scans have a terrific impact on performance.
How the indexing is getting worked?

Indexing is basically working by means of sorting of the columns.
When you create a primary key field a Clustered Index will automatically created. And once that created ou cann't add more clustered index to that table.

This is because there is already a clustered index there and there is no other posibility for sorting the other row since the indexes are working by means of sort.
Difference between VARCHAR and NVARCHAR

VARCHAR:

1.Storage: 8 bit
2.Abbreviation: Variable -Length Character String
3.Accepts only English character
4.Doesn't supports other language symbols
5.Runs faster than NVARCHAR as consumes less memory
6.Use this when you develop the application for only local purpose

NVARCHAR:

1.Storage: 16 bit
2.Abbreviation: uNicode
3.Accepts both English character and non-English symbols
4.supports other language symbols
5.Runs slower than VARCHAR as consumes less memory
6.Use this when you use your application globally
What is normalization?

Normalization is a process in RDBMS to minimize the redundancy of organising the data. It normally used to divide the tables in to one or two based on the relation between the data and organize to maintain the simplicity.
What is the maximum number of parameters SQL Stored procedure can have?

NOTE: This is objective type question, Please click question title for correct answer.
Difference between "RID" and "KEY" lock(s) in SQL Server

RID:- Row ID. A lock on a single row in a heap (Heap is a table which doesn't have Clustered Index)

KEY:- A Lock on a single row in an Index

Cheers
www.sqlserverbuddy.blogspot.com
Which recovery model is required for a replication in SQL Server ?

1. Replication will work properly with any of the following recovery models:
Simple
Bulk-logged
Full.

2. Merge replication tracks change by storing information in metadata tables.

3. Transactional replication tracks changes by from the transaction log, So, it will not affected by the recovery model.

www.sqlserverbuddy.blogspot.com
What are the advantages of using Stored procedure

Stored procedure provides high security - All Grand and Revoke access can be given to the base table using stored procedures
Stored procedures avoid network traffic - Sql server prepares a cache of the sp and it avoid network traffic
Stored procedure provide performance - Every execution of sp will be take care by query analyser to prepare a layout before executing
Difference between char and varchar data types in Sql Server?

Char:
1.Fixed length memory storage
2.CHAR takes up 1 byte per character
3.Use Char when the data entries in a column are expected to be the same size
5.Ex:
Declare test Char(100);
test="Test" -
Then "test" occupies 100 bytes first four bytes with values and rest with blank data.


VarChar:
1.Variable length memory storage(Changeable)
2.VARCHAR takes up 1 byte per character, + 2 bytes to hold length information
3.varchar when the data entries in a column are expected to vary considerably in size.
4.Ex:
Declare test VarChar(100);
test="Test" -
Then "test" occupies only 4+2=6 bytes. first four bytes for value and other two bytes for variable length information.

Conclusion:
1.When Using the fixed length data's in column like phone number, use Char
2.When using the variable length data's in column like address use VarChar
Difference between Triggers and Stored procedures

Both are database objects containing blocks lof code that can be used for implementing business logic

The differences are:

1)Triggers fire automatically but they need events for that.
(Example: create,alter,drop,insert,delete,update)
Procedures have to be explicitly called and then executed.
They do not need create,alter,drop,insert,delete,update.
we can also execute procedures automatically using the sp_procoption.

2))we cannot pass parameters inside the triggers,
but we can pass parameters inside stored procedures
-------------------
example: if we want to display a message "error"

using a trigger: we need some DDL/DML Statement
using a procedure: NO DDL/DML is needed
Whiach of these statements is not supported in Dynamic cursors?

NOTE: This is objective type question, Please click question title for correct answer.
Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories