Post content
✅ Top SQL Interview Questions with Answers: Part-4🧠 31. What are constraints in SQL? Constraints are rules applied to columns to enforce data integrity: • PRIMARY KEY – Uniquely identifies each record • FOREIGN KEY – Ensures referential integrity • UNIQUE – Ensures all values are different • NOT NULL – Prevents null values • CHECK – Restricts values based on condition • DEFAULT – Assigns a default value 32. What is a composite key? A composite key is a combination of two or more columns that together uniquely identify a row. Example: (StudentID, CourseID) in an enrollment table. 33. What are scalar vs table-valued functions? • Scalar function: Returns a single value (e.g., LEN(), GETDATE()) • Table-valued function: Returns a table/data set and can be used in FROM clause 34. How does indexing affect performance? Indexes improve read performance (SELECT) by allowing faster searches. Downsides: • Slower write operations (INSERT, UPDATE, DELETE) • Takes additional storage 35. What is data integrity? Ensures the accuracy, consistency, and reliability of data throughout its lifecycle. Maintained using constraints, transactions, and normalization. 36. What are triggers in SQL? Triggers are automatic actions executed in response to certain events on a table (e.g., INSERT, UPDATE, DELETE). Used for auditing, enforcing rules, or updating related tables. 37. What is a correlated subquery? A subquery that depends on the outer query for its values. It’s evaluated once for each row of the outer query. Example: SELECT name FROM employees e WHERE salary > (SELECT AVG(salary) FROM employees WHERE dept_id = e.dept_id); 38. What is a cross join? Combines each row from one table with every row from another — produces Cartesian product. Used rarely, typically when all combinations are needed. 39. What is UNION vs UNION ALL? • UNION: Combines two queries, removes duplicates • UNION ALL: Combines all rows, keeps duplicates Both require same number and type of columns. 40. Difference between EXISTS and IN • IN: Checks if a value exists in a list • EXISTS: Checks if subquery returns any rows EXISTS is often faster with large subqueries or joins. 💬Double Tap ❤️ For Part-5