SQL Interview Questions - 1
SQL Interview Questions - 1
Intermediate Level
26. What is Subquery?
27. What is Nested Query?
28. What is Correlated Subquery?
29. What is Group By in SQL?
30. What is the difference between Group By and Order By?
31. What is the use of LIMIT in SQL?
32. How to find the Second Highest Salary in SQL?
33. How to find Duplicate Records in a table?
34. What is CTE (Common Table Expression)?
35. What is Temporary Table in SQL?
36. What is Window Function in SQL?
What is the difference between ROW_NUMBER(), RANK(), and
37.
DENSE_RANK()?
38. What is CASE Statement in SQL?
39. What is COALESCE in SQL?
40. What is NVL Function in SQL?
41. What is Indexing in SQL?
42. What is Clustered Index?
43. What is Non-Clustered Index?
44. What is the difference between Clustered and Non-Clustered Index?
45. What is View in SQL?
46. What is the difference between View and Table?
47. What is Stored Procedure?
48. What is the difference between Function and Stored Procedure?
49. What is Trigger in SQL?
50. What is Cursor in SQL?
Created by: Vinay Kumar Panika
Advanced Level
51. What is the ACID Property in SQL?
52. What is a Transaction in SQL?
53. What is the difference between COMMIT and ROLLBACK?
54. What is Savepoint in SQL?
55. What is the difference between IN and EXISTS?
56. What is the difference between DELETE and TRUNCATE?
57. What is Index Fragmentation?
58. What is the difference between RANK() and DENSE_RANK()?
59. How to fetch common records from two tables?
60. What is the difference between UNION and JOIN?
61. What is Pivot Table in SQL?
62. What is Case Sensitivity in SQL?
63. How to find the Nth Highest Salary?
64. How to get First 3 Maximum Salaries?
65. What is the difference between Drop, Delete, and Truncate?
66. How to calculate Age from Date of Birth in SQL?
67. What is Recursive Query in SQL?
68. What is the difference between Temporary Table and CTE?
69. How to find Odd and Even records in SQL?
70. What is JSON in SQL?
71. What is XML in SQL?
72. How to handle NULL values in SQL?
73. What is Dynamic SQL?
74. How to calculate Percentage in SQL?
75. How to find the Employees who earn more than their Manager?
Created by: Vinay Kumar Panika
Real-Time Scenarios
76. How to find Duplicate Emails in the Employee Table?
88. How to fetch common records from two tables without JOIN?
90. How to find the Department with the highest Employee Count?
Created by: Vinay Kumar Panika
Optimization Techniques
91. How to Optimize SQL Queries?
Here, EmployeeID is the primary key that uniquely identifies each employee.
Here, DepartmentID in the Employees table is a foreign key that references the
DepartmentID column in the Departments table.
Created by: Vinay Kumar Panika
Here, the Email column has a UNIQUE constraint, ensuring no two employees can have the
same email address.
In this example, the Name column cannot have NULL values, while the Email column can
accept NULL values.
Removes
specific rows
based on a Yes (with
Slow (Row-by-
DELETE condition COMMIT/RO No
row deletion)
using the LLBACK)
WHERE
clause
Removes all
rows from the Faster than
TRUNCATE No No
table without DELETE
a condition
Deletes the
entire table Yes (Removes
DROP No Fastest
including data table structure)
and structure
Example:
Created by: Vinay Kumar Panika
Filter Execution
Clause Purpose Used With
Type Order
SELECT,
Filters rows before Row-level Applied before
WHERE UPDATE,
grouping filter GROUP BY
DELETE
Example:
In the WHERE clause, filtering is applied before grouping, while HAVING filters the aggregated
result.
Created by: Vinay Kumar Panika
Example:
This query returns the employee names along with their department names where the
DepartmentID is common in both tables.
Created by: Vinay Kumar Panika
Example:
This query returns all employee names from the Employees table, and if the department is not
assigned, the Department Name will be displayed as NULL.
Example:
This query returns all department names from the Departments table, and if no employee is
assigned to a department, the Employee Name will be displayed as NULL.
Created by: Vinay Kumar Panika
Example:
This query returns all employee names and department names, including those where there is no
matching DepartmentID between the two tables.
Example:
In this example, the Employees table joins with itself to show the employee's name along with their
manager's name based on the ManagerID column.
Created by: Vinay Kumar Panika
Example:
This query returns all possible combinations of Employees and Departments, with every employee
paired with every department.
Automatically sorts the result set Does not sort the result set
Syntax:
Created by: Vinay Kumar Panika
Example (UNION):
Used when duplicate data is not Used when duplicate data needs
Usage required to be preserved
Employee
EmployeeID Department DepartmentLocation
Name
101 Vinay 1
102 Awadhesh 1
Created by: Vinay Kumar Panika
Department Table:
1 IT Bangalore
101 Vinay 1
102 Awadhesh 2
Department Table:
DepartmentID DepartmentName
1 IT
2 HR
Created by: Vinay Kumar Panika
Denormalized Table:
101 Vinay IT
102 Awadhesh HR
In this example, the DepartmentName column is directly stored in the Employee table,
making data retrieval faster but increasing redundancy.
Always uses the specified Uses only the space required for
Memory
length, even if fewer characters the actual data plus one or two
Usage are stored bytes for length
Performance Faster for fixed-size data Slower for varying data sizes
Example:
In this example, Name will always take 10 bytes, while Address will use only the necessary
space based on the actual data length.
Created by: Vinay Kumar Panika
Example:
SQL:
MySQL:
MySQL stores the Employees table and processes the SQL query to retrieve data.
Output:
1 Vinay 50000
2 Awadhesh 40000
Intermediate Level
26. What is Subquery?
A Subquery is a query nested inside another query in SQL.
It is used to fetch data that will be used by the main query as a condition to filter or
manipulate the result.
Key Points:
Also called Inner Query or Nested Query.
Always executes before the main query.
The result of the subquery is used by the Outer Query.
Can be used with SELECT, INSERT, UPDATE, or DELETE statements.
Syntax:
Types of Subqueries:
1. Single Row Subquery: Returns only one value.
Example:
Explanation: It selects employees whose salary is higher than the average salary
of their own department.
Created by: Vinay Kumar Panika
Example:
Find employees who work in the 'Sales' department.
Explanation:
The inner query finds the department_id for the 'Sales' department.
The outer query selects employees based on that department_id.
Explanation:
The inner query calculates the average salary of each department.
The outer query checks if the employee's salary is higher than their department's average
salary.
Example:
Find the total sales amount for each product category.
Conclusion:
The GROUP BY clause is essential for data summarization and helps in analyzing data patterns
efficiently.
Created by: Vinay Kumar Panika
GROUP BY ORDER BY
Used to group rows based on the same values Used to sort the result set in ascending or
in one or more columns. descending order.
Syntax: Comes before ORDER BY. Syntax: Always comes after GROUP BY.
Groups the result into summary rows. Sorts the entire result set.
Conclusion:
GROUP BY is used to group data and perform aggregate calculations.
ORDER BY is used to sort the final result.
Created by: Vinay Kumar Panika
Example:
Fetch top 3 highest salaries:
Conclusion:
LIMIT helps to fetch limited data and is commonly used for Top N records or
Pagination.
Explanation:
The inner query gets the highest salary.
The outer query finds the highest salary below the top salary.
Explanation:
GROUP BY groups the same names.
HAVING COUNT(*) > 1 filters only duplicate names.
Created by: Vinay Kumar Panika
Example:
Find employees who work in the IT department and have a salary greater than 50000.
Explanation:
The CTE IT_Employees selects all employees from the IT department.
The SELECT query fetches employees with salary above 50000.
Example:
Create a temporary table to store employees with salary above 50000.
Explanation:
The table holds filtered data temporarily.
It is automatically deleted after the session ends.
3. DENSE_RANK()
Similar to RANK(), but does not skip ranks for duplicate values.
Syntax:
4. NTILE(n)
Divides the result set into n equal parts and assigns a group number to each row.
Syntax:
5. SUM()
Calculates the cumulative total of a column within a partition.
Syntax:
6. AVG()
Calculates the average value of a column within a partition.
Syntax:
8. LEAD()
Returns the next row's value in the result set.
Syntax:
Created by: Vinay Kumar Panika
9. LAG()
Returns the previous row's value in the result set.
Syntax:
Summary Table:
Skips
Function Purpose Duplicates Example Usage
Numbers
Conclusion:
Window Functions help in performing complex calculations across result sets without
grouping them into a single row, making them essential for ranking, running totals, and
trend analysis.
Created by: Vinay Kumar Panika
Key Differences:
Skips
Function Purpose Duplicates Example
Numbers
Example:
Created by: Vinay Kumar Panika
39. What is COALESCE in SQL?
COALESCE returns the first non-null value from a list of expressions.
Syntax:
Example:
Key Points:
Returns the first non-null value.
Used to handle NULL values.
Can accept multiple expressions.
40. What is NVL Function in SQL?
NVL function replaces NULL values with a specified value.
Syntax:
Example:
Example:
Key Points:
Speeds up SELECT queries.
Automatically maintained by the database.
Can be created on one or more columns.
Slows down INSERT, UPDATE, DELETE operations.
Created by: Vinay Kumar Panika
42. What is Clustered Index in SQL?
A Clustered Index sorts and stores the data physically in the table based on the indexed
column.
Syntax:
Example:
Key Points:
Only one clustered index is allowed per table.
Faster for data retrieval.
Automatically created on Primary Key by default.
Rearranges table rows physically.
Example:
Key Points:
Multiple Non-Clustered Indexes can be created on a table.
Improves search performance.
Does not affect the physical order of data.
Stores pointers to the actual data.
44. Difference between Clustered and Non-Clustered Index
Clustered Index Non-Clustered Index
Example:
Key Points:
Does not store data physically.
Simplifies complex queries.
Provides data security.
Can be used like a table in SELECT queries.
View Table
Syntax:
Example:
Key Points:
Improves code reusability.
Increases performance.
Supports input and output parameters.
Provides security by hiding SQL code.
48. What is the difference between Function and Stored
Procedure?
Example:
Key Points:
Automatically executes on INSERT, UPDATE, or DELETE.
Used for data validation and logging.
Cannot be called manually.
Improves data integrity.
Key Points:
Used to process row-by-row results.
Slower than set-based operations.
Helps in complex data manipulation.
Not recommended for large datasets.
Created by: Vinay Kumar Panika
Advanced Level
51. What is the ACID Property in SQL?
The ACID properties in SQL define the key principles to ensure that database transactions
are processed reliably without affecting data integrity.
ACID Stands for:
Property Description
Example:
Explanation:
1. Atomicity: If one of the two queries fails, both updates will be rolled back.
2. Consistency: The total amount in both accounts will remain the same.
3. Isolation: If another transaction is trying to access the same account, it will
wait until this transaction completes.
4. Durability: After COMMIT, changes will be saved permanently even in case
of a power failure.
Created by: Vinay Kumar Panika
52. What is a Transaction in SQL?
A Transaction in SQL is a group of SQL operations that are executed as a single unit to
perform a specific task on the database.
It follows ACID Properties to maintain data integrity.
Key ACID Properties:
Property Description
The database must remain in a valid state before and after the
Consistency
transaction.
Durability Once committed, changes are permanent even after system failure.
Transaction Commands:
Command Description
Example:
Created by: Vinay Kumar Panika
Transactions in SQL
COMMIT ROLLBACK
Once executed, changes cannot be undone. Restores the database to its previous state.
Example:
Created by: Vinay Kumar Panika
54. What is Savepoint in SQL?
Savepoint in SQL is used to temporarily save a transaction at a specific point, allowing you
to rollback only part of the transaction without affecting the entire transaction.
Key Points:
Allows setting multiple points in a transaction.
Helps in partial rollback.
Improves error handling.
Used with ROLLBACK.
Syntax:
Explanation:
The first update will be saved.
The second update will be rolled back.
Remaining changes will be committed.
IN EXISTS
DELETE TRUNCATE
Removes specific rows based on a Removes all rows from the table without
condition using the WHERE clause. any condition.
Maintains table structure and identity Resets identity column values to the initial
column values. seed.
Conclusion:
Index fragmentation slows down query performance and should be fixed regularly to
maintain database efficiency.
Created by: Vinay Kumar Panika
58. What is the difference between RANK() and
DENSE_RANK()?
RANK() DENSE_RANK()
Assigns a unique rank to each row, Assigns a unique rank to each row
but skips the next rank if there are without skipping ranks if there are
duplicate values. duplicate values.
UNION JOIN
Combines result sets vertically (rows) from two Combines result sets horizontally (columns) based
or more tables. on common columns.
Conclusion:
Pivot Tables help to summarize large datasets and present them in a structured format.
Key Points:
Column Names: Most databases are not case-sensitive (MySQL, SQL Server).
Table Names: Case sensitivity depends on the database and operating system.
String Values: By default, MySQL is case-insensitive for string comparisons.
Example in MySQL:
Created by: Vinay Kumar Panika
Output:
EmpID Name
101 Vinay
102 VINAY
Explanation:
ORDER BY Salary DESC → Sorts the salaries in descending order.
OFFSET 2 → Skips the top 2 salaries.
LIMIT 1 → Selects the next salary as the 3rd highest.
Created by: Vinay Kumar Panika
Method 2: Using Subquery with LIMIT
Conclusion:
Use LIMIT with OFFSET for faster results in MySQL.
This method is commonly asked in interviews.
Always use DISTINCT to remove duplicate salaries.
Explanation:
DISTINCT → Removes duplicate salaries.
ORDER BY Salary DESC → Sorts salaries in descending order.
LIMIT 3 → Fetches the top 3 salaries.
Created by: Vinay Kumar Panika
Removes all
TRUNCAT No (Keeps
rows from the No Fast No
E structure)
table
Conclusion:
Use DELETE for removing specific rows with conditions.
Use TRUNCATE for removing all rows quickly without rollback.
Use DROP to delete both data and table structure completely.
66. How to calculate Age from Date of Birth in SQL?
You can calculate the Age from the Date of Birth using the DATEDIFF() or YEAR()
functions depending on the database.
Method 1: Using DATEDIFF() (MySQL)
Explanation:
CURDATE() → Returns the current date.
DATEDIFF() → Calculates the difference between the current date and date
of birth in days.
FLOOR() → Converts the result into whole years.
Created by: Vinay Kumar Panika
Method 2: Using YEAR() (MySQL)
Explanation:
This method calculates the difference between the current year and the birth year.
Conclusion:
Use DATEDIFF() for accurate age calculation.
Use YEAR() for simple year-based age calculation.
Recursive Query:
Created by: Vinay Kumar Panika
Can be used multiple times within a session. Can be used only once in the same query.
Conclusion:
Use Temporary Tables when data needs to be reused multiple times.
Use CTE for short-term data manipulation and improved readability.
69. How to find Odd and Even records in SQL?
You can find Odd and Even records in SQL using the MOD() or
ROW_NUMBER() functions.
Method 1: Using MOD() Function (MySQL)
Explanation:
MOD(EmpID, 2) → Returns the remainder when EmpID is divided by 2.
If the remainder is 0, the record is Even.
If the remainder is 1, the record is Odd.
Method 2: Using ROW_NUMBER() (SQL Server, PostgreSQL)
Created by: Vinay Kumar Panika
Conclusion:
Use MOD() for databases like MySQL.
Use ROW_NUMBER() for databases that support Window Functions.
70. What is JSON in SQL?
JSON (JavaScript Object Notation) in SQL is used to store, retrieve, and manipulate data in
a structured, text-based format within relational databases.
Key Points:
Stores data in key-value pairs.
Lightweight and easy to read.
Commonly used for semi-structured data.
Supported in MySQL, SQL Server, and PostgreSQL.
MySQL Example:
Create Table with JSON Column:
Conclusion:
JSON helps to handle semi-structured data within relational databases without the need for
separate NoSQL databases.
Conclusion:
XML is used to store and transfer hierarchical data in relational databases, making it easier
to exchange data between applications.
Conclusion:
Use COALESCE() or IFNULL() to replace NULL values and ensure data consistency in
queries.
Created by: Vinay Kumar Panika
Conclusion:
Dynamic SQL provides flexibility in query execution but should always be used with
parameterized queries to prevent SQL injection attacks.
Query:
Output:
Conclusion: Use Self Join with a condition to compare employee salaries against their
managers' salaries. This query helps in hierarchical data analysis.
Created by: Vinay Kumar Panika
Real-Time Scenarios
76. How to find Duplicate Emails in the Employee Table?
To find duplicate emails in SQL, you can use the GROUP BY clause with the HAVING
condition.
Syntax:
Explanation:
GROUP BY groups the records based on the Email column.
COUNT(Email) counts how many times each email appears.
HAVING COUNT(Email) > 1 filters only those emails that have more than one
occurrence.
Explanation:
LIKE 'A%' → Finds names that start with 'A'.
% → Represents any number of characters after 'A'.
84. How to fetch Alternate Rows from a table?
You can fetch Alternate Rows using the MOD() or ROW_NUMBER() functions based on
row position.
Method 1: Using MOD() (MySQL)
Fetch Even Rows:
Conclusion:
Use ORDER BY with LIMIT for simple queries.
Use Subqueries or ALL for advanced scenarios.
This method works in all RDBMS.
88. How to fetch common records from two tables without
JOIN?
You can fetch common records from two tables without using JOIN by using the IN or
INTERSECT operators.
Conclusion:
Use ROW_NUMBER() for advanced databases.
Use GROUP BY for simple queries.
Always backup data before deleting duplicates.
Created by: Vinay Kumar Panika
Conclusion:
Use GROUP BY with ORDER BY for a simple approach.
Use Subqueries for better performance with large datasets.
Created by: Vinay Kumar Panika
Optimization Techniques
91. How to Optimize SQL Queries?
Optimizing SQL queries improves performance and execution speed while handling large
datasets.
Best Practices to Optimize SQL Queries:
1. Use Indexes: Create indexes on columns used in WHERE, JOIN, and
ORDER BY clauses.
5. Use LIMIT or TOP: Fetch only required rows using LIMIT or TOP.
Use:
SQL Server:
3. Use Joins Efficiently: Prefer INNER JOIN over OUTER JOIN when
possible.
4. Use WHERE Instead of HAVING: Filter rows early using WHERE.
Use:
Key Points:
Improves query performance on large datasets.
Simplifies data management.
Helps in faster data retrieval.
Each partition is stored separately.
Data can be partitioned by range, list, hash, or composite methods.
Types of Partitioning:
1. Range Partitioning – Divides data based on value ranges.
2. List Partitioning – Divides data based on specific column values.
3. Hash Partitioning – Distributes data evenly using a hash function.
4. Composite Partitioning – Combination of Range and Hash partitioning.
Created by: Vinay Kumar Panika
Example Query:
6. Proper Indexing:
Use Indexes to minimize the number of rows locked.
7. Break Large Transactions:
Split large transactions into smaller batches.
Example:
Without Deadlock Prevention:
Conclusion:
Follow consistent table access patterns, minimize lock times, and use
proper indexing to avoid deadlocks in SQL transactions.
Can return multiple values. Returns only one value (scalar or table).
Can call Functions inside it. Cannot call Stored Procedures inside it.
Focuses on data consistency and speed. Focuses on data aggregation and analysis.
SQL handles NULL values through the use of constraints and explicit SQL functions. The NOT NULL constraint ensures that specific columns cannot store NULL values, enforcing data integrity . Using COALESCE or ISNULL functions can replace NULLs with specified default values during retrieval . Conditional statements such as WHERE clauses can explicitly check for NULL using IS NULL or IS NOT NULL to filter datasets correctly . When performing aggregate functions, one must account for NULLs to avoid misleading results, often using COALESCE to handle them appropriately. By integrating these methods, SQL maintains integrity while querying datasets inclusive of potential NULL values.
To optimize SQL query performance, it is crucial to create indexes on columns used frequently in WHERE, JOIN, and ORDER BY clauses to speed up data retrieval . Selecting only necessary columns instead of using SELECT * reduces data transfer . Use INNER JOIN instead of OUTER JOIN when possible to minimize data processing . Implement the EXISTS clause instead of IN for improved performance with large datasets as it stops processing after finding the first match . Partitioning tables can enhance performance by dividing large tables into smaller, more manageable pieces .
JOIN operations in SQL combine data from two or more tables based on related columns. INNER JOIN returns only matching rows between the tables, ignoring non-matching rows . LEFT JOIN returns all records from the left table and matched rows from the right table, filling NULLs for unmatched rows on the right . RIGHT JOIN is the opposite, returning all rows from the right table, with NULLs for unmatched rows from the left . FULL JOIN combines the results of both LEFT and RIGHT JOIN, returning all records with NULLs where there are no matches . Each join type serves different purposes based on how comprehensive the result set needs to be versus how precise the relational match should be.
DELETE removes specific rows based on a condition and can be rolled back if within a transaction. It is slower due to row-level logging and does not affect the table structure . TRUNCATE removes all rows without conditions, cannot be rolled back, is faster than DELETE as it does not log each row, and also resets identity column values . DROP deletes the entire table including its structure, is the fastest option, and cannot be rolled back . Use DELETE for selective row removal, TRUNCATE to quickly clear a table while keeping its structure, and DROP to completely remove a table along with its data and structure.
Self joins allow a table to join with itself to compare different rows within the same table. In the case of comparing employee salaries with their managers, a self join is used to connect the Employee table to itself by matching the manager's ID in one instance to the employee's ID in another instance. This way, SQL can directly compare salaries from the same table where each row represents both an employee and their manager based on the relational conditions .
The Default Constraint in SQL specifies a default value for a column when no value is provided during insertion. This ensures that a predetermined value populates the column to circumvent NULLs being stored, which helps maintain consistent data integrity . Notably, it eliminates the need for explicit value provision during every insertion, reducing errors and streamlining data entry processes by automatically filling fields with standard values where data might be missing.
The ROW_NUMBER() function assigns a unique sequential integer to rows within a partition, which allows for the selection of alternate rows by filtering where its result divided by two yields a remainder of zero or one. In practical scenarios, this is useful for pagination, where alternating row selection can simulate 'page flips' between even or odd-numbered pages or for distributing workload evenly across resources based on ID allocation . This function provides flexibility in accessing structured sequences, aiding in dynamic data retrieval processes.
Without using the MAX() function, finding the highest salary in each department can be accomplished by using a subquery that orders salaries in descending order and then selects the top one per department for each entry. This can be achieved by utilizing ROW_NUMBER() partitioned by department and ordered by salary DESC, filtering where the row number is 1 . Alternatively, a self-join could eliminate all less-than-maximum salaries per department through comparative conditions, allowing non-MAX functions to indirectly facilitate the task.
SQL can identify duplicate records by using the GROUP BY clause combined with HAVING to count how many times each record appears. In the scenario of duplicate emails within the Employee table, GROUP BY groups records by the Email column, and HAVING COUNT(Email) > 1 filters out groups with more than one occurrence, effectively identifying duplicates . Managing these duplicates can involve using DELETE with CTE for precise removal or retaining the necessary unique record and removing others, ensuring database integrity by eliminating redundancy.
Indexes in SQL optimize query performance by allowing faster data retrieval through more efficient data sorting and direct pointer usage to access specific rows, greatly speeding up SELECT operations, especially on large datasets . However, excessive indexing can lead to performance degradation in data modification operations like INSERT, DELETE, and UPDATE due to overhead from maintaining multiple indexes. Additionally, over-indexing can lead to increased storage requirements and index fragmentation, potentially offsetting performance gains if not carefully managed . Thus, balancing index use is essential for optimal database performance.