Theoretical SQL Interview Questions
Theoretical SQL Interview Questions
. What is SQL?
○ SQL (Structured Query Language) is a standard programming language used for
7
managing and manipulating relational databases. It allows users to perform various
operations like querying data, updating records, and creating or modifying database
structures.
. What are the different types of SQL statements?
○ SQL statements are categorized into:
◆ DDL (Data Definition Language): Defines the structure of the database (e.g.,
CREATE, ALTER, DROP).
◆ DML (Data Manipulation Language): Manipulates data within tables (e.g.,
6
SELECT, INSERT, UPDATE, DELETE).
◆ DCL (Data Control Language): Controls access to data (e.g., GRANT,
REVOKE).
◆ TCL (Transaction Control Language): Manages transactions (e.g., COMMIT,
5
ROLLBACK).
. What is a primary key?
○ A primary key is a unique identifier for a record in a database table. It ensures that
no two rows have the same value in this column, enforcing entity integrity.
4
. What is a foreign key?
○ A foreign key is a field (or group of fields) in one table that uniquely identifies a row
in another table. It establishes a relationship between the two tables, enforcing
3
referential integrity.
. What is normalization?
○ Normalization is the process of organizing data in a database to minimize
redundancy and improve data integrity. It typically involves dividing large tables into
smaller ones and defining relationships between them.
. What are the normal forms?
○ Normal forms are guidelines for structuring a relational database:
◆ 1NF (First Normal Form): Ensures that all columns contain atomic values and
that each record is unique.
◆ 2NF (Second Normal Form): Requires that all non-key attributes are fully
2
functional dependent on the primary key.
◆ 3NF (Third Normal Form): Eliminates transitive dependencies, ensuring that
non-key attributes depend only on the primary key.
. What is denormalization?
1
8
○ Denormalization is the process of combining tables to improve read performance at
the cost of increased redundancy and potential data anomalies. It is often used in
read-heavy databases.
. What is a join?
○ A join is a SQL operation that combines rows from two or more tables based on a
related column. It enables users to retrieve data from multiple tables in a single
query.
. What are the types of joins?
○ INNER JOIN: Returns only the rows with matching values in both tables.
○ LEFT JOIN: Returns all rows from the left table and matched rows from the right
table, with NULLs for unmatched rows.
○ RIGHT JOIN: Returns all rows from the right table and matched rows from the left
table, with NULLs for unmatched rows.
○ FULL OUTER JOIN: Returns all rows when there is a match in either table.
○ CROSS JOIN: Returns the Cartesian product of the two tables, combining every row
from the first table with every row from the second.
. What is a subquery?
○ A subquery is a query nested within another SQL query. It can be used to retrieve
data that will be used in the main query, typically in the WHERE or FROM clauses.
. What is an index?
○ An index is a database object that improves the speed of data retrieval operations
on a table. It can be thought of as a lookup table that allows for quicker searches.
. What are the types of indexes?
○ Unique Index: Ensures that all values in a column are unique.
○ Composite Index: An index on multiple columns.
○ Full-Text Index: Optimized for searching text within a column.
○ Clustered Index: Determines the physical order of data in the table.
○ Non-Clustered Index: A separate structure that points to the data rows.
. What is a view?
○ A view is a virtual table that is based on the result of a SQL query. It presents data in
a specific format and can simplify complex queries.
. What is a stored procedure?
○ A stored procedure is a precompiled collection of SQL statements that can be
executed as a single unit. It is stored in the database and can accept parameters.
. What is a trigger?
○ A trigger is a special kind of stored procedure that automatically executes in
○ A trigger is a special kind of stored procedure that automatically executes in
response to certain events on a table, such as INSERT, UPDATE, or DELETE
operations.
. What is a transaction?
○ A transaction is a sequence of operations performed as a single logical unit of work.
A transaction must be completed in its entirety or not at all to maintain data
integrity.
. What is ACID?
○ ACID stands for:
◆ Atomicity: Ensures that all operations within a transaction are completed
successfully; if not, the transaction is aborted.
◆ Consistency: Ensures that a transaction brings the database from one valid
state to another.
◆ Isolation: Ensures that transactions are executed independently, without
interference from each other.
◆ Durability: Ensures that once a transaction is committed, it remains
permanent even in the event of a system failure.
. What is a schema?
○ A schema is a logical container for database objects such as tables, views, and
procedures. It defines the structure and organization of the database.
. What is a data type?
○ A data type specifies the kind of data that can be stored in a column, such as
INTEGER, VARCHAR, DATE, etc. It determines the type of operations that can be
performed on the data.
. What is a constraint?
○ A constraint is a rule that limits the type of data that can be inserted into a table.
Common constraints include NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, and
CHECK.
. What is the difference between UNION and UNION ALL?
○ UNION combines the results of two queries and removes duplicates, while UNION
ALL combines the results and retains all duplicates.
. What is a cross join?
○ A cross join produces a Cartesian product of two tables, returning every possible
combination of rows from both tables.
. What is a self join?
○ A self join is a join in which a table is joined with itself to compare rows within the
same table.
. What is a CTE (Common Table Expression)?
○ A CTE is a temporary result set that can be referenced within a SELECT, INSERT,
UPDATE, or DELETE statement. It simplifies complex queries and enhances
readability.
. What is the purpose of the GROUP BY clause?
○ The GROUP BY clause is used to group rows that have the same values in specified
columns into summary rows, often used with aggregate functions like COUNT, SUM,
AVG.
. What is the HAVING clause?
○ The HAVING clause is used to filter groups created by the GROUP BY clause based
on a specified condition, similar to the WHERE clause but for grouped data.
. What is a temporary table?
○ A temporary table is a table that exists temporarily during the session in which it
was created. It is often used to store intermediate results.
. What is a materialized view?
○ A materialized view is a database object that contains the results of a query and is
stored on disk. It can be refreshed periodically to reflect changes in the underlying
data.
. What is a surrogate key?
○ A surrogate key is an artificial key used to uniquely identify a record in a table. It is
often an auto-incremented number and has no business meaning.
. What is referential integrity?
○ Referential integrity is a property of data stating that relationships between tables
must remain consistent. For example, foreign keys must reference valid records in
the parent table.
. What are aggregate functions?
○ Aggregate functions perform calculations on a set of values and return a single
value. Common aggregate functions include COUNT, SUM, AVG, MAX, and MIN.
. What is a window function?
○ A window function performs calculations across a set of rows related to the current
row without collapsing the result set. It enables advanced analytics like running
totals.
. What is the difference between CHAR and VARCHAR?
○ CHAR is a fixed-length data type, meaning it reserves a specified number of bytes
for storage. VARCHARis variable-length, meaning it only uses as much space as
needed for the data.
. What is a SQL Server?
○ SQL Server is a relational database management system (RDBMS) developed by
○
Microsoft. It is used for storing, retrieving, and managing data in databases.
. What is partitioning in SQL?
○ Partitioning is the process of dividing a large table into smaller, more manageable
pieces, while maintaining a single logical structure. It improves performance and
manageability.
. What is a database trigger?
○ A database trigger is an automatic response to events on a table, such as INSERT,
UPDATE, or DELETE. It can enforce business rules or maintain audit trails.
. What is an execution plan?
○ An execution plan is a sequence of steps that the SQL Server query optimizer will
follow to execute a query. It provides insights into query performance and
optimization opportunities.
. What is a SQL function?
○ A SQL function is a stored program that can be called to perform a specific task and
return a single value or result set. It can be user-defined or built-in.
. What is a stored function?
○ A stored function is similar to a stored procedure but is designed to return a value. It
can be used in SQL statements as part of expressions.
. What are system views?
○ System views are built-in views that provide information about the database
structure, schema, and objects, allowing users to query metadata about the
database.
. What is a full-text search?
○ A full-text search allows for complex search queries on character-based data,
enabling users to search for words and phrases in large text fields efficiently.
. What is a default constraint?
○ A default constraint provides a default value for a column when no value is specified
during data insertion.
. What is a cascade delete?
○ A cascade delete is a referential action that automatically deletes rows in a child
table when the corresponding row in the parent table is deleted.
. What is a SQL injection attack?
○ SQL injection is a code injection technique that exploits vulnerabilities in an
application's software by executing malicious SQL statements. It can compromise
database security.
. What is a schema in SQL?
○ A schema is a logical grouping of database objects, such as tables and views, that
○
defines the structure of the database.
. What is a non-clustered index?
○ A non-clustered index is an index that does not alter the physical order of the data
rows in a table. It provides a separate structure to improve query performance.
. What is a clustered index?
○ A clustered index determines the physical order of data in a table. Each table can
have only one clustered index, which is typically created on the primary key.
. What is a data dictionary?
○ A data dictionary is a centralized repository that contains metadata about the
database's structure, including information about tables, columns, data types, and
relationships.
. What is a SQL constraint?
○ A SQL constraint is a rule enforced on data columns to ensure data integrity.
Examples include NOT NULL, UNIQUE, and CHECK constraints.
. What is a pivot table?
○ A pivot table is a data summarization tool used in SQL to aggregate data from a
table and transform rows into columns, enabling easier analysis.
Practical SQL Interview Questions
. Write a SQL query to find the second highest salary from a table.
sql
Copy code
SELECT MAX(salary)
. FROM employees
. WHERE salary < (SELECT MAX(salary) FROM employees);
.
. Write a query to retrieve all employees with a salary greater than the average salary.
sql
Copy code
SELECT *
. FROM employees
. WHERE salary > (SELECT AVG(salary) FROM employees);
.
SELECT department_id
. FROM employees
. GROUP BY department_id
. HAVING COUNT(*) > 10;
.
. Write a SQL query to get the names of employees whose names start with 'A'.
sql
Copy code
SELECT name
. FROM employees
. WHERE name LIKE 'A%';
.
.
. Write a SQL query to update the salary of an employee with a specific ID.
sql
Copy code
UPDATE employees
. SET salary = salary * 1.1
. WHERE employee_id = 101;
.
. Write a SQL query to join two tables and display the result.
sql
Copy code
. Write a query to retrieve employees who were hired in the last 30 days.
sql
Copy code
SELECT *
. FROM employees
. WHERE hire_date >= DATEADD(DAY, -30, GETDATE());
.
. Write a SQL query to select employees whose salary is in the top 10%.
sql
Copy code
SELECT *
. FROM employees
. WHERE salary > (SELECT PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY salary)
FROM employees);
.
. Write a query to display the employee with the highest salary in each department.
sql
Copy code
. Write a SQL query to count how many employees are there in each job title.
sql
Copy code
SELECT *
. FROM employees
. WHERE manager_id IS NULL;
.
. Write a SQL query to get the highest salary from each job title.
sql
Copy code
SELECT *
. FROM employees
. WHERE hire_date < '2020-01-01';
.
. Write a SQL query to find the average salary of employees in each department.
sql
Copy code
SELECT department_id, AVG(salary) AS average_salary
. FROM employees
. GROUP BY department_id;
.
. Write a query to retrieve all employees and their corresponding department names.
sql
Copy code