Frequently Asked SQL Interview Questions


What is SQL and what is it used for?

SQL (Structured Query Language) is a programming language used to manage and manipulate relational databases. It is used to create, modify, and query databases to store and retrieve data efficiently.

What are the types of SQL statements?

There are three types of SQL statements: Data Definition Language (DDL), Data Manipulation Language (DML), and Data Control Language (DCL). DDL statements are used to create, modify, and delete database objects, while DML statements are used to insert, update, and delete data in the database. DCL statements are used to grant or revoke access to the database.

What is normalization and why is it important?

Normalization is the process of organizing data in a database to reduce redundancy and improve data integrity. It is important because it eliminates duplicate data and reduces the chances of data inconsistencies, which can lead to data corruption and errors.

What is a join and what are the different types of joins?

A join is used to combine data from two or more tables based on a common column or key. The different types of joins are inner join, left join, right join, and full outer join. Inner join returns only the matching rows between two tables, left join returns all rows from the left table and matching rows from the right table, right join returns all rows from the right table and matching rows from the left table, and full outer join returns all rows from both tables.

What is a subquery and how is it used?

A subquery is a query within a query that is used to retrieve data that will be used by the main query. It is used to filter and manipulate data before it is used in the main query.

What is a view and how is it used?

A view is a virtual table that is based on the result of a select statement. It is used to simplify complex queries, improve performance, and control access to data by limiting the data that can be accessed by a user or application.

What is an index and how is it used?

An index is a data structure used to improve the performance of queries by allowing the database to quickly retrieve data based on a specific column or columns. It is used to speed up searches and reduce the time it takes to retrieve data.

What is a stored procedure and how is it used?

A stored procedure is a precompiled set of SQL statements that is stored in a database and can be executed by calling it from an application. It is used to encapsulate complex logic and improve performance by reducing the amount of data transferred between the application and the database.

What is a transaction and why is it important?

A transaction is a sequence of SQL statements that are executed as a single unit of work. It is important because it ensures data integrity by guaranteeing that all statements within a transaction are executed or none are executed. If a transaction fails, it can be rolled back to the previous state, ensuring data consistency.

What is a primary key and why is it important?

A primary key is a column or combination of columns that uniquely identifies each row in a table. It is important because it ensures data integrity by preventing duplicate data and providing a way to uniquely identify each row in the table.

What is a foreign key and how is it used?

A foreign key is a column or combination of columns that links a table to another table’s primary key. It is used to enforce referential integrity, ensuring that data in one table corresponds to data in another table.

What is a trigger and how is it used?

A trigger is a set of SQL statements that are automatically executed in response to certain events, such as a change in data or a database operation. It is used to enforce business rules, maintain data integrity, and automate data processing.

What is a constraint and how is it used?

A constraint is a rule that is applied to a column or a table to enforce data integrity. It is used to prevent invalid data from being entered into a database and to maintain the consistency of the data.

What is a NULL value and how is it used?

A NULL value is a value that represents the absence of data. It is used to indicate that a particular value is not known or does not exist. A NULL value can be used in columns that are optional or that do not have a value at the time of data entry.

What is a group by clause and how is it used?

A group by clause is used to group the result set by one or more columns. It is used with aggregate functions such as sum, count, and average to summarize data and provide meaningful insights.

What is a having clause and how is it used?

A having clause is used to filter data based on a condition that is applied to the results of a group by clause. It is used to filter data based on aggregate functions such as sum, count, and average.

What is a case statement and how is it used?

A case statement is used to conditionally execute SQL statements based on one or more conditions. It is used to transform data, calculate new values, and format output.

What is a cross join and how is it used?

A cross join is used to create a Cartesian product of two or more tables, which means that each row in one table is combined with every row in another table. It is used when there is no common column between the tables and is typically used with small tables.

What is a self-join and how is it used?

A self-join is used to join a table to itself. It is used when a table contains a hierarchical structure, such as an organizational chart, and is used to retrieve data at different levels of the hierarchy.

What is a temporary table and how is it used?

A temporary table is a table that is created in memory or on disk and is used to store intermediate results or temporary data. It is used to simplify complex queries, improve performance, and reduce the amount of data transferred between the application and the database.