Basic SQL Server

  Home  MS SQL Server  Basic SQL Server


“SQL Database Concepts frequently Asked Questions by expert members with experience in Basic SQL Database Concepts. These interview questions and answers on SQL Database Concepts will help you strengthen your technical skills, prepare for the interviews and quickly revise the concepts. So get preparation for the SQL Database Concepts job interview”



113 Basic SQL Server Questions And Answers

1⟩ Explain Differentiate between a HAVING CLAUSE and a WHERE CLAUSE?

HAVING CLAUSE

- HAVING CLAUSE is used only with the SELECT statement.

- It is generally used in a GROUP BY clause in a query.

- If GROUP BY is not used, HAVING works like a WHERE clause.

WHERE Clause

- It is applied to each row before they become a part of the GROUP BY function in a query.

 137 views

2⟩ Do you know query execution plan?

- The optimizer available in SQL Server optimizes the code to be effectively executed.

- A query execution plan shows how this optimizer would run the query.

- Query execution plan can be viewed by :

- Using the Show Execution Plan option available in Query Analyzer,

- Displaying Estimated Execution Plan on the query dropdown menu,

- Use the SET SHOWPLAN_TEXT ON command before running a query and capturing the execution plan event in a SQL Server Profiler trace.

 138 views

3⟩ Explain Comment on Transactions?

- Using transactions we can group all SQL commands into a single unit.

- The transaction begins with some task and finishes only when all tasks within it are over.

- The transaction gets over successfully only when all commands in it are successfully over. Even if one command fails, the whole transaction fails.

- The BEGIN TRANSACTION, ROLLBACK TRANSACTION, and COMMIT TRANSACTION statements are used to work with transactions.

- A group of tasks starts with the begin statement.

- In case of any problem, the rollback command is executed to abort the transaction.

- If all the tasks run successfully, all commands are executed through commit statement.

 132 views

4⟩ Do you know what is recursion? Is it possible for a stored procedure to call itself or recursive stored procedure? How many levels of SP nesting is possible?

Recursion is method of problem solving where the solution is arrived at by repetitively applying the logic and solution to the subsets of the problem.

Transact-SQL supports recursion. So, yes it is possible for a stored procedure to call itself.

Stored procedures and managed code references can be nested up to 32 levels.

 174 views

5⟩ Tell me what do you mean by an execution plan? Why is it used? How would you view it?

a.) An execution plan can be called as a road map that graphically or textually shows the data retrieval methods which have been chosen by the SQL

Server query optimizer, for a stored procedure or ad- hoc query.

b.) It is used because it is a very useful tool for a developer to understand the performance characteristics of a query or stored procedure.

c.) There exists an option called "Show Execution Plan" in Query Analyzer. If this option is turned on, it will display query execution plan in separate window when the query is run again.

 181 views

6⟩ Tell me When is the UPDATE_STATISTICS command used?

- When the processing of large data is done, this command is used.

- Whenever large number of deletions, modification or copy takes place into the tables, the indexes need to be updated to take care of these changes. UPDATE_STATISTICS performs this job.

 152 views

7⟩ Tell me what do you understand by a view? What does the WITH CHECK OPTION clause for a view do?

- A view is a virtual table that consists of fields from one or more real tables.

- It is usually used to join multiple tables and get the data.

- The WITH CHECK OPTION for a view prevents any modification to the data that does not confirm to the WHERE clause of the view definition.

- This allows the data belonging to the view to be updated through the view.

 162 views

8⟩ Explain what is the function of SQL Server Agent Windows service?

- It is a Windows service which handles the tasks scheduled within the SQL Server environment. These tasks are also called as job and are stored with in SQL server. The jobs may run through a trigger, a predefined schedule or on demand.

- This service is very useful in determining why a particular job did not run as intended.

 144 views

10⟩ Tell me what are the advantages of using Stored Procedures?

- They help in reducing the network traffic and latency which in turn boosts application performance.

- They help in promoting code reuse.

- They provide better security to data.

- It is possible to encapsulate the logic using stored procedures. This allows to change stored procedure code without affecting clients.

- It is possible to reuse stored procedure execution plans, which are cached in SQL Server's memory. This reduces server overhead.

 151 views

11⟩ Suppose you want to implement the following relationships while designing tables. How would you do it?a.) One-to-oneb.) One-to-manyc.) Many-to-many

a.) One-to-One relationship - can be implemented as a single table and rarely as two tables with primary and foreign key relationships.

b.) One-to-Many relationships - by splitting the data into two tables with primary key and foreign key relationships.

c.) Many-to-Many - by using a junction table with the keys from both the tables forming the composite primary key of the junction table.

 153 views

12⟩ Please differentiate between DELETE and TRUNCATE?

- Truncate can not be rolled back while Delete can be.

- Truncate keeps the lock on table while Delete keeps the lock on each row.

- Truncate resets the counter of the Identity column while Delete doesn't do so.

- Trigger is not fired in Truncate while it happens in Delete.

 138 views

14⟩ What is SQL COLLATION?

Collation is a type of sort order. There are mainly three types of sort orders, namely:

i.) Dictionary case sensitive

ii.)Dictionary - case insensitive

iii.)Binary.

 133 views

15⟩ What is SQL Stored Procedure?

- It is a set of T-SQL statements combined together to perform a single task formed by combining many small tasks.

- When you actually run a Stored procedure, a set of statements is run.

 123 views

16⟩ Explain ACID?

- ACID (Atomicity Consistency Isolation Durability) is a quality sought after in a reliable database. Here's the relevance of each quality:

- Atomicity is an all-or-none proposition.

- Consistency - it guarantees that your database is never left by a transaction in a half-finished state.

- Isolation - it keeps transactions separated from each other until they’re finished.

- Durability - it ensures that the database keeps a track of pending changes in a way that the server can recover from an abnormal termination.

 143 views

19⟩ Please differentiate between a Local and a Global temporary table?

- A local temporary table exists only for the duration of a connection or, if defined inside a compound statement, for the duration of the compound statement.

- Global temporary tables (created with a double “##”) are visible to all sessions.

- Global temporary tables are dropped when the session that created it ends, and all other sessions have stopped referencing it.

 147 views

20⟩ Can you explain different types of Locks in SQL Server?

There are 3 kinds of locks in SQL Server

i.) Shared locks - they are used for operations which do not allow any change or update of data. For e.g. SELECT.

ii.) Update locks - they are used when SQL Server wants to modify a page. The update page lock is then promoted to an exclusive page lock before actually making the changes.

iii.) Exclusive locks - they are used for the data modification operations. For e.g. UPDATE, INSERT, or DELETE.

 172 views