SQL Interview Questions
What is SQL and what is it used for?
Answer: SQL (Structured Query Language) is a standard language used to manage and manipulate relational databases. It is used to create, modify, and query databases to retrieve and store data.What is a relational database?
Answer: A relational database is a type of database that organizes data into one or more tables and establishes relationships between the tables based on common columns.What is a primary key in a database?
Answer: A primary key is a unique identifier for each record in a table. It is used to enforce the integrity of the data and ensure that each record has a unique identifier.What is a foreign key in a database?
Answer: A foreign key is a column in a table that refers to the primary key of another table. It is used to establish relationships between tables and enforce referential integrity.What is a join in SQL?
Answer: A join in SQL is an operation that combines rows from two or more tables based on a common column between them. The result is a single table that contains columns from all of the joined tables.What is a subquery in SQL?
Answer: A subquery in SQL is a query nested inside another query. It is used to retrieve data that will be used as input to the outer query.What is an index in a database?
Answer: An index in a database is a data structure that allows faster retrieval of data from a table. It works by creating a mapping between the values in the indexed column and their corresponding rows in the table.What is a view in a database?
Answer: A view in a database is a virtual table that is derived from one or more tables. It is used to simplify the structure of a database and allow users to access data from multiple tables as if they were a single table.What is the difference between a clustered and non-clustered index?
Answer: A clustered index determines the physical order of data in a table, whereas a non-clustered index provides a mapping between the values in a column and the corresponding rows in a table without changing the physical order of the data.What is a stored procedure in SQL?
Answer: A stored procedure in SQL is a pre-compiled collection of SQL statements that can be executed as a single unit. It is used to encapsulate complex logic and improve the performance and maintainability of a database system.What is a transaction in a database?
Answer: A transaction in a database is a series of one or more SQL statements that are executed as a single unit of work. A transaction is either committed or rolled back, ensuring that the data remains in a consistent state.What is the difference between
TRUNCATE
andDELETE
in SQL?
Answer: TheTRUNCATE
statement in SQL is used to delete all data from a table, whereas theDELETE
statement is used to delete specific rows from a table. TheTRUNCATE
statement is faster than theDELETE
statement because it does not generate any rollback information and does not fire any triggers.What is a trigger in SQL?
Answer: A trigger in SQL is a special type of stored procedure that is automatically executed in response to specific events, such as the insertion, update, or deletion of data in a table.
What is a constraint in SQL?
Answer: A constraint in SQL is a rule that is applied to a column or a set of columns in a table to ensure the integrity of the data. Constraints can be used to enforce unique values, referential integrity, or limit the range of values that can be inserted into a column.What is a normalization in a database?
Answer: Normalization in a database is the process of organizing data into separate tables to minimize data redundancy and improve data consistency. Normalization is typically performed by dividing a database into two or more tables and defining relationships between the tables.What is the difference between
GROUP BY
andHAVING
in SQL?
Answer: TheGROUP BY
clause in SQL is used to group rows that have the same values in specified columns into summary rows, whereas theHAVING
clause is used to filter groups based on aggregate values, such as the count, sum, or average of the values in a column.What is the difference between
WHERE
andHAVING
in SQL?
Answer: TheWHERE
clause in SQL is used to filter rows based on specific conditions, whereas theHAVING
clause is used to filter groups based on aggregate values, such as the count, sum, or average of the values in a column.What is a data type in SQL?
Answer: A data type in SQL is a classification that defines the type of data that can be stored in a column in a table. Common data types in SQL include string, numeric, date and time, and Boolean.What is a NULL value in SQL?
Answer: A NULL value in SQL is a special marker used to indicate that a data value does not exist in the database. It is used to represent missing or unknown values.What is the difference between
UNION
andUNION ALL
in SQL?
Answer: TheUNION
operator in SQL is used to combine the results of two or more SELECT statements and return only the unique values, whereas theUNION ALL
operator is used to combine the results of two or more SELECT statements and return all values, including duplicates.What is a correlated subquery in SQL?
Answer: A correlated subquery in SQL is a subquery that is executed once for each row of the outer query. The subquery references columns from the outer query and returns a result that is used as input for the outer query.What is a cursor in SQL?
Answer: A cursor in SQL is a database object that is used to retrieve and manipulate data from a result set one row at a time. Cursors are typically used for processing large amounts of data in a controlled and efficient manner.What is a stored function in SQL?
Answer: A stored function in SQL is a pre-compiled collection of SQL statements that can be executed as a single unit and return a single value. Stored functions are similar to stored procedures, but they return a value, whereas stored procedures do not.What is the difference between
INNER JOIN
andOUTER JOIN
in SQL?
Answer: TheINNER JOIN
in SQL returns only the rows from both tables where the join condition is true. AnOUTER JOIN
returns all the rows from one table, and the matching rows from the other table. If a row in one table does not have a matching row in the other table, the result set will contain NULL values for the columns of the non-matching table.
What is a self-join in SQL?
Answer: A self-join in SQL is a regular join, but the table is joined with itself. This is useful when you want to compare rows within the same table.What is an index in SQL?
Answer: An index in SQL is a database object that is used to improve query performance. An index creates a mapping between the values in a column and their corresponding rows in the table, allowing the database to locate the desired data more quickly.What is a view in SQL?
Answer: A view in SQL is a virtual table that is based on the result of a SELECT statement. A view can be used to simplify complex queries, to provide access to specific data, or to enforce data security by limiting access to sensitive columns.What is a subquery in SQL?
Answer: A subquery in SQL is a SELECT statement that is nested inside another SELECT statement. The result of a subquery is used as an input to the outer query.What is a transaction in SQL?
Answer: A transaction in SQL is a sequence of one or more database operations that are executed as a single unit of work. Transactions are used to ensure data consistency and to handle errors or exceptions in a controlled manner.What is a rollback in SQL?
Answer: A rollback in SQL is the process of canceling a transaction and undoing any changes that have been made to the database during the transaction. A rollback is typically performed when an error or exception is encountered during the execution of a transaction.What is a primary key in SQL?
Answer: A primary key in SQL is a column or a set of columns in a table that uniquely identifies each row in the table. A primary key is used to enforce referential integrity and to prevent duplicate values in the table.What is a foreign key in SQL?
Answer: A foreign key in SQL is a column or a set of columns in a table that refers to the primary key of another table. A foreign key is used to enforce referential integrity and to establish relationships between tables.What is a cascade in SQL?
Answer: A cascade in SQL is an action that is performed on a parent table when a related row is deleted or updated in a child table. A cascade can be used to automatically update or delete related rows in a child table when a row in a parent table is modified.What is a trigger in SQL?
Answer: A trigger in SQL is a special type of stored procedure that is automatically executed in response to specific events, such as the insertion, update, or deletion of data in a table.What is a constraint in SQL?
Answer: A constraint in SQL is a rule that is applied to a column or a set of columns in a table to ensure the integrity of the data. Constraints can be used to enforce unique values, referential integrity, or limit the range of values that can be inserted into a column.
What is a
GROUP BY
clause in SQL?
Answer: TheGROUP BY
clause in SQL is used to group rows in a table based on the values in one or more columns. The result of aGROUP BY
query is a set of aggregated data, where each group of rows is treated as a single row in the result set.What is a
HAVING
clause in SQL?
Answer: TheHAVING
clause in SQL is used to filter the result set of aGROUP BY
query based on aggregate conditions. TheHAVING
clause is used to restrict the groups that are returned based on the result of an aggregate function, such asCOUNT()
,SUM()
, orAVG()
.What is a
WITH
clause in SQL?
Answer: TheWITH
clause in SQL, also known as a Common Table Expression (CTE), is used to create a temporary, named result set that can be used within a SELECT, INSERT, UPDATE, or DELETE statement. A CTE can simplify complex queries by breaking them down into smaller, more manageable parts.What is a
UNION
operator in SQL?
Answer: TheUNION
operator in SQL is used to combine the result sets of two or more SELECT statements into a single result set. TheUNION
operator removes duplicates from the combined result set.What is a
MINUS
operator in SQL?
Answer: TheMINUS
operator in SQL is used to return the rows from the first SELECT statement that are not present in the result set of the second SELECT statement. TheMINUS
operator can be used to subtract one set of data from another.What is a
INTERSECT
operator in SQL?
Answer: TheINTERSECT
operator in SQL is used to return the rows that are common to the result sets of two or more SELECT statements. TheINTERSECT
operator can be used to find the intersection of two or more sets of data.What is a
LEFT JOIN
in SQL?
Answer: TheLEFT JOIN
in SQL is used to return all the rows from the left table, and the matching rows from the right table. If a row in the right table does not have a matching row in the left table, the result set will contain NULL values for the columns of the right table.What is a
RIGHT JOIN
in SQL?
Answer: TheRIGHT JOIN
in SQL is used to return all the rows from the right table, and the matching rows from the left table. If a row in the left table does not have a matching row in the right table, the result set will contain NULL values for the columns of the left table.What is a
FULL OUTER JOIN
in SQL?
Answer: TheFULL OUTER JOIN
in SQL is used to return all the rows from both tables, and the matching rows from the other table. If a row in one table does not have a matching row in the other table, the result set will contain NULL values for the columns of the non-matching table.What is a
CASE
statement in SQL?
Answer: TheCASE
statement in SQL is used to perform conditional logic within a query. TheCASE
statement allows you to return a value based on a specified condition.
What is the
LIKE
operator in SQL?
Answer: TheLIKE
operator in SQL is used to perform pattern matching in a query. TheLIKE
operator is used to search for a specified pattern within a column.What is a
NULL
value in SQL?
Answer: ANULL
value in SQL is a value that represents the absence of data. ANULL
value is used to indicate that a column does not contain any data.What is the difference between
NULL
andNOT NULL
in SQL?
Answer: The difference betweenNULL
andNOT NULL
in SQL is thatNULL
allows a column to contain aNULL
value, whileNOT NULL
specifies that a column must contain a value.What is a
UNIQUE
constraint in SQL?
Answer: AUNIQUE
constraint in SQL is used to enforce the uniqueness of values in a column. AUNIQUE
constraint specifies that the values in a column must be unique across all rows in a table.What is a
PRIMARY KEY
constraint in SQL?
Answer: APRIMARY KEY
constraint in SQL is used to enforce the uniqueness of values in a column and to identify a column as the primary key of a table. APRIMARY KEY
constraint is used to enforce referential integrity by creating a relationship between the primary key and foreign key in related tables.What is a
FOREIGN KEY
constraint in SQL?
Answer: AFOREIGN KEY
constraint in SQL is used to enforce referential integrity by creating a relationship between the primary key and foreign key in related tables. AFOREIGN KEY
constraint specifies that the values in a column must match the values in the primary key of another table.What is a
DEFAULT
constraint in SQL?
Answer: ADEFAULT
constraint in SQL is used to specify a default value for a column. If aDEFAULT
constraint is defined for a column, the database will use the specified default value if no value is provided when a new row is inserted.What is an
INDEX
in SQL?
Answer: AnINDEX
in SQL is used to improve the performance of queries by allowing the database to quickly locate rows that match a specified condition. AnINDEX
creates a data structure that can be searched more efficiently than the underlying table.What is a
VIEW
in SQL?
Answer: AVIEW
in SQL is a virtual table that is based on the result of a SELECT statement. AVIEW
does not store data, but instead provides a way to access the data in one or more tables as if it were a single table.What is a
TRIGGER
in SQL?
Answer: ATRIGGER
in SQL is a database object that is automatically executed in response to specific changes in the data in a table. Triggers can be used to enforce business rules or to automatically perform actions when data is inserted, updated, or deleted.
Leave a Comment