Java Interview questions on Database, SQL, and JDBC
Categories: Education
Let's look at some frequently asked questions about Database and JDBC. JDBC is used to join Databases from Java programs.
Q.1. How do you prevent SQL injection attacks?
To prevent SQL injection attacks, use parameterized queries (prepared statements) with bound parameters, input validation, and escape characters. Avoid dynamic SQL queries constructed by concatenating user input.
Q.2. What is the difference between WHERE and HAVING clause? The WHERE clause filters rows before the grouping and aggregation process, while the HAVING clause filters aggregated data after the grouping process based on specified conditions.
Q.3. What are transactions? What is ACID?
Transactions are a set of SQL statements that are executed as a single unit of work. ACID is an acronym for Atomicity, Consistency, Isolation, and Durability, which are properties that ensure the reliability of transactions in a database system.
Q.4. Difference between truncate, delete, and drop clause in SQL?
(i) TRUNCATE: Removes all rows from a table but retains the table structure and any associated constraints or indexes.
(ii) DELETE: Removes specific rows from a table based on a condition, but retains the table structure and associated constraints.
(iii) DROP: Deletes an entire table, including its structure, data, and associated constraints and indexes.
Q.5. What are window functions? How do they work?
Window functions perform calculations across a set of rows related to the current row within a query result set. They allow you to perform aggregate functions (such as SUM, AVG, COUNT) over a specified window or subset of rows, defined by the OVER clause. Window functions operate on a set of rows and return a single value for each row based on that set of rows. They are often used for tasks such as ranking, aggregation, and calculating running totals.