0% found this document useful (0 votes)
13 views20 pages

Top 50 SQL Interview Questions and Answers

The document contains the top 50 SQL interview questions and answers, covering fundamental concepts such as SQL, databases, primary and foreign keys, normalization, joins, and transactions. It also addresses advanced topics like ACID properties, triggers, views, and the differences between various SQL operators and data types. This resource serves as a comprehensive guide for individuals preparing for SQL-related job interviews.

Uploaded by

Basit Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views20 pages

Top 50 SQL Interview Questions and Answers

The document contains the top 50 SQL interview questions and answers, covering fundamental concepts such as SQL, databases, primary and foreign keys, normalization, joins, and transactions. It also addresses advanced topics like ACID properties, triggers, views, and the differences between various SQL operators and data types. This resource serves as a comprehensive guide for individuals preparing for SQL-related job interviews.

Uploaded by

Basit Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Harry

coding_knowledge

Top 50 SQL interview


questions and answers
Q 1. What is SQL?
ANS. SQL stands for Structured Query Language. It is a
programming language used for managing and manipulating
relational databases.

Q 2. What is a database?
ANS. A database is an organized collection of data stored and
accessed electronically. It provides a way to store, organize,
and retrieve large amounts of data efficiently.

Q 3. What is a primary key?


ANS. A primary key is a column or combination of columns
that uniquely identifies each row in a table. It enforces the
entity integrity rule in a relational database.

@coding_knowladge
@coding_knowladge
Q 4. What is a foreign key?
ANS. A foreign key is a column or combination of columns
that establishes a link between data in two tables. It ensures
referential integrity by enforcing relationships between
tables.

Q 5. What is the difference between a primary


key and a unique key?
ANS. A primary key is used to uniquely identify a row in a
table and must have a unique value. On the other hand, a
unique key ensures that a column or combination of columns
has a unique value but does not necessarily identify the row.

Q 6. What is normalization?
ANS. Normalization is the process of organizing data in a
database to minimize redundancy and dependency. It
involves breaking down a table into smaller tables and
establishing relationships between them.
@coding_knowladge
Q 7. What are the different types of normalization?
ANS. First Normal Form (1NF)
Second Normal Form (2NF)
Third Normal Form (3NF)
Boyce-Codd Normal Form (BCNF)
Fourth Normal Form (4NF)
Fifth Normal Form (5NF) or Project-Join Normal Form
(PJNF)

Q 8. What is a join in SQL?


ANS. A join is an operation used to combine rows from two or
more tables based on related columns. It allows you to
retrieve data from multiple tables simultaneously.

Q 9. What is the difference between DELETE and


TRUNCATE in SQL?
ANS. The DELETE statement is used to remove specific rows
from a table based on a condition. It can be rolled back and
generates individual delete operations for each row.

TRUNCATE, on the other hand, is used to remove all rows from


a table. It cannot be rolled back, and it is faster than DELETE as
it deallocates the data pages instead of logging individual row
deletions.
@coding_knowladge
Q 10. What is the difference between UNION and
UNION ALL?
ANS. UNION and UNION ALL are used to combine the result
sets of two or more SELECT statements. UNION removes
duplicate rows from the combined result set. whereas UNION
ALL includes all rows, including duplicates.

Q 11. What is the difference between the HAVING


clause and the WHERE clause?
ANS. The WHERE clause is used to filter rows based on a
condition before the data is grouped or aggregated. It
operates on individual rows.

The HAVING clause, on the other hand, is used to filter


grouped rows based on a condition after the data is grouped
or aggregated using the GROUP BY clause.

Q 12. What is a transaction in SQL?


ANS. A transaction is a sequence of SQL statements that are
executed as a single logical unit of work. It ensures data
consistency and integrity by either committing all changes or
rolling them back if an error occurs.
@coding_knowladge
Q 13. What is the difference between a clustered
and a non-clustered index?
ANS. A clustered index determines the physical order of data
in a table. It changes the way the data is stored on disk and
can be created on only one column. A table can have only one
clustered index. A non-clustered index does not affect the
physical order of data in a table. It is stored separately and
contains a pointer to the actual data. A table can have
multiple non-clustered indexes.

Q 14. What is ACID in the context of database


transactions?
ANS. ACID stands for Atomicity, Consistency, Isolation, and
Durability. It is a set of properties that guarantee reliable
processing of database transactions. Atomicity ensures that a
transaction is treated as a single unit of work, either all or none
of the changes are applied. Consistency ensures that a
transaction brings the database from one valid state to another.
Isolation ensures that concurrent transactions do not interfere
with each other. Durability ensures that once a transaction is
committed, its changes are permanent and survive system
failures.
@coding_knowladge
Q 15. What is a deadlock?
ANS. A deadlock occurs when two or more transactions are
waiting for each other to release resources, resulting in a
circular dependency. As a result, none of the transactions can
proceed, and the system may become unresponsive.

Q 16. What is the difference between a temporary


table and a table variable?
ANS. A temporary table is a table that is created and exists only
for the duration of a session or a transaction. It can be explicitly
dropped or is automatically dropped when the session or
transaction ends. A table variable is a variable that can store a
tablelike structure in memory. It has a limited scope within a
batch, stored procedure, or function. It is automatically
deallocated when the scope ends.

Q 17. What is the purpose of the GROUP BY clause?


ANS. The GROUP BY clause is used to group rows based on one
or more columns in a table. It is typically used in conjunction
with aggregate functions, such as SUM, AVG, COUNT, etc., to
perform calculations on grouped data.
Q 18. What is the difference between a database
and a schema?
ANS. A database is a container that holds multiple objects,
such as tables, views, indexes, and procedures. It represents a
logical grouping of related data. A schema, on the other hand,
is a container within a database that holds objects and
defines their ownership. It provides a way to organize and
manage database objects. @coding_knowladge

Q 19. What is the difference between CHAR and


VARCHAR data types?
ANS. CHAR is a fixed-length string data type, while VARCHAR
is a variable-length string data type.

Q 20. What is a stored procedure?


ANS. A stored procedure is a set of SQL statements that are
stored in the database and can be executed repeatedly. It
provides code reusability and better performance.
Q 21. What is a subquery?
ANS. A subquery is a query nested inside another query. It is
used to retrieve data based on the result of an inner query.
@coding_knowladge
Q 22. What is a view?
ANS. A view is a virtual table based on the result of an SQL
statement. It allows users to retrieve and manipulate data as if

Q 23. What is the difference between a cross join


and an inner join?
ANS. A cross join (Cartesian product) returns the combination
of all rows from two or more tables. An inner join returns only
the matching rows based on a join condition.

Q 24. What is the purpose of the COMMIT


statement?
ANS. The COMMIT statement is used to save changes made in a
transaction permanently. It ends the transaction and makes the
changes visible to other users.

Q 25. What is the purpose of the ROLLBACK


statement?
ANS. The ROLLBACK statement is used to undo changes made
in a transaction. It reverts the database to its previous state
before the transaction started.
@coding_knowladge
Q 26. What is the purpose of the NULL value in
SQL?
ANS. NULL represents the absence of a value or unknown
value. It is different from zero or an empty string and requires
special handling in SQL queries.

Q 27. What is the difference between a view and a


materialized view?
ANS. A materialized view is a physical copy of the view's result
set stored in the database, which is updated periodically. It
improves query performance at the cost of data freshness.

Q 28. What is a correlated subquery?


ANS. A correlated subquery is a subquery that refers to a
column from the outer query. It executes once for each row
processed by the outer query.

Q 29. What is the purpose of the DISTINCT


keyword?
ANS. The DISTINCT keyword is used to retrieve unique values
from a column or combination of columns in a SELECT
statement.
@coding_knowladge
Q 30. What is the difference between the CHAR
and VARCHAR data types?
ANS. CHAR stores fixed-length character strings, while
VARCHAR stores variable-length character strings. The
storage size of CHAR is constant, while VARCHAR adjusts
dynamically.

Q 31. What is the difference between the IN and


EXISTS operators?
ANS.The IN operator checks for a value within a set of values or
the result of a subquery. The EXISTS operator checks for the
existence of rows returned by a subquery.

Q 32. What is the purpose of the TRIGGER


statement?
ANS. The TRIGGER statement is used to associate a set of SQL
statements with a specific event in the database. It is executed
automatically when the event occurs

Q 33. What is the difference between a unique


constraint and a unique index?
ANS. A unique constraint ensures the uniqueness of values in
one or more columns, while a unique index enforces the
uniqueness and also improves query performance.
@coding_knowladge

Q 31. What is the difference between the IN and


EXISTS operators?
Ans: The IN operator checks for a value within a set of
values or the result of a subquery. The EXISTS
operator checks for the existence of rows
returned by a subquery.

Q 32. What is the purpose of the TRIGGER


statement?
Ans: The TRIGGER statement is used to associate a set
of SQL statements with a specific event in the
database. It is executed automatically when the
event occurs

Q 33. What is the difference between a unique


constraint and a unique index?
Ans: A unique constraint ensures the uniqueness of
values in one or more columns, while a unique index
enforces the uniqueness and also improves query
performance.
@coding_knowladge

Q 34. What is the purpose of the TOP or LIMIT


clause?
Ans: The TOP (in SQL Server) or LIMIT (in MySQL) clause
is used to limit the number of rows returned by a
query. It is often used with an ORDER BY clause.

Q 35. What is the difference between the UNION


and JOIN operators?
Ans: UNION combines the result sets of two or more
SELECT statements vertically, while JOIN combines
columns from two or more tables horizontally
based on a join condition.
@coding_knowladge

Q 36. What is a data warehouse?


Ans: A data warehouse is a large, centralized repository
that stores and manages data from various
sources. It is designed for efficient reporting,
analysis, and business intelligence purposes.

Q 37. What is the difference between a primary key


and a candidate key?
Ans: A primary key is a chosen candidate key that
uniquely identifies a row in a table. A candidate key
is a set of one or more columns that could
potentially become the primary key.
@coding_knowladge

Q 38. What is the purpose of the GRANT statement?


Ans: The GRANT statement is used to grant
specific permissions or privileges to users or
roles in a database.

Q 39. What is a correlated update?


Ans: A correlated update is an update statement that
refers to a column from the same table in a
subquery. It updates values based on the result of
the subquery for each row.
@coding_knowladge

Q 40. What is the purpose of the CASE statement?


Ans: The CASE statement is used to perform conditional
logic in SQL queries. It allows you to return
different values based on specified conditions.

Q 41. What is the purpose of the COALESCE


function?
Ans: The COALESCE function returns the first non-null
expression from a list of expressions. It is often used
to handle null values effectively.

Q 42. What is the purpose of the ROW_NUMBER()


function?
Ans: The ROW_NUMBER() function assigns a unique
incremental number to each row in the result set.
It is commonly used for pagination or ranking
purposes.ll values effectively.
@coding_knowladge

Q 43. What is the difference between a natural


join and an inner join?
Ans: A natural join is an inner join that matches rows
based on columns with the same name in the joined
tables. It is automatically determined by the
database.

Q 44. What is the purpose of the CASCADE


DELETE constraint?
Ans: The CASCADE DELETE constraint is used to
automatically delete related rows in child
tables when a row in the parent table is deleted.

Q 45. What is the purpose of the ALL keyword in


SQL?
Ans: The CASCADE DELETE constraint is used to
automatically delete related rows in child
tables when a row in the parent table is deleted.
@coding_knowladge

Q 46. What is the difference between the EXISTS


and NOT EXISTS operators?
Ans: The EXISTS operator returns true if a subquery
returns any rows, while the NOT EXISTS
operator returns true if a subquery returns no
rows.
Q 47. What is the purpose of the CROSS APPLY
operator?
Ans: The CROSS APPLY operator is used to invoke a
table- valued function for each row of a table
expression. It returns the combined result set.
@coding_knowladge
Q 48. What is a self-join?
Ans: A self-join is a join operation where a table is
joined with itself. It is useful when you want to
compare rows within the same table based on
related columns. It requiresbined result set.
Q 49. What is an ALIAS command?
Ans: ALIAS command in SQL is the name that can be
given to any table or a column. This alias name can
be referred in WHERE clause to identify a particular
table or a column.
@coding_knowladge
Q 50. Why are SQL functions used?
Ans: SQL functions are used for the following
purposes:
To perform some calculations on the
data To modify individual data items To
manipulate the output To format dates
and numbers To convert the data types

You might also like