This document discusses 50 SQL interview questions along with their answers. The questions cover a variety of SQL topics including data types, keys, joins, functions, triggers and more. The answers provide explanations of core SQL concepts and components.
This document discusses 50 SQL interview questions along with their answers. The questions cover a variety of SQL topics including data types, keys, joins, functions, triggers and more. The answers provide explanations of core SQL concepts and components.
SQL interview questions along with their answers 1. What are the different types of SQL commands?
ANSWER: SQL commands can be
categorized into four main types: Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), and Transaction Control Language (TCL).
2. What is the difference between
CHAR and VARCHAR data types?
ANSWER: CHAR is a fixed-length
character data type, while VARCHAR is a variable-length character data type. CHAR will always use the same amount of storage space regardless of the actual length of the data, while VARCHAR will only use as much storage as needed. 3. What is a primary key?
ANSWER: A primary key is a column or a
set of columns that uniquely identifies each row in a table. It must contain unique values and cannot contain NULL values.
4. What is a foreign key?
ANSWER: A foreign key is a column or a
set of columns in a table that establishes a link between data in two tables. It enforces referential integrity by ensuring that the values in the foreign key column(s) match the values in the primary key column(s) of another table. 5. What is a JOIN?
ANSWER: JOIN is used to combine rows
from two or more tables based on a related column between them. There are different types of JOINs such as INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN.
6. What is an INDEX?
ANSWER: An index is a database object
used to speed up the retrieval of rows from a table by creating a sorted list of values from one or more columns. 7. Explain the difference between the WHERE and HAVING clauses.
ANSWER: The WHERE clause is used to
filter rows based on a specified condition, while the HAVING clause is used to filter groups based on a specified condition when using aggregate functions.
8. What is a subquery?
ANSWER: A subquery is a query nested
within another query. It can be used to return a single value, a list of values, or a result set that can be used in the main query. 9. What is a stored procedure?
ANSWER: A stored procedure is a
precompiled collection of SQL statements that can be stored and executed in a database. It allows for code reusability and improved performance.
10. Explain the difference between
UNION and UNION ALL.
ANSWER: UNION is used to combine the
results of two or more SELECT statements into a single result set, eliminating duplicate rows. UNION ALL also combines the results of SELECT statements, but it retains all rows, including duplicates. 11. What is normalization?
ANSWER: Normalization is the process of
organizing data in a database to minimize redundancy and dependency by dividing large tables into smaller tables and defining relationships between them.
12. What is denormalization?
ANSWER: Denormalization is the process
of adding redundant data to a normalized database to improve read performance by reducing the number of joins needed to retrieve data. 13. Explain ACID properties in the context of database transactions. ANSWER: ACID stands for Atomicity, Consistency, Isolation, and Durability. Atomicity ensures that a transaction is treated as a single unit of work, Consistency ensures that the database remains in a consistent state before and after the transaction, Isolation ensures that the transaction is isolated from other transactions, and Durability ensures that the changes made by a transaction are permanent.
14. What is denormalization?
ANSWER: Denormalization is the process
of adding redundant data to a normalized database to improve read performance by reducing the number of joins needed to retrieve data. 15. What is the difference between a clustered and a non-clustered index?
ANSWER: A clustered index determines
the physical order of rows in a table and is created on the primary key column(s) by default. A non-clustered index does not affect the physical order of rows and is stored separately from the table data.
16. What is the difference between
DELETE and TRUNCATE commands?
ANSWER: DELETE is a DML command
used to remove rows from a table based on a specified condition, while TRUNCATE is a DDL command used to remove all rows from a table without generating a log of individual row deletions. 17. What is a view?
ANSWER: A view is a virtual table created
by a query that can be treated like a regular table. It does not store data itself but provides a way to present data stored in one or more tables in a specific format.
18. What is the difference between a
view and a table?
ANSWER: A table is a physical structure
that stores data, while a view is a virtual table that does not store data itself but provides a way to present data stored in one or more tables. 19. What is a self-join?
ANSWER: A self-join is a join that is used
to join a table to itself. It is often used when a table contains a hierarchical relationship or when comparing rows within the same table.
20. What is the purpose of the
GROUP BY clause?
ANSWER: The GROUP BY clause is used
to group rows that have the same values into summary rows, such as sum, count, max, etc., based on a specified column or expression. 21. What is the difference between the GROUP BY and ORDER BY clauses? ANSWER: The GROUP BY clause is used to group rows, while the ORDER BY clause is used to sort the result set. GROUP BY typically includes aggregate functions, while ORDER BY does not.
22. What is a trigger?
ANSWER: A trigger is a database object
that automatically executes a specified set of SQL statements when certain events occur, such as inserting, updating, or deleting rows from a table. 23. What is a constraint?
ANSWER: A constraint is a rule defined
on a column or a set of columns that enforces data integrity by limiting the type of data that can be stored in a table.
24. What is the difference between a
unique key and a primary key?
ANSWER: Both unique key and primary
key enforce uniqueness in a column or a set of columns. However, a table can have multiple unique keys, but only one primary key. 25. Explain the difference between an inner join and an outer join. ANSWER: An inner join returns only the rows that have matching values in both tables, while an outer join returns all rows from one table and the matched rows from the other table. Outer joins can be further categorized into left, right, and full outer joins..
26. What is the difference between a
correlated and a non-correlated subquery?
ANSWER: A correlated subquery is a
subquery that depends on the outer query for its values, while a non- correlated subquery can be executed independently of the outer query. 27. What is a transaction? ANSWER: A transaction is a logical unit of work that consists of one or more SQL statements that are executed as a single unit. It is either committed (made permanent) or rolled back (undone) as a whole.
28. What is the purpose of the
COMMIT and ROLLBACK commands? ANSWER: COMMIT is used to permanently save the changes made by a transaction. Once a COMMIT statement is executed, the changes become permanent and cannot be undone. ROLLBACK is used to undo the changes made by a transaction that has not been committed. It restores the database to its state before the transaction began. 29. What is a SQL injection? ANSWER: SQL injection is a type of security vulnerability that occurs when an attacker is able to manipulate SQL queries through input fields on a website or application. This can allow the attacker to execute arbitrary SQL code, potentially gaining unauthorized access to the database or causing data loss.
30. What is the difference between a
database and a schema? ANSWER: A database is a collection of related data and tables, while a schema is a logical container for database objects such as tables, views, indexes, etc., within a database. 31. What is a stored function? ANSWER: A stored function is a subroutine stored in the database that encapsulates a set of SQL statements and can be called by other SQL statements or applications. It typically returns a single value.
32. What is the purpose of the CASE
statement? ANSWER: The CASE statement is used to provide conditional logic within a SQL query. It allows you to perform different actions based on different conditions. 33. What is the difference between a view and a materialized view? ANSWER: A view is a virtual table that does not store data itself, while a materialized view is a physical copy of the result set of a query stored as a table. Materialized views are updated periodically to reflect changes in the underlying data.
34. Explain the difference between
an aggregate function and a scalar function. ANSWER: An aggregate function operates on a set of values and returns a single value summarizing the data, such as SUM, AVG, COUNT, etc. A scalar function operates on a single value and returns a single value based on that input, such as UPPER, LOWER, etc. 35. What is the purpose of the ROW_NUMBER() function? ANSWER: The ROW_NUMBER() function is used to assign a unique sequential integer to each row in a result set based on a specified ordering.
36. What is the purpose of the
TRIGGER?
ANSWER: A trigger is a database object
that automatically executes a specified set of SQL statements when certain events occur, such as inserting, updating, or deleting rows from a table. 37. What is the difference between a LEFT JOIN and a RIGHT JOIN? ANSWER: In a LEFT JOIN, all rows from the left table are returned, along with matching rows from the right table. In a RIGHT JOIN, all rows from the right table are returned, along with matching rows from the left table.
38. What is the purpose of the
COALESCE() function?
ANSWER: The COALESCE() function is
used to return the first non-null value in a list of expressions. It is commonly used to handle null values in SQL queries. 39. What is the difference between the CHARINDEX() and the INSTR() functions? ANSWER: CHARINDEX() is a function in SQL Server that returns the position of a substring within a string, while INSTR() is a function in Oracle that performs a similar function.
40. What is the purpose of the
EXISTS operator?
ANSWER: The EXISTS operator is used to
test whether a subquery returns any rows. It returns true if the subquery returns one or more rows, and false otherwise. 41. What is the difference between a natural join and an equi-join?
ANSWER: A natural join is a join that
automatically matches columns with the same name in the two tables, while an equi-join is a join that explicitly specifies the columns to be matched using the ON clause.
42. What is a cursor?
ANSWER: A cursor is a database object
used to retrieve and manipulate data row by row. It allows for sequential processing of query results. 43. What is the purpose of the ROWID pseudocolumn?
ANSWER: The ROWID pseudocolumn is a
unique identifier assigned to each row in a table by the database. It is typically used for low-level operations and should not be relied upon for application-level logic.
44. What is the difference between
the MIN() and the MAX() functions?
ANSWER: The MIN() function returns the
minimum value in a set of values, while the MAX() function returns the maximum value. 45. What is the purpose of the RANK() function?
ANSWER: The RANK() function is used to
assign a rank to each row in a result set based on a specified ordering. Rows with the same values are assigned the same rank, with gaps in the ranking sequence where there are ties.
46. What is a correlated subquery?
ANSWER: A correlated subquery is a
subquery that depends on the outer query for its values. It is executed once for each row processed by the outer query. 47. What are the advantages of using CTEs?
ANSWER: CTEs improve readability and
maintenance of complex queries, allow recursive queries, and can be referenced multiple times within the same query.
48. Can a CTE reference itself or
another CTE?
ANSWER: A CTE can reference itself in
the case of a recursive CTE. However, a CTE cannot reference another CTE unless they are chained in successive WITH clauses or in the main body of the query. 49. What are the differences between local and global temporary tables?
ANSWER: Local temporary tables are visible
only to the current session, and their names are prefixed with a single hash symbol (#TableName). Global temporary tables are visible to all sessions, and their names are prefixed with double hash symbols (##TableName).
50. In what scenarios would you use a
CTE over a temporary table, and vice versa? ANSWER: Use CTEs for simpler temporary result sets where recursion or readability is a primary concern, and no physical table creation is necessary. Temporary tables are preferable for more complex scenarios requiring significant manipulation of the temporary data, when indexing is needed for performance, or when the data needs to persist across multiple queries or stored procedure calls. Follow me RESHARE/REPOST