Top Most SQL Server Interview Questions Part – 3
1.What is RDBMS?
Relational Data Base Management Systems (RDBMS) are database management systems that maintain data records and indices in tables.Relationships may be created and maintained across and among the data and tables.In a relational database,relationships between data items are expressed by means of tables.
Interdependencies among these tables are expressed by data values rather than by pointers.This allows a high degree of data independence.An RDBMS has the capability to recombine the data items from different files,providing powerful tools for data usage.
2.What is normalization?
Database normalization is a data design and organization process applied to data structures based on rules that help build relational databases.In relational database design,the process of organizing data to minimize redundancy.
Normalization usually involves dividing a database into two or more tables and defining relationships between the tables.The objective is to isolate data so that additions,deletions,and modifications of a field can be made in just one table and then propagated through the rest of the database via the defined relationships.
3.What is Stored Procedure?
A stored procedure is a named group of SQL statements that have been previously created and stored in the server database.Stored procedures accept input parameters so that a single procedure can be used over the network by several clients using different input data.
And when the procedure is modified,all clients automatically get the new version.Stored procedures reduce network traffic and improve performance.Stored procedures can be used to help ensure the integrity of the database.
e.g.sp_helpdb,sp_renamedb,sp_depends etc.
4.What is Trigger?
A trigger is a SQL procedure that initiates an action when an event (INSERT,DELETE or UPDATE) occurs.Triggers are stored in and managed by the DBMS.Triggers are used to maintain the referential integrity of data by changing the data in a systematic fashion.A trigger cannot be called or executed; the DBMS automatically fires the trigger as a result of a data modification to the associated table.
Triggers can be viewed as similar to stored procedures in that both consist of procedural logic that is stored at the database level.Stored procedures,however,are not event-drive and are not attached to a specific table as triggers are.
Stored procedures are explicitly executed by invoking a CALL to the procedure while triggers are implicitly executed.In addition,triggers can also execute stored procedures.
Nested Trigger:
A trigger can also contain INSERT,UPDATE and DELETE logic within itself,so when the trigger is fired because of data modification it can also cause another data modification,thereby firing another trigger.A trigger that contains data modification logic within itself is called a nested trigger.
5.What is View?
A simple view 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 with.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.The data accessed through a view is actually constructed using standard T-SQL select command and can come from one to many different base tables or even other views.
6.What is de-normalization and when would you go for it?
As the name indicates,de-normalization is the reverse process of normalization.It is the controlled introduction of redundancy in to the database design.It helps improve the query performance as the number of joins could be reduced.
7.What’s the difference between a primary key and a unique key?
Both primary key and unique enforce uniqueness of the column on which they are defined.But by default primary key creates a clustered index on the column,where are unique creates a non-clustered index by default.Another major difference is that,primary key does not allow NULLs,but unique key allows one NULL only.
8.What is bit data type and what’s the information that can be stored inside a bit column?
Bit data type is used to store Boolean information like 1 or 0 (true or false).Until SQL Server
6.5 bit data type could hold either a 1 or 0 and there was no support for NULL.But from SQL Server
7.0 onwards,bit data type can represent a third state,which is NULL.
9.Define candidate key,alternate key,composite key.
A candidate key is one that can identify each row of a table uniquely.Generally a candidate key becomes the primary key of the table.If the table has more than one candidate key,one of them will become the primary key,and the rest are called alternate keys.
A key formed by combining at least two or more columns is called composite key.
10.What is a “constraint”?
8A constraint allows you to apply simple referential integrity checks to a table.There are four primary types of constraints that are currently supported by SQL Server: PRIMARY/UNIQUE – enforces uniqueness of a particular table column.DEFAULT – specifies a default value for a column in case an insert operation does not provide one.
FOREIGN KEY –
validates that every value in a column exists in a column of another table.CHECK – checks that every value stored in a column is in some specified list.Each type of constraint performs a specific type of action.Default is not a constraint.NOT NULL is one more constraint which does not allow values in the specific column to be null.And also it the only constraint which is not a table level constraint.
11.What is 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 is possible to create an 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.
Clustered indexes define the physical sorting of a database table’s rows in the storage media.For this reason,each database table may have only one clustered index.Non-clustered indexes are created outside of the database table and contain a sorted list of references to the table itself.
12.What is the difference between clustered and a non-clustered index?
A clustered index is a special type of index that reorders the way records in the table are physically stored.Therefore table can have only one clustered index.The leaf nodes of a clustered index contain the data pages.
A nonclustered index is a special type of index in which the logical order of the index does not match the physical stored order of the rows on disk.The leaf node of a nonclustered index does not consist of the data pages.Instead,the leaf nodes contain index rows.
13.What are the different index configurations a table can have?
A table can have one of the following index configurations:
1.No indexes
2.A clustered index
3.A clustered index and many nonclustered indexes
4.A nonclustered index
5.Many nonclustered indexes
14.What is cursors?
Cursor is a database object used by applications to manipulate data in a set on a row-by-row basis,instead of the typical SQL commands that operate on all the rows in the set at one time.
In order to work with a cursor we need to perform some steps in the following order:
1.Declare cursor
2.Open cursor
3.Fetch row from the cursor
4.Process fetched row
5.Close cursor
6.Deallocate cursor
15.What is the use of DBCC commands?
DBCC stands for database consistency checker.We use these commands to check the consistency of the
databases,i.e.,maintenance,validation task and status checks.E.g.DBCC CHECKDB – Ensures that tables in the db and the indexes are correctly linked.DBCC CHECKALLOC – To check that all pages in a db are correctly allocated.DBCC CHECKFILEGROUP – Checks all tables file group for any damage.
16.What is a “primary key”?
A PRIMARY INDEX or PRIMARY KEY is something which comes mainly from database theory.From its behaviour is almost the same as an UNIQUE INDEX,i.e.there may only be one of each value in this column.If you call such an INDEX PRIMARY instead of UNIQUE,you say something about your table design,which I am not able to explain in few words.
Primary Key is a type of a constraint enforcing uniqueness and data integrity for each row of a table.All columns participating in a primary key constraint must possess the NOT NULL property.
17.What is a “functional dependency”?How does it relate to database table design?
Functional dependency relates to how one object depends upon the other in the database.for example,procedure/function sp2 may be called by procedure sp
1.Then we say that sp1 has functional dependency on sp
18.What is the use of “join”?
‘join’ used to connect two or more tables logically with or without common field.
19.Explain Active/Active and Active/Passive cluster configurations.
A candidate key is one that can identify each row of a table uniquely.Generally a candidate key becomes the primary key of the table.If the table has more than one candidate key,one of them will become the primary key,and the rest are called alternate keys.A key formed by combining at least two or more columns is called composite key.
20.What structure can you implement for the database to speed up table reads?
Follow the rules of DB tuning we have to: 1] properly use indexes ( different types of indexes) 2] properly locate different DB objects across different tablespaces,files and so on.3] create a special space (tablespace) to locate some of the data with special datatype ( for example CLOB,LOB and …)