SQL Interview Questions
SQL Interview Questions
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;
SQL Joins are used to combine rows from two or more tables based on a related column.
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.
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:
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.
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?
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.
@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:
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.
CRUD stands for Create, Read, Update, and Delete, which are the four basic operations for
managing data in a database.
sql
Copy code
INSERT INTO employees (name, department_id) VALUES ('John Doe', 2);
sql
Copy code
SELECT * FROM employees;
sql
Copy code
UPDATE employees SET name = 'Jane Doe' WHERE id = 1;
sql
Copy code
DELETE FROM employees WHERE id = 1;