Challenge your knowledge of essential SQL interview questions with this quiz. Test your understanding of topics like stored procedures vs functions, clustered vs non-clustered indexes, DELETE vs TRUNCATE commands, and the use of WHERE and HAVING clauses.
Which statement correctly describes the handling of parameters in stored procedures and functions in SQL?
Explanation: Stored procedures can accept both input and output parameters, allowing two-way data transfer. Functions, on the other hand, only support input parameters and always return a single value. The statement indicating that both can have output parameters is incorrect, as is the idea that functions allow output parameters. The claim that neither can accept input parameters is false.
Which of the following SQL statements is typically NOT allowed inside a function?
Explanation: Functions in SQL cannot include DML statements like INSERT, UPDATE, or DELETE to modify data. However, using SELECT statements, returning a value, and performing arithmetic operations are permitted within functions. Thus, INSERT is not allowed, while the other options are acceptable in functions.
Where can a user-defined function be used in an SQL statement?
Explanation: User-defined functions can be used almost anywhere an expression is allowed, including SELECT, WHERE, and HAVING clauses. They are not limited to just SELECT or solely the WHERE clause. The statement about standalone usage is inaccurate, and they are not restricted to specific parts of a query.
Can you directly use a stored procedure in a SELECT statement’s FROM clause?
Explanation: Stored procedures cannot be used directly within a SELECT statement, as they do not return values compatible with table expressions. Table-valued functions, not stored procedures, can be used in SELECT queries. Procedures returning single values or using table-valued procedures are not standard in SQL.
Which SQL object supports transaction management: stored procedures or functions?
Explanation: Only stored procedures support transaction management, allowing BEGIN, COMMIT, and ROLLBACK statements. Functions cannot manage transactions directly. The answers stating both or neither support transactions are incorrect. Functions do not have transaction control.
Which object allows the use of Try-Catch blocks for error handling in SQL?
Explanation: Try-Catch error handling is allowed inside stored procedures but not inside functions. Functions are intentionally restricted from such procedural constructs. The distractors include 'both' and 'functions', but this feature isn’t available for functions.
What is the maximum number of clustered indexes that can be created on a single SQL table?
Explanation: A table can have only one clustered index, as the clustered index determines the physical ordering of rows. Having more would create ambiguity. Options suggesting 10 or 249 are incorrect limitations for non-clustered indexes, and 'unlimited' is never allowed.
How does a clustered index differ from a non-clustered index in terms of data storage?
Explanation: A clustered index organizes the table on disk and stores the actual row data at its leaf level, while a non-clustered index holds references or pointers to the data rows. The options claiming the reverse, or that both store only pointers, are incorrect about the structural differences.
Which Index ID is typically assigned to a clustered index in SQL?
Explanation: The clustered index is always assigned Index ID 1. Index ID 0 is usually reserved for heap tables that lack a clustered index. Both 249 and 999 are limits or IDs related to non-clustered indexes, not the clustered one.
What type of index does a primary key constraint create by default in SQL?
Explanation: By default, creating a primary key applies a clustered index unless one already exists or a specification is made. Non-clustered indexes are created by unique constraints by default. Heap is just an unordered table, and 'no index' is incorrect.
Which command allows the use of a WHERE clause to filter which records are removed?
Explanation: The DELETE command supports a WHERE clause to filter and remove specific rows. TRUNCATE, in contrast, removes all records unconditionally, without filtering. The options suggesting both or neither are not correct based on SQL behavior.
What happens to the identity column when TRUNCATE TABLE is executed?
Explanation: After TRUNCATE, the identity column (if present) is reset to its original seed value. Unlike DELETE, which retains the identity sequence, TRUNCATE discards previous numbering. The options stating NULL or 'no impact' are incorrect.
Which pair accurately matches the command type for DELETE and TRUNCATE?
Explanation: DELETE is a Data Manipulation Language (DML) operation, used for row-level actions, while TRUNCATE is regarded as a Data Definition Language (DDL), as it makes broad structural changes. The other combinations mislabel the commands' categories.
Which locking method does the DELETE command primarily use, compared to TRUNCATE?
Explanation: DELETE locks rows individually to facilitate precise deletions, while TRUNCATE typically locks the entire table for fast, bulk removal. The distractors referencing inverse locks or no locks aren't accurate. Page locks may be involved but aren't the primary lock for these commands.
In which SQL statements can the WHERE clause be used, compared to HAVING?
Explanation: The WHERE clause filters rows in SELECT, UPDATE, and DELETE statements, while HAVING is explicitly for filtering groups in SELECT statements with GROUP BY. It's incorrect to say HAVING applies to all DML or that both apply everywhere. HAVING is not for UPDATE or INSERT.