Light Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Latest commit

History

History
2994 lines (1577 loc) * 136 KB

README.md

File metadata and controls

2994 lines (1577 loc) * 136 KB

SQL Interview Questions & Answers

Click if you like the project. Pull Request are highly appreciated. Follow me @kansiris87 for technical updates.

Table of Contents

No. Questions
1 What is a database?
1 What is DBMS?
1 What is SQL?
1 What is PL/SQL?
1 What is the difference between SQL and PL/SQL?
1 What is RDBMS?
1 What is a database table?
1 What is a query?
1 What is subquery?
1 What are the types of subquery?
1 How to create a table in SQL?
1 What are tables and Fields?
1 How to delete a table in SQL Server?
1 How to update a database table using SQL?
1 What is a database relationship?
1 What is a primary key of a database?
1 What is a unique key?
1 What is a foreign key of a database?
1 What is database normalization?
1 What are database normalization forms?
1 What is Denormalization.
1 What is a stored procedure?
1 Why we use Stored Procedure?
1 How to create a Stored Procedure?
1 What is a function in SQL Server?
1 What are the different types of functions in SQL Server?
1 What is a trigger in SQL Server?
1 Why do we need triggers?
1 What are the different types of triggers?
1 What is a view in the database?
1 Why do I need views in a database?
1 What is the difference between Primary key and unique key?
1 How can you increase SQL performance?
1 What is the use of OLAP?
1 What is a measure in OLAP?
1 What are dimensions in OLAP?
1 What are levels in dimensions?
1 What are fact tables and dimension tables in OLAP?
1 What is DTS?
1 What is fill factor? or When does page split occurs?
1 What is RAID and how does it work?
1 SQL Server Difference between @@IDENTITY, SCOPE_IDENTITY () and IDENT_CURRENT
1 Difference between char varchar and nvarchar in SQL Server
1 What is the difference between DELETE TABLE and TRUNCATE TABLE commands?
1 If locking is not implemented, what issues can occur?
1 What are different transaction levels in SQL SERVER?
1 What are the different locks in SQL SERVER?
1 Can we suggest locking hints to SQL SERVER?
1 What is LOCK escalation?
1 What are the different ways of moving data between databases in SQL Server?
1 What is the difference between a HAVING CLAUSE and a WHERE CLAUSE?
1 What is the difference between UNION and UNION ALL SQL syntax?
1 What are the different types of triggers in SQL SERVER?
1 if we have multiple AFTER Triggers on table how can we define the sequence od the triggers?
1 What is SQL injection?
1 What is the difference between Stored Procedure (SP) and User Defined Function (UDF)?
1 How can you raise custom errors from stored procedure?
1 What is DBCC?
1 What is the purpose of Replication?
1 What are the different types of replication supported by SQL SERVER?
1 What is BCP utility in SQL SERVER?
1 What is a Cursor?
1 What are local and global variables and their differences?
1 What is an index?
1 Why do I need an index in a database?
1 What is a query in a database?
1 What are query types in a database?
1 What is a join in SQL Server?
1 What are different types of joins in SQL Server?
1 What is Self-Join?
1 What is Cross-Join?
1 What is user defined functions?
1 What are all types of user defined functions?
1 What is collation?
1 What are all different types of collation sensitivity?
1 Advantages and Disadvantages of Stored Procedure?
1 What is Online Transaction Processing (OLTP)?
1 What is CLAUSE?
1 What is recursive stored procedure?
1 What is Union, minus and Interact commands?
1 What is an ALIAS command?
1 What is the difference between TRUNCATE and DROP statements?
1 What are aggregate and scalar functions?
1 What is an inner join in SQL?
1 What is an outer join in SQL?
1 What is full join in SQL?
1 What is left join in SQL Server?
1 What is a right join in SQL Server?
1 What is database engine in SQL Server?
1 What are the Analysis Services in SQL Server?
1 What are the integration services in SQL Server?
1 What are the data quality services in SQL Server?
1 What are the reporting services in SQL Server?
1 What are the master data services in SQL Server?
1 What is replication in SQL Server?
1 How to I select data from an SQL Server table?
1 What is a check in SQL?
1 What is a default in SQL?
1 How to create a database using SQL?
1 What is a constraint in SQL?
1 What is data Integrity?
1 What is Auto Increment?
1 What is the difference between Cluster and Non-Cluster Index?
1 What is Datawarehouse?
1 How do I define constraints in SQL?
1 What is the meaning of Not Null in SQL?
1 How to alter a table schema in SQL Server?
1 How to create index in SQL Server?
1 How to get unique records in SQL?
1 How to create a date column in SQL Server?
1 What is ACID fundamental? What are transactions in SQL SERVER?
1 What is a candidate key?
1 How do GROUP and ORDER BY Differ?
1 Compare SQL & PL/SQL?
1 What is BCP? When is it used?
1 When is the UPDATE_STATISTICS command used?
1 Explain the steps needed to Create the scheduled job?
1 When are we going to use truncate and delete?
1 Explain correlated query work?
1 When is the Explicit Cursor Used?
1 Find What is Wrong in this Query?
1 Write the Syntax for STUFF function in an SQL server?
1 What are the three ways that Dynamic SQL can be executed?
1 In what version of SQL Server were synonyms released? How do synonyms work and explain its use cases? Synonyms were released with SQL Server 2005?
1 If you are a SQL Developer, how can you delete duplicate records in a table with no primary key?
1 Is it possible to import data directly from T-SQL commands without using SQL Server Integration Services? If so, what are the commands?
1 What is the native system stored procedure to execute a command against all databases?
1 How can a SQL Developer prevent T-SQL code from running on a production SQL Server?
1 How do you maintain database integrity where deletions from one table will automatically cause deletions in another table?
1 What port does SQL server run on?
1 What is the SQL CASE statement used for? Explain with an example?
1 What are the risks of storing a hibernate-managed object in cache? How do you overcome the problems?
1 When is the use of UPDATE_STATISTICS command?
1 What is SQL Profiler?
1 What command using Query Analyzer will give you the version of SQL server and operating system?
1 What does it mean to have QUOTED_IDENTIFIER ON? What are the implications of having it OFF?
1 What is the STUFF function and how does it differ from the REPLACE function in SQL?
1 How to get @@ERROR and @@ROWCOUNT at the same time?
1 What is de-normalization in SQL database administration? Give examples ?
1 Can you explain about buffer cash and log Cache in SQL Server?
1 Describe how to use Linked Server?
1 Explain how to send email from SQL database?
1 How to make remote connection in database?
1 What is the purpose of OPENXML clause SQL server stored procedure?
1 How to store pdf file in SQL Server?
1 Explain the use of keyword WITH ENCRYPTION. Create a Store Procedure with Encryption?
1 What is lock escalation?
1 What is Failover clustering overview?
1 What is Builtin/Administrator?
1 What XML support does the SQL server extend?
1 Difference between Primary Key and Foreign Key ?
1 SQL Query to find second highest salary of Employee?
1 SQL Query to find Max Salary from each department?
1 Write SQL Query to display the current date?
1 Write an SQL Query to check whether date passed to Query is the date of given format or not?
1 Write an SQL Query to print the name of the distinct employee whose DOB is between 01/01/1960 to 31/12/1975?
1 Write an SQL Query find number of employees according to gender whose DOB is between 01/01/1960 to 31/12/1975?
1 Write an SQL Query to find an employee whose Salary is equal or greater than 10000?
1 Write an SQL Query to find name of employee whose name Start with 'M'?
1 find all Employee records containing the word "Joe", regardless of whether it was stored as JOE, Joe, or joe.?
1 Write an SQL Query to find the year from date?
1 How can you create an empty table from an existing table?
1 How to fetch common records from two tables?
1 How to fetch alternate records from a table?
1 How to select unique records from a table?
1 What is the command used to fetch first 5 characters of the string?
1 Which operator is used in query for pattern matching?
1 Write SQL Query to find duplicate rows in a database? and then write SQL query to delete them?
1 There is a table which contains two column Student and Marks, you need to find all the students, whose marks are greater than average marks i.e. list of above average students.
1 How do you find all employees which are also manager?
1 You have a composite index of three columns, and you only provide the value of two columns in WHERE clause of a select query? Will Index be used for this operation?
1 What is the default join in SQL? Give an example query?
1 Describe all the joins with examples in SQL?
1 What is Union and Union All ? Explain the differences?

SQL Server

What is a database?

A database is described as an organized way of collection of DATA. It is the collection of schemes, tables, queries, reports, views and other objects.

Syntax: CREATEDATABASEDatabaseName

Example: CREATEDATABASE Student

or you can Create Database through Design/ Wizard form by right clicking on DATABASE option-New Database.

What is DBMS?

A Database Management System (DBMS) is a program that controls creation, maintenance and use of a database. DBMS can be termed as File Manager that manages data in a database rather than saving it in file systems.

What is SQL?

Structured Query Language, also known as SQL, is a programming language designed for managing Relational Database Management Systems (RDBMSs). SQL is an International Organization for Standardization (ISO) standard. In RDBMS all the data is stored in tables with each table consisting of rows and columns.

Example of Sql Server 2014 SQL format:

Example of Oracle SQL format below:

Create database:

Output: Here we can see our database is created.

What is PL/SQL?

PL/SQL Control Statements in Oracle.

Control Statements,

* Control statements are very important in PL/SQL.

* Control Statements are elements in a program that control the flow of program execution.

* The syntax of control statements are similar to regular English and are very similar to choices that we make every day.

* Branching statements are as follows:

o If statement

o If - THEN - ELSE o Nested IF

o Branching with logical connectivity

o While

o For Loop

What is the difference between SQL and PL/SQL?

SQL: It is referred as Structured Query Language.

* Only simple IF / Else statements.

* Through SQL you can interact with database through ADO.NET

* In SQL you can execute a line of code

* It can run only on windows

PL/SQL: It is referred as Procedure Language / Structure Query Language:

* In PL/SQL you can execute a block of code not a single line of code.

* Deep control statements

* It can run in UNIX also.

* PL/SQL language includes object oriented programming techniques such as encapsulation, function overloading, and information hiding (all but inheritance).

What is RDBMS?

RDBMS: It is referred as Relation Database Management Systems (RDBMS). RDBMS possesses a set of the below given characteristics:

* Write-intensive operations: The RDBMS is frequently written to and is often used in transaction-oriented applications.

* Data in flux or historical data: The RDBMS is designed to handle frequently changing data. Alternatively, RDBMS can also store vast amounts of historical data, which can later be analyzed or "mined".

* Application-specific schema: The RDBMS is configured on a per-application basis and a unique schema exists to support each application.

* Complex data models. The relational nature of the RDBMS makes it suitable for handling sophisticated, complex data models that require many tables, foreign key values, complex join operations, and so on.

* Data integrity: The RDBMS features many components designed to ensure data integrity. This includes rollback operations, referential integrity, and transaction-oriented operations.

What is a database table?

Database table: Table contains records in the form of rows and columns. A permanent table is created in the database you specify and remains in the database permanently, until you delete it.

Syntax:

  1. Create table TableName (ID INT, NAME VARCHAR(30) )

  2. Drop syntax: drop table TableName

  3. Select Syntax: Select * from TableName

What is a query?

A DB query is a code written in order to get the information back from the database. Query can be designed in such a way that it matched with our expectation of the result set. Simply, a question to the Database.

What is subquery?

A subquery is a query within another query. The outer query is called as main query, and inner query is called subquery. SubQuery is always executed first, and the result of subquery is passed on to the main query.

What are the types of subquery?

There are two types of subquery - Correlated and Non-Correlated.

A correlated subquery cannot be considered as independent query, but it can refer the column in a table listed in the FROM the list of the main query.

A Non-Correlated sub query can be considered as independent query and the output of subquery are substituted in the main query.

How to create a table in SQL?

SQL provides an organized way for table creation.

Syntax: Create table TableName (columnName1 datatype, columnName2 datatype )

The following is an example of creating a simple table-

create table Info (Name varchar(20), BirthDate date,Phone nvarchar(12), City varchar(20))

What are tables and Fields?

A table is a set of data that are organized in a model with Columns and Rows. Columns can be categorized as vertical, and Rows are horizontal. A table has specified number of column called fields but can have any number of rows which is called record.

How to delete a table in SQL Server?

Delete Data Record from Database Table and deleting an existing table by the following method:

Syntax: To delete all table records of a table:

Delete TableName DELETE info

How to update a database table using SQL?

To update an existing Table we use SQL Command UPDATE: It will update the records as per user defined query/need.

Syntax:

Update TableName SET ColumnName = NewData where Condition

Update info Set City = 'Baroda' where id = 2

What is a database relationship?

Relationships are created by linking the column in one table with the column in another table. There are four different types of relationship that can be created.

The relationships are listed below:

  1. One to One Relationship

  2. Many to One relationship

  3. Many to Many relationship

  4. One to One relationship

One to Many & Many to One Relationship:

For a One to many relationships, a single column value in one table has one or more dependent column values in another table. Look at the following diagram:

Many to Many Relationship:

The third table acts as a bridge between the tables that want to establish a Many to Many relationship. The bridge table stores the common information between Many to Many relationship tables. Have a look at the following diagram:

What is a primary key of a database?

Primary key:-

A table column with this constraint is called the key column for the table. This constraint helps the table to make sure that the value is not repeated and also that there are no null entries.

Now this column does not allow null values and duplicate values. You can try inserting values to violate these conditions and see what happens. A table can have only one Primary key. Multiple columns can participate on the primary key.

Example:

What is a unique key?

A Unique key constraint uniquely identified each record in the database. This provides uniqueness for the column or set of columns. A Primary key constraint has automatic unique constraint defined on it. But not, in the case of Unique Key. There can be many unique constraint defined per table, but only one Primary key constraint defined per table.

What is a foreign key of a database?

To define the relationship between two tables (one is called parent and the other one is the child table) connected by columns, a foreign key constraint is used. In this constraint the values of the child table must appear in the parent table, which means that for a foreign key, one table should point to a Primary Key in another table. A table can have multiple foreign keys and each foreign key can have a different referenced table.

Example: To understand the foreign key clearly let's assume the following two tables:

CUSTOMER {Cust_ID, Cust_Name, Age, ContactNo, Gender, Address} VENDOR {Vend_ID, Vend_Name, Cust_ID}

Example: Foreign Key Constraint while using CREATE TABLE statement.

Syntax: CREATE TABLE table_name(Col1 datatype NOT NULL, Col2 datatype NOT NULL, Col3 datatype NOT NULL, CONSTRAINT FK_Column FOREIGN KEY(Col1, Col2, Col3) REFERENCES parent _table(Col1, Col2, Col3) );

AT SINGLE COLUMN LEVEL

What is database normalization?

Database normalization is the process of organizing the fields and tables of a relational database to minimize redundancy and dependency. Normalization usually involves dividing large tables into smaller (and less redundant) tables and defining relationships among them. Normalization is a bottom-up technique for database design.

The evolution of Normalization theories is illustrated below:

* First Normal Form (1NF)

* Second Normal Form (2NF)

* Third Normal Form (3NF)

* Boyce-Codd Normal Form (BCNF)

* 4th Normal Form

* 5th Normal Form

* 6th Normal Form

What are database normalization forms?

Normalization is the process of organizing data into a related table. it also eliminates redundancy and increases the integrity which improves performance of the query. To normalize a database, we divide the database into tables and establish relationships between the tables.

* First Normal Form (1st NF)

* Second Normal Form (2nd NF)

* Third Normal Form (3rd NF)

* Boyce-Codd Normal Form (BCNF)

* Fourth Normal Form (4th NF)

* Fifth Normal Form (5th NF)

First Normal Form (1NF):

This should remove all the duplicate columns from the table. Creation of tables for the related data and identification of unique columns.

Second Normal Form (2NF):

Meeting all requirements of the first normal form. Placing the subsets of data in separate tables and Creation of relationships between the tables using primary keys. Third Normal Form (3NF):

This should meet all requirements of 2NF. Removing the columns which are not dependent on primary key constraints.

Fourth Normal Form (3NF):

Meeting all the requirements of third normal form and it should not have multi- valued dependencies.

What is Denormalization.

DeNormalization is a technique used to access the data from higher to lower normal forms of database. It is also process of introducing redundancy into a table by incorporating data from the related tables.

What is a stored procedure?

A Stored Procedure is a collection or a group of T-SQL statements. Stored Procedures are a precompiled set of one or more statements that are stored together in the database. They reduce the network load because of the precompilation. We can create a Stored Procedure using the "Create proc" statement.

Why we use Stored Procedure

There are several reasons to use a Stored Procedure. They are a network load reducer and decrease execution time because they are precompiled. The most important use of a Stored Procedure is for security purposes. They can restrict SQL Injection. We can avoid SQL injection by use of a Stored Procedure.

How to create a Stored Procedure

CREATE PROCEDURE spEmployee AS

BEGIN

SELECT EmployeeId, Name, Gender, DepartmentName FROM tblEmployees INNER JOIN tblDepartments ON tblEmployees.EmployeeDepartmentId = tblDepartments.DepartmentId

END

Advantages of using a Stored Procedure in SQL Server

* It is very easy to maintain a Stored Procedure and they are re-usable.

* The Stored Procedure retains the state of the execution plans.

* Stored Procedures can be encrypted and that also prevents SQL Injection Attacks

What is a function in SQL Server?

A function is a sequence of statements that accepts input, processes them to perform a specific task and provides the output. Functions must have a name but the function name can never start with a special character such as @, $, #, and so on.

Types of function

* Pre-Defined Function

* User-Defined Function

User-defined Function:

In a user-defined function we write our logic according to our needs. The main advantage of a user-defined function is that we are not just limited to pre-defined functions. We can write our own functions for our specific needs or to simplify complex SQL code. The return type of a SQL function is either a scalar value or a table.

Creation of a function

Create function ss(@id int)

returns table as return select * from item where itemId = @id

Execution of a Function

select * from ss(1)

Output:

What are the different types of functions in SQL Server?

A function must return a result. So that is also called a function that returns a result or a value. When we create it a function must specify a value type that will return a value.

* Functions only work with select statements.

* Functions can be used anywhere in SQL, such as AVG, COUNT, SUM, MIN, DATE and so on with select statements.

* Functions compile every time.

* Functions must return a value or result.

* Functions only work with input parameters.

* Try and catch statements are not used in functions.

Function Types:

The following is the function list in SQL Server databases.

SQL Server contains the following aggregates functions:

What is a trigger in SQL Server?

"A Trigger is a Database object just like a stored procedure or we can say it is a special kind of Stored Procedure which fires when an event occurs in a database.".

It is a database object that is bound to a table and is executed automatically. We cannot explicitly call any trigger. Triggers provide data integrity and used to access and check data before and after modification using DDL or DML query.

There are two types of Triggers:

  1. DDL Trigger

  2. DML trigger

DDL Triggers: They fire in response to DDL (Data Definition Language) command events that start with Create, Alter and Drop like Create_table, Create_view, drop_table, Drop_view and Alter_table.

Code of DDL Triggers:

create trigger saftey on database for

create_table, alter_table, drop_table as print 'you can not create ,drop and alter table in this database' rollback;

Output:

DML Triggers: They fire in response to DML (Data Manipulation Language) command events that start with Insert, Update and Delete like insert_table, Update_view and Delete_table.

Code of DML Trigger:

create trigger deep on emp for insert, update, delete as print 'you can notinsert,update and delete this table I' rollback;

Output:

When we insert, update or delete in a table in a database then the following message appears:

Why do we need triggers?

Why and when to use a trigger:

We use a trigger when we want some event to happen automatically on certain desirable scenarios. You have a table that changes frequently, now you want to know how many times and when these changes take place. In that case you can create a trigger that will insert the desired data into another table whenever any change in the main table occurs.

In SQL Server we can create the following 3 types of triggers:

* Data Definition Language (DDL) triggers

* Data Manipulation Language (DML) triggers

* Logon triggers

Example:

CREATE TRIGGER trgAfterInsert ON[dbo].[Employee_Test]

FOR INSERT

AS

declare@ empid int; declare@ empname varchar(100);

declare@ empsal decimal(10, 2);

declare@ audit_action varchar(100);

select@ empid = i.Emp_ID from inserted i;

select@ empname = i.Emp_Name from inserted i;

select@ empsal = i.Emp_Sal from inserted i;

set@ audit_action = 'Inserted Record -- After Insert Trigger.';

insert into Employee_Test_Audit (Emp_ID, Emp_Name, Emp_Sal, Audit_Action, Audit_Timestamp) values(@empid, @empname, @empsal, @audit_action, getdate());

PRINT 'AFTER INSERT trigger fired.'

GO

What are the different types of triggers?

Triggers are a special type of stored procedure which is executed automatically based on the occurrence of a database event. These events can be categorized as:

* Data Manipulation Language (DML) and

* Data Definition Language (DDL) events.

The benefit derived from triggers is based in their events driven nature. Once created, the trigger automatically fires without user intervention based on an event in the database.

A. Using DML Triggers: DML triggers are invoked when any DML command such as INSERT, DELETE, and UPDATE occurs on the data of a table and/or view.

* DML triggers are powerful objects for maintaining database integrity and consistency.

* DML triggers evaluate data before it has been committed to the database. o During this evaluation the following actions are performed.

We cannot use the following commands in DML trigger:

o ALTER DATABASE

o CREATE DATABASE

o DISK DATABASE

o LOAD DATABASE

o RESTORE DATABASE

B. Using DDL Triggers:

* These triggers focus on changes to the definition of database objects as opposed to changes to the actual data.

* This type of trigger is useful for controlling development and production database environments.

Let us create DDL trigger now-

The following is the syntax.

CREATE TRIGGER trigger_name ON{ALL SERVER | DATABASE }

[WITH < ddl_trigger_option > [, ...n]]{ FOR | AFTER } {

event_type | event_group }[, ...n] AS

{sql_statement[;][...n] | EXTERNAL NAME < method specifier > [;] }

CREATE TRIGGER tr_TableAudit ON DATABASE FOR CREATE_TABLE, ALTER_TABLE, DROP_TABLE AS PRINT 'You must disable the TableAudit trigger in order to change any table in this database ' ROLLBACK GO

What is a view in the database?

A View is nothing but a select query with a name given to it or we can simply say a view is a Named Query. Ok! Why do we need a view? There can be many answers for this. Some of the important stuff I feel is:

* A view can combine data from multiple tables using adequate joins and while bringing it may require complex filters and calculated data to form the required result set. From a user's point of view, all these complexities are hidden data queried from a single table.

* Sometimes for security purposes, access to the table, table structures and table relationships are not given to the database user. All they have is access to a view not knowing what tables actually exist in the database.

* Using the view, you can restrict the user update only portions of the records.

The following are the key points to be noted about views:

  1. Multiple views can be created on one table.

  2. Views can be defined as read-only or updatable.

  3. Views can be indexed for better performance.

  4. Insert, update, delete can be done on an updatable view.

Why do I need views in a database?

There are a number of scenarios where we have to look for a view as a solution.

* To hide the complexity of the underlying database schema, or customize the data and schema for a set of users.

* To control access to rows and columns of data.

* To aggregate data for performance.

Views are used for security purposes because they provide encapsulation of the name of the table. Data is in the virtual table, not stored permanently. Views display only selected data.

Syntax of a View:

CREATE VIEW view_name AS

SELECT column_name(s) FROM table_name WHERE condition

There are two types of views.

* Simple View

* Complex View

What is the difference between Primary key and unique key?

Primary key does not allow the null values but unique key allows one null value.

Primary key will create clustered index on column but unique key will create non-clustered index by default.

How can you increase SQL performance?

Following are tips which will increase your SQl performance:-

Every index increases the time takes to perform INSERTS, UPDATES, and DELETES, so the number of indexes should not be too much. Try to use maximum 4-5 indexes on one table, not more. If you have read-only table, then the number of indexes may be increased.

Keep your indexes as narrow as possible. This reduces the size of the index and reduces the number of reads required to read the index.

Try to create indexes on columns that have integer values rather than character values.

If you create a composite (multi-column) index, the orders of the columns in the key are very important. Try to order the columns in the key as to enhance selectivity, with the most selective columns to the leftmost of the key.

If you want to join several tables, try to create surrogate integer keys for this purpose and create indexes on their columns. Create surrogate integer primary key (identity for example) if your table will not have many insert operations.

Clustered indexes are more preferable than nonclustered, if you need to select by a range of values or you need to sort results set with GROUP BY or ORDER BY. If your application will be performing the same query over and over on the same table, consider creating a covering index on the table.

You can use the SQL Server Profiler Create Trace Wizard with "Identify Scans of Large Tables" trace to determine which tables in your database may need indexes. This trace will show which tables are being scanned by queries instead of using an index.

What is the use of OLAP?

OLAP is useful because it provides fast and interactive access to aggregated data and the ability to drill down to detail.

What is a measure in OLAP?

Measures are the key performance indicator that you want to evaluate. To determine which of the numbers in the data might be measures. A rule of thumb is if a number makes sense when it is aggregated, then it is a measure.

What are dimensions in OLAP?

Dimensions are the categories of data analysis. For example, in a revenue report by month by sales region, the two dimensions needed are time and sales region. Typical dimensions include product, time, and region.

What are levels in dimensions?

Dimensions are arranged in hierarchical levels, with unique positions within each level. For example, a time dimension may have four levels, such as Year, Quarter, Month, and Day. Or the dimension might have only three levels, for example, Year, Week, and Day. The values within the levels are called members. For example, the years 2002 and 2003 are members of the level Year in the Time dimension.

What are fact tables and dimension tables in OLAP?

Twist: - Can you explain the star schema for OLAP?

The dimensions and measures are physically represented by a star schema. Dimension tables revolve around fact table. A fact table contains a column for each measure as well as a column for each dimension. Each dimension column has a foreign -key relationship to the related dimension table, and the dimension columns taken together are the key to the fact table.

What is DTS?

DTS is used to import data and while importing it helps us to transform and modify data. The name itself is self explanatory DTS ( Data transformation Services).

What is fill factor ? or When does page split occurs?

The 'fill factor' option specifies how full SQL Server will make each index page. When there is no free space to insert new row on the index page, SQL Server will create new index page and transfer some rows from the previous page to the new one. This operation is called page splits. You can reduce the number of page splits by setting the appropriate fill factor option to reserve free space on each index page. The fill factor is a value from 1 through 100 that specifies the percentage of the index page to be left empty. The default value for fill factor is 0. It is treated similarly to a fill factor value of 100, the difference in that SQL Server leaves some space within the upper level of the index tree for FILLFACTOR = 0. The fill factor percentage is used only at the time when the index is created. If the table contains read-only data (or data that very rarely changed), you can set the 'fill factor' option to 100. When the table's data is modified very often, you can decrease the fill factor to 70% or whatever you think is best.

What is RAID and how does it work?

Redundant Array of Independent Disks (RAID) is a term used to describe the technique of improving data availability through the use of arrays of disks and various data-striping methodologies. Disk arrays are groups of disk drives that work together to achieve higher data-transfer and I/O rates than those provided by single large drives. An array is a set of multiple disk drives plus a specialized controller (an array controller) that keeps track of how data is distributed across the drives. Data for a particular file is written in segments to the different drives in the array rather than being written to a single drive.

For speed and reliability, it is better to have more disks. When these disks are arranged in certain patterns and are use a specific controller, they are called a Redundant Array of Inexpensive Disks (RAID) set. There are several numbers associated with RAID, but the most common are 1, 5 and 10.

RAID 1 works by duplicating the same writes on two hard drives. Let us assume you have two 20-Gigabyte drives. In RAID 1, data is written at the same time to both the drives. RAID1 is optimized for fast writes.

RAID 5 works by writing parts of data across all drives in the set (it requires at least three drives). If a drive failed, the entire set would be worthless. To combat this problem, one of the drives stores a "parity" bit. Think of a math problem, such as 3 + 7 = 10. You can think of the drives as storing one of the numbers, and the 10 is the parity part. By removing any one of the numbers, you can get it back by referring to the other two, like this: 3 + X = 10. Of course, losing more than one could be evil. RAID 5 is optimized for reads.

RAID 10 is a bit of a combination of both types. It does not store a parity bit, so it is faster, but it duplicates the data on two drives to be safe. You need at least four drives for RAID 10. This type of RAID is probably the best compromise for a database server.

Note :- It's difficult to cover complete aspect of RAID in this book. It's better to take some decent SQL SERVER book for in detail knowledge, but yes from interview aspect you can probably escape with this answer.

SQL Server Difference between @@IDENTITY, SCOPE_IDENTITY () and IDENT_CURRENT

@@IDENTITY

It will return last or newly inserted record id of any table in current session but it's not limited to current scope. In current session if any trigger or functions inserted record in any table that it will return that latest inserted record id regardless of table. We need to use this property whenever we don't have any other functions or triggers that run automatically.

Syntax: SELECT @@IDENTITY

SCOPE_IDENTITY()

This property will return last or newly inserted record id of table in current session or connection and it's limited to current scope that means it will return id of newly inserted record in current session / connection stored procedure or query executed by you in current scope even we have any other functions or triggers that run automatically. Its better we can go with property whenever we need to get last or newly inserted record id in table.

Syntax: SELECT SCOPE_IDENTITY()

IDENT_CURRENT

This property will return last or newly inserted record id of specified table. It's not limited to any session or scope it's limited to mentioned table so it will return last inserted record id of specified table.

Syntax: SELECT IDENT_CURRENT(table_name)

Finally we can say SCOPE_IDENTITY properties is best to get newly inserted record id from executed stored procedure or query when compared with other properties

Example

CREATE TABLE SAMPLE1 (Id INT IDENTITY)

CREATE TABLE SAMPLE2 (Id INT IDENTITY(100,1))

-- Trigger to execute while inserting data into SAMPLE1 table

GO

CREATE TRIGGER TRGINSERT ON SAMPLE1 FOR INSERT

AS

BEGIN

INSERT SAMPLE2 DEFAULT VALUES

END

GO SELECT * FROM SAMPLE1 -- It will return empty value

SELECT * FROM SAMPLE2 -- It will return empty value

When we execute above statements we will get output like as shown below

Now we will insert default values in "SAMPLE1" table by executing following query and check values of @@identity, scope_identity() and ident_current('tablenae')

INSERT SAMPLE1 DEFAULT VALUES

SELECT @@IDENTITY -- It returns value 100 this was inserted by trigger

SELECT SCOPE_IDENTITY() -- It returns value 1 this was inserted by insert query in SAMPLE1

SELECT IDENT_CURRENT('SAMPLE2') -- It returns value inserted in SAMPLE2 table

Difference between char varchar and nvarchar in sql server

Char DataType

Char datatype which is used to store fixed length of characters. Suppose if we declared char(50) it will allocates memory for 50 characters. Once we declare char(50) and insert only 10 characters of word then only 10 characters of memory will be used and other 40 characters of memory will be wasted.

varchar DataType

Varchar means variable characters and it is used to store non-unicode characters. It will allocate the memory based on number characters inserted. Suppose if we declared varchar(50) it will allocates memory of 0 characters at the time of declaration. Once we declare varchar(50) and insert only 10 characters of word it will allocate memory for only 10 characters.

nvarchar DataType

nvarchar datatype same as varchar datatype but only difference nvarchar is used to store Unicode characters and it allows you to store multiple languages in database. nvarchar datatype will take twice as much space to store extended set of characters as required by other languages.

So if we are not using other languages then it's better to use varchar datatype instead of nvarchar

Difference between bit tinyint smallint int and bigint datatypes in SQL Server

Bit DataType

This datatype represents a single bit that can be 0 or 1.

tinyint DataType

This datatype represents a single byte which is used to store values from 0 to 255 (MinVal: 0, MaxVal: 255). Its storage size is 1 byte.

smallint DataType

This datatype represents a signed 16-bit integer which is used to store values from -2^15 (-32,768) through 2^15 - 1 (32,767) and its storage size is 2 bytes.

int DataType

This datatype represents a signed 32-bit integer which is used to store values from -2^31(-2,147,483,648) to 2 ^31-1(2,147,483,647). Its storage size is 4 bytes.

Bigint DataType

This datatype represents a signed 64-bit integer which is used to store values from -2^63 (-9223372036854775808) through 2^63-1 (9223372036854775807). Its storage size is 8 bytes.

What is the difference between DELETE TABLE and TRUNCATE TABLE commands?

DELETE TABLE syntax logs the deletes thus make the delete operation slow. TRUNCATE table does not log any information but it logs information about deallocation of data page of the table so TRUNCATE table is faster as compared to delete table.

DELETE table can have criteria while TRUNCATE cannot.

TRUNCATE table does not invoke trigger.

I had mentioned that TRUNCATE table can not be rolled back while delete can be.

If locking is not implemented, what issues can occur?

Following are the problems that occur if you do not implement locking properly in SQLSERVER.

Lost Updates

Lost updates occur if you let two transactions modify the same data at the same time, and the transaction that completes first is lost. You need to watch out for lost updates with the READ UNCOMMITTED isolation level. This isolation level disregards any type of locks, so two simultaneous data modifications are not aware of each other. Suppose that a customer has due of 2000$ to be paid. He pays 1000$ and again buys a product of 500$ . Lets say that these two transactions are now been entered from two different counters of the company.

Now both the counter user starts making entry at the same time 10:00 AM. Actually speaking at 10:01 AM the customer should have 2000$-1000$+500 = 1500$ pending to be paid. But as said in lost updates the first transaction is not considered and the second transaction overrides it. So the final pending is 2000$+500$ = 2500$.....I hope the company does not loose the customer.

Non-Repeatable Read

Non-repeatable reads occur if a transaction is able to read the same row multiple times and gets a different value each time. Again, this problem is most likely to occur with the READ UNCOMMITTED isolation level. Because you let two transactions modify data at the same time, you can get some unexpected results. For instance, a customer wants to book flight, so the travel agent checks for the flights availability. Travel agent finds a seat and goes ahead to book the seat. While the travel agent is booking the seat, some other travel agent books the seat. When this travel agent goes to update the record, he gets error saying that "Seat is already booked". In short, the travel agent gets different status at different times for the seat.

Dirty Reads

Dirty reads are a special case of non-repeatable read. This happens if you run a report while transactions are modifying the data that you are reporting on. For example, there is a customer invoice report, which runs on 1:00 AM in afternoon and after that all invoices are sent to the respective customer for payments. Let us say one of the customer has 1000$ to be paid. Customer pays 1000$ at 1:00 AM and at the same time report is run. Actually, customer has no money pending but is still issued an invoice.

Phantom Reads

Phantom reads occur due to a transaction being able to read a row on the first read, but not being able to modify the same row due to another transaction deleting rows from the same table. Lets say you edit a record in the mean time somebody comes and deletes the record, you then go for updating the record which does not exist...Panicked.

Interestingly, the phantom reads can occur even with the default isolation level supported by SQL Server: READ COMMITTED. The only isolation level that does not allow phantoms is SERIALIZABLE, which ensures that each transaction is completely isolated from others. In other words, no one can acquire any type of locks on the affected row while it is being modified.

What are different transaction levels in SQL SERVER?

Twist: - What are different types of locks in SQL SERVER?

Transaction Isolation level decides how is one process isolated from other process. Using transaction levels, you can implement locking in SQL SERVER.

There are four transaction levels in SQL SERVER:-

READ COMMITTED

The shared lock is held for the duration of the transaction, meaning that no other transactions can change the data at the same time. Other transactions can insert and modify data in the same table, however, as long as it is not locked by the first transaction.

READ UNCOMMITTED

No shared locks and no exclusive locks are honored. This is the least restrictive isolation level resulting in the best concurrency but the least data integrity.

REPEATABLE READ

This setting disallows dirty and non-repeatable reads. However, even though the locks are held on read data, new rows can still be inserted in the table, and will subsequently be interpreted by the transaction.

SERIALIZABLE

This is the most restrictive setting holding shared locks on the range of data. This setting does not allow the insertion of new rows in the range that is locked; therefore, no phantoms are allowed.

Following is the syntax for setting transaction level in SQL SERVER.

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE

What are the different locks in SQL SERVER?

Depending on the transaction level, six types of lock can be acquired on data:-

Intent

The intent lock shows the future intention of SQL Server's lock manager to acquire locks on a specific unit of data for a particular transaction. SQL Server uses intent locks to queue exclusive locks, thereby ensuring that these locks will be placed on the data elements in the order the transactions were initiated. Intent locks come in three flavors: intent shared (IS), intent exclusive (IX), and shared with intent exclusive (SIX).

IS locks indicate that the transaction will read some (but not all) resources in the table or page by placing shared locks.

IX locks indicate that the transaction will modify some (but not all) resources in the table or page by placing exclusive locks.

SIX locks indicates that the transaction will read all resources, and modify some (but not all) of them. This will be accomplished by placing the shared locks on the resources read and exclusive locks on the rows modified. Only one SIX lock is allowed per resource at one time; therefore, SIX locks prevent other connections from modifying any data in the resource (page or table), although they do allow reading the data in the same resource.

Shared

Shared locks (S) allow transactions to read data with SELECT statements. Other connections are allowed to read the data at the same time; however, no transactions are allowed to modify data until the shared locks are released.

Update

Update locks (U) are acquired just prior to modifying the data. If a transaction modifies a row, then the update lock is escalated to an exclusive lock; otherwise, it is converted to a shared lock. Only one transaction can acquire update locks to a resource at one time. Using update locks prevents multiple connections from having a shared lock that want to eventually modify a resource using an exclusive lock. Shared locks are compatible with other shared locks, but are not compatible with Update locks.

Exclusive

Exclusive locks (X) completely lock the resource from any type of access including reads. They are issued when data is being modified through INSERT, UPDATE and DELETE statements.

Schema

Schema modification locks (Sch -M) are acquired when data definition language statements, such as CREATE TABLE, CREATE INDEX, ALTER TABLE, and so on are being executed. Schema stability locks (Sch-S) are acquired when store procedures are being compiled.

Bulk Update

Bulk update locks (BU) are used when performing a bulk-copy of data into a table with TABLOCK hint. These locks improve performance while bulk copying data into a table; however, they reduce concurrency by effectively disabling any other connections to read or modify data in the table.

Can we suggest locking hints to SQL SERVER?

We can give locking hints that helps you over ride default decision made by SQL Server. For instance, you can specify the ROWLOCK hint with your UPDATE statement to convince SQL Server to lock each row affected by that data modification. Whether it is prudent to do so is another story; what will happen if your UPDATE affects 95% of rows in the affected table? If the table contains 1000 rows, then SQL Server will have to acquire 950 individual locks, which is likely to cost a lot more in terms of memory than acquiring a single table lock. So think twice before you bombard your code with ROWLOCKS.

What is LOCK escalation?

Lock escalation is the process of converting of low-level locks (like rowlocks, page locks) into higher -level locks (like table locks). Every lock is a memory structure too many locks would mean, more memory being occupied by locks. To prevent this from happening, SQL Server escalates the many fine-grain locks to fewer coarse-grain locks. Lock escalation threshold was definable in SQL Server 6.5, but from SQL Server 7.0 onwards SQL Server dynamically manages it.

What are the different ways of moving data between databases in SQL Server?

There are lots of options available; you have to choose your option depending upon your requirements. Some of the options you have are BACKUP/RESTORE, detaching and attaching databases, replication, DTS, BCP, logshipping, INSERT...SELECT, SELECT...INTO, creating INSERT scripts to generate data.

What is the difference between a HAVING CLAUSE and a WHERE CLAUSE?

You can use Having Clause with the GROUP BY function in a query and WHERE Clause is applied to each row before, they are part of the GROUP BY function in a query.

What is the difference between UNION and UNION ALL SQL syntax?

UNION SQL syntax is used to select information from two tables. But it selects only distinct records from both the table, while UNION ALL selects all records from both the tables.

Note :- Selected records should have same datatype or else the syntax will not work.

What are the different types of triggers in SQl SERVER?

There are two types of triggers:-

INSTEAD OF triggers

INSTEAD OF triggers fire in place of the triggering action. For example, if an INSTEAD OF UPDATE trigger exists on the Sales table and an UPDATE statement is executed against the Salestable, the UPDATE statement will not change a row in the sales table. Instead, the UPDATE statement causes the INSTEAD OF UPDATE trigger to be executed.

AFTER triggers

AFTER triggers execute following the SQL action, such as an insert, update, or delete. This is the traditional trigger which existed in SQL SERVER.

INSTEAD OF triggers are executed automatically before the Primary Key and the Foreign Key constraints are checked, whereas the traditional AFTER triggers is executed after these constraints are checked. Unlike AFTER triggers, INSTEAD OF triggers can be created on views.

if we have multiple AFTER Triggers on table how can we define the sequence od the triggers?

If a table has multiple AFTER triggers, then you can specify which trigger should be executed first and which trigger should be executed last using the stored procedure sp_settriggerorder.

What is SQL injection?

It is a Form of attack on a database-driven Web site in which the attacker executes unauthorized SQL commands by taking advantage of insecure code on a system connected to the Internet, bypassing the firewall. SQL injection attacks are used to steal information from a database from which the data would normally not be available and/or to gain access to an organization's host computers through the computer that is hosting the database.

SQL injection attacks typically are easy to avoid by ensuring that a system has strong input validation.

As name suggest we inject SQL which can be relatively dangerous for the database. Example this is a simple SQL

SELECT email, passwd, login_id, full_name FROM members WHERE email = 'x'

Now somebody does not put "x" as the input but puts "x ; DROP TABLE members;". So the actual SQL which will execute is:-

SELECT email, passwd, login_id, full_name FROM members WHERE email = 'x'; DROP TABLE members;

Think what will happen to your database.

What is the difference between Stored Procedure (SP) and User Defined Function (UDF)?

Following are some major differences between a stored procedure and user defined functions:-

You can not change any data using UDF while you can do everything with a stored procedure.

UDF cannot be used in XML FOR clause but SP's can be used.

UDF does not return output parameters while SP's return output parameters.

If there is an error in UDF its stops executing. But in SP's it just ignores the error and moves to the next statement.

UDF cannot make permanent changes to server environments while SP's can change some of the server environment.

How can you raise custom errors from stored procedure?

The RAISERROR statement is used to produce an ad hoc error message or to retrieve a custom message that is stored in the sysmessages table. You can use this statement with the error handling code presented in the previous section to implement custom error messages in your applications. The syntax of the statement is shown here.

RAISERROR ({msg_id |msg_str }{,severity ,state }

[ ,argument [ ,,...n ] ] ))

[ WITH option [ ,,...n ] ]

A description of the components of the statement follows.

msg_id: The ID for