SQL - Advanced Interview Questions
SQL - Advanced Interview Questions
Interview Questions
(Practice Project)
Basic
Answer:
Normalization is a database design process that organizes data into related tables to reduce redundancy and
improve data integrity. It involves structuring data according to specific rules, such as ensuring that each table
has unique records and that attributes are fully dependent on primary keys.
What is an Entity-Relationship (ER) diagram, and what is its purpose in database design?
Answer:
An Entity-Relationship (ER) diagram is a visual representation of the entities, relationships, and attributes within
a database. It serves as a blueprint for designing and structuring the database.
Key Components:
Entities: Objects or concepts, like "Customer" or "Order," that represent data stored in the database.
Visual Planning: Helps in planning the database structure by showing how entities relate to each other.
Foundation for Development: Provides a foundation for creating the actual database schema, ensuring a well-
organized and efficient database
Answer:
A primary key is a column (or a set of columns) in a database table that uniquely identifies each row within that
table. The primary key ensures that no duplicate entries exist and that each record can be uniquely identified. It
automatically enforces uniqueness and creates an index on the primary key column(s). For example, in a table
of employees, the EmployeeID could be the primary key because it uniquely identifies each employee
What is the difference between the SELECT and SELECT DISTINCT statements?
Answer:
The SELECT statement is used to retrieve all matching records from a database table based on the specified
criteria. It returns all rows, including duplicates.
PW Skills
The SELECT DISTINCT statement, on the other hand, is used to return only unique rows, removing any duplicate
records from the result set. For example, SELECT DISTINCT Country FROM Customers; would return a list of unique
countries without duplicates.
PW Skills
What is a foreign key in SQL?
Answer:
A foreign key is a column or a set of columns in one table that uniquely identifies rows in another table. It
creates a link between the two tables, enforcing referential integrity. The foreign key in the child table points to
the primary key in the parent table, ensuring that the value in the foreign key column must match an existing
value in the parent table's primary key. For example, in a table of Orders, the CustomerID might be a foreign key
referencing the CustomerID primary key in the Customers table
Answer:
The DELETE statement is used to remove specific rows from a table based on a condition. You can use the
WHERE clause with DELETE to specify which records should be removed. It can be rolled back if used within a
transaction.
The TRUNCATE statement, on the other hand, removes all rows from a table, effectively resetting the table to its
empty state. Unlike DELETE, TRUNCATE is usually faster and does not generate individual row delete operations. It
cannot be rolled back in many databases, and it resets any auto-increment counters associated with the table
What is the difference between the GROUP BY and HAVING clauses in SQL?
Answer:
The GROUP BY clause is used to group rows that have the same values in specified columns into summary rows.
It is typically used with aggregate functions like COUNT, SUM, AVG, etc., to produce summary results. For
example, GROUP BY Department would group employees by their department.
The HAVING clause is used to filter the results of a GROUP BY query. While the WHERE clause filters rows before
they are grouped, the HAVING clause filters groups after they have been formed. For example, HAVING COUNT(*)
> 5 would filter out groups (e.g., departments) that have 5 or fewer employees.
Intermediate
What is a temporary table in SQL, and how does it differ from a regular table?
Answer:
A temporary table in SQL is a special type of table that is created and used within a specific session or
transaction. It is automatically deleted when the session ends or when explicitly dropped.
Temporary tables are useful for handling data that is needed only temporarily, without cluttering the database
with permanent tables.
PW Skills
PW Skills
What are stored procedures, and how do they differ from regular SQL queries?
Answer:
Stored procedures are precompiled collections of SQL statements that can be executed as a single unit. They
are stored in the database and can include control-flow logic, variables, and error handling.
Precompiled: Stored procedures are compiled once and can be executed multiple times, whereas regular
SQL queries are compiled and executed each time they are run
Reusability: Stored procedures can be reused with different parameters, while regular SQL queries are
typically written and executed for specific tasks
Performance: Stored procedures often perform better than regular queries because they are precompiled
and optimized by the database engine
Functionality: Stored procedures can include complex logic, such as loops and conditional statements,
which are not possible with regular SQL queries.
Stored procedures are valuable for encapsulating complex logic, improving performance, and promoting code
reuse in database operations
Explain the difference between input and output parameters in stored procedures.
Answer:
In stored procedures, input and output parameters are used to pass values into and out of the procedure.
Input Parameters
Purpose: Input parameters allow you to pass data into the stored procedure
Behavior: The values of input parameters are provided by the caller when the procedure is executed. These
values are used within the procedure but do not change after the procedure runs
Example: A procedure might take a customer ID as an input parameter to retrieve specific customer details.
Output Parameters
Purpose: Output parameters allow the stored procedure to return data back to the caller
Behavior: The procedure modifies the value of the output parameter during execution, and this modified
value is returned to the caller
Example: A procedure might calculate a total price and return it through an output parameter.
Key Difference:
Direction of Data Flow: Input parameters bring data into the procedure, while output parameters send data
back to the caller
What are User-Defined Functions (UDFs) in SQL, and how do they differ from stored procedures?
Answer:
User-Defined Functions (UDFs) in SQL are custom functions created by users to perform specific tasks and
return a value or a table. They are similar to built-in functions like SUM() or AVG(), but they are defined by the
user.
PW Skills
Differences from Stored Procedures
Return Type
UDFs: Always return a value, either a single scalar value (e.g., integer, string) or a table
Stored Procedures: May or may not return a value. They often perform operations that don't necessarily
produce a direct result, like updating a table
Usage in Queries
UDFs: Can be used directly in SQL statements (e.g., SELECT, WHERE, JOIN) like any other function
Stored Procedures: Cannot be used directly in SQL statements; they must be executed using EXEC or CALL
commands
Side Effects
UDFs: Typically do not allow modifications to the database (e.g., inserting, updating, or deleting records)
Stored Procedures: Can perform any kind of database modification
Complexity
UDFs: Designed for specific, often simple tasks, like calculations or formatting data
Stored Procedures: Can handle more complex logic, including multiple SQL statements, transaction
control, and error handling.
Summary:
UDFs are primarily used to return a value and can be embedded in SQL queries, whereas stored procedures are
more versatile, allowing complex operations and database modifications but are executed independently
Explain the difference between Scalar Functions and Table-Valued Functions (TVFs).
Answer:
Scalar Functions and Table-Valued Functions (TVFs) are both types of User-Defined Functions (UDFs) in SQL, but
they differ in the type of data they return and how they are used.
Scalar Functions
Return Type: Scalar functions return a single value, such as an integer, string, or date
Usage: They can be used wherever a single value is expected, such as in a SELECT list, WHERE clause, or
ORDER BY clause
Example: A scalar function might return the square of a number or format a date string.
Example:
RETURNS INT
AS
BEGIN
END;
PW Skills
Table-Valued Functions (TVFs)
Return Type: TVFs return a table, which can be treated like any other table or view in SQL
Usage: They can be used in SELECT statements, JOIN operations, or as part of a query that requires a set of
rows
Example: A TVF might return a list of customers based on specific criteria.
Example:
RETURNS TABLE
AS
RETURN
FROM Customers
);
Key Differences
Answer:
Aggregate functions and scalar functions in SQL both perform operations on data, but they differ in how they
process and return results.
Aggregate Functions
Purpose: Aggregate functions perform calculations on a set of values and return a single value summarizing
that set
Scope: They work on multiple rows of data and return a summary result
Examples: Common aggregate functions include SUM(), AVG(), COUNT(), MIN(), and MAX()
Usage: Typically used in SELECT statements with GROUP BY clauses to summarize data across multiple rows.
Scalar Functions
Purpose: Scalar functions perform operations on a single value and return a single result
Scope: They operate on individual data points rather than a set of rows
Examples: Examples of scalar functions include ABS(), ROUND(), UPPER(), and user-defined scalar functions
Usage: Used in SELECT statements, WHERE clauses, or anywhere a single value is needed.
PW Skills
PW Skills
Example: SELECT UPPER(FirstName) FROM Employees;
Key Differences
What are the key differences between a view and a table in SQL?
Answer:
In SQL, views and tables are both used to store and retrieve data, but they have distinct characteristics and
purposes. Here are the key differences between them:
1. Definition
Table: A table is a physical storage structure in a database that holds data in rows and columns. It is where
data is actually stored
View: A view is a virtual table created by a query that selects data from one or more tables. It does not store
data itself; instead, it presents data stored in tables.
2. Data Storage
Table: Data is physically stored in the database
View: Does not store data; it dynamically generates the data based on the underlying tables each time it is
queried.
3. Updatability
Table: Data in tables can be inserted, updated, or deleted directly
View: Views can be read-only or updatable, depending on how they are defined. Certain views (especially
those based on joins or aggregate functions) may not allow data modifications.
4. Purpose
Table: Used for storing actual data in the database
View: Used to simplify complex queries, encapsulate business logic, or provide a specific representation of
data for security or convenience.
5. Performance
Table: Direct access to table data is generally more efficient since data is physically stored
View: May have performance overhead since the underlying query needs to be executed each time the view
is accessed. However, indexed views can improve performance in some cases.
6. Security
Table: Access to tables can be controlled through permissions at the table level
View: Can be used to restrict access to specific data by exposing only certain columns or rows, providing an
additional layer of security.
PW Skills
Summary:
Tables are the primary data storage structures in a database, while views are virtual tables that provide a way
to simplify data access, enhance security, and present data in a specific format without storing it physically
What are the different types of cursors available in SQL, and how do they differ from each other?
Answer:
In SQL, cursors are used to retrieve and manipulate data row by row. There are several types of cursors, each
with distinct characteristics. The main types of cursors are:
1. Static Cursor
Description: A static cursor creates a temporary copy of the result set. It does not reflect changes made to
the underlying data after the cursor is opened
Use Case: Suitable when the data needs to remain unchanged during processing, such as generating
reports.
2. Dynamic Cursor
Description: A dynamic cursor reflects all changes made to the data in the underlying tables while the
cursor is open. This includes inserts, updates, and deletes
Use Case: Useful when real-time data is required, as it provides the most current view of the data.
3. Forward-Only Cursor
Description: A forward-only cursor allows traversal of the result set in one direction—from the first row to the
last row. You cannot move backward
Use Case: Efficient for processing large result sets where you only need to read data sequentially.
4. Keyset-Driven Cursor
Description: A keyset-driven cursor allows updates to the rows identified by the cursor, but new rows added
after the cursor is opened are not visible. Changes to existing rows are visible
Use Case: Suitable when you need to work with a stable set of keys while allowing updates.
5. Read-Only Cursor
Description: A read-only cursor is designed for retrieving data only. You cannot update the data through this
cursor
Use Case: Efficient for scenarios where data retrieval is the only requirement.
Differences
Data Reflection: Static cursors do not reflect changes, while dynamic cursors do. Keyset-driven cursors allow
updates but not new rows
Directionality: Forward-only cursors allow movement only in one direction, while others may allow both
forward and backward navigation
Modification Capability: Some cursors allow data modifications (e.g., dynamic and keyset-driven), while
others are read-only.
Summary:
The choice of cursor type depends on the specific needs of your application, including whether you require
real-time data updates, the direction of data processing, and whether you need to modify the data.
PW Skills
PW Skills
Key Differences from Stored Procedures
Execution Context
Trigger: Automatically executed in response to data modification events on a table
Stored Procedure: Executed explicitly by the user or application through a CALL or EXEC statement
Invocation
Trigger: Cannot be called directly; it is invoked by the database system when the specified event occurs
Stored Procedure: Can be called at any time when needed by the user or application
Purpose
Trigger: Primarily used for data validation, enforcing referential integrity, logging changes, or auditing
data modifications
Stored Procedure: Used for a broader range of tasks, including complex business logic, data
manipulation, and retrieval
Return Value
Trigger: Does not return a value to the caller; it operates in the context of the event that triggered it
Stored Procedure: Can return values or result sets to the caller
Transaction Handling
Trigger: Executes as part of the transaction that initiated the triggering event; if the transaction is rolled
back, so are any changes made by the trigger
Stored Procedure: Can manage its own transactions independently of the calling context.
Summary:
Triggers are automatic, event-driven operations tied to data modifications in a table, while stored procedures
are explicit, user-defined functions that can perform a wide range of tasks
Answer:
AFTER and BEFORE triggers are types of database triggers that determine when the trigger action occurs in
relation to the triggering event (such as INSERT, UPDATE, or DELETE). Here are the key differences:
1. Timing of Execution
BEFORE Trigger
Executes before the triggering event occurs
Can be used to validate or modify data before it is written to the database
AFTER Trigger
Executes after the triggering event has occurred
Typically used for actions that depend on the successful completion of the data modification.
2. Use Cases
BEFORE Trigger
Useful for enforcing business rules, such as validating input data, transforming data before insertion, or
preventing invalid data from being saved
Example: Ensuring a new record meets certain PWcriteria
Skillsbefore it is added to the table
AFTER Trigger
Useful for actions that need to occur as a consequence of the data modification, such as logging
changes, updating related records, or performing additional calculations
Example: Auditing changes to a record or notifying other systems after a successful update.
PW Skills
3. Impact on Data Modification
BEFORE Trigger
Can modify the values being inserted or updated, as the trigger runs before the actual data operation
AFTER Trigger
Cannot modify the values of the operation being performed since it runs after the data modification is
completed.
Summary:
BEFORE triggers execute before the data modification, allowing validation and data transformation, while AFTER
triggers execute after the modification, focusing on follow-up actions or side effects of the change
What is a CASE statement in SQL and What is the difference between CASE and COALESCE in SQL.
Answer:
CASE: Evaluates a list of conditions and returns one of multiple possible result expressions.
Example:
SELECT
EmployeeName,
Salary,
CASE
ELSE 'Low'
END AS SalaryCategory
FROM Employees;
Difference: CASE is more flexible with multiple conditions, while COALESCE is simpler and best used for dealing
with NULL values
Answer:
RANK(): Assigns a unique rank to each row within a partition of a result set. If there are ties, it leaves gaps in the
ranking.
DENSE_RANK(): Similar to RANK(), but without leaving gaps in the ranking sequence if there are ties.
ROW_NUMBER(): Assigns a unique number to each row within a partition, with no regard for ties
Explain the difference between LAG() and LEAD() functions in SQL. In what scenarios might you use each of
these functions?
Answer:
LAG(): Retrieves the value of a previous row from the current row within the same result set.
LEAD(): Retrieves the value of a following row from the current row within the same result set.
PW Skills
PW Skills
Use Cases:
LAG(): Useful for comparing a row with the previous one (e.g., comparing sales figures month over month).
LEAD(): Useful for forecasting or planning (e.g., getting the next month's sales target).
Example:
SELECT
EmployeeName,
Salary,
FROM Employees;
Advanced
Write an SQL query to assign a unique row number to each employee within their respective departments,
ordered by their salary. Use the ROW_NUMBER() window function.
Answer:
SELECT
Department,
EmployeeName,
Salary,
FROM Employees;
Write an SQL query to calculate the cumulative total of salaries for employees, ordered by their hiring
date. Use the SUM() window function with appropriate ordering.
PW
PW Skills
Skills
Answer:
SELECT
EmployeeName,
HireDate,
Salary,
FROM Employees;
Write an SQL query using a CASE statement to assign a grade ('A', 'B', 'C') to students based on their
scores, with the following conditions: 'A' for scores above 80, 'B' for scores between 50 and 80, and 'C' for
scores below 50.
Answer:
A: Scores above 80
C: Scores below 50
SELECT
StudentName,
Score,
CASE
ELSE 'C'
END AS Grade
PW Skills
FROM Students;
PW Skills
Create a temporary table to store a list of employees whose salaries are above the average salary. Then,
write a query to retrieve these employees' names and salaries from the temporary table.
Answer:
FROM Employees
Create a stored procedure that accepts an employee ID and returns the employee's name and salary.
Answer:
BEGIN
FROM Employees
END;
PW
PW Skills
Skills
Write a stored procedure to update the salary of an employee. The procedure should accept the employee
ID and the new salary as input parameters.
Answer:
BEGIN
UPDATE Employees
END;
Create a Scalar Function to Calculate the Net Salary After Deducting a Fixed Percentage of Tax.
Answer:
RETURNS DECIMAL(10,2)
AS
BEGIN
END;
PW
PW Skills
Skills
Create a Table-Valued Function to Retrieve All Employees in a Specific Department.
Answer:
RETURNS TABLE
AS
RETURN
FROM Employees
);
Create a view that lists all customers who have placed orders worth more than $1000.
Answer:
FROM Orders
GROUP BY CustomerName
PW
PW Skills
Skills
Write a cursor that iterates through all orders and applies a 10% discount to orders where the total value
exceeds $1000.
Answer:
FROM Orders
OPEN OrderCursor;
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE Orders
END;
CLOSE OrderCursor;
DEALLOCATE OrderCursor;
Create a trigger that automatically logs any deletions from a Products table into a DeletedProductsLog
table, capturing the product's Product_ID, Product_Name, and the deletion date.
PW Skills
Answer:
BEGIN
END;
PW Skills