DB2 Interview Questions
DB2 database is a very popular relational database. Many companies working around DB2 ask questions based on extensive knowledge of it in their interviews both from the freshers as well as from experienced candidates. In this article, we are going to learn about some of the most famous DB2 interview questions.
What is a Database?
A database is a collection of logically related (or simply inter-related) data. For example, a database of a school will contain data about the faculty members such as their name, Employee ID, Date of Joining, Leaves Taken so far in the current year, Classes which the teacher is teaching, and so on. It will also contain the data of students like the Enrollment Number of the student, name of the student, age and parent’s name, etc.
What is a Relational Database?
A relational database is such a database in which the data is arranged in the form of tables i.e. rows (known as tuples) and columns (known as attributes). For example, a student database, which shows the details of students in a school is shown below:
What is DB2?
DB2 in general is a family of data management products. It is developed by IBM. The AI-powered family of data management tools helps us in the management of both structured as well as unstructured data. The DB2 relational database is a relational database that is part of the DB2 family of data management tools.
So, now we are going to discuss the most important questions asked in interviews. We have divided the questions into two sections. The first section is going to be the basic section where we will discuss the questions that are basic in level and also have a high probability of appearing in the interviews for a fresher and the other section consists of advanced questions for experienced candidates.
DB2 Interview Questions for Freshers
1. Explain DB2.
DB2 is a family of data management tools developed by IBM. However, if we specifically talk about the databases, DB2 is a relational database that was released in 1989 by IBM. DB2 was written using many programming languages that include C, C++, Java, and even Assembly Language. It has an operating system that works on Linux and Windows.
2. What are the different data types available in DB2?
There is a total of 8 data types in DB2. They are SMALLINT, INTEGER, FLOAT, DECIMAL, CHAR, VARCHAR, DATE, and TIME.
3. What is the difference between SMALLINT and INTEGER?
The difference is in the range and precision. While the SMALLINT can store the numbers up to a precision of 15 bits with a range of -32768 to +32767, the INTEGER data type can store the numbers up to a precision of 31 bits having the range from -2,147,483,648 to +2147483648. There is also one more data type to store the integer type of data. It is called BIGINT and its range is even higher than INTEGER.
4. What is the difference between CHAR and VARCHAR?
As the name suggests, the main difference is that CHAR is of fixed length whereas VARCHAR is of variable length. This means that char always has a fixed length to store the text whereas the VARCHAR adjusts its length according to the length of the text and this helps in saving the memory. Also, the CHAR data type can have a maximum size of 254 bytes whereas VARCHAR can have a maximum size of 4046 bytes.
5. Write a Query to find the number of rows in a DB2 table.
Here, the interviewer might ask you to write down the query on a piece of paper or a google doc or any other method. Mostly, the interviewers do not ask you to write a query upon a pre-existing table.
The query is:
SELECT COUNT * FROM tablename
6. Write a query to delete all the rows from a table in DB2.
The query to delete all the rows from a table in DB2 is:
DELETE * FROM table-name
7. Write a query to delete the entire table from the database in DB2.
The query to delete the entire table from the database is given below:
DROP TABLE table-name
8. What is the difference between the above two queries i.e. DELETE and DROP?
The DELETE * FROM table-name query deletes all the rows from the table but, there is still an empty table with no rows and no columns in the database. However, the DROP TABLE table-name query deletes all the rows and columns and along with that, the table gets deleted from the database too i.e. the table is completely removed (deleted) from the database.
9. What are UNION and UNION ALL in DB2? How do you differentiate between them?
The union command is used to merge 2 or more SELECT statements and the select statements can be applied on a single table as well as on multiple tables. The major difference between UNION and UNION ALL is that UNION removes the duplicate rows when it is applied on the tables whereas UNION ALL retains the duplicates.
10. What is concurrency in terms of DB2?
Concurrency means that more than one application process of DB2 can access the data at the same time. Concurrency can cause some issues like lost updates, access to unrepeatable reads and uncommitted data.
11. What is a CURSOR and What is the use of it?
CURSOR is a programming device that is used to find a set of rows corresponding to a SELECT statement. However, the rows are displayed one at a time.
12. What is MAX in DB2? Is it possible to use MAX in a CHAR column?
MAX() is an aggregate function that returns the maximum value from a set of values. For example, if we have a database of movies, we can apply MAX(rating) on the rating attribute and it will select the rows in which we have the movies with the highest ratings. Yes, MAX can be used on a CHAR column.
13. How can you move a tablespace to a different DASD volume allocated to that tablespace if you have to use STOGROUP?
The method to move the tablespace to a different DASD volume allocated to that tablespace is:
ALTER STOGROUP - ADD VOLUME (new volume) DELETE VOLUME (old volume)
REORG TABLESPACE or RECOVER TABLESPACE
So, basically, the method is that we create a new STOGROUP that points to the new volume and ALTER the tablespace and REORG or RECOVER the tablespace.
14. What are the two levels of isolation and what is the major difference between the 2 levels?
The two levels of isolation are Cursor Stability (CS) and Repeatable Read (RR). The major difference between these 2 levels is that the Cursor Stability (CS) releases the lock on a page after the use whereas the Repeatable Read (RR) retains all the locks till the end of a transaction.
15. As you know there are different sizes of locks. Can you tell how DB2 decides which lock size should be used?
The lock size is determined by three methods. These methods are:
- The lock size can be decided based on the lock size given while creating a new TABLESPACE.
- Also, Programming directs DB2 about the lock size.
- If the lock size ANY is specified, then DB2 (in most of the cases) chooses the lock size of PAGE.
16. What is DCLGEN in DB2?
DCLGEN stands for Declarations generator. The programs that we write should declare the tables and views that it accesses. The declarations generator i.e. DCLGEN produces these DECLARE statements for C, Cobol and PL/I programs so that one does not need to code the statements manually. It also generates corresponding host variable structures. When DCLGEN is used to generate a table declaration, the DB2 uses the DB2 catalog to get the relevant information from it. DCLGEN, then uses this information to create an SQL DECLARE TABLE statement for the table/view.
17. How is INNER JOIN different from simply writing a JOIN?
If we do not specify the type of join, by default, the join is an INNER JOIN. So, there is no difference between writing INNER JOIN or simply writing JOIN.
Now that we have seen some of the most popular interview questions of DB2 for freshers, let us move to some intermediate and advanced questions too that are asked in the interviews from an experienced candidate.
DB2 Interview Questions for Experienced
1. Can more than one cursor be opened in a program?
Yes, more than one cursor can be opened in the same program.
2. What is SQLCA?
The full form of SQLCA is the SQL communication area or Structured Query Language Communication Area. It is a structure of variables that is updated after every execution of an SQL statement.
3. How many SQLCA must be provided to an application that contains SQL statements (executable)?
Only one SQLCA is needed for an application that contains executable SQL statements. However, SQLCA is not applicable to Java applications.
4. Do you know any programming language where more than one SQLCA is used?
Yes, FORTRAN applications require more than one SQLCA.
5. What is the maximum length of SQLCA?
The maximum length of SQLCA is 136.
6. Is it possible for you to alter the table (adding a column to it) while some other person is accessing the table and even updating some values in it?
To be short and crisp, the answer is yes, we can alter the table at the same time when some other transaction is taking place but this does not exactly happen as it sounds to be.
The DB2 table will not change its structure in any form till a transaction or multiple transactions are taking place. The table-altering commands will be stored by the database engine and will not be executed till all the transactions are complete. After one user has modified the values in the table, then the column will be added.
7. Let us say that we have a Student Database in which we have the details of the students like their names, address, attendance percentage, etc. Now we run an SQL Query that says: SELECT AVG ATTENDANCE FROM STUDENTS.
Can you tell me any issues that may be here?
Yes, there might be an issue. For instance, if there is such a case where we have set that the attendance of any student cannot be null and at the same time we have some students in the table whose attendance we don’t know, the AVG function will calculate these students as well. Hence, we will get an answer but, it might not be correct.
8. What are ACQUIRE and RELEASE in BIND?
A certain point in a program when DB2 acquires or releases the locks against the tables and/or tablespaces. These include the intent locks as well.
9. In a COBOL-DB2 program, where are all the SQL statements coded?
In a COBOL-DB2 program, all the statements are coded in Area B.
10. What are SPUFI and QMF? What is the difference between them?
- SPUFI: SPUFI is an execution engine for SQL where the SQL commands are executed. Several SQL queries can be executed at once and the result of the queries can be stored in PS or PDS members. Also, the SQL is known to us even after the execution of the query.
- QMF: It is a reporting environment and supports the formatting of reports. More than one query cannot be executed and the result of the query cannot be persisted. Also, the SQL code of the query cannot be known.
11. Let us say that I have a program it uses dynamic SQL. It has been working fine till now, however, lately, I have found that the performance has been affected and deteriorated. Can you guess what can be the reason behind this?
There can be 2 possibilities:
- RUN STATS is not done and it seems that the program is using an incorrect/wrong index due to incorrect/wrong stats.
- It might also be the case that the RUN STATS has been done but the optimizer has chosen a wrong access path based on the latest statistics.
12. What is the RUN STATS that was mentioned in the previous answer?
RUN STATS is a DB2 utility used to collect statistics about the data value in TABLES. It is used by the optimizer to decide the access path. RUN STATS also collects statistics that are used for space management. These statistics are stored in the DB2 catalog tables.
Usually, the RUN STATS is run after the following:
- After a load.
- After a mass update.
- After any major deletions, insertions, or
- After REORG the table.
13. Is there any specific reason why SELECT is not preferred in embedded SQL programs?
The SELECT statement is not preferred in embedded SQL programs for 3 reasons:
- If there is a change in the structure of the table by addition or deletion of a field, the program gets modified and using the SELECT statement might retrieve those columns that the user may not even use.
- The use of SELECT in the embedded SQL programs can lead t the input-output overhead and,
- The chances of an index-only scan are eliminated by the use of SELECT in embedded SQL programs.
14. What is DSNDB07? What does it do?
DSNDB07 is a database where the DB2 performs its sorting operations. It includes the sort work-area of DB2 and external storage.
15. How will you achieve record level locking in DB2 versions where record level locking is not allowed?
We can achieve the record level locking in DB2 versions where the record level locking is not allowed by having the length of the record greater than that of a page itself.
Now that we have so much knowledge from the questions that we have studied, let us now move to practice some of the MCQs that will help test your knowledge too.
Useful Interview Resources
DB2 MCQ Questions
How many clustering indices can you define for a single table?
What is the maximum size of the VARCHAR data type in DB2?
The three statements TRUNCATE, DELETE and DROP perform the same task.
What is the action performed by DB2 when a program executes successfully?
Which of the following is TRUE regarding a DB2 table.
There cannot be multiple tables in a DB2 database.
There are 3 types of “Integral” data types in DB2. They are:
Which of the following is true about an INSERT Statement?
The SQLCA is:
What is the default size of a buffer pool page?