sql_interview_questions
sql_interview_questions
1. 1. What is SQL?
Answer: WHERE is used to filter rows before grouping, while HAVING is used to filter
groups after aggregation.
Answer: A primary key uniquely identifies each record in a table. It must contain unique
values and cannot contain NULLs.
Answer: A foreign key is a column that creates a relationship between two tables. It refers
to the primary key in another table.
6. 6. What is normalization?
Answer: JOIN is used to combine rows from two or more tables based on related columns.
Types include:
- INNER JOIN
- LEFT JOIN (LEFT OUTER JOIN)
- RIGHT JOIN (RIGHT OUTER JOIN)
- FULL JOIN (FULL OUTER JOIN)
- CROSS JOIN
- SELF JOIN
Answer: UNION removes duplicate records, while UNION ALL includes all duplicates.
Answer: A subquery is a query nested inside another SQL query. It can be used in SELECT,
FROM, or WHERE clauses.
Answer: Indexing is used to speed up the retrieval of rows by using a pointer. It is similar to
an index in a book.
Answer: DELETE removes specific rows and logs each row deletion; TRUNCATE removes all
rows without logging individual deletions.
Answer: A view is a virtual table based on the result set of a SELECT query. It simplifies
complex queries and provides data security.
Answer: A stored procedure is a group of SQL statements that perform a task, stored in the
database and can be executed repeatedly.
15. 15. What is the difference between a clustered and a non-clustered index?
Answer: A clustered index determines the physical order of data in a table; only one per
table. A non-clustered index creates a separate structure to point to data locations and can
be multiple per table.
Answer: Constraints are rules enforced on data in tables, such as NOT NULL, UNIQUE,
PRIMARY KEY, FOREIGN KEY, CHECK, and DEFAULT.
Answer: Functions that perform a calculation on a set of values and return a single value.
Examples: COUNT(), SUM(), AVG(), MAX(), MIN().
Answer: ```sql
SELECT column_name, COUNT(*)
FROM table_name
GROUP BY column_name
HAVING COUNT(*) > 1;
```
20. 20. What is the difference between RANK(), DENSE_RANK(), and ROW_NUMBER()?