Professional Documents
Culture Documents
71 - SQL Interview QnA
71 - SQL Interview QnA
71 - SQL Interview QnA
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.
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
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.
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.
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
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.
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.
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.
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
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.
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.
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
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
age INT,
salary DECIMAL(10,2)
);
Th
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:
Example:
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
Example:
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.
- 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.
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:
Example:
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
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
Example:
ON employees.department_id = departments.id;
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:
Example:
SELECT employees.name, departments.name FROM employees FULL OUTER JOIN
departments ON employees.department_id = departments.id;
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.
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');
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.
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
Example:
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.
Example:
y
MY SQL Interview QnA
m
What is the command to create a new database in MySQL?
de
Answer: CREATE DATABASE database_name;
Answer: INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1,
value2, value3, ...);
Answer: UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE
condition;
eT
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;