Java

Java SQL Interview Questions

46 questions with answers · Java Interview Guide

SQL queries, joins, and database operations as they relate to Java backend development.

bar_chartQuick stats
Total questions46
High frequency2
With code examples0
1

Familiar with SQL

Yes, proficient with SQL for queries, joins, subqueries, transactions, indexes, and optimization.

2

Are you familiar with SQL

Yes, proficient with SQL for DML/DDL operations, optimization, and query design.

3

What types of Join do you know

INNER JOIN returns matching rows from both tables; LEFT/RIGHT JOIN includes unmatched rows from one side; FULL OUTER JOIN includes all unmatched rows; CROSS JOIN produces Cartesian product.

4

What is the difference between Having and Where

WHERE filters rows before aggregation; HAVING filters groups after aggregation, typically used with GROUP BY.

5

What is a “performance”, View, and why it is used

A stored procedure is precompiled SQL code stored in the database, executed on server-side for performance, reusability, and reduced network traffic; used for complex business logic.

6

What is a “temporary table”, for which it is used

A temporary table exists only for the current session/transaction and is automatically dropped; used for storing intermediate results, complex transformations, and improving query performance.

7

What is a “performance”, View, and why it is used

A view is a virtual representation of table data with computed or filtered columns; used for security, simplifying queries, and data abstraction.

8

What is a “temporary table”, for which it is used

Temporary tables exist only during a session or transaction, used for intermediate processing, staging data, or avoiding repeated complex calculations.

9

What does NULL mean in SQL

NULL in SQL represents missing or undefined values; it's not equal to zero or empty string and requires IS NULL or IS NOT NULL operators.

10

What are the SQL operators

SQL operators include arithmetic (+, -, *, /), comparison (=, <>, <, >), logical (AND, OR, NOT), and aggregate functions (SUM, AVG, COUNT).

11

What is a cursor in a relational database

A cursor is a database object that retrieves and iterates through query result sets row-by-row, used in stored procedures for procedural processing.

12

What are triggers in a relational database

Triggers are database objects that automatically execute SQL statements (stored procedures) in response to events (INSERT, UPDATE, DELETE) on specific tables.

13

Tell me about the full syntax of Select in a relational database

SELECT [DISTINCT] column_list FROM table [WHERE condition] [GROUP BY columns] [HAVING condition] [ORDER BY columns] [LIMIT offset, count].

14

Why is SQL HAVING

HAVING filters aggregated result sets post-GROUP BY, while WHERE filters raw rows pre-aggregation.

15

What Merge does

MERGE (SQL) inserts or updates based on existence, combining INSERT/UPDATE logic in a single statement.

16

What is Join

JOIN combines rows from two tables based on a condition: INNER JOIN (matching rows), LEFT/RIGHT/FULL OUTER JOIN (include unmatched rows).

17

What are stored procedures and what method of calling through JDBC

Stored procedures are precompiled SQL routines stored in database; call them via CallableStatement with syntax {call procedure_name(?, ?)} and retrieve results using getObject() or OUT parameters.

18

What are stored procedures and what method of calling through JDBC

Stored procedures are precompiled SQL statements; call them via CallableStatement with {call procedure_name(?, ?)} syntax in JDBC.

19

What are restrictions in SQL

SQL restrictions (WHERE clause) filter rows based on conditions; examples include WHERE age > 18, WHERE status = 'active'.

20

That it is better to use Join or subclasses

JOIN is preferred for most cases (single query, index optimization); subqueries for complex logic or when JOINing same table multiple times. JOINs are typically faster due to query optimization.

21

What is the ORDER BY operator used for

ORDER BY sorts query results by one or more columns in ascending (ASC) or descending (DESC) order.

22

How Group by processes Null value

GROUP BY treats NULL as a distinct group value, so all NULL rows are grouped together separately.

23

List the main aggregate functions

Main aggregate functions: COUNT, SUM, AVG, MIN, MAX, and STDDEV.

24

What is the difference between Count (*) and Count ({column})

COUNT(*) counts all rows including NULLs; COUNT(column) counts only non-NULL values in that column.

25

What is operators: in, Between, Like

IN checks if value exists in a list; BETWEEN checks range inclusively; LIKE performs pattern matching with wildcards.

26

When the keyword UNION is used

UNION combines result sets from multiple queries, removing duplicates; UNION ALL keeps duplicates.

27

What are the differences between Primary and Unique restrictions

Primary Key is unique and non-null (one per table); Unique Key allows one NULL and multiple constraints per table.

28

Is the value in the column, which is imposed on the restriction of Foreign Key, is NULL

Yes, Foreign Key columns can contain NULL values; NULL is treated as 'unknown' and doesn't violate FK constraints.

29

How to create an index

CREATE INDEX index_name ON table_name(column_name); or CREATE UNIQUE INDEX for uniqueness constraint.

30

What is the difference between Delete and Truncate operators

DELETE removes rows and generates rollback logs (slower, reversible); TRUNCATE removes all rows instantly (faster, irreversible).

31

Describe the difference between DATETIME and TIMESTAMP data

DATETIME stores date and time (8 bytes, no timezone); TIMESTAMP stores a point in time (4 bytes, timezone-aware in some systems).

32

What is the purpose of Pivot and Unpivot operators in Transact-SQL

PIVOT transforms rows to columns (aggregating data); UNPIVOT transforms columns back to rows.

33

Tell me about the main functions of ranking in Transact-SQL

ROW_NUMBER, RANK, DENSE_RANK, NTILE assign row numbers/ranks; used with OVER clause for partitioning and ordering.

34

Why are Intersect operators used, Except in Transact-SQL

INTERSECT returns rows common to both queries; EXCEPT returns rows from first query not in second.

35

What are restrictions in SQL

SQL constraints enforce data integrity: PRIMARY KEY ensures uniqueness, FOREIGN KEY maintains referential integrity, UNIQUE prevents duplicates, NOT NULL requires values, CHECK validates conditions, and DEFAULT provides fallback values.

36

What are stored procedures and what method of calling through JDBC

Stored procedures are precompiled SQL scripts stored in database executed via CALL statement. Call via JDBC using CallableStatement: `CallableStatement cs = conn.prepareCall("{call procName(?)}");` then set parameters and execute.

37

What is Join

JOIN combines rows from multiple tables based on related columns: INNER JOIN returns matching rows, LEFT JOIN includes unmatched left rows, RIGHT JOIN includes unmatched right rows, FULL JOIN includes all unmatched rows from both.

38

What Merge does

MERGE (or UPSERT) inserts or updates rows atomically: if row exists update it, else insert new row. Syntax: `MERGE INTO target USING source ON condition WHEN MATCHED THEN UPDATE WHEN NOT MATCHED THEN INSERT`.

39

Why is SQL HAVING

HAVING filters aggregated data after GROUP BY, similar to WHERE but for groups. WHERE filters rows before grouping; HAVING filters grouped results, so HAVING can use aggregate functions like COUNT, SUM.

40

Tell me about the full syntax of Select in a relational database

SELECT syntax: `SELECT [DISTINCT] columns FROM table [JOIN conditions] [WHERE conditions] [GROUP BY columns] [HAVING conditions] [ORDER BY columns] [LIMIT n]`. Execution order: FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT.

41

What are triggers in a relational database

Triggers are database objects that execute automatically when INSERT, UPDATE, or DELETE occurs. They enforce business rules, maintain audit trails, and ensure data consistency at database level.

42

What is a cursor in a relational database

Cursor is a database object that iterates through result sets row-by-row. Declare cursor with SELECT, open it, fetch rows sequentially, and close it; used mainly in stored procedures since application-level iteration is preferred.

43

What are the SQL operators

SQL operators include: comparison (=, !=, <, >, <=, >=), logical (AND, OR, NOT), arithmetic (+, -, *, /, %), aggregate (COUNT, SUM, AVG, MIN, MAX), and set operators (UNION, INTERSECT, EXCEPT).

44

What does NULL mean in SQL

NULL represents missing or unknown data in SQL; it's not equal to zero or empty string and requires IS NULL/IS NOT NULL operators for comparison.

45

What is a “temporary table”, for which it is used

Temporary tables exist only during a session and are used for intermediate data storage, reducing complexity of complex queries and improving performance.

46

What is a “performance”, View, and why it is used

A view is a virtual table based on a query; it's used for security (restricting column access), simplifying complex queries, and data abstraction without storing physical data.

Knowing the answers is half the battle

The other half is explaining them clearly under pressure.

Try a free mock interviewarrow_forward

More Java topics