0% found this document useful (0 votes)
5 views

SQL Interview Questions Part-4

The document outlines key SQL concepts including the differences between clustered and non-clustered indexes, DELETE, TRUNCATE, and DROP commands, and the purpose of Common Table Expressions (CTEs). It also explains recursive CTEs, window functions like RANK(), DENSE_RANK(), and ROW_NUMBER(), as well as the distinctions between transactional (OLTP) and analytical (OLAP) databases. Additionally, it covers triggers, partitioning, the MERGE statement, and deadlock handling in SQL.

Uploaded by

rohitsul1112003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

SQL Interview Questions Part-4

The document outlines key SQL concepts including the differences between clustered and non-clustered indexes, DELETE, TRUNCATE, and DROP commands, and the purpose of Common Table Expressions (CTEs). It also explains recursive CTEs, window functions like RANK(), DENSE_RANK(), and ROW_NUMBER(), as well as the distinctions between transactional (OLTP) and analytical (OLAP) databases. Additionally, it covers triggers, partitioning, the MERGE statement, and deadlock handling in SQL.

Uploaded by

rohitsul1112003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

SQL Interview

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.

Here, the CTE dept_avg stores the average salaries by


department, which is used in the main query to display
employees along with their department's average salary.
How does a recursive CTE work in SQL?
A recursive CTE is a self-referencing query that continues
to execute until a specified condition is met. Recursive
CTEs are useful when working with hierarchical
data like employee-manager relationships or directory
structures. The recursive part of the query repeatedly
references the CTE itself.
What is the difference between RANK(),
DENSE_RANK(), and ROW_NUMBER()?
These window functions assign a rank or number to each
row within a partition but behave differently when
handling ties:
● RANK(): Skips numbers if there are ties (e.g., 1, 2, 2, 4).
● DENSE_RANK(): Does not skip numbers, even if there are
ties (e.g., 1, 2, 2, 3).
● ROW_NUMBER(): Assigns a unique sequential number
to each row, without considering ties.

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.

2. Analytical Databases (OLAP):


○ Optimized for complex queries and data analysis over large
datasets.
○ Used for generating reports and trends over historical data.
○ Example: Business intelligence platforms, data warehouses.
How do triggers work in SQL, and
when are they used?
A trigger is a stored procedure that automatically executes
before or after an event occurs on a table, such as an
INSERT, UPDATE, or DELETE. Triggers enforce business
rules and maintain data integrity by validating or recording
changes.

This trigger logs the old and new salaries whenever an


employee's salary is updated.
What is partitioning, and how does it
improve performance?
Partitioning splits a large table into smaller pieces based
on a column’s value, such as date or region. Queries only
scan the relevant partitions instead of the entire
table, improving performance for large datasets.

This table is partitioned by year, allowing efficient queries


on specific years.
What is the purpose of the MERGE
statement in SQL?
The MERGE statement performs upserts—it inserts,
updates, or deletes records based on a condition. It is
useful when synchronizing two datasets, as it combines
multiple operations into a single query.

This query updates existing employees' salaries or inserts


new employees.
How does SQL handle deadlocks, and
how can they be prevented?
A deadlock occurs when two transactions block each other
by holding resources the other needs. SQL databases
detect and resolve deadlocks by terminating one
of the transactions and rolling it back to free resources.

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!

You might also like