SQL Interview QnA
SQL Interview QnA
1. What is SQL?
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.
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.
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).
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.
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.
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.
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.
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.
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.
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.
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.
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: