Вы находитесь на странице: 1из 18

Q1. What is the basic difference between a join and a union?

A1. A join selects columns from 2 or more tables. A union selects


Q2. What is normalization and what are the five normal forms?

A2. Normalization is a design procedure for representing data in

tabular format. The five normal forms are progressive rules to
represent the data with minimal redundancy.

Q3. What are foreign keys?

A3. These are attributes of one table that have matching values in a
primary key in another table, allowing for relationships between

Q4. Describe the elements of the SELECT query syntax.

A4. SELECT element FROM table WHERE conditional statement.

Q5. Explain the use of the WHERE clause.

A5. WHERE is used with a relational statement to isolate the object

element or row.

Q6. What techniques are used to retrieve data from more than one
table in a single SQL statement?

A6. Joins, unions and nested selects are used to retrieve data.

Q7. What is a view? Why use it?

A7. A view is a virtual table made up of data from base tables and
other views, but not stored separately.

Q8. Explain an outer join.

A8. An outer join includes rows from tables when there are no
matching values in the tables.
Q9. What is a subselect? Is it different from a nested select?

A9. A subselect is a select which works in conjunction with another

select. A nested select is a kind of subselect where the inner select
passes to the where criteria for the outer select.

Q10. What is the difference between group by and order by?

A10. Group by controls the presentation of the rows, order by

controls the presentation of the columns for the results of the SELECT

Q11. What keyword does an SQL SELECT statement use for a string

A11. The LIKE keyword allows for string searches. The % sign is
used as a wildcard.

Q12. What are some sql aggregates and other built-in functions?

A12. The common aggregate, built-in functions are AVG, SUM,


Q13. How is the SUBSTR keyword used in sql?

A13. SUBSTR is used for string manipulation with column name, first
position and string length used as arguments. Eg. SUBSTR (NAME, 1
3) refers to the first three characters in the column NAME.

Q14. Explain the EXPLAIN statement.

A14. The explain statement provides information about the

optimizer's choice of access path of the sql.

Q15. What is referential integrity?

A15. Referential integrity refers to the consistency that must be

maintained between primary and foreign keys, ie every foreign key
value must have a corresponding primary key value.
Q16. What is a NULL value? What are the pros and cons of using

A16. A NULL value takes up one byte of storage and indicates that a
value is not present as opposed to a space or zero value.

Q17. What is a synonym? How is it used?

A17. A synonym is used to reference a table or view by another

name. The other name can then be written in the application code
pointing to test tables in the development stage and to production
entities when the code is migrated. The synonym is linked to the
AUTHID that created it.

Q18. What is an alias and how does it differ from a synonym?

A18. An alias is an alternative to a synonym, designed for a

distributed environment to avoid having to use the location qualifier of
a table or view. The alias is not dropped when the table is dropped.

Q19. When can an insert of a new primary key value threaten

referential integrity?

A19. Never. New primary key values are not a problem. However,
the values of foreign key inserts must have corresponding primary key
values in their related tables. And updates of primary key values may
require changes in foreign key values to maintain referential

Q20. What is the difference between static and dynamic sql?

A20. Static sql is hard-coded in a program when the programmer

knows the statements to be executed. For dynamic sql the program
must dynamically allocate memory to receive the query results.

Q21. Compare a subselect to a join.

A21. Any subselect can be rewritten as a join, but not vice versa.
Joins are usually more efficient as join rows can be returned
immediately, subselects require a temporary work area for inner
selects results while processing the outer select.

Q22. What is the difference between IN subselects and EXISTS


A22. If there is an index on the attributes tested an IN is more

efficient since DB2 uses the index for the IN. (IN for index is the

Q23. What is a Cartesian product?

A23. A Cartesian product results from a faulty query. It is a row in

the results for every combination in the join tables.

Q24. What is a tuple?

A24. A tuple is an instance of data within a relational database.

Q25. What is the difference between static and dynamic sql?

A25. Static sql is compiled and optimized prior to its execution;

dynamic is compiled and optimized during execution.

Q26. Any SQL implementation covers data types in couple of main

categories. Which of the following are those data types ? (Check all

A26. A,B,C. Not all SQL implementations have a BLOB or a BIT data

Q27. We have a table with a CHARACTER data type field. We apply a

">" row comparison between this field and another CHARACTER field
in another table. What will be the results for records with field value of
NULL ? (Check one that applies the best) A. TRUE B. FALSE C.
UNKNOWN D. Error. E. Those records will be ignored

A27. C. NULL in a row when compared will give an UNKNOWN result.

Q28. Any database needs to go through a normalization process to
make sure that data is represented only once. This will eliminate
problems with creating or destroying data in the database. The
normalization process is done usually in three steps which results in
first, second and third normal forms. Which best describes the process
to obtain the third normal form? (Check one that applies the best) A.
Each table should have related columns. B. Each separate table
should have a primary key. C. We have a table with multi-valued
key. All columns that are dependent on only one or on some of the
keys should be moved in a different table. D. If a table has columns
not dependent on the primary keys, they need to be moved in a
separate table. E. Primary key is always UNIQUE and NOT NULL.

A28. D. All columns in a table should be dependent on the primary

key. This will eliminate transitive dependencies in which A depends on
B, and B depends on C, but we're not sure how C depends on A.

Sql server can have 2 billion tables per DB and 1024 columns per

Varchar can store up to 8000 bytes.

sp_executesql and Execute statements are used to execute a sql

stored Procedure. The execute statement doesn't support parameter
substitution in the executed string.

Truncate method is fast, Non-logged method of deleting all rows in a

table, where delete logs each entry. Truncate immediately frees all
the space occupied by table's indexes and data.

@@Rowcount is used to display the number of rows affected by last

SQL statement.

@@Error displays the error number for the last SQL statement
executed. The value is zero, if there is no error.

@@identity returns the last inserted identity value.

Default sizes for databases

Master database - Stores system files - 11MB
Model database - Template for all databases - 0.75MB
Temp database - Stores temporary objects - 8MB
Msdb database - Used by SQL server agents for scheduling alerts and
jobs and recording operators- 12MB
The min. value for DB size is 512KB
The default size for DB is 1MB

Implementation of relationships

1-1 Relationship: It is the most uncommon relationship. You might

use one-one relationship to divide a table with many fields, to isolate a
part of table for security reasons can be implemented by referencing a
primary key in one table to a primary key in other table.

1-m Relationship: It is the most common type of relationship, a

record in Table A can have multiple matching records in B, and where
as record in B should have only one matching record in A. It can be
implemented by Primary-foreign key relationships.

M-m Relationship: Database doesn’t support m-m relationships, the

same can be implemented by introducing a third (Intermediate) table,
which connects both, and it is equivalent to having 2 1-m relationships
with junction table. The Intermediate table contains a foreign key
which is made up of Primary keys in I & II table. For Example the
orders and products table have many-many relationship as defined, by
creating 2 1-m relationships with Order details table.

There are five normal forms

1st NF: Eliminate the repeating groups in the table.
2nd NF: All Non-key columns should depend on entire primary key, but
not on partial key.
3rd NF: Eliminate interdependencies between non-key attributes.
If the table should be in 3rd normal form, the minimum no. of tables
should be one.

Q. Why one should not prefix user stored procedures with sp_?
A. If the stored procedure starts with sp_, sql server first searches
that sp in master database and comes to current database. The stored
procedures in master database is global, It is available to all DB’s. It
becomes a performance issue.

Query Optimizer: It is a component which analysis the query and

determines the most efficient way to request the data and thus
optimizing the sql statement. The process of choosing an execution
plan out of several plans is called optimizing and this is done by query
SQL Server Query Optimizer is a cost based optimizer. Each possible
execution plan has an associated cost in terms of computing
resources. The query optimizer chooses that plan which has lowest
estimated cost.

Query Execution plan deals with the sequence in which the source
tables are accessed, and the methods used to extract the data from
the table.

Optimizer Hints are used to override the Query Optimizer’s choice of

execution. The five categories of hints are Table hints, Join hints,
Query hints, View hints, Lock hints.

Sql Server 2000 provides two ways to design the triggers.

1) After Trigger (Default)
2) Instead of trigger

You can specify multiple after triggering actions (Insert, Update,

Delete).It can be applied to only tables.
Instead of Triggers executes instead of triggering action. It can be
applied to views and tables. The primary advantage of this trigger is
one can update a view which is made up of multiple tables.

The two virtual tables that will be used by trigger are Inserted and
Deleted tables.

Local variables are user-defined identified by @.The value can

change during batch or stored procedure in which it is used.
and Global variables are system-supplied and predefined identified
by @@.

Local Temporary table is used by single user and will be deleted

when user disconnects
where as global temporary tables can be used by multiple users and
the tables will remain until all users disconnect.

The order for accessing objects


A derived table is a select statement that is used as a table; it is

always enclosed in parenthesis and follows from or join. Here is an
example of a derived table.

SELECT *FROM Accounts a

INNER JOIN (SELECT CustomerID, CustomerName FROM Customers) c
ON a.CustomerID = c.CustomerID.

The SELECT statement that gets data from the Customers table is the
derived table.

A view is a virtual table that is made up of one or more tables. It can

be used for security purpose, hiding complex queries.

An Indexed view is a view that has unique clustered index created

on it. This takes physical storage i.e. it stores data. It will be used in
OLAP, decision support where inserts/updates are low. It can be used
in the following scenarios
1) Joins and aggregation of big tables.
2) Repeated joins of the same tables on the same keys.

Replication is a set of technologies for copying and distributing the

data and database objects from one database to other and
synchronizing between databases for consistency.
It allows multiple sites to keep the copies of same data .This is useful
when multiple sites need to read the same data, or need multiple
servers for reporting applications.
It separates OLTP from OLAP.

Snapshot replication:
It distributes data exactly as it appears at a specific moment in time
and doesn’t monitor for updates. It can be used when data changes
are infrequent. It is often used for browsing data such as price lists,
online catalog, or data for decision support where the current data is
not required and data is used as read only.

Transactional replication:
With this an initial snapshot of data is applied, and whenever data
modifications are made at the publisher, the individual transactions are
captured and propagated to the subscribers.

Merge Replication:
It is the process of distributing the data between publisher and
subscriber, it allows the publisher and subscriber to update the data
while connected or disconnected, and then merging the updates
between the sites when they are connected.

The distributor is the server that is responsible for synchronizing the

data between publishers and subscribers.

The where clause applies to individual rows and having clause

applies only to groups.
Q.)Write a query to fetch all the managers names with the employee
names in the order of manager name.
Table: EMP
EmpId (Primary Key)
EmpName (varchar)

A) create table emp(

EmpId char(2) Primary Key,
EmpName varchar(20),
MgrIdchar(2) references emp(empid))

select empl.EmpName as employee,mgr.EmpName as manager

from emp empl,emp mgr
where empl.mgrid=mgr.empid

Q) Write a query to update all the names that has spaces in front of
their names (above table).
A) Update emp set empname='updated' where empname in (select
empname from emp where empname like ' %')

Q) Write a query to delete all the names that starts with J.

A) Delete from emp where empname like 'j%'

Set NoCount stops the message displaying the number of rows


Temporary tables are like permanent table but they are created in
tempdb, and they are deleted automatically when no longer in use.

Collation specifies bit patterns that represent each character, and

rules by which characters are compared and sorted.

Only Bulk-Insert can run in Query Analyzer.

Alternative ways for getting row count

1) Select count (*) from table--- It will do full table scan, performance
2) Select rows from sysindexes where id=object_id ('table_name') and
Use constraints instead of triggers, rules and defaults, because
constraints are more efficient and boost performance.

OSQL utility allows Transact-SQL statement, stored procedures, and

script files. This utility uses ODBC to communicate with the server and
ISQL uses DBLibrary to communicate with SQL server.

BULK INSERT command is better when there is a text file import

operation. Bulk Insert can’t copy bulk data from Sql server to a data
file. BCP utility is widely used for export operations from SQL server to
data file.

Two types of BCP – Logged and Non-logged BCP, Logged BCP will
log the modifications and can be used for recovery, whenever there is
a failure and it is slow,
Non-logged is fast.

DBCC statements check the physical and logical consistency of the

data base. It can fix the detected problems. Ex DBCC SHOWCONTIG,

The database can be detached from one server and can be attached
to another server/instance or to the same server leaving the data
intact, while drop statement removes the database permanently.

New Features of SQL server

1) XML support
2) Table data types
3) Instead of Triggers
4) Cascading
5) Indexed views

Extended stored Procedure’s are DLL’s that can be loaded and

executed in the address space of SQL Server.
Distributed Query is the query which accesses the data from multiple
heterogeneous data.

The purpose of with no check option in alter table is Check and

foreign key constraints are not checked during table modification.

If a view is made up of one table that view can be modified


If a view consists of few columns in the table as opposed to all, then

view can be modified only when the left out columns are Nullable.

If a view is created using with check option, the data modification

statements are validated against the view’s select statement.

If a view is made up of one or more tables than view can’t be

modified except through indexed views.

Using constraints is preferred to using triggers, rules, and defaults.

The query optimizer also uses constraint definitions to build high-
performance query execution plans.

Which databases fully support Microsoft Transaction Server


The following databases fully support Microsoft Transaction Server.

This includes the ability to participate in distributed transactions.

Databases Comments
Microsoft SQL Microsoft SQL Server™ 6.5 fully supports Microsoft
Server™ Transaction Server
IBM DB2 IBM DB2 is fully supported in the Microsoft
Transaction Server 2.0 release. See "Using an IBM
DB2 Database with Microsoft Transaction Server"
below for more details.
Oracle Oracle 7.3.3 and later releases and Oracle 8 are
fully supported in the Microsoft Transaction Server
2.0 release. See "Using an Oracle Database with
Microsoft Transaction Server" below for more

Which databases partially support Microsoft Transaction


The following databases partially support Microsoft Transaction Server.

The notes indicate the restrictions that apply.

Database Comments
StarQuest StarSQL StarQuest's StarSQL Pro allows Microsoft
Pro Transaction Server components to access IBM DB2
databases on Windows NT, OS/2, AIX, AS/400,
and MVS.

StarSQL supports the following platforms and

versions of DB2:

DB2/MVS (DB2 for OS/390) Version 2.3 and above

on MVS.

DB2/400 Version 2.2 and above on AS/400.

SQL/DS (DB2 for VM and VSE) Version 3.3 and


DB2/CS (DB2/UDB) Version 2.1 and above on

Windows NT, OS/2, and AIX.

StarSQL uses DRDA and connects to IBM

platforms over TCP/IP, Microsoft SNA Server,
Novell NetWare for SAA, IBM APPC Networking
Services for Windows, and a variety of other

StarQuest's StarSQL Pro currently supports

connection pooling.
For more information see the StarQuest Web site
at http://www.starquest.com/ or contact Mark
Rampel at mark.rampel@starquest.com


Distributed transactions are not supported.

Microsoft Access Currently supports Microsoft Transaction Server

connection pooling.


Distributed transactions are not supported

because this database does not externalize
distributed transactions.

Microsoft FoxPro® Currently supports Microsoft Transaction Server

connection pooling.


Distributed transactions are not supported

because this database does not externalize
distributed transactions.

Which databases will support Microsoft Transaction Server in

the future?

The following databases will fully support Microsoft Transaction Server

in the future. This includes support for connection pooling and the
ability to participate in distributed transactions. This will allow
Microsoft Transaction Server applications to update databases on any
of the platform that these database vendors support. All updates can
be performed under distributed transaction protection. This will allow
updates to these databases to be synchronized with updates to other
transaction-protected resources, including Microsoft SQL Server,
Microsoft Message Queue Server (MSMQ), Oracle databases, IBM DB/2
databases, and the like.
Databases Comments
Informix Informix is enhancing their ODBC driver to work
with Microsoft Transaction Server.

Beta testing is now underway. For more

information contact Informix in Menlo Park,

Ingres II from Computer Associates International, Inc. is

Computer enhancing Ingres II to work with Microsoft
Associates Transaction Server.

Currently in Beta testing, no release date has yet

been announced. For more information, contact
David Thole at Computer Associates at

Sybase Sybase is enhancing their ODBC driver to work

with Microsoft Transaction Server.

Sybase is planning to provide Microsoft

Transaction Server support in the second half of
1998. For more information contact Sybase in
Emeryville, California.

Tandem NonStop Tandem NonStop SQL will fully support Microsoft

SQL Transaction Server

Beta or release dates have not been announced.

For more information contact Tandem Computers
in Cupertino, California.

Which databases do not support Microsoft Transaction Server?

The following databases do not support Microsoft Transaction Server.

Database Comments
Lotus Notes Lotus Notes has not committed to supporting
Microsoft Transaction Server.
Oracle RDB Oracle RDB currently has no plans to support
Microsoft Transaction Server.

What must an ODBC-compliant database do to support

Microsoft Transaction Server?

An ODBC-compliant database must do the following to support

Microsoft Transaction Server:

• It must provide an ODBC driver on Windows NT or Windows 95.

• The ODBC driver must be thread-safe and not require thread


• The ODBC driver must support the SQLSetConnectionAttr

(SQL_ATTR_ENLIST_IN_DTC) call that enlists the ODBC connection
in the component's current transaction.

• The database must support either the XA or the OLE

Transactions standard for transaction coordination.

For databases that do not support transactions, only the first two
requirements apply.

What must other databases do to support Microsoft Transaction


A database that provides an interface other than ODBC must do the

following to support Microsoft Transaction Server:

• The database should include a resources dispenser that pools

database connections. This makes it much cheaper for Microsoft
Transaction Server components to connect to and disconnect from
the database.

• The resource dispenser must be thread-safe and not require

thread affinity.

• The resource dispenser should automatically enlist database

connections in the component's current transaction.

• The database must support either the XA or the OLE

Transactions standard for transaction coordination.
For databases that do not support transactions, only the first two
requirements apply.

What is Microsoft doing to help database vendors integrate

their products with Microsoft Transaction Server?

We are doing a great deal to assist database and ODBC driver vendors
to support Microsoft Transaction Server. We have contacted all of the
leading database and ODBC driver vendors and offered the following

• We provide technical briefings on Microsoft Transaction Server.

• We provide prerelease versions of Microsoft Transaction Server
at no charge.

• We offer technical assistance from the Microsoft Transaction

Server development team.

• We provide the Microsoft Transaction Server SDK release.

The SDK includes the Microsoft Transaction Server software and

documentation needed to integrate a database with Microsoft
Transaction Server.

• We provide product test suites.

Can I access databases on UNIX and other platforms and can

these databases participate in transactions?

Yes, applications can access databases distributed across a mixture of

Windows NT, UNIX, Digital VMS, IBM AS/400, IBM MVS, and other
systems. This is possible because an ODBC driver on one system can
communicate with a database server running on another system. This
is a standard feature of most ODBC drivers.

If the ODBC driver and database support distributed transactions, all

of the work done on behalf of the application component will be
performed under the control of a single atomic distributed transaction.
The Microsoft Distributed Transaction Coordinator running on the
Windows NT-based system will coordinate the transaction.
If I access databases on UNIX and other platforms using
transactions, must Microsoft Distributed Transaction
Coordinator be running on those platforms?

A Microsoft Transaction Server transaction can include databases on a

mixture of Windows NT-based systems, UNIX systems, IBM AS/400,
IBM MVS systems, and the like. Those systems need not be running
Microsoft Distributed Transaction Coordinator.

This is possible because the Distributed Transaction Coordinator

running on the Windows NT-based system acts as the transaction
coordinator. The Microsoft Distributed Transaction Coordinator
communicates with the ODBC driver running on the NT system to tell it
the outcome of the transaction. The ODBC driver relays this
information to the database on the remote UNIX, Digital VMS, AS/400,
MVS, or other system. The database then commits or aborts the
transaction as necessary. There is no need to have a Distributed
Transaction Coordinator on the system containing the database server