SQL INTERVIEW QUESTIONS
SQL INTERVIEW QUESTIONS
SELECT column_name
FROM table_name
WHERE column_name IN (value1, value2, value3, ...);
SELECT *
FROM Employees
WHERE department_id IN (1, 2, 3);
BETWEEN Operator
Used to filter records within a specific range of values (inclusive of both boundaries).
Ideal when filtering data between two continuous values like dates, numbers, or
alphabets.
SELECT column_name
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
SELECT *
FROM Orders
WHERE order_date BETWEEN '2024-01-01' AND '2024-12-31';
2. Explain Joins
In SQL, a JOIN is used to combine rows from two or more tables based on a related column between
them. Joins are essential when you need to retrieve data from multiple tables in a relational database
that are related to each other through keys (such as primary and foreign keys).
An INNER JOIN returns only the rows where there is a match in both tables.
It's the most commonly used join type when you need to select records that have matching
values in both tables.
Example:
FROM orders
This query will return the order ID and customer name only for customers who have placed
orders.
A LEFT JOIN returns all records from the left table (the first table), and the matched records from
the right table (the second table). If there is no match, the result will contain NULL for columns
from the right table.
Use this when you want to include all records from the left table, even if there is no match in the
right table.
Example:
FROM customers
This query will return all customers, along with their order IDs if they have placed any. If a
customer has not placed an order, the order ID will be NULL.
A RIGHT JOIN is similar to a LEFT JOIN, but it returns all records from the right table, and the
matched records from the left table. If there is no match, the result will contain NULL for
columns from the left table.
Use this when you want to include all records from the right table, even if there is no match in
the left table.
Example:
FROM orders
This query will return all orders along with customer names. If there is no corresponding
customer for an order, the customer name will be NULL.
4. FULL JOIN (or FULL OUTER JOIN)
A FULL JOIN returns all records when there is a match in either left (first) or right (second) table.
If there is no match, the result will contain NULL values for columns from the table that does not
have the matching row.
Use this when you want to retrieve all records from both tables, regardless of whether there is a
match.
Example:
FROM customers
This query will return all customers and all orders, with NULL values where there is no match.
5. CROSS JOIN
A CROSS JOIN returns the Cartesian product of the two tables, meaning it returns all possible
combinations of rows from both tables.
This is used rarely, often for generating combinations or for cases where you need all possible
pairings between two sets of data.
Example:
FROM customers
This query will return every combination of customer and order, regardless of whether they are
related.
6. SELF JOIN
SELF JOIN is a join where a table is joined with itself. This can be useful for hierarchical or
recursive data.
Self joins are often used when you need to compare rows within the same table.
Example:
SELECT e1.employee_name AS Employee, e2.employee_name AS Manager
FROM employees e1
This query shows the employees along with their managers by joining the employees table with
itself.
Select TB.ID, TB.ProductName FROM Product_Details TB, Product_Details TB2 WHERE TB.AMOUNT <
TB2.AMOUNT;
LEFT JOIN All rows from the left table, and matching rows from the right table (NULL if no match).
RIGHT JOIN All rows from the right table, and matching rows from the left table (NULL if no match).
FULL JOIN All rows from both tables, with NULL where there is no match.
CROSS JOIN Cartesian product of both tables, returning every combination of rows.
SELF JOIN Joining the table with itself, often for hierarchical or recursive data relationships.
"An INNER JOIN only returns rows where there is a match in both tables. A LEFT JOIN returns all rows
from the left table, and the matched rows from the right table; if there is no match, NULL is returned for
the right table's columns."
"A RIGHT JOIN is used when you want to make sure that all records from the right table are included in
the result, regardless of whether they have a match in the left table."
"A Cartesian product occurs when every row in the first table is combined with every row in the second
table. For example, if the first table has 3 rows and the second table has 4 rows, the result will have 12
rows (3 x 4)."
"Yes, you can use multiple JOINs in a single query to combine more than two tables. The order of the
JOINs matters, and the logic of how they combine rows should be considered carefully."
3. Explain Normalization?
Normalization is the process of organizing data in a database to reduce redundancy and improve data
integrity. It involves dividing a database into smaller tables and defining relationships between them.
The main goal of normalization is to ensure that data is stored efficiently and consistently.
2. Ensures Data Integrity: Maintains consistency in the data by ensuring dependencies are properly
enforced.
3. Improves Query Performance: Simplifies database structure, making queries more efficient.
4. Simplifies Maintenance: Easier to update and maintain the database due to reduced complexity.
Normalization is achieved through a series of steps called normal forms (NF). Each normal form builds
upon the previous one. The most commonly used are:
Objective: Eliminate duplicate columns and ensure each column contains atomic (indivisible) values.
Rules:
2 Bob TV NULL
After Normalization:
1 Alice Laptop
1 Alice Phone
2 Bob TV
2. Second Normal Form (2NF):
Objective: Ensure that all non-key attributes are fully functionally dependent on the primary key. Partial
dependencies are removed.
Rules:
Non-key attributes should depend on the whole primary key, not just a part of it.
2 102 TV Bob
After Normalization:
1. Product Table:
ProductID ProductName
101 Laptop
102 TV
2. Order Table:
1 101 Alice
2 102 Bob
Objective: Remove transitive dependencies, where non-key attributes depend on other non-key
attributes.
Rules:
No transitive dependency (a non-key attribute should not depend on another non-key attribute).
1 John D01 HR
After Normalization:
1. Employee Table:
1 John D01
2 Sarah D02
2. Department Table:
DepartmentID DepartmentName
D01 HR
D02 Finance
Objective: A stricter version of 3NF, ensuring that every determinant is a candidate key.
Rule:
5NF: Deals with join dependencies and ensures no loss of data after decomposition.
Advantages of Normalization
Disadvantages of Normalization
1. Complex Queries: Data spread across multiple tables can require complex joins.
3. Storage Considerations: May require more metadata for keys and relationships.
The SQL commands DROP, DELETE, and TRUNCATE are used to remove data or database objects.
1. DROP
Definition: Removes the entire database object (e.g., table, database, or index) from the
database.
Operation: Deletes the structure of the table along with all its data.
Use Case: Use when you want to completely remove a table or database and no longer need it.
This will delete the employees table, including all its data and structure.
2. DELETE
(Without a WHERE clause, it deletes all rows but keeps the table structure.)
Use Case: Use when you want to delete specific records or all records from a table while
retaining the table structure.
Deletes all employees from the employees table where the department is HR.
3. TRUNCATE
Definition: Removes all rows from a table, but keeps the table structure intact.
Operation: Resets the table, clearing all data faster than DELETE.
Rollback: Not reversible in most databases (behaves like DROP in terms of recovery).
Use Case: Use when you want to quickly remove all rows from a table without logging individual
row deletions.
Deletes all rows in the employees table, but the table structure remains.
Key Differences
Fast (removes
Speed Slower (logs each row deletion). Faster (minimal logging).
everything).
Table
Deleted along with data. Retained. Retained.
Structure
When to Use Each
2. DELETE: Use when you need to remove specific rows or records based on a condition.
3. TRUNCATE: Use when you need to quickly remove all data from a table and reset it for reuse.
Answer: No, TRUNCATE removes all rows in the table and does not support a WHERE clause. Use DELETE
if you need to delete specific rows.
Answer: TRUNCATE is faster because it does not log individual row deletions, only the deallocation of
pages. DELETE logs each deleted row, making it slower.
Answer: In most databases, TRUNCATE cannot be rolled back as it does not log individual row deletions.
However, in some databases that support transactional DDL (e.g., SQL Server with an active transaction),
it may be possible.
Answer: Dropping a table also removes all constraints, indexes, and relationships associated with it.
SELECT *
FROM Employee
LIMIT 5;
SELECT *
FROM Employee
LIMIT 5 OFFSET 5;
✅ Explanation:
FROM employees
ORDER BY employee_id
LIMIT 1 OFFSET 5;
SELECT DepartmentID
FROM Employee
GROUP BY DepartmentID
ORDER BY AVG(Salary) DESC
LIMIT 1;
Write a query to find employees whose salaries are higher than their managers.
SELECT e.EmployeeID, e.Salary
FROM Employee e
JOIN Employee m ON e.ManagerID = m.EmployeeID
WHERE e.Salary > m.Salary;