SQL Interview Questions Part-4
SQL Interview Questions Part-4
Q&A Part-4
What is the difference between
clustered and non-clustered indexes?
Indexes improve query performance by enabling quick
data lookups, but there are key differences between
clustered and non-clustered indexes:
1. Clustered Index:
It sorts and stores the actual data rows of the table in the
order of the index key.
○ There can only be one clustered index per table since
the physical order of the table can only follow one
sequence.
○ Example: If a clustered index is created on employee_id,
the table will store rows physically ordered by employee_id.
2. Non-Clustered Index:
○ It stores a separate structure (a pointer) with the
indexed column and a reference to the actual data row.
○ A table can have multiple non-clustered indexes, each
improving search on different columns.
What is the difference between
DELETE, TRUNCATE, and DROP?
1. DELETE:
○ Removes specific rows from a table based on a
condition.
○ Supports ROLLBACK if used within a transaction.
○ Slower than TRUNCATE as each row deletion is logged.
2. TRUNCATE:
○ Removes all rows from a table quickly without logging
individual deletions.
○ Cannot be rolled back in most databases.
○ Resets identity counters, like auto-increment columns.
3. DROP:
○ Deletes the table and its structure from the database.
○ All data and indexes are permanently removed.
What is the purpose of CTE (Common
Table Expression) in SQL?
A Common Table Expression (CTE) provides a temporary
result set that simplifies complex queries by breaking
them into smaller parts. CTEs improve readability and
help avoid using subqueries multiple times in a query.
They are useful when performing joins, aggregates, or
recursive operations.
If two employees have the same salary, RANK() will skip the
next number, while DENSE_RANK() will not.
What is the difference between
transactional (OLTP) and analytical (OLAP)
databases?
1. Transactional Databases (OLTP):
○ Handle real-time operations such as inserts, updates, and
deletes.
○ Prioritize data integrity and speed for individual transactions.
○ Example: Bank systems, e-commerce websites.
Prevention Techniques:
● Access resources in a consistent order across
transactions.
● Keep transactions short to minimize lock times.
● Use row-level locks instead of table-level locks.
Liked these SQL
questions?🤔
Follow for more
data insights,
interview tips,
and tech content!