Interview Questions

1) What is SQL?


SQL (Structured Query Language) is a standard programming language used to manage and manipulate relational databases.

2) What are the types of SQL commands?


The main types of SQL commands are:

  • DML (Data Manipulation Language) - SELECT, INSERT, UPDATE, DELETE
  • DDL (Data Definition Language) - CREATE, ALTER, DROP
  • DCL (Data Control Language) - GRANT, REVOKE
  • TCL (Transaction Control Language) - COMMIT, ROLLBACK, SAVEPOINT

3) What is the difference between INNER JOIN and LEFT JOIN?


  • INNER JOIN returns records that have matching values in both tables.
  • LEFT JOIN returns all records from the left table and the matched records from the right table. If no match, NULL is returned for right table columns.

4) What is normalization?


Normalization is the process of organizing data in a database to reduce redundancy and improve data integrity. It involves dividing large tables into smaller tables and using relationships between them.

5) What are the different types of joins in SQL?


  • INNER JOIN
  • LEFT JOIN
  • RIGHT JOIN
  • FULL OUTER JOIN
  • CROSS JOIN
  • SELF JOIN

6) What is a primary key?


A primary key is a column or a set of columns in a table that uniquely identifies each row in that table. It must contain unique values and cannot contain NULLs.

7) What is a foreign key?


A foreign key is a column that creates a relationship between two tables. It points to the primary key in another table, ensuring referential integrity.

8) What is the difference between DELETE and TRUNCATE?


    • DELETE removes rows from a table based on a condition, can be rolled back.
    • TRUNCATE removes all rows from a table without logging individual row deletions, cannot be rolled back.

 

 

9) What is an index in SQL?


An index is a database object that improves the speed of data retrieval operations on a table at the cost of additional space and slower insertions, deletions, and updates.

10) What is the difference between HAVING and WHERE?


  • WHERE is used to filter rows before aggregation.
  • HAVING is used to filter rows after aggregation.

11) What is a subquery?


Answer: A subquery is a query within another query, typically within the WHERE, FROM, or SELECT clause, used to return results that are then used by the outer query.

12) What are aggregate functions in SQL?


Aggregate functions perform a calculation on a set of values to return a single value. Common aggregate functions include:

  • COUNT()
  • SUM()
  • AVG()
  • MIN()
  • MAX()

13) What is a view in SQL?


A view is a virtual table based on the result set of a SQL query. It does not store data but provides a way to look at data from one or more tables.

14) What is the difference between UNION and UNION ALL?


  • UNION combines the results of two queries, removing duplicates.
  • UNION ALL combines the results without removing duplicates.

15) What is a transaction in SQL?


A transaction is a sequence of SQL statements that are executed as a single unit of work. It ensures that all changes to the database are committed or rolled back together.

16) What is a stored procedure?


A stored procedure is a precompiled collection of one or more SQL statements that can be executed on demand, typically to perform a specific task.

17) What is the difference between CHAR and VARCHAR?


  • CHAR is a fixed-length string data type.
  • VARCHAR is a variable-length string data type.

18) What is a trigger in SQL?


A trigger is a set of SQL statements automatically executed in response to certain events (e.g., INSERT, UPDATE, DELETE) on a table.

19) What is the GROUP BY clause used for?


The GROUP BY clause is used to group rows that have the same values in specified columns into summary rows, like calculating aggregate values.

20) What is the DISTINCT keyword used for?


The DISTINCT keyword is used to remove duplicate values from the result set.

21) What is a composite key?


A composite key is a primary key that consists of two or more columns in a table, where the combination of values uniquely identifies a row.

 

22) What is a self join?


A self join is a type of join where a table is joined with itself.

23) What is the purpose of the LIKE operator in SQL?


The LIKE operator is used to search for a specified pattern in a column.

24) What is an alias in SQL?


An alias is a temporary name assigned to a table or column for the purpose of making the query more readable or concise.

25) What is a non-clustered index?


A non-clustered index is an index that stores a separate structure from the data, and it points to the location of data instead of storing the data itself.

26) What is the purpose of RANK() in SQL?


The RANK() function assigns a rank to each row within a partition of a result set, with gaps between ranks for duplicate values.

27) What is a transaction isolation level?


Transaction isolation level determines the visibility of uncommitted changes made by one transaction to other transactions. Levels include:

  • READ UNCOMMITTED
  • READ COMMITTED
  • REPEATABLE READ
  • SERIALIZABLE

28) What is a correlated subquery?


A correlated subquery is a subquery that references columns from the outer query, making it dependent on the outer query for execution.

29) What is a CASE statement in SQL?


The CASE statement is used to create conditional logic in SQL queries, allowing you to return different values based on specified conditions.

30) What is EXPLAIN in SQL?


EXPLAIN is a command used to show the execution plan of a query, which helps analyze and optimize the performance of the query.