21-February 2023
Training

SQL Interview Questions

..
SQL Interview Questions

 

SQL Interview Questions with Correct Answer and Explanation 

 

What is SQL?

Answer: SQL stands for Structured Query Language. It is a programming language that is used to manage and manipulate relational databases.

 

Explanation: SQL is a standard language used to manage and manipulate data in relational databases. It is used to create, modify and delete tables, add, modify and delete records in tables, retrieve data from tables and perform various types of queries on data stored in tables.

 

What is a database?

Answer: A database is a collection of data that is organized and stored in a computer system.

 

Explanation: A database is a collection of data that is organized and stored in a computer system. It can be a relational database, hierarchical database, network database or object-oriented database. A relational database is the most common type of database that is used in organizations today.

 

What is a table in a database?

Answer: A table is a collection of related data that is organized in rows and columns.

 

Explanation: A table in a database is a collection of related data that is organized in rows and columns. Each column represents a specific attribute of the data and each row represents a specific record. Tables are used to store data in a database and are essential for data management.

 

What is a primary key in a table?

Answer: A primary key is a column or a set of columns that uniquely identifies each record in a table.

 

Explanation: A primary key in a table is used to uniquely identify each record in a table. It ensures that each record is unique and helps to maintain the integrity of the data in the table. A primary key can be a single column or a set of columns, and it cannot have null values.

 

What is a foreign key in a table?

Answer: A foreign key is a column or a set of columns that refers to the primary key of another table.

Explanation: A foreign key in a table is used to establish a relationship between two tables. It refers to the primary key of another table and ensures that the data in the two tables is consistent. A foreign key can be a single column or a set of columns and it can have null values.

 

What is a join in SQL?

Answer: A join in SQL is used to combine rows from two or more tables based on a related column between them.

Explanation: A join in SQL is used to combine rows from two or more tables based on a related column between them. There are different types of joins, such as inner join, left join, right join, and full outer join. Joining tables is an important aspect of working with relational databases and enables us to combine data from multiple tables to perform complex queries.

 

What is normalization in a database?

Answer: Normalization is the process of organizing data in a database to reduce redundancy and improve data integrity.

Explanation: Normalization is an important process in database design that helps to reduce redundancy and improve data integrity. It involves breaking down tables into smaller tables and establishing relationships between them. There are different levels of normalization, such as first normal form (1NF), second normal form (2NF), and third normal form (3NF).

 

What is a stored procedure in SQL?

Answer: A stored procedure is a set of SQL statements that are stored in the database and can be executed by calling the procedure.

Explanation: A stored procedure in SQL is a set of SQL statements that are stored in the database and can be executed by calling the procedure. Stored procedures are useful for performing complex queries or operations that need to be executed frequently. They can be used to improve performance, reduce network traffic, and enhance security.

 

What is a subquery in SQL?

Answer: A subquery in SQL is a query that is nested inside another query.

Explanation: A subquery in SQL is a query that is embedded within another query. It is used to retrieve data that will be used in the main query. Subqueries can be used in the SELECT, FROM, WHERE, and HAVING clauses of a query.

 

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.

Explanation: A view in SQL is a virtual table that is created based on the result of a SELECT statement. It is used to simplify complex queries and to restrict access to certain data. A view can be used to provide a customized view of the data in a table and can be queried like a regular table.

 

What is a trigger in SQL?

Answer: A trigger in SQL is a set of SQL statements that are automatically executed in response to a specific event.

Explanation: A trigger in SQL is a set of SQL statements that are automatically executed in response to a specific event, such as an insert, update or delete operation on a table. Triggers can be used to enforce data integrity, audit data changes, and perform other automated tasks.

 

What is a stored function in SQL?

Answer: A stored function in SQL is a user-defined function that is stored in the database and can be called from SQL statements.

Explanation: A stored function in SQL is a user-defined function that is stored in the database and can be called from SQL statements. It is used to perform complex calculations or transformations on data. Stored functions can take input parameters and return a value.

 

What is the difference between a stored procedure and a stored function in SQL?

Answer: A stored procedure in SQL is a set of SQL statements that are stored in the database and can be executed by calling the procedure, while a stored function is a user-defined function that is stored in the database and can be called from SQL statements.

Explanation: Stored procedures are used to perform complex queries or operations that need to be executed frequently. They can be used to improve performance, reduce network traffic, and enhance security. Stored functions, on the other hand, are used to perform complex calculations or transformations on data. They can take input parameters and return a value.

 

What is the difference between a clustered and a non-clustered index in SQL?

Answer: A clustered index in SQL determines the physical order of data in a table, while a non-clustered index creates a separate structure to store the indexed data.

Explanation: A clustered index in SQL determines the physical order of data in a table, which means that it can only be created on one column per table. Non-clustered indexes, on the other hand, create a separate structure to store the indexed data and can be created on multiple columns per table.

 

What is the difference between a left join and a right join in SQL?

Answer: A left join in SQL returns all the rows from the left table and the matching rows from the right table, while a right join returns all the rows from the right table and the matching rows from the left table.

Explanation: Left join and right join are two types of outer joins in SQL. In a left join, all the rows from the left table are returned along with the matching rows from the right table. In a right join, all the rows from the right table are returned along with the matching rows from the left table. If there are no matching rows in the right table, the result will contain null values.

 

What is the difference between a primary key and a unique key in SQL?

Answer: A primary key is a column or set of columns that uniquely identifies each row in a table, while a unique key is a column or set of columns that ensures the values in the column or columns are unique.

Explanation: Both primary keys and unique keys are used to ensure that each row in a table is unique, but there are some differences. A primary key is a column or set of columns that uniquely identifies each row in a table and cannot contain null values. A unique key, on the other hand, is a column or set of columns that ensures the values in the column or columns are unique and can contain null values.

 

What is a self-join in SQL?

Answer: A self-join in SQL is a join where a table is joined with itself.

Explanation: A self-join is a join where a table is joined with itself. It is used to compare rows within the same table. To perform a self-join, the table must be given an alias so that it can be referred to in the join statement.

 

What is the difference between the WHERE and HAVING clauses in SQL?

Answer: The WHERE clause is used to filter rows based on a condition, while the HAVING clause is used to filter groups based on a condition.

Explanation: The WHERE clause is used to filter rows based on a condition. It is used with the SELECT, UPDATE, and DELETE statements. The HAVING clause, on the other hand, is used to filter groups based on a condition. It is used with the GROUP BY clause in a SELECT statement.

 

What is the difference between an inner join and an outer join in SQL?

Answer: An inner join in SQL returns only the rows that have matching values in both tables, while an outer join returns all the rows from one table and the matching rows from the other table.

Explanation: An inner join in SQL returns only the rows that have matching values in both tables. It is used to combine data from two tables based on a common column. An outer join, on the other hand, returns all the rows from one table and the matching rows from the other table. If there are no matching rows in the second table, the result will contain null values.

 

What is the purpose of the GROUP BY clause in SQL?

Answer: The GROUP BY clause in SQL is used to group rows based on a column or set of columns.

Explanation: The GROUP BY clause in SQL is used to group rows based on a column or set of columns. It is used with the SELECT statement to create groups of data based on common values. The GROUP BY clause is often used with aggregate functions, such as SUM, COUNT, AVG, MIN, and MAX.

 

I hope these SQL objective questions were helpful to you!

Do contact us if you wish to get training on PHP, JAVA, ASP.NET, PYTHON, AI, MACHINE LEARNING, SQL, ORACLE etc. at 91-9936804420. 

People also read

Leave a comment

Your email address will not be published. Required fields are marked *

Categories

Popular Post