You are on page 1of 4

Database:

1. What is the difference between stored procedure and function?


Stored Procedure UDF

SP may return a value or not Function must return a value

SP can’t be used in a SQL query Function can be used in a SQL query (exception: If the function itself contains DML statements then
it can’t be called in any SQL query.)

SP parameters are IN (default), OUT Function parameters are always IN

2. What is a trigger?
It is an event driven Procedural code that is automatically executed in response to certain events on a particular table or view in a
database.

3. What is Mutating Error?


If a Trigger fired as a result of DML operation on a table and the trigger itself fires a Select on the same table which owns the Trigger,
a mutating error will occur.

4. Have you heard about indexing? What are the type and how it helps?
A database index is a data structure that improves the speed of data retrieval operations on a database table at the cost of slower
writes and increased storage space.

Type of Index (architecture):


1. Clustered Index tree  (Root Node – Intermediate Node – Leaf Node Physical data page):Only one per table
2. Non Clustered Index tree(Root Node – Intermediate Node – Leaf Node  Logical Row Locator): Up to 999/ table

Type of Index (architecture):


 Bitmap Index  a special kind of index that stores the bulk of its data as bit arrays (bitmaps) and answers most queries by
performing bitwise logical operations on these bitmaps, works well for data such as gender, which has Low Cardinality i.e. a
small number of distinct values, for example male and female, but many occurrences of those values
3. B-Tree index

5. How many types of joins are there? Can you explain self join with example?
1. Inner Join : SELECT * FROM TBL1 INNER JOIN TBL2
WHERE Tbl1.Column1 = Tbl2.Column2
1. Equi Join (Inner join using only “=” operator)
1. Natural join(Returns only one column for each pair of equally-named columns): SELECT * FROM TBL1
NATURAL JOIN TBL2
2. Cross Join (No WHERE Clause - -> Cartesian Product)
Ex: SELECT * FROM TBL1 CROSS JOIN TBL2
3. Self (Inner join to the same table)
Ex: SELECT * FROM EMP employee INNER JOIN EMP mgr ON employee.mgrID = mgr.empId

2. Outer Join (Left/ Right/ Full)


1. All records from Left[Right[Both]] table, containing NULL columns from Right[Left[Other]] table for no match

6. Do you have any idea regarding ANSI SQL?


ANSI SQL is the American National Standards Institute standardized Structured Query Language. ANSI SQL is the base for several
different SQL languages such as T-SQL and PL/SQL. ANSI SQL is used to Create, Alter, and View data stored within a database.

7. What is ER Model?
Entity-Relationship Model (ERM) is an abstract and conceptual representation of data. Entity-relationship modeling is a
database modeling method, used to produce a type of conceptual schema or semantic data model of a system, often a
relational database, and its requirements in a top-down fashion
 Entity:
An entity is something of significance about which the enterprise wishes to store information.
Entities can be thought of as nouns. Examples: Organization, employee, a mathematical theorem.
 Attribute:
An attribute holds a particular piece of information about an entity, such as the order date on an order.
 Relationship:
A relationship captures how two or more entities are related to one another.
Relationships can be thought of as verbs, linking two or more nouns.
Examples: an owns relationship between a company and a computer, a supervises relationship between an employee and a
department, a performs relationship between an artist and a song, a proved relationship between a mathematician and a
theorem
Participation Constraint:
Lines are drawn between entity sets and the relationship sets they are involved in. If all entities in an entity set must participate
in the relationship set, a thick or double line is drawn. This is called a participation constraint
8. What is Normalization?
It is the set of rules that has been established to aid in the design of tables that are meant to be connected through
relationships.
Benefits of normalizing your database include:
 Avoiding repetitive entries
 Reducing required storage space
 Preventing the need to restructure existing tables to accommodate new data.
 Increased speed and flexibility of queries, sorts, and summaries.

9. What are different types of normalization?


a. 1st Normal Form: For a table to be in first normal form
i. data must be broken up into the smallest meaningful values/ units possible
ii. tables in first normal form should not contain repetitions of groups of fields

Ex: Table before Normalization:

Table in 1st Normal form:

b. 2nd Normal Form: For a table to be in second normal form


i. Each field in a composite primary key table must be directly related to the entire primary key.

Ex: In the above table of customer, city is not linked to any primary field. City is now shifted to a different master
table

c. 3rd Normal Form: For a table to be in third normal form


i. A non-key field should not depend on other Non-key field. The field "Total" is dependent on "Unit price" and "qty".

So now the "Total" field is removed and is multiplication of Unit price * Qty
10. What is Execution Plan?
o A query execution plan outlines how the SQL Server query optimizer actually ran (or will run) a specific query to find
out why a specific query is running slow.
o Different ways to view a query's execution plan:
 From within Query Analyzer
o "Show Execution Plan" (located on the Query drop-down menu)
o "Display Estimated Execution Plan" (located on the Query drop-down menu)  Want to see an execution plan, but
you don't want to run the query
 SET SHOWPLAN_TEXT ON: Query Analyzer sessions will not be run, but a text-based version of the query plan will
be displayed.
 If the query you are running uses temp tables, then you will have to run the command, SET STATISTICS PROFILE
ON before running the query.

11. What is Statistics in SQL Server?


It is the Information that the server collects about the distribution of data in columns and indexes. This data in turn is used by
the query optimizer to determine the plan of attack for returning results when you run a query. While in the majority of cases
SQL Server takes care of this statistical recordkeeping for you automatically, it's useful to have some understanding of what's
going on, and to know when you might need to intervene.

12. How do you tune database performance?


o Design of the Database - the design of the database is of utmost importance and the DBA should be very sure that the
current design is what he can think of as the best possible considering so many factors including normalization (or selective
denormalization to improve performance), access paths, replication, etc.
o SQL Tuning – Using Query Analyzer/ Explain Plan
o Index tuning
o Use Clustered Index  Large Data / Sequential data access (sorting/ Ordering)
o Use Non Clustered index  Small Data set/ Exact Match Query
o Index fragmentation  By Adjusting Fill Factor (Ex: Fill Factor = 80% =>20% free space in leaf Node i.e. only 20%
room for growth)
 High Fill Factor (Less Room for growth)  Table with READ
 Low Fill Factor (More room for Growth)  Table with Heavy INSERT/ UPDATE
o (Tool) Database Engine Tuning Advisor  Tool to analyze & report indexing potential
 Recommendations
 Overall estimate of the improvement that it can make to your database's performance. Then
comes its list of how to make this improvement
 Reports
 15 reports on Index usage/ Index detail/database access etc.
o OS Tuning - finally the DBA might focus on the underlying OS and see if it's in the right shape to ensure the proper
execution of the DB installed on the system.

13. Is it always preferable to go with database partitioning? If so why?


A partition is a division of a logical database or its constituting elements into distinct independent parts. Database partitioning is
normally done for manageability, performance or availability reasons.

A popular and favorable application of partitioning is in a distributed database management system. Each partition may be spread
over multiple nodes, and users at the node can perform local transactions on the partition. This increases performance for sites that
have regular transactions involving certain views of data, whilst maintaining availability and security.

The partitioning can be done by either building separate smaller databases (each with its own tables, indices, and transaction logs),
or by splitting selected elements, for example just one table.

Horizontal partitioning involves putting different rows into different tables. Perhaps customers with ZIP codes less than 50000 are
stored in CustomersEast, while customers with ZIP codes greater than or equal to 50000 are stored in CustomersWest. The two
partition tables are then CustomersEast and CustomersWest, while a view with a union might be created over both of them to
provide a complete view of all customers.
Vertical partitioning involves creating tables with fewer columns and using additional tables to store the remaining columns.
Normalization also involves this splitting of columns across tables, but vertical partitioning goes beyond that and partitions columns
even when already normalized. Different physical storage might be used to realize vertical partitioning as well; storing infrequently
used or very wide columns on a different device, for example, is a method of vertical partitioning. Done explicitly or implicitly, this
type of partitioning is called "row splitting" (the row is split by its columns). A common form of vertical partitioning is to split (slow to
find) dynamic data from (fast to find) static data in a table where the dynamic data is not used as often as the static. Creating a view
across the two newly created tables restores the original table with a performance penalty, however performance will increase
when accessing the static data e.g. for statistical analysis.
Example:
Create Table Table1
(PK int not null identity constraint pk primary key,
Charcol1 char(10),
Charcol2 char(10),
Textcol varchar(8000))
You can store a total of 8096/ (4+10+10) = 338 rows per page, ignoring per-row overhead. If you stored Varchar(8000) data in a
varchar(8000) column, assuming you have 8 KB of data in this column, you would have one row stored per page. If the majority of
your queries do not return the text column, this storage-engine optimization will be highly beneficial to you. If the majority of your
queries do return the text column, you may wish to use the text in row option. The table option text in row stores text data in a
single database page if the amount of text is less than a configurable threshold.

14. SQL Server Table, Column information


 From Table : information_schema.table, information_schema.column

15. What is Collate?


 Collate is used to create/ alter Collation on Database/ Table/ Column
 Collation  Set of Rules on Database/ Table/Column:
o How data Sorted & compared
 Case sensitivity (A,B, a, b)
 Width Sensitivity (Single byte character/ Double byte Character)

You might also like