This Document Contains Bakchodi
This Document Contains Bakchodi
1. What is SQL?
Answer: DELETE removes rows one by one and can be rolled back. TRUNCATE
removes all rows from a table without logging individual row deletions and
cannot be rolled back.
Answer: DROP removes the table structure and data permanently, whereas
TRUNCATE only removes data but keeps the table structure intact.
Answer: A primary key uniquely identifies each record in a table and cannot
contain NULL values.
Answer: A unique key prevents duplicate values in a column but allows NULL
values (unlike a primary key).
Answer: A candidate key is a column (or set of columns) that can qualify as a
primary key.
Answer: A super key is a set of one or more columns that uniquely identify a
row in a table.
Answer:
- LEFT JOIN: Returns all records from the left table and matching ones from
the right.
- RIGHT JOIN: Returns all records from the right table and matching ones
from the left.
- FULL JOIN: Returns all records when a match exists in either table.
Answer: UNION removes duplicates, while UNION ALL retains all records
including duplicates.
Answer: A cross join produces a Cartesian product of both tables, returning all
possible combinations.
Answer: Indexes reduce the number of disk I/O operations by allowing quick
lookups.
Answer: Indexes consume additional storage and slow down DML operations
like INSERT and UPDATE.
Answer: Use indexes, avoid SELECT \*, use proper joins, normalize tables,
analyze execution plans.
Answer: A query inside another query that provides input data for the main
query.
Answer: A subquery that depends on the outer query for its execution.
Answer: Yes, but only if the view does not contain joins, aggregations, or
calculated columns.
26. What is the difference between NOT NULL and NULL constraints?
Answer: NOT NULL ensures that a column cannot have NULL values, while
NULL allows missing values.
Answer:
Answer: The WHERE clause is used to filter records before grouping (used with
SELECT, UPDATE, DELETE statements), whereas the HAVING clause is used to
filter records after grouping (used with GROUP BY to filter aggregate results).
Answer: UNION combines the result sets of two queries and removes
duplicates.
UNION ALL combines the result sets without removing duplicates, making it
faster.
Answer:
Answer: A stored procedure is a precompiled SQL script that can be
executed multiple times. It improves performance and security.
Answer: A trigger is a stored procedure that automatically runs when a
specific event (INSERT, UPDATE, DELETE) occurs in a table.
Top 50 SQL Interview Questions and Answers (Inder Code)
Answer:
Answer: Normalization is the process of organizing data to eliminate
redundancy and improve efficiency.
It prevents anomalies and ensures data integrity.
Answer:
Answer: Denormalization is the process of merging tables to improve query
performance, often used in data warehousing.
Answer:
● Clustered Index: Sorts and stores data physically in order (only one per
table).
● Non-Clustered Index: Creates a separate structure pointing to the
actual data (multiple per table).
Answer: An execution plan shows how SQL Server will execute a query. It
helps optimize performance by identifying bottlenecks.
Answer:
Answer:
Answer:
Answer: COALESCE returns the first non-null value from a list of expressions.
Answer: A self-join is a join where a table is joined with itself, useful for
hierarchical data structures like employee-manager relationships.
Top 50 SQL Interview Questions and Answers (Inder Code)
Answer: A cursor is a database object used to retrieve, manipulate, and
traverse row-by-row processing.
Answer: Indexing speeds up data retrieval by creating a data structure for
quick lookups.
Answer:
49. What is the difference between a primary key and a unique key?
Answer:
● Primary Key: Uniquely identifies a row, does not allow NULL, and only
one per table.
● Unique Key: Ensures uniqueness but allows one NULL value and
multiple unique keys per table.
Answer:
-- Find the nth highest salary in a table (e.g., 3rd highest salary)
-- Write a query to fetch all employees who have the same salary as
another employee
SELECT e.* FROM Employee e WHERE salary > ( SELECT AVG(salary) FROM
Employee WHERE department_id = e.department_id );