In the world of relational database management systems, MS SQL plays a crucial role, as it is developed by Microsoft and has more users than other RDBMS systems. Being an employee in an RDBMS organization under Microsoft is a masterstroke in anyone’s career. MS SQL is in high demand since it is in collaboration with Microsoft, which makes it a cross-platform application. In this blog, we have discussed some of the most frequently asked MS SQL interview questions and answers, along with other types of questions as well, like MS SQL interview questions and MS SQL Server interview questions. These questions and answers are curated just for you to excel in any RDBMS interview. Check out our website, SLA Institute, to learn more about all the courses and training we offer.
MS SQL interview questions and answers
- What do you mean by constraints in MS SQL?
Constraints are the rules and regulations that define the tables and columns in a database. Constraints ensure that the data in the database is accurate and consistent. Constraints are mainly responsible for increasing the reliability of a database.
- What are the types of constraints in MS SQL?
This is one of the important MS SQL interview questions. Types of constraints are discussed below:
- Primary key: The primary key constraint guarantees that every row in a table possesses a distinct identifier, and it is not permissible for this identifier to be NULL. Typically, this constraint is assigned to the column that uniquely distinguishes each record.
- Foreign key: A foreign key creates a vital link between two tables by associating the foreign key column in one table with the primary key column in another, ensuring the preservation of referential integrity.
- Unique constraint: The unique constraint guarantees that the values in a column (or a combination of columns) remain distinct throughout all rows in a table. In contrast to a primary key, this type of constraint permits the presence of NULL values.
- Check constraint: A check constraint establishes criteria that values in a column must meet, restricting the acceptable range of values for that column.
- Default constraint: A default constraint sets a predefined value for a column, automatically assigning it if no value is supplied during an INSERT operation.
- What are the various types of database management systems (DBMS)?
- Relational database management system: This type of system categorizes the entire table with rows and columns, thereby establishing relationships between them with keys.
- NoSQL database management system: These systems usually handle unstructured or semi-structured data as it forms into a schema structure.
- Object-oriented Database Management Systems: These systems store the data as objects. Example: ObjectDB.
- Hierarchical Database Management System: organizes the data into a hierarchical tree-like structure where each record in the database has a parent-child relationship.
- How can a user handle exceptions in a MS SQL program?
Exceptions are also referred to as errors. In MS SQL programs, errors can be handled using the TRY…CATCH block, which eventually enables the T-SQL code that will detect and respond to the error.
Recommended Read: MS SQL Server DBA Training.
- What do you mean by triggers in MS SQL?
This is an important MS SQL interview question. Whenever a modification of data happens in the form of commands such as INSERT, UPDATE, or DELETE on a certain table, a response in the form of a procedure is automatically fired. This response is called a trigger.
- What are the types of triggers in MS SQL?
This is one of the important MS SQL Server interview questions. Mainly, there are two types of triggers in MS SQL:
- AFTER Triggers (AFTER UPDATE, AFTER INSERT, AFTER DELETE): These triggers are executed after the occurrence of triggers like insertion, deletion, and update. These triggers are mainly used to recording logs, updating related tables, etc.
- INSTEAD OF Triggers(INSTEAD OF INSERT, INSTEAD OF UPDATE, INSTEAD OF DELETE): Just like the name suggests, these triggers are triggered instead of triggering events, so custom actions become the alternative. They are commonly applied with views to implement intricate logic or address specific scenarios.
- What are the available character manipulation functions in MS SQL?
This is one of the important MS SQL interview questions. MS SQL has certain manipulation functions available; some of them are discussed below:
- LEN(): This character provides the count of characters in a string.
- LEFT(): Retrieves a specified number of characters from the left side of a string.
- RIGHT(): Fetches a specified number of characters from the right side of a string.
- SUBSTRING(): Returns a segment of a string, starting at a designated position for a specified length.
- CHARINDEX(): Indicates the starting position of a specified substring within a string.
- What do you mean by bulk copying MS SQL?
Bulk copying is the process of transferring a large volume of data in and out of a database in MS SQL. This process is usually achieved through the Bulk Copy Program (BCP). BCP easily facilitates the transfer and import/export of data sets between SQL server tables and external data files. Populating data warehouses is one of the tasks that can be done by using this.
- What is a stored procedure in MS SQL?
A stored procedure is a set of multiple already-established collections of SQL statements. These statements can be summoned and executed multiple times as a single unit.
10. What is the difference between a clustered and non-clustered index in MS SQL?
- The clustered index describes the data in a structural manner. It decides how the data is arranged in a table. Each table can have only one structured index.
- An unclustered index, on the other hand, is the opposite of clustered index, where it usually doesn’t change the physical order of rows in a table and is stored independently. Each table can have multiple unstructured indexes.
11. What is the difference between DELETE and TRUNCATE statements in MS SQL?
- In MS SQL, DELETE and TRUNCATE are variations of the same functions.
- DELETE statement can be used to remove rows from a table based on certain conditions, which can be undone and rolled back.
- TRUNCATE statements, on the other hand, are used to remove all rows from a table without any conditions, and they cannot be undone and rolled back.
- TRUNCATE is usually faster than DELETE.
12. Describe a foreign key.
A foreign key in MS SQL is a set of columns in a table that link to and refer to the primary key in another table, thereby establishing a link between the two tables.
Recommended Read: Primary Key and Foreign Key in SQL.
13. How do you optimize queries on the MS SQL server?
This is one of the most important MS SQL Server interview questions, and we have discussed optimizing queries in MS SQL Server. Optimizing queries can be done by:
- Using appropriate JOIN types.
- Indexing tables accurately.
- Functions in WHERE clauses should be avoided.
- Usage of stored procedures
- Query execution plans must be analyzed.
Recommended Read: Understanding View and Materialized View in SQL.
14. Explain deadlock in MS SQL Server.
Deadlock is a situation that occurs when one database resource is locked by two or more transactions, leading to the blocking of both of those transactions where each is waiting for the other to release the lock, which leads to a loop dependency.
15. How is a deadlock situation released in the MS SQL server?
An SQL server automatically detects a deadlock situation by marking one of the transactions as a deadlock victim and canceling that particular transaction with an error.
16. What does a SQL server agent service do in MS SQL?
It is a service that handles, executes and runs scheduled administrative tasks like jobs, alerts, maintenance plans, etc.
17. What is the purpose of SQL Server Reporting Services (SSRS)?
It is generally used to create, manage, and deliver reports, which will help organizations visualize and analyze their data in a structured manner that will be easy to view. SSRS will schedule reports and also enable permissions.
18. What is an index in MS SQL Server?
An index in MS SQL functions in the same way as an index in a book. In MS SQL Server, an index helps a database quickly retrieve specific information from a table, making it faster to search for and access particular data. This speeds up the process of retrieving data when you run queries, similar to how a book index speeds up finding information in a book.
Recommended read: Types of SQL Server Indexes.
19. What is a recursive stored procedure?
This is one of the important MS SQL Server interview questions. In Microsoft SQL Server, a recursive stored procedure is crafted to execute a repetitive or iterative operation by invoking itself within its code. This type of stored procedure engages in a cycle of performing a specific task or computation, modifying parameters, and calling itself until a predefined termination condition is satisfied. This termination condition, often termed the base case, is crucial as it prevents the recursion from perpetuating endlessly. Once this condition is met, the recursive process concludes, and the results are then returned. An illustrative scenario for a recursive stored procedure involves navigating through a hierarchical data structure, such as a tree or graph, where each recursive call guides the process to a distinct branch or level.
20. What is referential integrity in MS SQL?
This is one of the most important MS SQL Server interview questions. Referential integrity within Microsoft SQL Server is a principle aimed at preserving the consistency and precision of connections among tables in a relational database. This principle relies on the employment of foreign keys to create associations between tables, thereby upholding the integrity of these relationships. The central objective of referential integrity is to implement regulations that uphold the accuracy of data dependencies within interconnected tables.
21. What are some of the fundamental aspects of referential integrity in MS SQL?
Here are the fundamental aspects of referential integrity in MS SQL:
Utilization of Foreign Keys:
Referential integrity hinges on the utilization of foreign keys, represented by columns in a table that reference the primary key in another table. The table containing the foreign key is denoted as the “child” table, while the table with the primary key assumes the role of the “parent” table.
Establishment of Relationships:
Connections between tables are formed through the definition of foreign key constraints. These constraints articulate how data in the foreign key column should correspond to the primary key in the referenced table.
Enforcement of Relationships:
Upon enforcing referential integrity, it guarantees the maintenance of relationships between tables. Any attempt to insert, update, or delete data that breaches these relationships triggers a constraint violation error.
Actions during update or delete operations:
Referential integrity constraints allow for the specification of actions when a referenced row in the parent table undergoes an update or deletion. Common actions encompass:
CASCADE: Update or delete corresponding rows in the child table.
SET NULL: Set foreign key values in the child table to NULL.
SET DEFAULT: Assign foreign key values to their default values.
NO ACTION: Restrict the update or delete it if it jeopardizes referential integrity.
Prevention of Orphaned Rows:
Referential integrity acts as a safeguard against the creation of “orphaned” rows in the child table, signifying rows referencing non-existent rows in the parent table.
22. What is a checkpoint in MS SQL?
In the context of Microsoft SQL Server, a checkpoint serves as a pivotal process overseeing the transfer of all modified pages residing in the buffer cache to the disk. This crucial operation is meticulously designed to guarantee the persistent and synchronized storage of database information on the disk. The significance of a checkpoint extends beyond mere data preservation; it extends to the meticulous flushing of log records that encapsulate the details of alterations. This dual-pronged approach ensures not only the durability of stored data but also the integrity of the transaction log, solidifying the database’s resilience in the face of potential system crashes or restarts. In essence, a checkpoint acts as a safeguard, embodying the commitment to maintaining the coherence and reliability of stored data within the Microsoft SQL Server environment.
23. What is a subquery in MS SQL?
In MS SQL, a subquery, alternatively referred to as a nested or inner query, is a query intricately woven within another. Enclosed within parentheses, the subquery serves the purpose of fetching data essential for the condition specified in the main query. The outcomes of the subquery are subsequently handed over to the main query, facilitating additional operations and computations.
Conclusion:
Gaining knowledge of SQL in and of itself is a great start towards a good career, but Microsoft SQL makes the opportunity richer with its reputation in the world of technology. This blog contains all the valid questions in terms of MS SQL interview questions and MS SQL Server interview questions too. We from Softlogic Institute wish you all the best with your interviews. We hope this blog comes in handy.