WHERE Tbl1.Column1 Tbl2.Column2: Stored Procedure UDF
WHERE Tbl1.Column1 Tbl2.Column2: Stored Procedure UDF
WHERE Tbl1.Column1 Tbl2.Column2: Stored Procedure UDF
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.)
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.
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.
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
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.
Ex: In the above table of customer, city is not linked to any primary field. City is now shifted to a different master
table
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.
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.