0% found this document useful (0 votes)
17 views

50 Most Asked Theory SQL Question

Questions

Uploaded by

kiran kokate
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

50 Most Asked Theory SQL Question

Questions

Uploaded by

kiran kokate
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Here are 50 commonly asked SQL theory interview questions along with their answers:

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.

2. What are the different types of SQL commands?

Answer:
SQL commands are divided into several categories:

 DDL (Data Definition Language): CREATE, ALTER, DROP


 DML (Data Manipulation Language): SELECT, INSERT, UPDATE, DELETE
 DCL (Data Control Language): GRANT, REVOKE
 TCL (Transaction Control Language): COMMIT, ROLLBACK, SAVEPOINT

3. What is a primary key?

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.

4. What is a foreign key?

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.

6. What are the different normal forms?

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.

10. What is a view in SQL?

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.

11. What is the difference between WHERE and HAVING clauses?

Answer:

 WHERE: Filters rows before the aggregation (used with SELECT, INSERT, UPDATE,
DELETE).
 HAVING: Filters rows after the aggregation (used with GROUP BY).

12. What is a subquery?


Answer:
A subquery is a query nested inside another query. It is used to return data that will be used in
the main query as a condition.

13. What is the difference between DELETE, TRUNCATE, and DROP?

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.

14. What are aggregate functions in SQL?

Answer:
Aggregate functions perform calculations on a set of values and return a single value. Common
functions include:

 COUNT(): Counts the number of rows.


 SUM(): Sums the values of a column.
 AVG(): Calculates the average value.
 MAX(): Finds the maximum value.
 MIN(): Finds the minimum value.

15. What is a stored procedure?

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.

16. What are triggers in SQL?

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.

17. What is a transaction in SQL?

Answer:
A transaction is a sequence of one or more SQL operations treated as a single unit of work.
Transactions follow the ACID properties:

 Atomicity: All operations succeed or fail together.


 Consistency: The database remains in a consistent state before and after the transaction.
 Isolation: Transactions are isolated from each other.
 Durability: Changes made by a transaction are permanent once committed.

18. What is the difference between UNION and UNION ALL?

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.

19. What is a constraint in SQL?

Answer:
Constraints enforce rules on data in tables. Common constraints include:

 NOT NULL: Ensures that a column cannot have NULL values.


 UNIQUE: Ensures that all values in a column are unique.
 PRIMARY KEY: Uniquely identifies each row in a table.
 FOREIGN KEY: Ensures referential integrity between tables.
 CHECK: Ensures that all values in a column satisfy a specific condition.
 DEFAULT: Assigns a default value if no value is provided.

20. What is the difference between GROUP BY and ORDER BY?

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.

21. What is a cursor in SQL?

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.

22. What is a self-join?

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.

23. What are wildcards in SQL?


Answer:
Wildcards are special characters used in the LIKE operator for pattern matching:

 %: Represents zero, one, or multiple characters.


 _ : Represents a single character.

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.

25. What is a composite key?

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.

26. What is an alias in SQL?

Answer:
An alias is a temporary name assigned to a table or column for the duration of a query, using the
AS keyword.

27. What is a schema in SQL?

Answer:
A schema is a logical container that groups database objects such as tables, views, and
procedures. It helps organize and manage these objects.

28. What is the difference between CHAR and VARCHAR?

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.

29. What is a data type in SQL?


Answer:
A data type defines the kind of data that can be stored in a column, such as INT, VARCHAR,
DATE, FLOAT, etc.

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.

31. What is a correlated subquery?

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.

32. What is a CROSS JOIN?

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.

33. What is a unique key?

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.

34. What is a sequence in SQL?

Answer:
A sequence is a database object used to generate unique numerical values, often used for auto-
incrementing primary keys.

35. What is a synonym in SQL?

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.

36. What is a clustered index?


Answer:
A clustered index determines the physical order of data in a table. A table can have only one
clustered index because the data rows themselves are stored in the order of the index.

37. What is referential integrity in SQL?

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.

38. What is the difference between a database and a 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.

39. What are window functions in SQL?

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().

40. What is a composite index?

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.

41. What is a NULL value in SQL?

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.

42. What is the difference between EXISTS and IN?

Answer:

 EXISTS: Returns TRUE if a subquery returns any rows.


 IN: Checks if a value matches any value in a list or subquery result.
43. What is a MERGE statement in SQL?

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.

44. What is SQL injection?

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.

45. What is an ER diagram?

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.

46. What is a recursive query?

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).

47. What is the difference between OLTP and OLAP?

Answer:

 OLTP (Online Transaction Processing): Focuses on managing transaction-oriented


applications, typically dealing with a large number of short online transactions.
 OLAP (Online Analytical Processing): Focuses on analyzing and querying large
amounts of data, usually in a data warehouse setting.

48. What are the advantages and disadvantages of using SQL?

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.

49. What is a materialized view?

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.

50. What is the difference between DROP, DELETE, and TRUNCATE?

Answer:

 DROP: Removes a table or database entirely.


 DELETE: Removes specific rows from a table, can be rolled back.
 TRUNCATE: Removes all rows from a table, cannot be rolled back, and is faster than
DELETE.

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:

1. What is the difference between INNER JOIN and NATURAL JOIN?

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.

2. What is a full outer join?

Answer:
A FULL OUTER JOIN returns all rows from both tables, with matching rows where available,
and NULLs where there is no match.

3. What is a candidate key?

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.

4. What is a surrogate key?


Answer:
A surrogate key is an artificial key that is created for uniquely identifying a row in a table. It is
usually an auto-incremented integer and has no business meaning.

5. What is a bitmap index?

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.

6. What is a unique constraint?

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.

7. What is a non-clustered index?

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.

8. What is the difference between a sequence and an identity column?

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.

9. What is a cascading referential integrity constraint?

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.

10. What is the COALESCE function?

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.

12. What is an execution plan?

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.

13. What is a lateral join?

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.

14. What is the difference between IS NULL and = NULL?

Answer:

 IS NULL: Checks if a value is NULL.


 = NULL: Incorrect syntax; comparison to NULL should always be done with IS NULL.

15. What is the difference between a primary key and a composite key?

Answer:

 Primary Key: A single column that uniquely identifies a row.


 Composite Key: A combination of two or more columns that together uniquely identify
a row.

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).

17. What is the difference between RANK() and DENSE_RANK()?

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.

18. What is the difference between a function and a procedure?

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.

19. What is the use of the NVL function?

Answer:
The NVL function replaces a NULL value with a specified value. It's commonly used in Oracle
databases.

20. What is a partial index?

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.

21. What is a check constraint?

Answer:
A CHECK constraint ensures that all values in a column satisfy a specific condition or predicate.

22. What is a partitioned table?

Answer:
A partitioned table is divided into smaller, more manageable pieces called partitions, which can
improve performance and manageability for large tables.

23. What is a clustered columnstore index?

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.

24. What is a covering index?


Answer:
A covering index is an index that includes all the columns needed to satisfy a query, allowing the
database to retrieve data directly from the index without accessing the table.

25. What is the purpose of the GROUPING SETS clause?

Answer:
The GROUPING SETS clause allows you to define multiple groupings in a single query,
effectively creating multiple GROUP BY clauses in one query.

26. What is a rollup in SQL?

Answer:
ROLLUP is an extension of the GROUP BY clause that creates subtotals and a grand total for a
dataset.

27. What is a cube in SQL?

Answer:
A CUBE is an extension of the GROUP BY clause that creates subtotals for all possible
combinations of columns.

28. What is the difference between VARCHAR and TEXT?

Answer:

 VARCHAR: A variable-length string with a defined maximum length.


 TEXT: A string of arbitrary length, used for storing large text data.

29. What is a materialized view log?

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.

30. What is the difference between CURRENT_DATE and SYSDATE?

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.

31. What is the difference between LEAD() and LAG()?


Answer:

 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.

32. What is an analytic function?

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.

33. What is a recursive CTE?

Answer:
A recursive Common Table Expression (CTE) is a CTE that references itself, typically used to
traverse hierarchical or tree-structured data.

34. What is the MERGE statement used for?

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.

35. What is a catalog in a database?

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.

36. What is the difference between REVOKE and DENY?

Answer:

 REVOKE: Removes previously granted or denied permissions.


 DENY: Explicitly denies a permission to a user or role, overriding any granted
permissions.

37. What is a schema binding in SQL?

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.

38. What is the WITH ROLLUP clause used for?


Answer:
WITH ROLLUP is used to generate summary rows for each group, as well as a grand total row,
in a GROUP BY query.

39. What is the purpose of the HAVING clause?

Answer:
The HAVING clause is used to filter groups after aggregation, often used in conjunction with
GROUP BY.

40. What is a scalar subquery?

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.

41. What is an updatable view?

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).

You might also like