SQL Top 100 Interview Questions
SQL Top 100 Interview Questions
1. What is SQL?
2. What is a database?
A database is an organized collection of data stored electronically, typically structured in tables with
rows and columns. It is managed by a database management system (DBMS), which allows for
efficient storage, retrieval, and manipulation of data.
• CHAR: Fixed-length storage. If the defined length is not fully used, it is padded with spaces.
• VARCHAR2: Variable-length storage. Only the actual data is stored, saving space when the
full length is not needed.
A primary key is a unique identifier for each record in a table. It ensures that no two rows have the
same value in the primary key column(s), and it does not allow NULL values.
A foreign key is a column (or set of columns) in one table that refers to the primary key in another
table. It establishes and enforces a relationship between the two tables, ensuring data integrity.
The DEFAULT constraint assigns a default value to a column when no value is provided during
an INSERT operation. This helps maintain consistent data and simplifies data entry.
Normalization is the process of organizing data in a database to reduce redundancy and improve
data integrity. This involves dividing large tables into smaller, related tables and defining
relationships between them to ensure consistency and avoid anomalies.
Denormalization is the process of combining normalized tables into larger tables for performance
reasons. It is used when complex queries and joins slow down data retrieval, and the performance
benefits outweigh the drawbacks of redundancy.
SQL 100 Interview questions
10. What is a query in SQL?
A query is a SQL statement used to retrieve, update, or manipulate data in a database. The most
common type of query is a SELECT statement, which fetches data from one or more tables based on
specified conditions.
• Arithmetic Operators: +, -, *, /, %
A view is a virtual table created by a SELECT query. It does not store data itself, but presents data
from one or more tables in a structured way. Views simplify complex queries, improve readability,
and enhance security by restricting access to specific rows or columns.
The UNIQUE constraint ensures that all values in a column (or combination of columns) are distinct.
This prevents duplicate values and helps maintain data integrity.
• INNER JOIN: Returns rows that have matching values in both tables.
• LEFT JOIN (LEFT OUTER JOIN): Returns all rows from the left table, and matching rows from
the right table.
• RIGHT JOIN (RIGHT OUTER JOIN): Returns all rows from the right table, and matching rows
from the left table.
• FULL JOIN (FULL OUTER JOIN): Returns all rows when there is a match in either table.
15. What is the difference between INNER JOIN and OUTER JOIN?
• INNER JOIN: Returns only rows where there is a match in both tables.
• OUTER JOIN: Returns all rows from one table (LEFT, RIGHT, or FULL), and the matching rows
from the other table. If there is no match, NULL values are returned for the non-matching
side.
The GROUP BY clause is used to arrange identical data into groups. It is typically used with aggregate
functions (such as COUNT, SUM, AVG) to perform calculations on each group rather than on the
entire dataset.
A subquery is a query nested within another query. It is often used in the WHERE clause to filter data
based on the results of another query, making it easier to handle complex conditions.
19. What is the difference between the WHERE and HAVING clauses?
• HAVING: Filters grouped data after the GROUP BY clause has been applied.
In short, WHERE applies to individual rows, while HAVING applies to groups.
Indexes are database objects that improve query performance by allowing faster retrieval of rows.
They function like a book’s index, making it quicker to find specific data without scanning the entire
table. However, indexes require additional storage and can slightly slow down data
modification operations.
• DELETE: Removes rows one at a time and records each deletion in the transaction log,
allowing rollback. It can have a WHERE clause.
• TRUNCATE: Removes all rows at once without logging individual row deletions. It cannot
have a WHERE clause and is faster than DELETE for large data sets.
The ORDER BY clause sorts the result set of a query in either ascending (default) or descending
order, based on one or more columns. This helps present the data in a more meaningful or readable
sequence.
23. What are the differences between SQL and NoSQL databases?
• SQL Databases:
• NoSQL Databases:
SQL 100 Interview questions
o Use flexible, schema-less structures (e.g., key-value pairs, document stores).
A table is a structured collection of related data organized into rows and columns. Columns define
the type of data stored, while rows contain individual records.
• FOREIGN KEY: Ensures referential integrity by linking to a primary key in another table.
A cursor is a database object used to retrieve, manipulate, and traverse through rows in a result set
one row at a time. Cursors are helpful when performing operations that must be processed
sequentially rather than in a set-based manner.
A trigger is a set of SQL statements that automatically execute in response to certain events on a
table, such as INSERT, UPDATE, or DELETE. Triggers help maintain data consistency, enforce business
rules, and implement complex integrity constraints.
The SELECT statement retrieves data from one or more tables. It is the most commonly used
command in SQL, allowing users to filter, sort, and display data based on specific criteria.
NULL represents a missing or unknown value. It is different from zero or an empty string. NULL
values indicate that the data is not available or applicable.
A stored procedure is a precompiled set of SQL statements stored in the database. It can take input
parameters, perform logic and queries, and return output values or result sets. Stored procedures
improve performance and maintainability by centralizing business logic.
These commands are used to define and modify the structure of database objects such
as tables, indexes, and views. For example, the CREATE command creates a new table,
the ALTER command modifies an existing table, and the DROP command removes a table
entirely. DDL commands primarily focus on the schema or structure of the database.
Example:
These commands deal with the actual data stored within database objects. For instance,
the INSERT command adds rows of data to a table, the UPDATE command modifies existing data,
and the DELETE command removes rows from a table. In short, DML commands allow you to query
and manipulate the data itself rather than the structure.
Example:
The ALTER command is used to modify the structure of an existing database object. This command is
essential for adapting our database schema as requirements evolve.
A composite primary key is a primary key made up of two or more columns. Together, these columns
must form a unique combination for each row in the table. It’s used when a single column isn’t
sufficient to uniquely identify a record.
Example:
Consider an Orders table where OrderID and ProductID together uniquely identify each record
because multiple orders might include the same product, but not within the same order.
SQL 100 Interview questions
CREATE TABLE OrderDetails (
OrderID INT,
ProductID INT,
Quantity INT,
PRIMARY KEY (OrderID, ProductID)
);
Data integrity refers to the accuracy, consistency, and reliability of the data stored in the database.
SQL databases maintain data integrity through several mechanisms:
• Constraints: Ensuring that certain conditions are always met. For example, NOT
NULL ensures a column cannot have missing values, FOREIGN KEY ensures a valid
relationship between tables, and UNIQUE ensures no duplicate values.
• Transactions: Ensuring that a series of operations either all succeed or all fail, preserving
data consistency.
• Normalization: Organizing data into multiple related tables to minimize redundancy and
prevent anomalies.
These measures collectively ensure that the data remains reliable and meaningful over time.
• Improved Performance: Stored procedures are precompiled and cached in the database,
making their execution faster than sending multiple individual queries.
• Reduced Network Traffic: By executing complex logic on the server, fewer round trips
between the application and database are needed.
• Enhanced Security: Stored procedures can restrict direct access to underlying tables,
allowing users to execute only authorized operations.
• Reusability and Maintenance: Once a procedure is written, it can be reused across multiple
applications. If business logic changes, you only need to update the stored procedure, not
every application that uses it.
The UNION operator combines the result sets of two or more SELECT queries into a single result set,
removing duplicate rows. This is useful when we need a consolidated view of data from multiple
tables or queries that have similar structure.
Example:
• UNION ALL: Includes all rows from each query, including duplicates.
Example:
The CASE statement is SQL’s way of implementing conditional logic in queries. It evaluates conditions
and returns a value based on the first condition that evaluates to true. If no condition is met, it can
return a default value using the ELSE clause.
Example:
SELECT ID,
CASE
WHEN Salary > 100000 THEN 'High'
WHEN Salary BETWEEN 50000 AND 100000 THEN 'Medium'
ELSE 'Low'
END AS SalaryLevel
FROM Employees;
Scalar functions operate on individual values and return a single value as a result. They are often
used for formatting or converting data. Common examples include:
Example:
The COALESCE function returns the first non-NULL value from a list of expressions. It’s commonly
used to provide default values or handle missing data gracefully.
Example:
41. What are the differences between SQL’s COUNT() and SUM() functions?
Example:
42. What is the difference between the NVL and NVL2 functions?
Example:
• RANK(): Assigns a rank to each row, with gaps if there are ties.
Example:
If two employees have the same salary, they get the same rank, but RANK() will skip a number for the
next rank, while DENSE_RANK() will not.
• RANK(): Assigns the same number to tied rows and leaves gaps for subsequent ranks.
Example:
A CTE is a temporary result set defined within a query. It improves query readability and can be
referenced multiple times.
Example:
WITH TopSalaries AS (
SELECT Name, Salary
SQL 100 Interview questions
FROM Employees
WHERE Salary > 50000
)
SELECT * FROM TopSalaries WHERE Name LIKE 'A%';
46. What are window functions, and how are they used?
Window functions perform calculations across a set of rows that are related to the current row.
Unlike aggregate functions, they don’t collapse the result set.
1. Index
• An index is a database object created to speed up data retrieval. It stores a sorted reference
to table data, which helps the database engine find rows more quickly than scanning the
entire table.
• Example: A non-unique index on a column like LastName allows quick lookups of rows where
the last name matches a specific value.
2. Key
• A key is a logical concept that enforces rules for uniqueness or relationships in the data.
• For instance, a PRIMARY KEY uniquely identifies each row in a table and ensures that no
duplicate or NULL values exist in the key column(s).
• A FOREIGN KEY maintains referential integrity by linking rows in one table to rows in
another.
Indexing allows the database to locate and access the rows corresponding to a query condition much
faster than scanning the entire table. Instead of reading each row sequentially, the database uses the
index to jump directly to the relevant data pages. This reduces the number of disk I/O
operations and speeds up query execution, especially for large tables.
Example:
The index on LastName lets the database quickly find all rows matching ‘Smith’ without scanning
every record.
Advantages
• Faster query performance, especially for SELECT queries with WHERE clauses, JOIN
conditions, or ORDER BY clauses.
SQL 100 Interview questions
• Improved sorting and filtering efficiency.
Disadvantages:
• Additional overhead for write operations (INSERT, UPDATE, DELETE), as indexes must be
updated whenever the underlying data changes.
• Potentially slower bulk data loads or batch inserts due to the need to maintain index
integrity.
In short, indexes make read operations faster but can slow down write operations and
increase storage requirements.
1. Clustered Index:
• Organizes the physical data in the table itself in the order of the indexed column(s).
• Example: If EmployeeID is the clustered index, the rows in the table are stored physically
sorted by EmployeeID.
2. Non-Clustered Index:
• Maintains a separate structure that contains a reference (or pointer) to the physical data in
the table.
• Useful for specific query conditions that aren’t related to the primary ordering of the data.
• Example: A non-clustered index on LastName allows fast lookups by last name even if the
table is sorted by another column.
51. What are temporary tables, and how are they used?
Temporary tables are tables that exist only for the duration of a session or a transaction. They are
useful for storing intermediate results, simplifying complex queries, or performing operations on
subsets of data without modifying the main tables.
Example:
52. What is a materialized view, and how does it differ from a standard view?
• Standard View:
o Does not store data; the underlying query is executed each time the view is
referenced.
• Materialized View:
o materialized view is used to store aggregated sales data, updated nightly, for fast
reporting.
A sequence is a database object that generates a series of unique numeric values. It’s often used to
produce unique identifiers for primary keys or other columns requiring sequential values.
Example:
54. What are the advantages of using sequences over identity columns?
1. Greater Flexibility:
2. Dynamic Adjustment: Can alter the sequence without modifying the table structure.
3. Cross-Table Consistency: Use a single sequence for multiple related tables to ensure unique
identifiers across them.
In short, sequences offer more control and reusability than identity columns.
• PRIMARY KEY: Combines NOT NULL and UNIQUE, guaranteeing that each row is uniquely
identifiable.
• FOREIGN KEY: Ensures referential integrity by requiring values in one table to match primary
key values in another.
• CHECK: Validates that values meet specific criteria (e.g., CHECK (Salary > 0)).
By automatically enforcing these rules, constraints maintain data reliability and consistency.
56. What is the difference between a local and a global temporary table?
Example:
The MERGE statement combines multiple operations INSERT, UPDATE, and DELETE into one. It is
used to synchronize two tables by:
Example:
58. How can you handle duplicates in a query without using DISTINCT?
WITH CTE AS (
SELECT Column1, Column2, ROW_NUMBER() OVER (PARTITION BY Column1 ORDER BY Column2)
AS RowNum
FROM TableName
)
SELECT * FROM CTE WHERE RowNum = 1;
A correlated subquery is a subquery that references columns from the outer query. It is re-executed
for each row processed by the outer query. This makes it more dynamic, but potentially less efficient.
Example:
SELECT Name,
(SELECT COUNT(*)
FROM Orders
WHERE Orders.CustomerID = Customers.CustomerID) AS OrderCount
FROM Customers;
60. What are partitioned tables, and when should we use them?
Partitioned tables divide data into smaller, more manageable segments based on a column’s value
(e.g., date or region). Each partition is stored separately, making queries that target a specific
partition more efficient. It is used when
• Scenarios where queries frequently filter on partitioned columns (e.g., year, region).
• To improve maintenance operations, such as archiving older partitions without affecting the
rest of the table.
This section covers complex SQL topics, including performance tuning, complex indexing
strategies, transaction isolation levels, and advanced query optimization techniques. By tackling
these challenging questions, we’ll gain a deeper understanding of SQL, preparing us for senior-level
roles and technical interviews.
1. Atomicity:
• A transaction is treated as a single unit of work, meaning all operations must succeed or fail
as a whole.
• If any part of the transaction fails, the entire transaction is rolled back.
2. Consistency:
• A transaction must take the database from one valid state to another, maintaining all defined
rules and constraints.
3. Isolation:
• Even if multiple transactions occur simultaneously, each must operate as if it were the only
one in the system until it is complete.
4. Durability:
• Once a transaction is committed, its changes must persist, even in the event of a system
failure.
• This ensures the data remains stable after the transaction is successfully completed.
Isolation levels define the extent to which the operations in one transaction are isolated from those
in other transactions. They are critical for managing concurrency and ensuring data integrity.
Common isolation levels include:
1. Read Uncommitted:
• Can result in dirty reads, where a transaction reads data that might later be rolled back.
2. Read Committed:
• Prevents dirty reads but does not protect against non-repeatable reads or phantom reads.
3. Repeatable Read:
• Ensures that if a transaction reads a row, that row cannot change until the transaction is
complete.
• Prevents dirty reads and non-repeatable reads but not phantom reads.
4. Serializable:
• Prevents dirty reads, non-repeatable reads, and phantom reads, but may introduce
performance overhead due to locking and reduced concurrency.
63. What is the purpose of the WITH (NOLOCK) hint in SQL Server?
• The WITH (NOLOCK) hint allows a query to read data without acquiring shared locks,
effectively reading uncommitted data.
• It can improve performance by reducing contention for locks, especially on large tables that
are frequently updated.
• Results may be inconsistent or unreliable, as the data read might change or be rolled back.
Example:
SELECT *
FROM Orders WITH (NOLOCK);
This query fetches data from the Orders table without waiting for other transactions to release their
locks.
Deadlocks occur when two or more transactions hold resources that the other transactions need,
resulting in a cycle of dependency that prevents progress. Strategies to handle deadlocks include:
• Many database systems have mechanisms to detect deadlocks and terminate one of the
transactions to break the cycle.
• The terminated transaction can be retried after the other transactions complete.
• Use indexes and optimized queries to minimize the duration and scope of locks.
• Conversely, higher isolation levels (like Serializable) may ensure a predictable order of
operations, reducing deadlock risk.
• Ensure that transactions acquire resources in the same order to prevent cyclical
dependencies.
• Backup and recovery: Snapshots can serve as a point-in-time recovery source if changes
need to be reversed.
• Testing: Providing a stable dataset for testing purposes without the risk of modifying the
original data.
Example:
66. What are the differences between OLTP and OLAP systems?
• Handles large volumes of simple transactions (e.g., order entry, inventory updates).
67. What is a live lock, and how does it differ from a deadlock?
1. Live Lock
• Occurs when two or more transactions keep responding to each other’s changes, but no
progress is made.
• Unlike a deadlock, the transactions are not blocked; they are actively running, but they
cannot complete.
2. Deadlock
• Occurs when transactions are blocked waiting for each other to release locks.
Example:
Use Case:
• To verify that certain data exists in one dataset but not in another.
Performance Considerations:
• EXCEPT works best when the datasets involved have appropriate indexing and when the
result sets are relatively small.
• Large datasets without indexes may cause slower performance because the database has to
compare each row.
69. How do you implement dynamic SQL, and what are its advantages and risks?
Dynamic SQL is SQL code that is constructed and executed at runtime rather than being fully defined
and static. In SQL Server: Use sp_executesql or EXEC. In other databases: Concatenate query strings
and execute them using the respective command for the database platform.
Syntax:
Advantages:
• Flexibility: Dynamic SQL can adapt to different conditions, tables, or columns that are only
known at runtime.
Risks:
• SQL Injection Vulnerabilities: If user input is not sanitized, attackers can inject malicious SQL
code.
• Performance Overhead: Because dynamic SQL is constructed at runtime, it may not benefit
from cached execution plans, leading to slower performance.
Partitioning is a database technique used to divide data into smaller, more manageable pieces.
SQL 100 Interview questions
• Horizontal Partitioning:
o Divides the rows of a table into multiple partitions based on values in a specific
column.
o Use Case: When dealing with large datasets, horizontal partitioning can improve
performance by limiting the number of rows scanned for a query.
• Vertical Partitioning:
o Example: Storing infrequently accessed columns (e.g., large text or binary fields) in a
separate table or partition.
• Key Difference:
71. What are the considerations for indexing very large tables?
1. Indexing Strategy:
• Focus on the most frequently queried columns or those involved in JOIN and WHERE
conditions.
2. Index Types:
• Use clustered indexes for primary key lookups and range queries.
• Use non-clustered indexes for filtering, ordering, and covering specific queries.
3. Partitioned Indexes:
• If the table is partitioned, consider creating local indexes for each partition. This improves
manageability and can speed up queries targeting specific partitions.
4. Maintenance Overhead:
• Index rebuilding and updating can be resource-intensive. Plan for regular index maintenance
during off-peak hours.
6. Indexing large tables requires a careful approach to ensure that performance gains from faster
queries outweigh the costs of increased storage and maintenance effort.
1. Sharding
• Sharding involves splitting a database into multiple smaller, independent databases (shards).
Each shard operates on a subset of the overall data and can be hosted on separate servers.
• Sharding is a horizontal scaling strategy that distributes data across multiple databases,
typically to handle massive data volumes and high traffic.
• Purpose: Horizontal scaling to handle large volumes of data and high query loads.
• Example: A global user database might be divided into shards by region, such as a shard for
North America, Europe, and Asia.
• Key Benefit: Each shard can be queried independently, reducing the load on any single
server.
2. Partitioning
• Partitioning splits a single table into smaller, logical pieces, usually within the same database.
• Purpose: Improve query performance by reducing the amount of data scanned, and simplify
maintenance tasks such as archiving or purging old data.
• Example: A sales table could be partitioned by year so that queries targeting recent sales do
not need to scan historical data.
73. What are the best practices for writing optimized SQL queries?
• Apply WHERE clauses as early as possible to reduce the amount of data processed.
3. **Avoid SELECT *:
• Retrieve only the columns needed. This reduces I/O and improves performance.
• Use execution plans to identify bottlenecks, missing indexes, or inefficient query patterns.
• Choose INNER JOIN, LEFT JOIN, or OUTER JOIN based on the data relationships and
performance requirements.
• Instead of a single monolithic query, use temporary tables or CTEs to process data in stages.
8. Optimize Aggregations:
• Continuously analyze query performance and fine-tune as data volumes grow or usage
patterns change.
Review the execution plan of queries to understand how the database is retrieving data, which
indexes are being used, and where potential bottlenecks exist.
Identify where queries are waiting, such as on locks, I/O, or CPU, to pinpoint the cause of slowdowns.
• SQL Server: Use Query Store, DMVs (Dynamic Management Views), and performance
dashboards.
• Monitor key performance metrics (query duration, IOPS, CPU usage) and set thresholds.
• Regularly revisit and tune queries as data grows or application requirements change.
1. Indexing
• Advantages:
o Speeds up read operations and improves query performance without changing the
data structure.
o Consider indexing when you need faster lookups without altering the data model.
• Disadvantages:
2. Denormalization
• Advantages:
o Can improve performance for read-heavy workloads where complex joins are
frequent.
• Disadvantages:
SQL handles recursive queries using Common Table Expressions (CTEs). A recursive CTE repeatedly
references itself to process hierarchical or tree-structured data.
Key Components:
• Recursive Member: A query that references the CTE to continue building the result set.
• Termination Condition: Ensures that recursion stops after a certain depth or condition is
met.
Example:
77. What are the differences between transactional and analytical queries?
1. Transactional Queries:
2. Analytical Queries:
3. Key Differences:
78. How can you ensure data consistency across distributed databases?
1. Use Distributed Transactions: Implement two-phase commit (2PC) to ensure all participating
databases commit changes simultaneously or roll back if any part fails.
2. Implement Eventual Consistency: If strong consistency isn’t required, allow data to become
consistent over time. This approach is common in distributed systems where high availability is a
priority.
3. Conflict Resolution Mechanisms: Use versioning, timestamps, or conflict detection rules to resolve
inconsistencies.
4. Data Replication and Synchronization: Use reliable replication strategies to ensure that changes
made in one database are propagated to others.
5. Regular Audits and Validation: Periodically verify that data remains consistent across databases
and fix discrepancies as needed.
The PIVOT operator transforms rows into columns, making it easier to summarize or rearrange data
for reporting.
Example:
SQL 100 Interview questions
Converting a dataset that lists monthly sales into a format that displays each month as a separate
column.
80. What is a bitmap index, and how does it differ from a B-tree index?
1. Bitmap Index:
• Represents data with bitmaps (arrays of bits) to indicate the presence or absence of a value
in each row.
• Can perform fast logical operations (AND, OR, NOT) on multiple columns simultaneously.
2. B-tree Index:
• Suitable for high-cardinality columns (e.g., unique identifiers, large ranges of values).
3. Key Difference:
• Bitmap indexes excel with low-cardinality data and complex boolean conditions.
• B-tree indexes are better for unique or high-cardinality data and range queries.
This section is dedicated to questions that focus on writing and understanding SQL queries. By
practicing these examples, we’ll learn how to retrieve, manipulate, and analyze data effectively,
building the problem-solving skills needed for real-world scenarios.
Explanation:
This query identifies the second-highest salary by selecting the maximum salary that is less than the
overall highest salary. The subquery determines the top salary, while the outer query finds the next
highest value.
82. Write a query to retrieve employees who earn more than the average salary.
SQL 100 Interview questions
SELECT *
FROM Employee
WHERE Salary > (SELECT AVG(Salary) FROM Employee);
Explanation:
This query fetches details of employees whose salary exceeds the average salary. The subquery
calculates the average salary, and the main query filters rows based on that result.
83. Write a query to fetch the duplicate values from a column in a table.
Explanation:
The query uses GROUP BY to group identical values and HAVING COUNT(*) > 1 to identify values that
appear more than once in the specified column.
84. Write a query to find the employees who joined in the last 30 days.
SELECT *
FROM Employee
WHERE JoiningDate > DATE_SUB(CURDATE(), INTERVAL 30 DAY);
Explanation:
By comparing the JoiningDate to the current date minus 30 days, this query retrieves all employees
who joined within the last month.
SELECT *
FROM Employee
ORDER BY Salary DESC
LIMIT 3;
Explanation:
The query sorts employees by salary in descending order and uses LIMIT 3 to return only the top
three earners.
86. Write a query to delete duplicate rows in a table without using the ROWID keyword.
Explanation:
SQL 100 Interview questions
This query retains only one row for each set of duplicates by keeping the row with the
smallest EmployeeID. It identifies duplicates using GROUP BY and removes rows not matching the
minimum ID.
SELECT *
FROM TableA
INNER JOIN TableB ON TableA.ID = TableB.ID;
Explanation:
An INNER JOIN is used to find rows present in both tables by matching a common column (in this
case, ID).
88. Write a query to fetch employees whose names start and end with ‘A’.
SELECT *
FROM Employee
WHERE Name LIKE 'A%' AND Name LIKE '%A';
Explanation:
The query uses LIKE with wildcard characters to filter rows where the Name column starts and ends
with the letter ‘A’.
89. Write a query to display all departments along with the number of employees in each.
Explanation:
By grouping employees by their DepartmentID and counting rows in each group, the query produces
a list of departments along with the employee count.
SELECT *
FROM Employee
WHERE ManagerID IS NULL;
Explanation:
This query selects employees whose ManagerID column is NULL, indicating they don’t report to a
manager.
91. Write a query to fetch the 3rd and 4th highest salaries.
SELECT Salary
FROM Employee
ORDER BY Salary DESC
LIMIT 2 OFFSET 2;
Explanation:
SQL 100 Interview questions
Using ORDER BY and the combination of LIMIT and OFFSET, the query skips the top two salaries and
retrieves the next two highest.
SELECT
MAX(CASE WHEN ColumnName = 'Condition1' THEN Value END) AS Column1,
MAX(CASE WHEN ColumnName = 'Condition2' THEN Value END) AS Column2
FROM TableName;
Explanation:
This query converts specific row values into columns using conditional aggregation with CASE. Each
column’s value is determined based on a condition applied to rows.
93. Write a query to fetch records updated within the last hour.
SELECT *
FROM TableName
WHERE UpdatedAt >= NOW() - INTERVAL 1 HOUR;
Explanation:
By comparing the UpdatedAt timestamp to the current time minus one hour, the query retrieves
rows updated in the last 60 minutes.
94. Write a query to list employees in departments that have fewer than 5 employees.
SELECT *
FROM Employee
WHERE DepartmentID IN (
SELECT DepartmentID
FROM Employee
GROUP BY DepartmentID
HAVING COUNT(*) < 5
);
Explanation:
The subquery counts employees in each department, and the main query uses those results to find
employees working in departments with fewer than 5 members.
SELECT CASE
WHEN EXISTS (SELECT * FROM TableName) THEN 'Has Records'
ELSE 'No Records'
END AS Status;
Explanation:
The query uses EXISTS to determine if any rows exist in the table, returning a status of ‘Has Records’
or ‘No Records’ based on the result.
96. Write a query to find employees whose salaries are higher than their managers.
SQL 100 Interview questions
SELECT e.EmployeeID, e.Salary
FROM Employee e
JOIN Employee m ON e.ManagerID = m.EmployeeID
WHERE e.Salary > m.Salary;
Explanation:
This query joins the Employee table with itself to compare employee salaries to their respective
managers’ salaries, selecting those who earn more.
SELECT *
FROM Employee
WHERE MOD(RowID, 2) = 0;
Explanation:
By applying a modulo operation on a unique identifier (RowID), the query returns rows with even
IDs, effectively fetching every other row.
98. Write a query to find departments with the highest average salary.
SELECT DepartmentID
FROM Employee
GROUP BY DepartmentID
ORDER BY AVG(Salary) DESC
LIMIT 1;
Explanation:
Grouping by DepartmentID and ordering by the average salary in descending order, the query returns
the department with the highest average.
SELECT *
FROM Employee
LIMIT 1 OFFSET (n-1);
Explanation:
Using LIMIT 1 OFFSET (n-1), this query retrieves a single row corresponding to the specified position
(nth) in the ordered result set.
100. Write a query to find employees hired in the same month of any year.
SELECT *
FROM Employee
WHERE MONTH(JoiningDate) = MONTH(CURDATE());
Explanation:
By comparing the month of JoiningDate to the current month, the query selects all employees who
were hired in that month regardless of the year.
SQL 100 Interview questions