Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views13 pages

SQL Guide

Uploaded by

Kundan rm619
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views13 pages

SQL Guide

Uploaded by

Kundan rm619
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

DEEPAK GOYAL

Founder & CEO


Azurelib.com
Connect on LinkedIn
Join WhatsApp Channel here

Welcome to the Ultimate SQL Interview Guide!

Whether you're preparing for your first job or aiming for a senior data role, SQL is a skill
that shows up in almost every data interview.
In this guide, I’ve compiled the most commonly asked SQL interview questions,
complete with clear explanations, examples, and real-world insights to help you
crack any interview with confidence.

This guide is structured to not just give you the what, but also the why behind each
answer, so you can answer with clarity, not just by heart.
Pro Tip: Don’t just read. Try these queries in your own SQL editor (MySQL, Postgres, or
Snowflake) to get hands-on comfort.

Don’t forget to follow this free stuff to Make Yourself Expert: Free Guide

1. What is the difference between WHERE and HAVING clause?

Answer:

 WHERE is used to filter rows before any grouping is done.

 HAVING is used to filter groups after a GROUP BY clause.


Example:

sql
CopyEdit

-- Filter individual records

SELECT * FROM Sales WHERE amount > 1000;

-- Filter aggregated groups

SELECT customer_id, SUM(amount) AS total

FROM Sales

Join WhatsApp Group for Free Material


DEEPAK GOYAL
Founder & CEO
Azurelib.com
Connect on LinkedIn
Join WhatsApp Channel here

GROUP BY customer_id

HAVING SUM(amount) > 5000;

✅ Use WHERE to filter raw data


✅ Use HAVING to filter grouped data

2. What is the difference between RANK() and DENSE_RANK()?

Answer:

 RANK() skips numbers after a tie.

 DENSE_RANK() does not skip any numbers.


Example:

sql

CopyEdit

SELECT name, marks,

RANK() OVER (ORDER BY marks DESC) AS rank,

DENSE_RANK() OVER (ORDER BY marks DESC) AS dense_rank


FROM Students;

Name Marks Rank Dense_Rank

A 95 1 1

B 95 1 1

C 90 3 2

D 85 4 3

3. What is the difference between INNER JOIN and LEFT JOIN?

Join WhatsApp Group for Free Material


DEEPAK GOYAL
Founder & CEO
Azurelib.com
Connect on LinkedIn
Join WhatsApp Channel here

Answer:

 INNER JOIN: Returns records that have matching values in both tables.

 LEFT JOIN: Returns all records from the left table, and matched records from
the right table (null if no match).
Example:

sql

CopyEdit

SELECT a.name, b.salary

FROM Employees a

INNER JOIN Salaries b ON a.id = b.emp_id;

SELECT a.name, b.salary

FROM Employees a

LEFT JOIN Salaries b ON a.id = b.emp_id;

4. How to find the second highest salary in SQL?

Answer:

sql

CopyEdit
SELECT MAX(salary) AS second_highest

FROM Employees

WHERE salary < (SELECT MAX(salary) FROM Employees);

Alternative using DISTINCT and LIMIT/OFFSET in MySQL:

sql

Join WhatsApp Group for Free Material


DEEPAK GOYAL
Founder & CEO
Azurelib.com
Connect on LinkedIn
Join WhatsApp Channel here

CopyEdit

SELECT DISTINCT salary

FROM Employees

ORDER BY salary DESC

LIMIT 1 OFFSET 1;

5. What are Aggregate functions in SQL? Name a few.

Answer:

Aggregate functions perform calculations on a set of values and return a single value.
Examples:

 SUM()

 AVG()

 MAX()

 MIN()

 COUNT()
Example:

sql

CopyEdit

SELECT COUNT(*) AS total_orders, AVG(amount) AS avg_amount

FROM Orders;

Join WhatsApp Group for Free Material


DEEPAK GOYAL
Founder & CEO
Azurelib.com
Connect on LinkedIn
Join WhatsApp Channel here

6. What is the difference between DELETE, TRUNCATE, and DROP?

Deletes Rollback Resets Removes Table


Command
Data Possible Identity Structure

DELETE ✅ ✅ ✅ ✅

TRUNCATE ✅ (All Rows) ✅ (in most DBs) ✅ ✅

DROP ✅ (All Rows) ✅ ✅ ✅

7. What is a Subquery? Types?

Answer:

A subquery is a query nested inside another SQL query.

Types:

 Scalar subquery: Returns single value

 Correlated subquery: Depends on outer query

 Nested subquery: Inside IN, EXISTS, ANY, etc.


Example:

sql

CopyEdit

SELECT name

FROM Employees
WHERE dept_id = (

SELECT id FROM Departments WHERE name = 'Finance'

);

Join WhatsApp Group for Free Material


DEEPAK GOYAL
Founder & CEO
Azurelib.com
Connect on LinkedIn
Join WhatsApp Channel here

8. What is the use of CASE WHEN in SQL?

Answer:

CASE WHEN is SQL’s way of using conditional logic.

Example:

sql

CopyEdit

SELECT name, salary,

CASE
WHEN salary > 100000 THEN 'High'

WHEN salary BETWEEN 50000 AND 100000 THEN 'Medium'

ELSE 'Low'

END AS salary_range

FROM Employees;

9. What are window functions in SQL?

Answer:

Window functions operate on a set of rows related to the current row without
collapsing the result.
Example:

sql

CopyEdit

SELECT name, department, salary,

AVG(salary) OVER (PARTITION BY department) AS dept_avg_salary

Join WhatsApp Group for Free Material


DEEPAK GOYAL
Founder & CEO
Azurelib.com
Connect on LinkedIn
Join WhatsApp Channel here

FROM Employees;

10. How to remove duplicate rows in SQL?

Answer:

Find duplicates:

sql

CopyEdit

SELECT name, COUNT(*)

FROM Employees
GROUP BY name

HAVING COUNT(*) > 1;

Delete duplicates (keeping the lowest id):

sql

CopyEdit

DELETE FROM Employees

WHERE id NOT IN (

SELECT MIN(id)

FROM Employees

GROUP BY name

);

11. What is a Primary Key? Can a table have multiple Primary Keys?

Join WhatsApp Group for Free Material


DEEPAK GOYAL
Founder & CEO
Azurelib.com
Connect on LinkedIn
Join WhatsApp Channel here

Answer:

A Primary Key uniquely identifies each record in a table.

 It cannot contain NULL values.

 It must be unique.

 A table can have only one primary key, but it can consist of multiple columns
(composite key).
Example:

sql

CopyEdit

CREATE TABLE Students (

student_id INT,

course_id INT,

PRIMARY KEY (student_id, course_id)

);

12. What is a Foreign Key?

Answer:

A Foreign Key creates a link between two tables. It enforces referential integrity.

 Points to a primary key in another table


 Ensures that values in one table match values in another
Example:

sql

CopyEdit

CREATE TABLE Orders (

Join WhatsApp Group for Free Material


DEEPAK GOYAL
Founder & CEO
Azurelib.com
Connect on LinkedIn
Join WhatsApp Channel here

order_id INT PRIMARY KEY,

customer_id INT,

FOREIGN KEY (customer_id) REFERENCES Customers(customer_id)

);

13. What is the difference between UNION and UNION ALL?

Answer:

 UNION combines results and removes duplicates.

 UNION ALL combines results with duplicates.


Example:

sql

CopyEdit

SELECT name FROM Employees_US

UNION

SELECT name FROM Employees_UK;

SELECT name FROM Employees_US

UNION ALL

SELECT name FROM Employees_UK;

14. What are Indexes in SQL and why are they important?

Answer:

Indexes are used to speed up search operations in a table.

Join WhatsApp Group for Free Material


DEEPAK GOYAL
Founder & CEO
Azurelib.com
Connect on LinkedIn
Join WhatsApp Channel here

 Work like an index in a book

 Improve performance for SELECT, WHERE, and JOIN


 May slow down INSERT/UPDATE/DELETE

Example:

sql

CopyEdit

CREATE INDEX idx_customer_name ON Customers(name);

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


Answer:

A CTE is a temporary result set used within a WITH clause that makes queries easier
to read and maintain.
Example:

sql

CopyEdit
WITH SalesByCustomer AS (

SELECT customer_id, SUM(amount) AS total

FROM Sales

GROUP BY customer_id
)

SELECT * FROM SalesByCustomer WHERE total > 10000;

16. What is the difference between IN, EXISTS, and JOIN?

Join WhatsApp Group for Free Material


DEEPAK GOYAL
Founder & CEO
Azurelib.com
Connect on LinkedIn
Join WhatsApp Channel here

Clause Description Performance

IN Checks if a value exists in a list Good for small lists

EXISTS Returns true if a subquery returns rows Better for correlated subqueries

JOIN Combines data from multiple tables Good for large datasets

17. What is normalization? What are its types?

Answer:

Normalization is the process of organizing data to reduce redundancy and improve


integrity.

Forms:

1. 1NF – Atomic columns (no arrays)

2. 2NF – Remove partial dependencies

3. 3NF – Remove transitive dependencies

Real-world: Helps maintain cleaner, scalable databases

18. How do you find duplicate records in a table?

Answer:

sql

CopyEdit

SELECT name, COUNT(*)


FROM Employees

GROUP BY name

HAVING COUNT(*) > 1;

Join WhatsApp Group for Free Material


DEEPAK GOYAL
Founder & CEO
Azurelib.com
Connect on LinkedIn
Join WhatsApp Channel here

You can delete duplicates using ROW_NUMBER():

sql

CopyEdit

WITH ranked AS (

SELECT *, ROW_NUMBER() OVER (PARTITION BY name ORDER BY id) AS rn

FROM Employees

DELETE FROM ranked WHERE rn > 1;

19. What is the use of COALESCE() function?

Answer:

COALESCE() returns the first non-null value from a list.

Example:

sql

CopyEdit

SELECT name, COALESCE(phone, 'Not Provided') AS contact

FROM Customers;

20. What is the difference between CHAR, VARCHAR, and TEXT?

Data Type Description Use Case

CHAR(n) Fixed length Always reserves n bytes

VARCHAR(n) Variable length Uses only required space

Join WhatsApp Group for Free Material


DEEPAK GOYAL
Founder & CEO
Azurelib.com
Connect on LinkedIn
Join WhatsApp Channel here

Data Type Description Use Case

TEXT For large text Limited string operations

Thanks for going through this SQL Interview Guide!

Remember interviews are not just about memorizing syntax; they’re about solving
problems, explaining logic, and showing confidence.

Revise this guide before every interview, and you’ll start seeing common patterns and
repeated questions.

✅ Feel free to share this guide with your friends or colleagues who might benefit. Let’s
grow together
Deepak Goyal
Founder, AzureLib Academy
Helping IT professionals transform into cloud data engineers

Join WhatsApp Group for Free Material

You might also like