Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
13 views4 pages

SQL Interview Questions Detailed

The document contains a series of SQL interview questions and detailed answers covering fundamental concepts such as SQL importance, JOIN types, data retrieval, and database management. Key topics include the use of clauses like WHERE and HAVING, the creation of tables, handling duplicates, and the significance of normalization. It also explains advanced topics like subqueries, stored procedures, and aggregate functions.

Uploaded by

karthik12420
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

SQL Interview Questions Detailed

The document contains a series of SQL interview questions and detailed answers covering fundamental concepts such as SQL importance, JOIN types, data retrieval, and database management. Key topics include the use of clauses like WHERE and HAVING, the creation of tables, handling duplicates, and the significance of normalization. It also explains advanced topics like subqueries, stored procedures, and aggregate functions.

Uploaded by

karthik12420
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

SQL Interview Questions with Detailed Answers

1. What is SQL, and why is it important for data management?

SQL (Structured Query Language) is the standard language used to interact with relational databases. It is

essential for performing tasks such as querying data, updating records, and managing database schemas.

SQL ensures efficient data management, making it crucial for data-driven applications.

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

INNER JOIN returns only the rows where there is a match in both joined tables. OUTER JOIN returns all

rows from one table and the matched rows from the second table. If there is no match, NULLs are returned

for the columns of the table with no match. Types include LEFT OUTER JOIN, RIGHT OUTER JOIN, and

FULL OUTER JOIN.

3. How do you retrieve unique values from a column in SQL?

To get unique values from a column, use the DISTINCT keyword:

Example: SELECT DISTINCT column_name FROM table_name;

4. Explain the difference between WHERE and HAVING clauses.

WHERE is used to filter rows before any grouping is performed. HAVING is used to filter groups after the

GROUP BY clause has been applied. Example: Use WHERE to filter employees with salary > 50000 before

grouping by department; use HAVING to filter departments with average salary > 50000.

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

GROUP BY is used to group rows that have the same values in specified columns into summary rows. It's

commonly used with aggregate functions like COUNT(), SUM(), AVG(), etc.
SQL Interview Questions with Detailed Answers

6. How do you create a table in SQL? Can you give an example?

Use the CREATE TABLE statement:

Example:

CREATE TABLE Employees (

ID INT PRIMARY KEY,

Name VARCHAR(100),

Age INT,

Department VARCHAR(50)

);

7. What is the purpose of a primary key in a database table?

A primary key uniquely identifies each record in a table. It must contain unique values and cannot have

NULLs. It helps enforce entity integrity in the database.

8. How can you delete duplicate rows in SQL?

You can use a CTE (Common Table Expression) or subquery with ROW_NUMBER or MIN/MAX:

Example:

DELETE FROM employees

WHERE id NOT IN (

SELECT MIN(id)

FROM employees

GROUP BY name, department

);
SQL Interview Questions with Detailed Answers

9. What is a subquery, and how is it used in SQL?

A subquery is a query nested inside another query. It can be used in SELECT, INSERT, UPDATE, or

DELETE statements. It helps in filtering or transforming data for the main query.

10. What are the differences between UNION and UNION ALL?

UNION combines results from multiple SELECT statements and removes duplicates. UNION ALL combines

results and includes duplicates. UNION ALL is faster as it doesn't check for duplicates.

11. How do you find the second highest salary in a table?

You can use the MAX function and a subquery:

SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);

12. What is a stored procedure in SQL, and why would you use one?

A stored procedure is a saved collection of SQL statements that perform a specific task. It enhances

performance, promotes reusability, and improves security.

13. Explain the purpose of the COUNT function in SQL.

COUNT() returns the number of rows that match a condition. Example:

SELECT COUNT(*) FROM employees WHERE department = 'Sales';

14. What is normalization, and why is it important in databases?

Normalization is the process of organizing data to minimize redundancy and dependency. It involves dividing

large tables into smaller ones and defining relationships. It improves data integrity and efficiency.
SQL Interview Questions with Detailed Answers

15. How do you write an SQL query to join three tables?

Use multiple JOIN clauses:

SELECT * FROM orders

JOIN customers ON orders.customer_id = customers.id

JOIN products ON orders.product_id = products.id;

You might also like