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

SQL_Interview_Questions

The document provides a comprehensive overview of SQL concepts, including its importance in data analytics, various types of joins, and aggregate functions. It also covers intermediate and advanced SQL topics such as subqueries, window functions, and performance optimization techniques. Additionally, it explains the differences between key SQL operations like TRUNCATE, DELETE, and DROP.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

SQL_Interview_Questions

The document provides a comprehensive overview of SQL concepts, including its importance in data analytics, various types of joins, and aggregate functions. It also covers intermediate and advanced SQL topics such as subqueries, window functions, and performance optimization techniques. Additionally, it explains the differences between key SQL operations like TRUNCATE, DELETE, and DROP.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

SQL Interview Questions and Answers

1. What is SQL, and why is it important in data analytics?

SQL (Structured Query Language) is used to manage and manipulate databases. It is important in
data analytics as it allows data extraction, transformation, and analysis efficiently.

2. Difference between INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN

- INNER JOIN: Returns matching rows from both tables.


- LEFT JOIN: Returns all rows from the left table and matching rows from the right.
- RIGHT JOIN: Returns all rows from the right table and matching rows from the left.
- FULL OUTER JOIN: Returns all rows from both tables, with NULLs where there is no match.

3. Difference between WHERE and HAVING

WHERE filters rows before grouping, while HAVING filters groups after aggregation.

4. How to use GROUP BY and HAVING in a query?

Example:
SELECT department, COUNT(*) AS num_employees FROM employees
GROUP BY department HAVING COUNT(*) > 5;

5. Query to find duplicate records in a table

SELECT column_name, COUNT(*) FROM table_name


GROUP BY column_name HAVING COUNT(*) > 1;

6. Retrieve unique values from a table

Use DISTINCT:
SELECT DISTINCT column_name FROM table_name;

7. Aggregate functions: COUNT(), SUM(), AVG(), MIN(), MAX()

- COUNT(): Counts records.


- SUM(): Adds numeric values.
- AVG(): Computes average.
- MIN()/MAX(): Finds minimum/maximum values.

8. Purpose of DISTINCT keyword

Removes duplicate values from query results.

Intermediate SQL:

1. Query to find second-highest salary

SELECT DISTINCT salary FROM employees ORDER BY salary DESC LIMIT 1 OFFSET 1;

2. What are subqueries?

A subquery is a query within another query.


Example:
SELECT name FROM employees WHERE salary = (SELECT MAX(salary) FROM employees);

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

CTEs improve query readability and recursion.


Example:
WITH dept_salary AS (SELECT department, AVG(salary) AS avg_salary FROM employees
GROUP BY department)
SELECT * FROM dept_salary WHERE avg_salary > 50000;

4. Window functions: ROW_NUMBER(), RANK(), DENSE_RANK()

- ROW_NUMBER(): Assigns unique row numbers.


- RANK(): Skips ranks for duplicates.
- DENSE_RANK(): No rank gaps for duplicates.

5. UNION vs UNION ALL

- UNION: Combines results, removes duplicates.


- UNION ALL: Combines results, keeps duplicates.

6. What are indexes and how they improve performance?


Indexes speed up data retrieval by creating efficient lookup structures.

7. Query to calculate total sales per month

SELECT MONTH(order_date) AS month, SUM(sales) FROM orders GROUP BY


MONTH(order_date);

Advanced SQL:

1. Optimizing slow-running SQL queries

- Use indexes.
- Avoid SELECT *.
- Optimize joins and subqueries.
- Use EXPLAIN PLAN.

2. Views in SQL

A view is a virtual table based on a query.

3. Difference between stored procedure and function

- Stored Procedure: Executes multiple statements, returns nothing or multiple results.


- Function: Returns a single value, used in SQL expressions.

4. Difference between TRUNCATE, DELETE, and DROP

- TRUNCATE: Deletes all rows, resets identity.


- DELETE: Deletes specific rows with WHERE.
- DROP: Deletes table permanently.

5. Windowing functions and analytics

Used for running totals, rankings, moving averages.

6. Using PARTITION BY and ORDER BY in window functions

Example:
SELECT name, department, salary, RANK() OVER (PARTITION BY department ORDER BY salary
DESC) AS rank FROM employees;

7. Handling NULL values with COALESCE, ISNULL

- COALESCE: Returns first non-null value.


- ISNULL: Replaces NULL with default value.

You might also like