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

41⟩ Tell me what is use of EXCEPT clause? How it differs from NOT IN clause?

-When we combine two queries using EXCEPT clause, it will returns distinct rows from the first SELECT statement that are not returned by the second one.

-EXCEPT clause works the same way as the UNION operator of SQL and MINUS clause in Oracle.

-The syntax of EXCEPT clause is as follow

SELECT column1 [, column2 ]

FROM table1 [, table2 ]

[WHERE condition]

EXCEPT

SELECT column1 [, column2 ]

FROM table1 [, table2 ]

[WHERE condition]

-The difference between EXCEPT and NOT IN clause is EXCEPT operator returns all distinct rows from the rows returned by first select statement which does not exist in the rows returned by second select statement. On the other hand “NOT IN” will return all rows from returned by first select statement which does not exist in the rows returned by second select statement.

 122 views

42⟩ Explain what is lock escalation?

Lock escalation from SQL Server 7.0 onwards, is dynamically managed by SQL Server. It is the process of converting a lot of low level locks into higher level locks..

 136 views

43⟩ Do you know what is difference between Index Seek vs. Index Scan?

Index Seek and Index Scan are operation for query tuning in execution plans.

Table Scan scans every record of the table. So the cost of proportional is the number of rows of that table.

The Index Scan is preferred only when the table is small.

Index Seek only touches the rows which qualify and the pages that contain that qualifying rows, so the cost of proportional is the number of qualifying rows and pages instead of the number of rows in the table.

Index seek is preferred for highly sensitive queries.

 139 views

44⟩ Explain what is Public Role in SQL Server?

Every database has a public role which holds all the default permissions for the users in a database. This role cannot be dropped and cannot have roles or groups assigned. For security reasons, permissions granted to public role should be minimized..

 136 views

45⟩ Explain what is ROW_NUMBER function?

RANK is one of the Ranking functions which are used to give rank to each row in the result set of a SELECT statement.

For using this function first specify the function name, followed by the empty parentheses.

Then specify the OVER function. For this function, you have to pass an ORDER BY clause as an argument. The clause specifies the column(s) that you are going to rank.

For Example

SELECT ROW_NUMBER() OVER(ORDER BY Salary DESC) AS [RowNumber], EmpName, Salary, [Month], [Year] FROM EmpSalary

In the result you will see that the highest salary got the first rand and the lowest salary got the last rank. Here the rows with equal salaries will not get same ranks.

 132 views

46⟩ Tell me about BuiltinAdministrator?

The built in Administrator Account is basically used during some setup to join some machine in the domain. The built in administrator should be disabled immediately thereafter. For any disaster recovery, the account will be automatically enabled. It should not be used for normal operations.

 117 views

47⟩ What is Triggers in MS SQL Server?

-In SQL the Trigger is the procedural code that executed when you INSERT, DELETE or UPDATE data in the table.

-Triggers are useful when you want to perform any automatic actions such as cascading changes through related tables, enforcing column restrictions, comparing the results of data modifications and maintaining the referential integrity of data across a database.

-For example, to prevent the user to delete the any Employee from EmpDetails table, following trigger can be created.

create trigger del_emp

on EmpDetails

for delete

as

begin

rollback transaction

print "You cannot delete any Employee!"

end

-When someone will delete a row from the EmpDetails table, the del_emp trigger cancels the deletion, rolls back the transaction, and prints a message "You cannot delete any Employee!"

 139 views

48⟩ Explain about SQL Server Login?

SQL server login is used to connect to SQL server. This used when login in through the windows login credentials is not existent. This is possible when SQL Server is configured to operate in Mixed Mode. SQL server login is typically needed when applications are connected to external vendors or for users who don’t have any windows NT/2000 account.

 114 views

49⟩ Explain what is Scheduled job and how to create it?

-If we want to execute any procedural code automatically on specific time either once or repeatedly then we can create a Scheduled job for that code.

-Following are the steps to create a Scheduled Job.

1. Connect to your database of SQL server in SQL Server Management Studio.

2. On the SQL Server Agent. There you will find a Jobs folder. Right click on jobs and choose Add New.

3. A New Job window will appear. Give a related name for the job.

4. Click next on the "Steps" in the left menu. A SQL job can have multiple steps either in the form of SQL statement or a stored procedure call.

5. Click on the "Schedules" in the left menu. A SQL job can contain one or more schedules. A schedule is basically the time at which sql job will run itself. You can specify recurring schedules also.

-Using scheduled job you can also create alert and notifications.

 131 views

50⟩ What is failover clustering overview?

Failover clustering is mainly used for data availability. Typically in a failover cluster, there are two machines. One machine provides the basic services and the second is available to run the service when the primary system fails. The primary system is monitored periodically to check if it works. This monitoring may be performed by the failover computer or an independent system also called as cluster controller. In an event of failure of primary computer, the failover system takes control.

 129 views

51⟩ Do you know what is OPENXML in SQL Server?

-OPENXML provides an easy way to use an XML document as a data-source for your procedures.

-OPENXML data can be manipulated the same way we deal with database tables by treating xml tags in the form of columns and the value in the form of rows.

-By using OPENXML Data can be inserted or updated very quickly without multiple trips to the database.

-Example:

DECLARE @count int

DECLARE @xml varchar(5000)

SET @xml ='<Employees>

<Employee id="1">

<Name>DEF</Name>

<Employee >1234</ Employee >

</Employee >

<Employee id="2">

<Name>ABC</Name>

<PhoneNo>2211</PhoneNo>

</Employee >

</Employees>'

EXEC sp_xml_preparedocument @count OUTPUT, @xml

SELECT *

FROM OPENXML (@count, Employees/Employee')

WITH (id Varchar(10), Name varchar(100) 'Name' , PhoneNo Varchar(50) 'PhoneNo')

EXEC sp_xml_removedocument @index

It will give following result.

1 DEF 1234

2 ABC 2211

 154 views

53⟩ Explain what are Sparse columns?

Sparse column is a type of column which is used to optimize storage for null values.

When a column there is big number of null then by defining that column as spars column we can save a large amount of disk space.

The drawback of sparse column is that it requires more space for the non null values. When we define a column as sparse it requires additional 4 Byte for not null values.

For example, a DATETIME column in a non-sparse column required 8 bytes of storage whereas if we define that column as a sparse column then it will require 12 bytes.

It is not possible to set ROWGUIDCOL and IDENTITY properties in sparse column.

 125 views

54⟩ Tell me in brief how SQL server enhances scalability of the database system?

SQL Server has efficient ways to enhance scalability of the database system. SQL Server 2000 automatically adjusts the granularity of locking for each table used by a query. Parallel Execution Plans is in placed automatically. This split the processing of a SQL statement into pieces that can be run on a different CPU, thus allowing the complete result set to build faster.

 125 views

55⟩ Do you know what is RANK function?

-RANK is one of the Ranking functions which are used to give rank to each row in the result set of a SELECT statement.

-For using this function first specify the function name, followed by the empty parentheses.

-Then specify the OVER function. For this function, you have to pass an ORDER BY clause as an argument. The clause specifies the column(s) that you are going to rank.

For Example

SELECT RANK() OVER(ORDER BY Salary DESC) AS [RowNumber], EmpName, Salary, [Month], [Year] FROM EmpSalary

-In the result you will see that the highest salary got the first rand and the lowest salary got the last rank.

Here the rows with equal salaries will get same ranks.

-Remember that the rank depends on the row's position in the result set, not on the sequential number of the row.

 159 views

56⟩ Explain the XML support SQL server extends?

SQL Server (server-side) supports 3 major elements:

a. Creation of XML fragments: This is done from the relational data using FOR XML to the select query.

b. Ability to shred xml data to be stored in the database.

c. Finally, storing the xml data.

Client-side XML support in SQL Server is in the form of SQLXML. It can be described in terms of

XML Views: providing bidirectional mapping between XML schemas and relational tables.

Creation of XML Templates: allows creation of dynamic sections in XML.

 121 views

57⟩ Tell me what are cursors and when they are useful?

-When we execute any SQL operations, SQL Server opens a work area in memory which is called Cursor.

-When it is required to perform the row by row operations which are not possible with the set-based operations then Cursor is used.

-There are two of cursors

1. Implicate Cursor

SQL Server automatically manages cursors for all data manipulation statements. These cursors are called implicit cursors.

2. Explicit Cursor

When the programmer wants to perform the row by row operations for the result set containing more than one row, then he explicitly declare a cursor with a name.

They are managed by OPEN, FETCH and CLOSE.

%FOUND, %NOFOUND, %ROWCOUNT and %ISOPEN attributes are used in both types of cursors.

 129 views

58⟩ Can you explain what is SQL Server English Query?

SQL Server English Query helps to build applications that can accept query in questions form (in English). The application passes the questions string to the English query engine. English Query then returns to the application a SQL statement or a Multi-Dimensional Expression (MDX) query that will return the answer to the user’s question.

 157 views

59⟩ Tell me what is log shipping?

-SQL has introduced Log Shipping feature to synchronize the Distributed Database Server. Synchronize the database by copying Transaction logs, Backing up, Restoring data. SQL Server Job Agents is used to make these processes automatic.

-In the case of failure the Log Shipping will not transfer the server. That means it will not redirect your application to other server. This has to be done manually.

-Log shipping synchronizes the database only. The main functions of Log Shipping are as follows:

1. To Back up the transaction log of the primary database

2. To Copy the transaction log backup to every secondary server

3. To Restore the transaction log backup on the secondary database

 118 views

60⟩ Explain the rules for designing Files and File groups in SQL Server?

A file or file group can only be used by one database. For example, the files abc.mdf and abc.ndf contains data and objects for abc database, cannot be used by any other database.

A file can be a member of only one file group.

Data and transaction log information cannot be part of the same file or file group.

Transaction log files can't be part of a file group.

 157 views