1. How do you optimize a slow-running SQL query?
Answer:
Use EXPLAIN or SHOW PLAN to analyze execution
Avoid SELECT *
Use indexes on filter and join columns
Minimize nested subqueries
Use JOIN instead of correlated subqueries
Sample Code:
-- Avoid SELECT *
SELECT id, name FROM employees WHERE department_id = 5;
-- Use indexed columns in WHERE clause
CREATE INDEX idx_department_id ON employees(department_id);
📌 Indexing Strategies
2. What are clustered and non-clustered indexes?
Answer:
Clustered Index: Sorts and stores data rows in the table based on the index key.
Non-Clustered Index: Contains pointers to the actual data rows.
Sample Code:
-- Clustered Index
CREATE CLUSTERED INDEX idx_emp_id ON employees(emp_id);
-- Non-Clustered Index
CREATE NONCLUSTERED INDEX idx_dept_id ON employees(department_id);
🔐 Encryption & Security
3. How do you implement column-level encryption in SQL Server?
Answer: Use symmetric keys and certificates for encryption.
Sample Code:
-- Create a certificate and symmetric key
CREATE CERTIFICATE MyCert WITH SUBJECT = 'Data Encryption';
CREATE SYMMETRIC KEY MySymKey WITH ALGORITHM = AES_256 ENCRYPTION BY CERTIFICATE
MyCert;
-- Encrypt data
OPEN SYMMETRIC KEY MySymKey DECRYPTION BY CERTIFICATE MyCert;
UPDATE employees SET ssn_encrypted = EncryptByKey(Key_GUID('MySymKey'), ssn);
-- Decrypt data
SELECT ssn = CONVERT(varchar, DecryptByKey(ssn_encrypted)) FROM employees;
⚙️Stored Procedures & Bulk Processing
4. How do you handle bulk data in stored procedures?
Answer: Use table-valued parameters and batch operations.
Sample Code:
-- Table type
CREATE TYPE EmployeeType AS TABLE (emp_id INT, name VARCHAR(100));
-- Stored Procedure
CREATE PROCEDURE InsertEmployees
@EmpList EmployeeType READONLY
AS
BEGIN
INSERT INTO employees(emp_id, name)
SELECT emp_id, name FROM @EmpList;
END;
🔁 Triggers & Automation
5. What is a trigger and when would you use one?
Answer: A trigger is a special procedure that runs automatically in response to events like INSERT,
UPDATE, or DELETE.
Sample Code:
-- Audit trigger
CREATE TRIGGER trg_AuditInsert
ON employees
AFTER INSERT
AS
BEGIN
INSERT INTO audit_log(emp_id, action, timestamp)
SELECT emp_id, 'INSERT', GETDATE() FROM inserted;
END;
🧾 T-SQL & PL/SQL Scripting
6. How do you handle exceptions in T-SQL?
Answer: Use TRY...CATCH blocks.
Sample Code:
BEGIN TRY
-- risky operation
UPDATE employees SET salary = salary * 1.1 WHERE department_id = 5;
END TRY
BEGIN CATCH
PRINT 'Error occurred: ' + ERROR_MESSAGE();
END CATCH;
📊 Advanced SQL Concepts
7. How do you retrieve the 3rd highest salary from a table?
Answer: Use DENSE_RANK() or ROW_NUMBER().
Sample Code:
SELECT salary FROM (
SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk
FROM employees
) AS ranked
WHERE rnk = 3;
8. What is a window function?
Answer: Performs calculations across a set of rows related to the current row.
Sample Code:
SELECT emp_id, salary,
RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS dept_rank
FROM employees;