0% found this document useful (0 votes)
2 views7 pages

SQL Interview Questions

The document provides an overview of SQL operations including update queries, various types of joins (inner, outer, and self joins), and the use of the UNION operator. It explains the purpose of databases, how to communicate with them, and outlines CRUD operations. Additionally, it discusses the differences between DELETE and TRUNCATE commands, as well as JPA annotations like @JoinColumn and cascade types.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views7 pages

SQL Interview Questions

The document provides an overview of SQL operations including update queries, various types of joins (inner, outer, and self joins), and the use of the UNION operator. It explains the purpose of databases, how to communicate with them, and outlines CRUD operations. Additionally, it discusses the differences between DELETE and TRUNCATE commands, as well as JPA annotations like @JoinColumn and cascade types.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

1.

Write a Query for Update

To update records in a table, you can use the UPDATE statement. Here’s an example:

sql
Copy code
UPDATE employees
SET salary = 50000
WHERE id = 1;

This query updates the salary of the employee with id 1 to 50000.

2. SQL Joins. Write a Query for Inner Join

SQL Joins are used to combine rows from two or more tables based on a related column.

Inner Join Example:

sql
Copy code
SELECT e.name, d.department_name
FROM employees e
INNER JOIN departments d ON e.department_id = d.id;

This query retrieves the names of employees and their corresponding department names by
joining the employees and departments tables.

3. What Are Joins and Types?

Joins are SQL operations that allow you to combine rows from two or more tables based on a
related column. The main types of joins are:

 Inner Join: Returns records with matching values in both tables.


 Left Join (or Left Outer Join): Returns all records from the left table and the matched
records from the right table. Unmatched records from the right table will return NULL.
 Right Join (or Right Outer Join): Returns all records from the right table and the matched
records from the left table. Unmatched records from the left table will return NULL.
 Full Join (or Full Outer Join): Returns all records when there is a match in either left or
right table records. Unmatched records from both tables will return NULL.
4. Outer Joins

Outer joins are joins that return records even when there are no matches in one of the tables.
They are of three types:

 Left Outer Join: Includes all records from the left table and matched records from the
right table.
 Right Outer Join: Includes all records from the right table and matched records from the
left table.
 Full Outer Join: Includes all records from both tables, with NULLs in place where there are
no matches.

Example of Left Outer Join:

sql
Copy code
SELECT e.name, d.department_name
FROM employees e
LEFT JOIN departments d ON e.department_id = d.id;

5. What is Union?

The UNION operator is used to combine the results of two or more SELECT statements. It removes
duplicate rows between the queries.

Example:

sql
Copy code
SELECT name FROM employees
UNION
SELECT name FROM contractors;

This query retrieves unique names from both the employees and contractors tables.

6. What is SQL?

SQL (Structured Query Language) is a standardized programming language used for managing
and manipulating relational databases. It allows you to perform operations such as querying,
updating, inserting, and deleting data in a database.
7. Why Do We Use a Database?

Databases are used to:

 Store Data: Efficiently store vast amounts of structured data.


 Data Management: Provide tools for managing data, including querying and updating.
 Data Integrity: Ensure data consistency and integrity through constraints and
relationships.
 Multi-user Access: Allow multiple users to access and manipulate data concurrently
without conflicts.
 Data Security: Implement access controls to protect sensitive data.

8. How to Communicate with a Database?

You can communicate with a database using:

 SQL Queries: Use SQL statements to perform operations (e.g., SELECT, INSERT, UPDATE).
 Database Drivers: Use specific drivers (like JDBC for Java) to establish a connection
between your application and the database.
 Database Management Tools: Use tools like MySQL Workbench, pgAdmin, or SQL Server
Management Studio for direct interaction.

9. Inner Join

An inner join retrieves records that have matching values in both tables. It only returns the rows
where there is a match.

Example:

sql
Copy code
SELECT e.name, d.department_name
FROM employees e
INNER JOIN departments d ON e.department_id = d.id;
10. Self Join

A self join is a join in which a table is joined with itself. It is useful for comparing rows within the
same table.

Example:

sql
Copy code
SELECT a.name AS Employee1, b.name AS Employee2
FROM employees a, employees b
WHERE a.manager_id = b.id;

This query retrieves pairs of employees and their managers from the same employees table.

11. @JoinColumn, Cascade Types

 @JoinColumn: This annotation is used in JPA to specify the foreign key column for a
relationship between two entities. It defines how the two entities are related, specifying
the column that should be used to join the tables.

Example:

java
Copy code
@Entity
public class Employee {
@Id
private Long id;

@ManyToOne
@JoinColumn(name = "department_id") // Foreign key column
private Department department;
}

 Cascade Types: Cascade types in JPA specify how operations performed on a parent entity
should propagate to related child entities. Common cascade types include:
o CascadeType.PERSIST: Save the associated entity when the parent is saved.
o CascadeType.MERGE: Update the associated entity when the parent is updated.
o CascadeType.REMOVE: Delete the associated entity when the parent is deleted.
o CascadeType.ALL: Apply all cascade operations.
12. Joins and Types of Joins

Joins are SQL operations used to combine rows from two or more tables based on a related
column. Types of joins include:

 Inner Join: Returns records with matching values in both tables.


 Left Outer Join (Left Join): Returns all records from the left table and matched records
from the right table.
 Right Outer Join (Right Join): Returns all records from the right table and matched records
from the left table.
 Full Outer Join: Returns all records when there is a match in either left or right table
records.

13. MySQL Query: FindById, FindAll, Where Condition

Assuming a table named employees, here are examples of these queries:

 Find By ID:

sql
Copy code
SELECT * FROM employees WHERE id = 1;

 Find All:

sql
Copy code
SELECT * FROM employees;

 Where Condition:

sql
Copy code
SELECT * FROM employees WHERE department_id = 2;
14. Difference Between DELETE and TRUNCATE

 DELETE:
o A DML (Data Manipulation Language) command that removes rows from a table
based on a condition.
o Can be rolled back if used within a transaction.
o Triggers can be fired (if defined).
o More flexible, allowing WHERE clauses to specify which rows to delete.
 TRUNCATE:
o A DDL (Data Definition Language) command that removes all rows from a table
without logging individual row deletions.
o Cannot be rolled back once executed.
o Resets any auto-increment values to the seed value.
o Faster than DELETE because it does not log individual row deletions.

15. CRUD Operations

CRUD stands for Create, Read, Update, and Delete, which are the four basic operations for
managing data in a database.

 Create: Inserting new records into a table.

sql
Copy code
INSERT INTO employees (name, department_id) VALUES ('John Doe', 2);

 Read: Retrieving records from a table (using SELECT).

sql
Copy code
SELECT * FROM employees;

 Update: Modifying existing records in a table.

sql
Copy code
UPDATE employees SET name = 'Jane Doe' WHERE id = 1;

 Delete: Removing records from a table.

sql
Copy code
DELETE FROM employees WHERE id = 1;

You might also like