50 Most Asked Theory SQL Question
50 Most Asked Theory SQL Question
1. What is SQL?
Answer:
SQL (Structured Query Language) is a standard language used to communicate with relational
databases. It is used for querying, inserting, updating, and deleting data, as well as managing
database structures.
Answer:
SQL commands are divided into several categories:
Answer:
A primary key is a column (or a set of columns) in a table that uniquely identifies each row. It
ensures that no duplicate values exist and that no NULL values are allowed.
Answer:
A foreign key is a column or set of columns in one table that references the primary key of
another table, establishing a relationship between the two tables.
5. What is normalization?
Answer:
Normalization is the process of organizing data in a database to reduce redundancy and improve
data integrity. It involves dividing large tables into smaller, related tables and defining
relationships between them.
Answer:
1NF (First Normal Form): Ensures that each column contains atomic, indivisible
values.
2NF (Second Normal Form): Ensures that the table is in 1NF and all non-key attributes
are fully functionally dependent on the primary key.
3NF (Third Normal Form): Ensures that the table is in 2NF and all attributes are only
dependent on the primary key.
BCNF (Boyce-Codd Normal Form): A stricter version of 3NF where every determinant
is a candidate key.
7. What is denormalization?
Answer:
Denormalization is the process of combining tables to reduce the complexity of queries, often at
the expense of introducing redundancy. It is used to improve performance in certain situations.
8. What is a JOIN?
Answer:
A JOIN is an SQL operation that combines rows from two or more tables based on a related
column. Types include INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN.
9. What is an index?
Answer:
An index is a database object that improves the speed of data retrieval operations on a table at the
cost of additional storage space and slower write operations. It is created on one or more
columns of a table.
Answer:
A view is a virtual table based on the result set of an SQL query. It does not store data itself but
provides a way to simplify complex queries and restrict access to certain data.
Answer:
WHERE: Filters rows before the aggregation (used with SELECT, INSERT, UPDATE,
DELETE).
HAVING: Filters rows after the aggregation (used with GROUP BY).
Answer:
DELETE: Removes rows from a table based on a condition, can be rolled back.
TRUNCATE: Removes all rows from a table without logging individual row deletions,
cannot be rolled back.
DROP: Deletes the entire table or database.
Answer:
Aggregate functions perform calculations on a set of values and return a single value. Common
functions include:
Answer:
A stored procedure is a set of SQL statements that can be executed as a program. It is stored in
the database and can be reused, reducing redundancy and improving performance.
Answer:
A trigger is a database object that automatically executes a predefined set of SQL statements in
response to specific events on a table, such as INSERT, UPDATE, or DELETE.
Answer:
A transaction is a sequence of one or more SQL operations treated as a single unit of work.
Transactions follow the ACID properties:
Answer:
UNION: Combines the result sets of two or more SELECT statements, removing
duplicates.
UNION ALL: Combines the result sets of two or more SELECT statements without
removing duplicates.
Answer:
Constraints enforce rules on data in tables. Common constraints include:
Answer:
GROUP BY: Groups rows that have the same values in specified columns into summary
rows.
ORDER BY: Sorts the result set by one or more columns.
Answer:
A cursor is a database object used to retrieve, manipulate, and navigate through a result set one
row at a time, often used in stored procedures.
Answer:
A self-join is a join in which a table is joined with itself. It is useful for querying hierarchical
data or comparing rows within the same table.
24. What is the difference between SQL injection and parameterized queries?
Answer:
SQL Injection: A security vulnerability where an attacker can manipulate SQL queries
by injecting malicious code.
Parameterized Queries: Prevent SQL injection by separating SQL code from data
inputs, treating inputs as parameters rather than executable code.
Answer:
A composite key is a primary key that consists of two or more columns in a table. It is used when
a single column is not sufficient to uniquely identify rows.
Answer:
An alias is a temporary name assigned to a table or column for the duration of a query, using the
AS keyword.
Answer:
A schema is a logical container that groups database objects such as tables, views, and
procedures. It helps organize and manage these objects.
Answer:
CHAR: A fixed-length character data type. It pads extra spaces to reach the defined
length.
VARCHAR: A variable-length character data type. It only uses as much space as the
actual length of the data.
30. What is the difference between INNER JOIN and OUTER JOIN?
Answer:
INNER JOIN: Returns only the rows that have matching values in both tables.
OUTER JOIN: Returns all rows from one table and the matched rows from the other.
Types include LEFT OUTER JOIN, RIGHT OUTER JOIN, and FULL OUTER JOIN.
Answer:
A correlated subquery is a subquery that references columns from the outer query. It is executed
once for each row processed by the outer query.
Answer:
A CROSS JOIN returns the Cartesian product of two tables, meaning it combines all rows from
one table with all rows from another table.
Answer:
A unique key ensures that all values in a column or a set of columns are unique. Unlike a
primary key, a unique key can contain NULL values.
Answer:
A sequence is a database object used to generate unique numerical values, often used for auto-
incrementing primary keys.
Answer:
A synonym is an alias for a database object like a table, view, or stored procedure, allowing it to
be referenced with a different name.
Answer:
Referential integrity ensures that relationships between tables remain consistent, typically
enforced using foreign keys. For example, a foreign key in one table must match a primary key
in another table.
Answer:
Database: A collection of related data and its structure organized in a specific way. It
contains tables, views, stored procedures, etc.
Table: A collection of related data entries organized in rows and columns within a
database.
Answer:
Window functions perform calculations across a set of table rows related to the current row.
Examples include ROW_NUMBER(), RANK(), DENSE_RANK(), and NTILE().
Answer:
A composite index is an index that includes multiple columns. It is used to speed up queries that
filter on more than one column.
Answer:
A NULL value represents missing or undefined data. It is different from an empty string or zero
and requires special handling in SQL queries.
Answer:
Answer:
The MERGE statement allows you to perform INSERT, UPDATE, or DELETE operations on a target
table based on the results of a join with a source table. It’s often used for UPSERT operations.
Answer:
SQL injection is a code injection technique that exploits vulnerabilities in an application's
software by inserting or "injecting" malicious SQL statements. It can be prevented by using
parameterized queries.
Answer:
An ER (Entity-Relationship) diagram is a graphical representation of the entities in a database
and the relationships between them. It is used in database design.
Answer:
A recursive query is a query that refers to itself or another recursive query in its definition,
typically used to query hierarchical data. Commonly implemented using a Common Table
Expression (CTE).
Answer:
Answer:
Advantages:
o Efficient data management and retrieval.
o Standardized across different database systems.
o Flexible and powerful querying capabilities.
Disadvantages:
o Can become complex for large databases.
o Performance can be an issue with poorly written queries.
o Requires a good understanding of relational databases.
Answer:
A materialized view is a database object that contains the results of a query. Unlike a regular
view, the result is stored and can be refreshed periodically. It is used to improve query
performance for complex calculations.
Answer:
These questions and answers cover a wide range of SQL theory topics, providing a strong
foundation for SQL-related interviews.
Certainly! Here are 50 more SQL theory interview questions along with their answers:
Answer:
INNER JOIN: Combines rows from two tables based on a specified condition.
NATURAL JOIN: Automatically joins tables based on all columns with the same name
and datatype in both tables.
Answer:
A FULL OUTER JOIN returns all rows from both tables, with matching rows where available,
and NULLs where there is no match.
Answer:
A candidate key is a column or set of columns that can uniquely identify a row in a table. A table
can have multiple candidate keys, but only one primary key.
Answer:
A bitmap index is a type of index that uses bitmaps (bit arrays) and is typically used for columns
with a low cardinality of distinct values, such as gender or status fields.
Answer:
A UNIQUE constraint ensures that all values in a column or a group of columns are unique
across the rows in a table. Unlike the primary key, it allows NULLs.
Answer:
A non-clustered index is an index where the logical order of the index does not match the
physical order of the rows in the table. It provides a pointer to the data rather than storing the
data itself.
Answer:
Sequence: An object that generates a sequence of numeric values, which can be used
across multiple tables.
Identity Column: A table-specific auto-incrementing column used as a primary key.
Answer:
A cascading referential integrity constraint automatically updates or deletes related rows in child
tables when a row in a parent table is updated or deleted.
Answer:
The COALESCE function returns the first non-NULL value in a list of expressions. It is used to
handle NULL values in SQL queries.
11. What is the difference between a CROSS APPLY and an OUTER APPLY?
Answer:
CROSS APPLY: Similar to an INNER JOIN but used with table-valued functions.
OUTER APPLY: Similar to a LEFT JOIN but used with table-valued functions.
Answer:
An execution plan is a graphical or textual representation of how the SQL server executes a
query. It shows the steps the database engine takes to retrieve the desired results.
Answer:
A lateral join allows a subquery in the FROM clause to refer to columns of preceding tables in
the same FROM clause, making it possible to generate a new column for each row.
Answer:
15. What is the difference between a primary key and a composite key?
Answer:
16. What is the difference between SET and SELECT when assigning variables
in SQL?
Answer:
SET: Assigns a value to a variable, commonly used for single variable assignments.
SELECT: Can assign values to multiple variables simultaneously and can handle
multiple rows (if not careful).
Answer:
RANK(): Assigns ranks to rows with gaps between rank values if there are ties.
DENSE_RANK(): Assigns ranks to rows without gaps between rank values if there are
ties.
Answer:
Function: Returns a single value or table and can be used in a SELECT statement.
Procedure: Performs an action and can return multiple values, but cannot be used in a
SELECT statement.
Answer:
The NVL function replaces a NULL value with a specified value. It's commonly used in Oracle
databases.
Answer:
A partial index is an index created on a subset of rows in a table, often used to optimize queries
on frequently queried data subsets.
Answer:
A CHECK constraint ensures that all values in a column satisfy a specific condition or predicate.
Answer:
A partitioned table is divided into smaller, more manageable pieces called partitions, which can
improve performance and manageability for large tables.
Answer:
A clustered columnstore index is a data storage format that stores data in a columnar format,
optimized for high compression and performance in analytics queries.
Answer:
The GROUPING SETS clause allows you to define multiple groupings in a single query,
effectively creating multiple GROUP BY clauses in one query.
Answer:
ROLLUP is an extension of the GROUP BY clause that creates subtotals and a grand total for a
dataset.
Answer:
A CUBE is an extension of the GROUP BY clause that creates subtotals for all possible
combinations of columns.
Answer:
Answer:
A materialized view log is a table associated with a materialized view that records changes to the
base table, enabling fast refreshes of the materialized view.
Answer:
CURRENT_DATE: Returns the current date and time according to the server's time
zone.
SYSDATE: Returns the current date and time according to the database's time zone.
LEAD(): Returns the value of a specified column from the next row in the result set.
LAG(): Returns the value of a specified column from the previous row in the result set.
Answer:
An analytic function performs calculations across a set of table rows that are related to the
current row, often used in conjunction with the OVER() clause.
Answer:
A recursive Common Table Expression (CTE) is a CTE that references itself, typically used to
traverse hierarchical or tree-structured data.
Answer:
The MERGE statement allows you to perform insert, update, or delete operations based on the
result of a join, often used to synchronize two tables.
Answer:
A catalog in a database is a set of schemas that describe the structure of the database, including
information about tables, columns, data types, and relationships.
Answer:
Answer:
Schema binding locks a view or function to the schema of the underlying tables, preventing
changes to the tables that could invalidate the view or function.
Answer:
The HAVING clause is used to filter groups after aggregation, often used in conjunction with
GROUP BY.
Answer:
A scalar subquery is a subquery that returns a single value, which can be used in an expression or
comparison in the outer query.
Answer:
An updatable view is a view that allows data to be inserted, updated, or deleted through the view,
provided certain conditions are met (e.g., the view is based on a single table).