Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

SQL

Uploaded by

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

SQL

Uploaded by

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

Nitya CloudTech Pvt Ltd.

SQL Scenario-Based Interview


Questions & Answers

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

List of some of the most popular SQL interview questions along with
sample answers that are commonly asked for data-related positions like
Data Analyst, Business Analyst, or Data Engineer roles:
Basic to Intermediate Questions
1. What is SQL, and what are its main functions?
o Answer: SQL (Structured Query Language) is a standard

language for managing and manipulating relational databases.


Its main functions include:
▪ DDL (Data Definition Language): Defines database

structure (e.g., CREATE, ALTER, DROP).


▪ DML (Data Manipulation Language): Manages data in

tables (e.g., SELECT, INSERT, UPDATE, DELETE).


▪ DCL (Data Control Language): Controls data access

(e.g., GRANT, REVOKE).


▪ TCL (Transaction Control Language): Manages

transactions (e.g., COMMIT, ROLLBACK, SAVEPOINT).

http://www.nityacloudtech.com/ @nityacloudtech
Nitya CloudTech Pvt Ltd.
2. What is the difference between WHERE and HAVING clauses?
o Answer: WHERE filters rows before aggregation, whereas

HAVING filters aggregated data. Use WHERE with individual


rows and HAVING with groups created by GROUP BY.

3. How do you use the JOIN clause? Explain different types of


joins.
o Answer: Joins combine rows from two or more tables based on

a related column.
▪ INNER JOIN: Returns only matching rows from both

tables.
▪ LEFT JOIN (or LEFT OUTER JOIN): Returns all rows

from the left table and matched rows from the right table.
▪ RIGHT JOIN (or RIGHT OUTER JOIN): Returns all

rows from the right table and matched rows from the left
table.
▪ FULL JOIN (or FULL OUTER JOIN): Returns all rows

when there is a match in either left or right table.

http://www.nityacloudtech.com/ @nityacloudtech
Nitya CloudTech Pvt Ltd.
▪ CROSS JOIN: Produces a Cartesian product of the two
tables.

4. Explain the difference between DELETE, TRUNCATE, and DROP


statements.
o Answer:

▪ DELETE: Removes rows from a table based on a

condition. It can be rolled back.


▪ TRUNCATE: Removes all rows from a table without

logging individual row deletions. Cannot be rolled back.


▪ DROP: Deletes the entire table, including its structure. It

cannot be rolled back.

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

5. What is normalization? Explain its different forms.


o Answer: Normalization is the process of organizing data to

reduce redundancy. Forms include:


▪ 1NF: Ensures columns hold atomic values and each record

is unique.
▪ 2NF: Removes partial dependencies on the primary key.

▪ 3NF: Removes transitive dependencies on non-primary

attributes.
▪ BCNF: Stronger version of 3NF, requiring every

determinant to be a candidate key.

6. What is a primary key? Can it have null values?


o Answer: A primary key uniquely identifies each row in a table and
cannot have null values. It ensures data integrity by enforcing

http://www.nityacloudtech.com/ @nityacloudtech
Nitya CloudTech Pvt Ltd.
uniqueness.
7. What is a foreign key?
o Answer: A foreign key is a column or set of columns in a table

that establishes a link between data in two tables. It enforces


referential integrity by linking the foreign key in one table to the
primary key in another.

8. What is an index in SQL, and why is it used?


o Answer: An index is a database object that improves the speed

of data retrieval operations on a table. It is created on columns


that are frequently searched or filtered. However, indexes slow
down INSERT, UPDATE, and DELETE operations.

http://www.nityacloudtech.com/ @nityacloudtech
Nitya CloudTech Pvt Ltd.
9. Explain the GROUP BY clause and its use with aggregate
functions.
o Answer: GROUP BY groups rows sharing a property so

aggregate functions (like SUM, COUNT, AVG, MIN, MAX) can be


applied to each group. For example, to find the total sales per
product, you’d use GROUP BY on the product column.

10. What is a stored procedure?


o Answer: A stored procedure is a set of SQL statements saved in
the database. It can be reused and is typically used for
operations like batch processing, data validation, and business
logic encapsulation.

http://www.nityacloudtech.com/ @nityacloudtech
Nitya CloudTech Pvt Ltd.
Advanced Questions
11. What is the difference between UNION and UNION ALL?
o Answer: UNION combines results of two queries and removes
duplicates, whereas UNION ALL includes duplicates.

12. How do you optimize a SQL query?


o Answer: Common optimization techniques include:
▪ Creating indexes on frequently used columns in JOIN,

WHERE, and ORDER BY clauses.


▪ Using EXISTS instead of IN for subqueries.

▪ Avoiding SELECT *; specifying only required columns.

http://www.nityacloudtech.com/ @nityacloudtech
Nitya CloudTech Pvt Ltd.
▪ Analyzing the query execution plan to identify bottlenecks.

13. Explain EXISTS vs. IN.


o Answer: IN checks if a value exists within a specified set,
while EXISTS checks for the existence of rows in a subquery.
EXISTS is generally faster for subqueries.

14. What are window functions, and give an example?


o Answer: Window functions perform calculations across a set of
rows related to the current row. Examples include
ROW_NUMBER, RANK, LEAD, and LAG.
SELECT employee_id, department, salary,
RANK() OVER (PARTITION BY department
ORDER BY salary DESC) as rank
FROM employees;

This ranks employees’ salaries within each department.

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

15. What is a CTE (Common Table Expression)?


o Answer: A CTE is a temporary result set defined in a WITH
clause, used to simplify complex queries by breaking them into
modular parts.

http://www.nityacloudtech.com/ @nityacloudtech
Nitya CloudTech Pvt Ltd.
16. How do you handle NULL values in SQL?
o Answer: Use IS NULL or IS NOT NULL to filter rows with
null values. Functions like COALESCE and IFNULL help
replace nulls with specified values.

17. Explain the difference between RANK and DENSE_RANK


functions.
o Answer: Both are ranking functions. RANK assigns ranks with

gaps for ties, while DENSE_RANK assigns consecutive ranks


without gaps.

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

18. How do you calculate the nth highest salary in a table?


o Answer: You can use the LIMIT with offset or subquery.
SELECT salary
FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET (n - 1);

Or with a subquery:
SELECT salary FROM employees e1
WHERE (n - 1) = (
SELECT COUNT(DISTINCT salary) FROM
employees e2 WHERE e2.salary > e1.salary
);

http://www.nityacloudtech.com/ @nityacloudtech
Nitya CloudTech Pvt Ltd.
19. What are triggers in SQL?
o Answer: Triggers are automated actions executed before or
after events (INSERT, UPDATE, DELETE) on a table. They are
commonly used for auditing, data validation, and enforcing
business rules.

20. Explain the use of CASE statements.


o Answer: CASE statements allow conditional logic within SQL.
SELECT employee_id,
salary,
CASE
WHEN salary > 50000 THEN 'High'
WHEN salary BETWEEN 30000 AND
50000 THEN 'Medium'
ELSE 'Low'
END AS salary_range
FROM employees;

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

These questions cover core SQL concepts, advanced querying,


optimization techniques, and specific SQL functions that are frequently
discussed in interviews. Familiarizing yourself with these can help build
strong foundations for SQL-based roles.

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