Computer Knowledge

Database and SQL

4,213 Questions

Master structured query language commands, database joins, table constraints, and alias generation. This section covers relational database management concepts and query outputs essential for technical aptitude. These technical questions feature prominently in banking IT officer exams and computer knowledge sections.

SQL queries and aliasesDatabase table constraintsDatabase joins and transformationsStored procedures and functions

Database and SQL Questions

Multiple choice technology databases
  1. ALTER TABLE DROP COLUMN

  2. ALTER TABLE DROP CONSTRAINT

  3. ALTER TABLE DISABLE CONSTRAINT

  4. -

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

In Oracle SQL, the CASCADE CONSTRAINTS clause is used with ALTER TABLE DROP COLUMN to automatically drop any referential integrity constraints (like foreign keys) that refer to the column being dropped. This prevents errors when removing columns that are part of a relationship.

Multiple choice technology databases
  1. CONNECT

  2. RENAME

  3. INSERT

  4. DELETE

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

In SQL databases, DDL (Data Definition Language) commands like RENAME typically cause an automatic commit of the current transaction. DML commands like INSERT and DELETE don't auto-commit unless autocommit mode is enabled. CONNECT establishes a session but doesn't commit anything.

Multiple choice technology databases
  1. NULL

  2. NOT NULL

  3. Function NVL2 is not defined

  4. None of the above

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

To solve this question, the user needs to know the function NVL2 in SQL.

The NVL2 function in SQL returns one value if an expression is not null, or another value if it is null. The syntax for the NVL2 function is:

NVL2( expression1, expression2, expression3 )

If expression1 is not null, then NVL2 returns expression2. If expression1 is null, then NVL2 returns expression3.

In this case, expression1 is NULL, so NVL2 will return the value of expression3, which is also NULL.

Therefore, the answer is:

The Answer is: A. NULL

Multiple choice technology databases
  1. When a SELECT statement returns more than one row

  2. When a SELECT statement returns no rows

  3. When INTO statement is missing in the SELECT statement

  4. Both I and II

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

To answer this question, the user needs to know the basics of PL/SQL and how it handles exceptions.

A. When a SELECT statement returns more than one row: This is one of the cases where PL/SQL raises an exception. If a SELECT statement returns more than one row in a context where a single value is expected, such as in an assignment statement or a SELECT INTO statement, then PL/SQL will raise the TOO_MANY_ROWS exception.

B. When a SELECT statement returns no rows: This is another case where PL/SQL raises an exception. If a SELECT statement returns no rows in a context where a single value is expected, such as in an assignment statement or a SELECT INTO statement, then PL/SQL will raise the NO_DATA_FOUND exception.

C. When INTO statement is missing in the SELECT statement: This is not a case where PL/SQL raises an exception. If an INTO statement is missing in a SELECT statement, then a compilation error will occur, but it will not result in an exception at runtime.

D. Both I and II: This option is correct. PL/SQL raises exceptions in both cases where a SELECT statement returns more than one row (TOO_MANY_ROWS) and where a SELECT statement returns no rows (NO_DATA_FOUND).

Therefore, the correct answer is: D.

Multiple choice technology databases
  1. NULL

  2. NOT NULL

  3. Function NVL2 is not defined

  4. None of the above

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

NVL2(expr1, expr2, expr3) returns expr2 if expr1 is NOT NULL, otherwise returns expr3. Since the first argument is NULL, NVL2 returns the third argument NULL. The second argument 'NOT NULL' is just a string literal in this context, not the SQL keyword.

Multiple choice technology databases
  1. SELECT INITCAP(TRIM ('HelloWorld', 1,1)) FROM dual;

  2. SELECT SUBSTR( 'HelloWorld',1) FROM dual;

  3. SELECT LOWER(SUBSTR('HelloWorld', 1, 1) FROM dual;

  4. SELECT LOWER(TRIM ('H' FROM 'HelloWorld')) FROM dual;

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

To solve this question, the user needs to know the basics of SQL SELECT statements and string manipulation functions. The user should be familiar with the INITCAP, TRIM, SUBSTR, and LOWER functions.

Now, let's go through each option and explain why it is right or wrong:

A. SELECT INITCAP(TRIM ('HelloWorld', 1,1)) FROM dual;

This option is incorrect. The TRIM function removes characters from the beginning and end of a string. In this case, the second and third arguments of the TRIM function are incorrect. INITCAP capitalizes the first letter of a string and converts the rest of the string to lowercase. Therefore, this SELECT statement will not return the correct result.

B. SELECT SUBSTR( 'HelloWorld',1) FROM dual;

This option is incorrect. The SUBSTR function returns a substring from a given string. In this case, the function only returns the first character of the string 'HelloWorld'. Therefore, this SELECT statement will not return the correct result.

C. SELECT LOWER(SUBSTR('HelloWorld', 1, 1) FROM dual;

This option is incorrect. The SUBSTR function returns a substring from a given string. In this case, the function only returns the first character of the string 'HelloWorld'. The LOWER function converts the substring to lowercase. Therefore, this SELECT statement will not return the correct result.

D. SELECT LOWER(TRIM ('H' FROM 'HelloWorld')) FROM dual;

This option is correct. The TRIM function removes the character 'H' from the beginning of the string 'HelloWorld'. The LOWER function converts the remaining string to lowercase. Therefore, this SELECT statement will return the correct result.

The Answer is: D

Multiple choice technology databases
  1. Both tables have NULL values.

  2. You want all matched data from both tables.

  3. You want all unmatched data from both tables.

  4. You want all unmatched data from one table.

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

A FULL OUTER JOIN returns all rows when there is a match in either left or right table records. It is specifically used to ensure that unmatched data from both tables is included in the result set.

Multiple choice technology programming languages
  1. ALTER TABLE students ADD PRIMARY KEY student_id;

  2. ALTER TABLE students ADD CONSTRAINT PRIMARY KEY(student_id);

  3. ALTER TABLE students ADD CONSTRAINT stud_id_pk PRIMARY KEY student_id;

  4. ALTER TABLE students ADD CONSTRAINT stud_id_pk PRIMARY KEY (student_id);

  5. ALTER TABLE Students MODIFY CONSTRAINT stud_id_pk PRIMARY KEY (student_id);

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

To solve this problem, the user needs to know the SQL syntax for adding a primary key to a table using the ALTER TABLE statement.

A primary key is a column or set of columns that uniquely identifies each row in a table. It is a constraint that ensures that the values in the column(s) are unique and not null.

Now let's go through each option and explain why it is right or wrong:

A. ALTER TABLE students ADD PRIMARY KEY student_id;

This statement is incorrect because it is missing the keyword "CONSTRAINT". The correct syntax for adding a primary key constraint is "ADD CONSTRAINT".

B. ALTER TABLE students ADD CONSTRAINT PRIMARY KEY(student_id);

This statement is incorrect because it is missing the name of the constraint. All constraints should have a unique name for easy identification and management.

C. ALTER TABLE students ADD CONSTRAINT stud_id_pk PRIMARY KEY student_id;

This statement is incorrect because the syntax is wrong. The correct syntax for adding a primary key constraint is "ADD CONSTRAINT PRIMARY KEY ()".

D. ALTER TABLE students ADD CONSTRAINT stud_id_pk PRIMARY KEY (student_id);

This statement is correct. It adds a primary key constraint named "stud_id_pk" to the "students" table on the "student_id" column.

E. ALTER TABLE Students MODIFY CONSTRAINT stud_id_pk PRIMARY KEY (student_id);

This statement is incorrect because it uses the "MODIFY CONSTRAINT" syntax, which is not valid for adding constraints.

Therefore, the correct answer is:

The Answer is: D. ALTER TABLE students ADD CONSTRAINT stud_id_pk PRIMARY KEY (student_id);

Multiple choice technology programming languages
  1. NOT NULL

  2. PRIMARY KEY

  3. FOREIGN KEY

  4. CHECK

  5. UNIQUE

Reveal answer Fill a bubble to check yourself
B,E Correct answer
Explanation

Oracle automatically creates unique indexes for PRIMARY KEY and UNIQUE constraints to enforce uniqueness. NOT NULL, FOREIGN KEY, and CHECK constraints don't create unique indexes - they enforce other rules. The index ensures that duplicate values cannot be inserted.

Multiple choice technology programming languages
  1. A view can be created as read only.

  2. A view can be created as a join on two or more tables

  3. A view cannot have a n ORDER BY clause in the SELECT statement.

  4. A view cannot be created with a GROUP BY clause in the SELECT statement.

Reveal answer Fill a bubble to check yourself
A,B Correct answer
Explanation

Views can be created as read-only using the WITH CHECK OPTION or through complex queries. Views can join multiple tables - that's one of their main purposes. Views CAN have ORDER BY in Oracle if used with a subquery. Views CAN use GROUP BY for aggregation.

Multiple choice technology programming languages
  1. USER_TAB_PRIVS_MADE

  2. USER_TAB_PRIVS

  3. USER_COL_PRIVS_MADE

  4. USER_COL_PRIVS

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

USER_COL_PRIVS shows column-level object privileges granted TO the current user. USER_TAB_PRIVS shows table-level privileges received. The _MADE variants show privileges granted BY the user. For column-specific privileges, query USER_COL_PRIVS.

Multiple choice technology web technology
  1. Using lookup() function

  2. Using Generate Records component

  3. Using re_index() function

  4. Using next_in_sequence() function

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

In Ab Initio, the next_in_sequence() function is standardly used to generate unique, sequential integer values, which serve as surrogate keys during data transformation.

Multiple choice technology web technology
  1. $_GET
  2. $_POSTS
  3. $_GETS
  4. All the above

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

$_GET is the PHP superglobal array that contains query parameters passed through the URL after the ? character. The other options are wrong - $_POST (not $_POSTS) contains POST request data, and $_GETS doesn't exist as a PHP superglobal. Query parameters in URLs are automatically parsed into $_GET.