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
TRUNCATEandDELETEin SQL?
Answer: TheTRUNCATEstatement in SQL is used to delete all data from a table, whereas theDELETEstatement is used to delete specific rows from a table. TheTRUNCATEstatement is faster than theDELETEstatement 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 BYandHAVINGin SQL?
Answer: TheGROUP BYclause in SQL is used to group rows that have the same values in specified columns into summary rows, whereas theHAVINGclause 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
WHEREandHAVINGin SQL?
Answer: TheWHEREclause in SQL is used to filter rows based on specific conditions, whereas theHAVINGclause 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
UNIONandUNION ALLin SQL?
Answer: TheUNIONoperator in SQL is used to combine the results of two or more SELECT statements and return only the unique values, whereas theUNION ALLoperator 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 JOINandOUTER JOINin SQL?
Answer: TheINNER JOINin SQL returns only the rows from both tables where the join condition is true. AnOUTER JOINreturns 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 BYclause in SQL?
Answer: TheGROUP BYclause in SQL is used to group rows in a table based on the values in one or more columns. The result of aGROUP BYquery is a set of aggregated data, where each group of rows is treated as a single row in the result set.What is a
HAVINGclause in SQL?
Answer: TheHAVINGclause in SQL is used to filter the result set of aGROUP BYquery based on aggregate conditions. TheHAVINGclause is used to restrict the groups that are returned based on the result of an aggregate function, such asCOUNT(),SUM(), orAVG().What is a
WITHclause in SQL?
Answer: TheWITHclause 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
UNIONoperator in SQL?
Answer: TheUNIONoperator in SQL is used to combine the result sets of two or more SELECT statements into a single result set. TheUNIONoperator removes duplicates from the combined result set.What is a
MINUSoperator in SQL?
Answer: TheMINUSoperator 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. TheMINUSoperator can be used to subtract one set of data from another.What is a
INTERSECToperator in SQL?
Answer: TheINTERSECToperator in SQL is used to return the rows that are common to the result sets of two or more SELECT statements. TheINTERSECToperator can be used to find the intersection of two or more sets of data.What is a
LEFT JOINin SQL?
Answer: TheLEFT JOINin 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 JOINin SQL?
Answer: TheRIGHT JOINin 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 JOINin SQL?
Answer: TheFULL OUTER JOINin 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
CASEstatement in SQL?
Answer: TheCASEstatement in SQL is used to perform conditional logic within a query. TheCASEstatement allows you to return a value based on a specified condition.
What is the
LIKEoperator in SQL?
Answer: TheLIKEoperator in SQL is used to perform pattern matching in a query. TheLIKEoperator is used to search for a specified pattern within a column.What is a
NULLvalue in SQL?
Answer: ANULLvalue in SQL is a value that represents the absence of data. ANULLvalue is used to indicate that a column does not contain any data.What is the difference between
NULLandNOT NULLin SQL?
Answer: The difference betweenNULLandNOT NULLin SQL is thatNULLallows a column to contain aNULLvalue, whileNOT NULLspecifies that a column must contain a value.What is a
UNIQUEconstraint in SQL?
Answer: AUNIQUEconstraint in SQL is used to enforce the uniqueness of values in a column. AUNIQUEconstraint specifies that the values in a column must be unique across all rows in a table.What is a
PRIMARY KEYconstraint in SQL?
Answer: APRIMARY KEYconstraint 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 KEYconstraint is used to enforce referential integrity by creating a relationship between the primary key and foreign key in related tables.What is a
FOREIGN KEYconstraint in SQL?
Answer: AFOREIGN KEYconstraint in SQL is used to enforce referential integrity by creating a relationship between the primary key and foreign key in related tables. AFOREIGN KEYconstraint specifies that the values in a column must match the values in the primary key of another table.What is a
DEFAULTconstraint in SQL?
Answer: ADEFAULTconstraint in SQL is used to specify a default value for a column. If aDEFAULTconstraint 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
INDEXin SQL?
Answer: AnINDEXin SQL is used to improve the performance of queries by allowing the database to quickly locate rows that match a specified condition. AnINDEXcreates a data structure that can be searched more efficiently than the underlying table.What is a
VIEWin SQL?
Answer: AVIEWin SQL is a virtual table that is based on the result of a SELECT statement. AVIEWdoes 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
TRIGGERin SQL?
Answer: ATRIGGERin 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