Computer Knowledge

Database and SQL

3,929 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 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 databases
  1. Create each procedure with a distinct name followed by a semi-colon and a group name.

  2. Create each procedure with a distinct name followed by a semi-colon and a group number.

  3. Create each procedure with the same name followed by a semi-colon and a group name.

  4. Create each procedure with the same name followed by a semi-colon and a distinct number.

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

In SQL Server, stored procedures can be grouped by giving them the same name followed by a semicolon and a distinct number (e.g., 'procname;1', 'procname;2'). This allows dropping the entire group with a single DROP PROCEDURE statement. Options A and B are wrong - the names must be identical, not distinct. Option C is wrong because it needs distinct numbers, not a group name.

Multiple choice technology databases
  1. Mark the computed column as PERSISTED.

  2. Define the computed column as NULL.

  3. Specify the collation for the column using COLLATE Physical.

  4. Do nothing. Computed columns are stored in the table by default.

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

By marking a computed column as PERSISTED, SQL Server physically stores the computed values in the table and updates them when dependent columns change, allowing you to create indexes on it.

Multiple choice technology programming languages
  1. Oracle not available (the database is down)

  2. Invalid Username/Password

  3. Snapshot too old (Rollback has been overwritten)

  4. Unique constraint violated

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

ORA-01017 specifically means 'invalid username/password; logon denied' - Oracle cannot authenticate the credentials provided. This occurs when the username doesn't exist, the password is wrong, or the account is locked/expired. Option A would be ORA-03114 (not connected), Option C is ORA-01555 (snapshot too old), and Option D is ORA-00001 (unique constraint).

Multiple choice technology programming languages
  1. PL/SQL Error

  2. No data found

  3. Insufficient privileges

  4. Unique constraint violated. (Invalid data has been rejected)

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

ORA-00001 is Oracle's error for 'unique constraint violated' - it fires when an INSERT or UPDATE tries to create a duplicate value in a column with a UNIQUE constraint or PRIMARY KEY. The parenthesized text clarifies that the offending data was rejected. Other options are different errors: ORA-06500 is PL/SQL error, ORA-01403 is no data found, ORA-00990 is insufficient privileges.

Multiple choice technology programming languages
  1. PL/SQL Error

  2. No data found

  3. Insufficient privileges

  4. Unique constraint violated. (Invalid data has been rejected)

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

ORA-00001 is Oracle's error for 'unique constraint violated' - it fires when an INSERT or UPDATE tries to create a duplicate value in a column with a UNIQUE constraint or PRIMARY KEY. The parenthesized text clarifies that the offending data was rejected. Other options are different errors: ORA-06500 is PL/SQL error, ORA-01403 is no data found, ORA-00990 is insufficient privileges.

Multiple choice technology programming languages
  1. Not connected to ORACLE

  2. Table or view does not exist

  3. Internal error (contact support)

  4. None Of The Above

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

ORA-03114 means 'not connected to ORACLE' - the client has no active database connection. This occurs when trying to execute SQL after the connection was closed, timed out, or never established. Option B is ORA-00942 (table/view doesn't exist), Option C is ORA-00600 (internal error). This is a connectivity/state error, not a SQL logic error.

Multiple choice technology programming languages
  1. Not connected to ORACLE

  2. Table or view does not exist

  3. Internal error (contact support)

  4. None Of The Above

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

ORA-03114 means 'not connected to ORACLE' - the client has no active database connection. This occurs when trying to execute SQL after the connection was closed, timed out, or never established. Option B is ORA-00942 (table/view doesn't exist), Option C is ORA-00600 (internal error). This is a connectivity/state error, not a SQL logic error.

Multiple choice technology databases
  1. The indexed column is declared as NOT NULL.

  2. The indexed columns are used in the FROM clause.

  3. The indexed columns are part of an expression.

  4. The indexed column contains a wide range of values.

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

Indexes are most effective on columns with high cardinality (wide range of unique values). Low-cardinality columns (like boolean flags or status enums) benefit less from indexing because the index doesn't significantly reduce the search space.

Multiple choice technology databases
  1. SELECT TO_CHAR(2000, '$#,###.##') from dual
  2. SELECT TO_CHAR(2000, '$0,000.00') from dual
  3. SELECT TO_CHAR(2000, '$9,999.00') from dual
  4. SELECT TO_CHAR(2000, '$9,999.99') from dual
  5. SELECT TO_CHAR(2000, '$2,000.00') FROM dual;
  6. SELECT TO_CHAR(2000, '$N,NNN.NN') FROM dual;
Reveal answer Fill a bubble to check yourself
B,C,D Correct answer
Explanation

In Oracle TO_CHAR, format codes control number display: '0' forces a digit (shows 0 if absent), '9' is optional (shows space if absent), and ',' inserts thousand separators. Options B, C, and D all produce "\$2,000.00" for input 2000: B uses mandatory digits for exact formatting, while C and D use optional digits that still render correctly since the value has digits in those positions. Option A would fail because '$#,###.##' omits trailing zeros, showing "\$2,000" instead of "\$2,000.00". Option E treats the format string literally, and Option F uses invalid format symbols.

Multiple choice technology databases
  1. It returns a single result row based on single rows

  2. It returns a single result row based on groups of rows

  3. It can only appear in ORDER BY clauses

  4. It cannot appear in select lists

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

Aggregate functions (SUM, COUNT, AVG, MAX, MIN) operate on groups of rows and return a single result row per group. When used without a GROUP BY clause, they treat all rows as one group and return one result row. When used with GROUP BY, they return one row per unique group. Option B correctly captures this behavior. Option A is wrong because aggregates work on sets of rows, not single rows. Options C and D are incorrect because aggregates appear in SELECT lists, HAVING, and ORDER BY (with restrictions), not just ORDER BY.