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

SQL Basic to Advance Interview Question and Answer 1731934628

The document is a compilation of intermediate to advanced SQL interview questions and answers, covering topics such as subqueries, views, ACID properties, and indexing. It provides detailed explanations and examples for each question to aid understanding. The content is aimed at helping candidates prepare for SQL-related interviews effectively.

Uploaded by

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

SQL Basic to Advance Interview Question and Answer 1731934628

The document is a compilation of intermediate to advanced SQL interview questions and answers, covering topics such as subqueries, views, ACID properties, and indexing. It provides detailed explanations and examples for each question to aid understanding. The content is aimed at helping candidates prepare for SQL-related interviews effectively.

Uploaded by

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

Nitya CloudTech Pvt Ltd.

SQL Scenario-Based Interview


Questions & Answers

http://www.nityacloudtech.com/ @nityacloudtech
Nitya CloudTech Pvt Ltd.

Intermediate to Advanced SQL Questions


1. What is the difference between correlated and non-correlated
subqueries?
➢ Answer:
▪ Non-correlated subquery: A subquery that can be run

independently and returns a result which is used by the


outer query.
▪ Correlated subquery: A subquery that refers to columns

in the outer query, requiring it to be executed for each row


processed by the outer query.

2. What is a view, and why would you use it?


➢ Answer: A view is a virtual table created by a SELECT query.
It allows for simplified, reusable queries, data security (by
restricting access to specific columns), and abstraction over
complex joins or aggregations.

http://www.nityacloudtech.com/ @nityacloudtech
Nitya CloudTech Pvt Ltd.

3. How can you find duplicate records in a table?


➢ Answer:
SELECT column_name, COUNT(*)
FROM table_name
GROUP BY column_name
HAVING COUNT(*) > 1;

This query groups by the specified column and shows values


that appear more than once.

4. What is the purpose of the MERGE statement?


➢ Answer: MERGE allows you to perform INSERT, UPDATE, or
DELETE operations in a single statement based on conditions.
http://www.nityacloudtech.com/ @nityacloudtech
Nitya CloudTech Pvt Ltd.
It’s commonly used for handling data changes in data
warehousing.

5. What is a recursive CTE?


➢ Answer: A recursive CTE is a CTE that references itself. It's
useful for hierarchical data, such as organizational structures or
folder directories.
WITH RECURSIVE hierarchy AS (
SELECT employee_id, manager_id
FROM employees
WHERE manager_id IS NULL
UNION ALL
SELECT e.employee_id, e.manager_id
FROM employees e
http://www.nityacloudtech.com/ @nityacloudtech
Nitya CloudTech Pvt Ltd.
INNER JOIN hierarchy h ON e.manager_id
= h.employee_id
)
SELECT * FROM hierarchy;

6. What is the difference between CHAR and VARCHAR?


➢ Answer: CHAR is a fixed-length string, padding with spaces if
necessary, whereas VARCHAR is a variable-length string, storing
only the actual characters. VARCHAR is more space-efficient for
variable-length data.

http://www.nityacloudtech.com/ @nityacloudtech
Nitya CloudTech Pvt Ltd.
7. Explain ACID properties in SQL.
➢ Answer:
▪ Atomicity: Ensures that all operations within a transaction

are completed; if one fails, the transaction is aborted.


▪ Consistency: Guarantees data integrity by ensuring the

database remains valid after a transaction.


▪ Isolation: Ensures transactions are executed

independently.
▪ Durability: Ensures completed transactions are saved even

if the system crashes.

8. What is a materialized view, and how does it differ from a


regular view?
➢ Answer: A materialized view stores the query results physically
on disk, making data retrieval faster. Unlike regular views, it
doesn’t require re-running the query each time.

http://www.nityacloudtech.com/ @nityacloudtech
Nitya CloudTech Pvt Ltd.

9. What are ROLLBACK, COMMIT, and SAVEPOINT?


➢ Answer:
▪ COMMIT: Saves changes made by the transaction.

▪ ROLLBACK: Reverts changes made by the transaction.

▪ SAVEPOINT: Creates a checkpoint within a transaction

to roll back to if needed without rolling back the entire


transaction.

http://www.nityacloudtech.com/ @nityacloudtech
Nitya CloudTech Pvt Ltd.

10. What is a partitioned table, and why use it?


➢ Answer: Partitioning divides a large table into smaller,
manageable pieces, improving query performance by reducing
the data scanned. Common partitioning strategies are range, list,
and hash partitioning.

11. Explain the OVER clause and give an example of its use.
➢ Answer: The OVER clause defines a window for applying
window functions. For example:
SELECT employee_id, salary,
AVG(salary) OVER (PARTITION BY
department_id) AS avg_dept_salary
FROM employees;

This calculates the average salary for each department.

http://www.nityacloudtech.com/ @nityacloudtech
Nitya CloudTech Pvt Ltd.

12. What is database sharding?


➢ Answer: Sharding is a database architecture pattern that
partitions large datasets across multiple servers. It enhances
performance and allows horizontal scaling.

http://www.nityacloudtech.com/ @nityacloudtech
Nitya CloudTech Pvt Ltd.
13. How can you retrieve the current date and time in SQL?
➢ Answer: Use the CURRENT_TIMESTAMP function or its
variations like GETDATE() (SQL Server) or NOW() (MySQL).
14. How do you calculate the difference between two dates in
SQL?
➢ Answer:
SELECT DATEDIFF(day, start_date, end_date)
AS date_difference;

The function and syntax vary across database systems


(DATEDIFF, TIMESTAMPDIFF, etc.).

15. What is the purpose of indexing in SQL, and when should


you avoid it?
➢ Answer: Indexing speeds up data retrieval. Avoid over-
indexing or indexing frequently updated columns, as it can slow
down INSERT, UPDATE, and DELETE operations.

16. Explain UNIQUE constraint vs. PRIMARY KEY.


➢ Answer: Both enforce uniqueness, but a table can have multiple
UNIQUE constraints, while it can only have one PRIMARY
KEY, which also disallows nulls.

http://www.nityacloudtech.com/ @nityacloudtech
Nitya CloudTech Pvt Ltd.

17. How do you implement many-to-many relationships in


SQL?
➢ Answer: Use a junction (or associative) table with foreign keys
linking the two tables in a many-to-many relationship.

18. What is the purpose of COALESCE and NULLIF functions?


➢ Answer:
▪ COALESCE: Returns the first non-null value in a list of

arguments.
▪ NULLIF: Returns null if two expressions are equal;

otherwise, it returns the first expression.


19. How do you handle stored procedure error handling in
SQL?
➢ Answer: Use TRY...CATCH blocks (in SQL Server) or
DECLARE EXIT HANDLER (in MySQL) to catch and handle
errors in stored procedures.

http://www.nityacloudtech.com/ @nityacloudtech
Nitya CloudTech Pvt Ltd.

20. What is data denormalization, and when might it be used?


➢ Answer: Denormalization adds redundancy to speed up read
operations by combining tables. It’s useful in read-heavy
applications, particularly in OLAP (Online Analytical
Processing).

These questions expand on essential SQL topics, including database


design, data manipulation, indexing, transaction handling, and
optimization. Knowing these concepts in depth will help you tackle a
broad range of SQL interview questions confidently.

If you like the content, please consider supporting us by sharing it with


others who may benefit. Your support helps us continue creating valuable
resources!

http://www.nityacloudtech.com/ @nityacloudtech

You might also like