71 - SQL Interview QnA

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Top 71+ SQL Interview and Questions.

1. What is SQL and what are its uses?


Answer: SQL stands for Structured Query Language. It is a programming language used to
manage relational databases. It is used to insert, query, update, and delete data from

y
databases.

m
2. What is a database?
Answer: A database is a collection of data. It is organized in a way that it can be accessed

de
and managed easily.

3. What is a table in SQL?


ca
Answer: A table in SQL is a collection of data stored in rows and columns. It is the primary
unit of storage where data is stored.

4. What are the types of SQL commands?


gA
Answer: There are 3 types of SQL commands: Data Definition Language (DDL), Data
Manipulation Language (DML) and Data Control Language (DCL).
tin

5. What is a join?
Answer: A join is used to combine data from two or more tables based on a related column
between them.
es

6. What is the difference between INNER JOIN and OUTER JOIN?


Answer: INNER JOIN returns only the matching rows between two tables while OUTER
JOIN returns all rows whether they match or not.
eT

7. What is a primary key?


Answer: A primary key is a unique identifier for each record in a table. It has a unique value
Th

for every column in the table.

8. What is a foreign key?


Answer: A foreign key is a column in a table that references the primary key of another table.
It is used to establish a relationship between two tables.

9. What is a subquery?
Answer: A subquery is a query placed inside another query. It is used to retrieve information
that will be used in the main query.

10. What is a view in SQL?


Answer: A view in SQL is a virtual table that contains data from one or more tables. It is used
to simplify complex queries and hide the complexity of the underlying tables.

11. What is normalization in SQL?


Answer: Normalization is the process of organizing data in a database to avoid duplication
and ensure data consistency.

12. What is a trigger in SQL?


Answer: A trigger in SQL is a set of actions that are taken automatically whenever a certain
event occurs in the database.

13. What is a stored procedure in SQL?

y
Answer: A stored procedure in SQL is a set of pre-written SQL codes that are saved in the

m
database server. It is used to perform complex operations in databases.

14. What is a transaction in SQL?

de
Answer: A transaction in SQL is a sequence of SQL statements that are executed as a
single unit of work. It ensures data consistency and integrity.

ca
15. What types of indexing are there in SQL?
Answer: There are two types of indexing in SQL, clustered indexing and non-clustered
indexing.
gA
16. What is a clustered index?
Answer: A clustered index is an index in which the data is physically stored in the same
order as the index.
tin

17. What is a non-clustered index?


Answer: A non-clustered index is an index in which the data is physically stored separately
from the index.
es

18. What is a cursor in SQL?


Answer: A cursor in SQL is a pointer to a specific row in a result set. It is used to process
eT

each row one at a time.

19. What is a group by clause in SQL?


Th

Answer: A GROUP BY clause in SQL is used to group rows together based on a specific
column. It is used with aggregate functions to calculate summary data.

20. What is a HAVING clause in SQL?


Answer: A HAVING clause in SQL is used to filter the results of a GROUP BY clause based
on a specific condition.

21. What is the difference between CHAR and VARCHAR data types?
Answer: CHAR is a fixed length data type while VARCHAR is a variable length data type.

22. What is a union in SQL?


Answer: A union in SQL is used to combine the results of two or more SELECT statements
into a single result set.

23. What is a unique constraint?


Answer: A unique constraint is a constraint that ensures that no two rows in a table have the
same values in a certain column.

24. What is an index in SQL?


Answer: An index in SQL is a database object that contains an ordered list of values from
one or more columns in a table.

25. What is a database transaction?

y
Answer: A database transaction is a single unit of work performed on a database. It ensures

m
that all modifications to the database are made as a single action.

26. What is normalization?

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

ca
27. What are the four forms of normalization?
Answer: The four forms of normalization are First Normal Form (1NF), Second Normal Form
(2NF), Third Normal Form (3NF), and Boyce-Codd Normal Form (BCNF).
gA
28. What is a self-join?
Answer: A self-join is a join in which a table is joined with itself.
tin

29. What is recursion in SQL?


Answer: Recursion in SQL is the process of repeatedly calling a stored procedure or function
within itself.
es

30. What is a trigger in SQL?


Answer: A trigger in SQL is a set of actions that are automatically executed when a certain
event occurs in a database.
eT

31. What is the difference between a left join and a right join?
Answer: A left join returns all records from the left table and matching records from the right
Th

table, while a right join returns all records from the right table and matching records from the
left table.

32. What is a scalar function?


Answer: A scalar function is a function that returns a single value for each row in a table.

33. What is a table valued function?


Answer: A table valued function is a function that returns a table for each row in a table.

34. What is a full-text search?


Answer: A full-text search is a search technique used to find keywords or phrases within a
document.
35. What is the difference between an index and a constraint?
Answer: An index is used to improve the performance of a query while a constraint ensures
data integrity.

36. What are the different types of constraints in SQL?


Answer: The different types of constraints in SQL are primary key, foreign key, unique,
check, and default.

37. What is a sub-query?


Answer: A sub-query is a query within a query.

y
38. What is a correlated sub-query?

m
Answer: A correlated sub-query is a sub-query that references a column of the outer query.

39. What is a self-contained sub-query?

de
Answer: A self-contained sub-query is a sub-query that does not reference any column from
the outer query.

ca
40. What is a CTE (common table expression)?
Answer: A CTE (common table expression) is a temporary result set that can be referred to
within a SELECT, INSERT, UPDATE, or DELETE statement.
gA
41. What is a pivot table?
Answer: A pivot table is a table used to summarize data by aggregating and grouping them.
tin

42. What is the difference between a clustered and a non-clustered index?


Answer: A clustered index determines the physical order of data in a table, while a
non-clustered index does not.
es

43. What is a cursor in SQL?


Answer: A cursor in SQL is a database object used to traverse through a result set one row
at a time.
eT

44. What are the different types of cursors in SQL?


Answer: The different types of cursors in SQL are FORWARD_ONLY, STATIC, KEYSET,
Th

DYNAMIC, and FAST_FORWARD.

45. What is a DML (Data Manipulation Language) statement?


Answer: A DML (Data Manipulation Language) statement is used to insert, update, and
delete data from a database.

46. What is a DDL (Data Definition Language) statement?


Answer: A DDL (Data Definition Language) statement is used to create, modify, and delete
database objects such as tables, views, and indexes.

47. What is an ACID transaction?


Answer: An ACID transaction is a transaction that ensures the following: Atomicity,
Consistency, Isolation, and Durability.

48. What is the difference between a DELETE and TRUNCATE statement?


Answer: A DELETE statement removes individual rows from a table, while a TRUNCATE
statement removes all rows from a table.

49. What is a database schema?


Answer: A database schema is the structure of a database that defines how data is stored in
it.

50. What is a database index?

y
Answer: A database index is a data structure that improves the speed of data retrieval from

m
a database.

de
Q. How to create a table in SQL?

ca
To create a table in SQL, use the CREATE TABLE statement followed by the table name and
the list of columns and their data types. The syntax is:
gA
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
...
tin

);

Example:
es

CREATE TABLE employees (


id INT PRIMARY KEY,
name VARCHAR(50),
eT

age INT,
salary DECIMAL(10,2)
);
Th

Q. How to insert data into a table in SQL?

To insert data into a table in SQL, use the INSERT INTO statement followed by the table
name and the values to be inserted. The syntax is:

INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);

Example:

INSERT INTO employees (id, name, age, salary) VALUES (1, 'John Doe', 30, 50000.00);
Q. How to retrieve data from a table in SQL?

To retrieve data from a table in SQL, use the SELECT statement followed by the columns to
be selected and the table name. The syntax is:

SELECT column1, column2, ... FROM table_name;

Example:

SELECT name, age FROM employees;

Q. How to update data in a table in SQL?

y
m
To update data in a table in SQL, use the UPDATE statement followed by the table name,
the column to be updated, and the new value. The syntax is:

de
UPDATE table_name SET column1 = new_value1, column2 = new_value2 WHERE
condition;

Example: ca
UPDATE employees SET salary = 55000.00 WHERE id = 1;
gA
Q. How to delete data from a table in SQL?

To delete data from a table in SQL, use the DELETE statement followed by the table name
tin

and the condition. The syntax is:

DELETE FROM table_name WHERE condition;


es

Example:

DELETE FROM employees WHERE age > 40;


eT

Q. What is a join in SQL?


Th

A join is a SQL operation that combines rows from two or more tables based on a related
column between them. It is used to retrieve data from multiple tables in a single query.

Q. What are the types of joins in SQL?

There are four types of joins in SQL:

- INNER JOIN: returns only the rows that have matching values in both tables.
- LEFT JOIN: returns all the rows from the left table and the matching rows from the right
table.
- RIGHT JOIN: returns all the rows from the right table and the matching rows from the left
table.
- FULL OUTER JOIN: returns all the rows from both tables, including the non-matching rows.

Q. How to perform an INNER JOIN in SQL?

To perform an INNER JOIN in SQL, use the JOIN keyword followed by the table name and
the ON clause that specifies the related columns between the tables. The syntax is:

SELECT columns FROM table1 JOIN table2 ON table1.column = table2.column;

Example:

SELECT employees.name, departments.name FROM employees JOIN departments ON

y
employees.department_id = departments.id;

m
Q. How to perform a LEFT JOIN in SQL?

de
To perform a LEFT JOIN in SQL, use the LEFT JOIN keyword followed by the table name
and the ON clause that specifies the related columns between the tables. The syntax is:

ca
SELECT columns FROM table1 LEFT JOIN table2 ON table1.column = table2.column;

Example:
gA
SELECT employees.name, departments.name FROM employees LEFT JOIN departments
ON employees.department_id = departments.id;
tin

Q. How to perform a RIGHT JOIN in SQL?

To perform a RIGHT JOIN in SQL, use the RIGHT JOIN keyword followed by the table name
and the ON clause that specifies the related columns between the tables. The syntax is:
es

SELECT columns FROM table1 RIGHT JOIN table2 ON table1.column = table2.column;


eT

Example:

SELECT employees.name, departments.name FROM employees RIGHT JOIN departments


Th

ON employees.department_id = departments.id;

Q. How to perform a FULL OUTER JOIN in SQL?

To perform a FULL OUTER JOIN in SQL, use the FULL OUTER JOIN keyword followed by
the table name and the ON clause that specifies the related columns between the tables.
The syntax is:

SELECT columns FROM table1 FULL OUTER JOIN table2 ON table1.column =


table2.column;

Example:
SELECT employees.name, departments.name FROM employees FULL OUTER JOIN
departments ON employees.department_id = departments.id;

Q. What is a subquery in SQL?

A subquery is a SQL query that is nested inside another query and is used to retrieve data
that will be used in the main query. It is often used to filter or aggregate data from multiple
tables.

Q. How to write a subquery in SQL?

y
To write a subquery in SQL, use the SELECT statement followed by the columns to be

m
selected and the condition. The subquery is then enclosed in parentheses and used as a
condition in the main query. The syntax is:

de
SELECT columns FROM table_name WHERE column_name IN (SELECT columns FROM
another_table WHERE condition);

Example: ca
SELECT name FROM employees WHERE department_id IN (SELECT id FROM
gA
departments WHERE name = 'Marketing');

Q. What is a view in SQL?


tin

A view is a virtual table that is based on the result of a SELECT statement. It is used to
simplify complex queries, to restrict access to certain columns, or to aggregate data.

Q. How to create a view in SQL?


es

To create a view in SQL, use the CREATE VIEW statement followed by the view name, the
columns to be selected, and the SELECT statement. The syntax is:
eT

CREATE VIEW view_name AS SELECT columns FROM table_name WHERE condition;


Th

Example:

CREATE VIEW employee_details AS SELECT name, age, salary FROM employees;

Q. What is a stored procedure in SQL?

A stored procedure is a precompiled block of SQL code that is stored in the database and
can be executed whenever needed. It is used to encapsulate complex business logic, to
improve performance, and to enforce security.

Q. How to create a stored procedure in SQL?


To create a stored procedure in SQL, use the CREATE PROCEDURE statement followed by
the procedure name, the parameters, and the SQL code. The syntax is:

CREATE PROCEDURE procedure_name (parameter1 datatype, parameter2 datatype, ...)


AS SQL code;

Example:

CREATE PROCEDURE get_employee_details @id INT AS


SELECT name, age, salary FROM employees WHERE id = @id;

y
MY SQL Interview QnA

m
What is the command to create a new database in MySQL?

de
Answer: CREATE DATABASE database_name;

What is the command to create a new table in MySQL?


ca
Answer: CREATE TABLE table_name (column1 datatype, column2 datatype, column3
datatype, ...);
gA
How can you add a new column to an existing table in MySQL?
Answer: ALTER TABLE table_name ADD column_name datatype;

What is the command to insert data into a table in MySQL?


tin

Answer: INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1,
value2, value3, ...);

What is the command to update data in a table in MySQL?


es

Answer: UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE
condition;
eT

How can you delete a table in MySQL?


Answer: DROP TABLE table_name;
Th

What is the command to retrieve all records from a table in MySQL?


Answer: SELECT * FROM table_name;

How can you retrieve records from a table with a specific condition in MySQL?
Answer: SELECT * FROM table_name WHERE condition;

What is the command to sort records in ascending order by a specific column in MySQL?
Answer: SELECT * FROM table_name ORDER BY column_name ASC;

How can you count the number of records in a table in MySQL?


Answer: SELECT COUNT(*) FROM table_name;
Th
eT
es
tin
gA
ca
de
m
y

You might also like