Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

SQL Interview QnA

The document contains a list of the top 30 SQL interview questions and their answers, covering fundamental concepts such as SQL, primary keys, foreign keys, and various SQL commands. It explains the differences between various SQL operations, data types, and indexing, as well as the importance of normalization and ACID properties. Additionally, it discusses the use of subqueries, views, and relationships in databases.

Uploaded by

Arun Vastrad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL Interview QnA

The document contains a list of the top 30 SQL interview questions and their answers, covering fundamental concepts such as SQL, primary keys, foreign keys, and various SQL commands. It explains the differences between various SQL operations, data types, and indexing, as well as the importance of normalization and ACID properties. Additionally, it discusses the use of subqueries, views, and relationships in databases.

Uploaded by

Arun Vastrad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Top 30 SQL Interview Questions and Answers:

1. What is SQL?

Answer: SQL stands for Structured Query Language. It is a programming


language used for managing and manipulating relational databases.

2. What is a primary key?

Answer: A primary key is a unique identifier for a record in a database table. It


ensures that each record in the table can be uniquely identified.

3. What is a foreign key?

Answer: A foreign key is a field in a database table that refers to the primary
key of another table. It establishes a relationship between two tables.

4. What is the difference between DELETE and TRUNCATE in SQL?

Answer: DELETE is a DML (Data Manipulation Language) command that


removes specific records from a table. TRUNCATE is a DDL (Data Definition
Language) command that removes all records from a table.

5. What is the difference between UNION and UNION ALL in SQL?

Answer: UNION combines the result sets of two or more SELECT statements,
removing duplicate rows. UNION ALL also combines result sets but includes
all rows, including duplicates.

6. Explain the difference between INNER JOIN and OUTER JOIN?

Answer: INNER JOIN returns only the matching records from both tables
based on the specified join condition. OUTER JOIN returns all records from
one table and the matching records from the other table(s).

1|Page LinkedIn: Shubham Sultane


7. What is Normalization in SQL?

Answer: Normalization is the process of organizing data in a database to


eliminate redundancy and dependency issues. It involves breaking down a table
into smaller tables and establishing relationships between them.

8. What are Indexes in SQL?

Answer: Indexes are database objects used to improve the performance of data
retrieval operations. They help in faster searching, sorting, and filtering of data
by creating a data structure that allows efficient access to the data.

9. Explain the ACID properties in SQL?

Answer: ACID stands for Atomicity, Consistency, Isolation and Durability.


These properties ensure that database transactions are processed reliably in a
database system. Atomicity ensures that transactions are treated as a single unit.
Consistency ensures that the database remains in a valid state. Isolation ensures
that transactions are independent of each other. Durability ensures that once a
transaction is committed, its changes are permanent.

10. What is a subquery in SQL?

Answer: A subquery is a query inside another query. It is used to retrieve data or


information back from the database. In a subquery, the outer query is called as
the main query whereas the inner query is called subquery. Subqueries are always
executed first and the result of the subquery is passed on to the main query. It can
be nested inside a SELECT, UPDATE or any other query. A subquery can also
use any comparison operators such as >, < or =.

11. What is the difference between a view and a table in SQL?

Answer: A table is a basic structure that holds data, whereas a view is a virtual
table derived from one or more tables. Views are created based on predefined
queries and can be used for data retrieval and manipulation.

2|Page LinkedIn: Shubham Sultane


12. What is the difference between CHAR and VARCHAR data types in
SQL?

Answer: CHAR is a fixed-length character data type, while VARCHAR is a


variable-length character data type. CHAR always stores the specified length,
while VARCHAR only stores the actual data and saves storage space.

13. What is the difference between a stored procedure and a function in


SQL?

Answer: A stored procedure is a set of pre-compiled SQL statements that


perform a specific task. It can modify data and may or may not return a value. A
function, on the other hand, always returns a value and is used for calculations
or data manipulation.

14. Explain the GROUP BY clause in SQL?

Answer: The GROUP BY clause is used to group rows based on a specified


column(s) in a SELECT statement. It is often used in conjunction with
aggregate functions like SUM, COUNT, AVG, etc., to perform calculations on
grouped data.

15. What is the difference between HAVING clause and WHERE clause in
SQL?

Answer: The WHERE clause is used to filter rows before grouping them,
whereas the HAVING clause is used to filter rows after grouping them. The
HAVING clause is applied to the result of the GROUP BY operation.

16. Explain the difference between a clustered index and a non-clustered


index?

Answer: A clustered index determines the physical order of data in a table.


There can only be one clustered index per table. A non-clustered index is a
separate structure that contains a sorted list of keys and a pointer to the
corresponding data.

3|Page LinkedIn: Shubham Sultane


17. What is the difference between a LEFT JOIN and a RIGHT JOIN?

Answer: In a LEFT JOIN, all records from the left table and the matching
records from the right table are returned. In a RIGHT JOIN, all records from the
right table and the matching records from the left table are returned.

18. What is the difference between a database and a schema in SQL?

Answer: A database is a collection of related data, while a schema is a logical


container within a database that holds objects such as tables, views, procedures,
etc. A database can have multiple schemas.

19. Explain the difference between CHARINDEX and INDEXOF functions


in SQL?

Answer: CHARINDEX and INDEXOF are similar functions that return the
starting position of a specified substring within a string. CHARINDEX is used
in SQL Server, while INDEXOF is used in other database systems.

20. What is a self-join in SQL?

Answer: A self-join is a type of join where a table is joined with itself. It is


used to combine rows from a single table based on a related column.

21. What is a UNIQUE constraint?

Answer: The UNIQUE Constraint prevents identical values in a column from


appearing in two records. The UNIQUE constraint guarantees that every value
in a column is unique.

22. What is the SELECT statement?

Answer: A SELECT command gets zero or more rows from one or more
database tables or views. The most frequent data manipulation language (DML)
command is SELECT in most applications. SELECT queries define a result set,
but not how to calculate it, because SQL is a declarative programming
language.

4|Page LinkedIn: Shubham Sultane


23. What is the difference between cross join and natural join?

Answer: The cross join produces the cross product or Cartesian product of two
tables whereas the natural join is based on all the columns having the same name
and data types in both the tables.

24. What are the different types of a subquery?


Answer: There are two types of subquery namely, Correlated and Non-
Correlated.

Correlated subquery: These are queries which select the data from a table
referenced in the outer query. It is not considered as an independent query as it
refers to another table and refers the column in a table.

Non-Correlated subquery: This query is an independent query where the output


of subquery is substituted in the main query.

25. What is the need for group functions in SQL?


Answer: Group functions work on the set of rows and return one result per group.
Some of the commonly used group functions are: AVG, COUNT, MAX, MIN,
SUM, and VARIANCE.

26. What is a Relationship and what are they?


Answer: Relation or links are between entities that have something to do with
each other. Relationships are defined as the connection between the tables in a
database. There are various relationships, namely:

One to One Relationship.


One to Many Relationship
Many to One Relationship.
Self-Referencing Relationship.

27. How can you insert NULL values in a column while inserting the data?
Answer: NULL values in SQL can be inserted in the following ways:

Implicitly by omitting column from column list.


Explicitly by specifying NULL keyword in the VALUES clause

5|Page LinkedIn: Shubham Sultane


operators?
Answer: BETWEEN operator is used to display rows based on a range of values
in a row whereas the IN-condition operator is used to check for values contained
in a specific set of values.

29. What is the main difference between SQL and PL/SQL?


Answer: SQL is a query language that allows you to issue a single query or

which allows you to write a full program (loops, variables, etc.)


to accomplish multiple operations such as selects/inserts/updates/deletes.

30. What is a View?


Answer: A view is a virtual table which consists of a subset of data contained in
a table. Since views are not present, it takes less space to store. View can have
data of one or more tables combined and it depends on the relationship.

6|Page LinkedIn: Shubham Sultane

You might also like