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 databases
  1. SELECT * FROM employees where salary > (SELECT MIN(salary) FROM employees GROUP BY department_id);

  2. SELECT * FROM employees WHERE salary = (SELECT AVG(salary) FROM employees GROUP BY department_id);

  3. SELECT distinct department_id FROM employees WHERE salary > ANY(SELECT AVG(salary) FROM employees GROUP BY department_id);

  4. SELECT department_id FROM employees WHERE salary > ALL(SELECT AVG(salary) FROM employees GROUP BY department_id);

  5. SELECT department_id FROM employees WHERE salary > ALL (SELECT AVG(salary) FROM employees GROUP BY AVG(SALARY));

Reveal answer Fill a bubble to check yourself
C,D,E Correct answer
Multiple choice technology databases
  1. SELECT ord_id, cust_id, ord_total FROM orders, customers WHERE cust_name='Martin' AND ord_date IN ('18-JUL-2000','21-JUL-2000');

  2. SELECT ord_id, cust_id, ord_total FROM orders WHERE ord_date IN (SELECT ord_date FROM orders WHERE cust_id = (SELECT cust_id FROM customers WHERE cust_name = 'Martin'));

  3. SELECT ord_id, cust_id, ord_total FROM orders WHERE ord_date IN (SELECT ord_date FROM orders, customers WHERE cust_name = 'Martin');

  4. SELECT ord_ id, cust_id, ord_total FROM orders WHERE cust_id IN (SELECT cust_id FROM customers WHERE cust name = 'Martin');

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

To solve this question, the user needs to know SQL syntax and how to retrieve data from a database table. The user must select the appropriate columns from the orders table where the order date is the same as the order date for Martin's orders.

Option A: This option is incorrect because it joins the orders and customers tables but does not specify a relationship between them. Also, it selects orders on two specific dates (18-JUL-2000 and 21-JUL-2000) which may not be the dates Martin placed his orders.

Option B: This option is correct. It selects the order ID, customer ID, and order total from the orders table where the order date is the same as the order date for Martin's orders. It uses subqueries to find the order date and customer ID for Martin and then selects orders with the same order date.

Option C: This option joins the orders and customers tables but does not specify a relationship between them, so it is incorrect. Also, it selects orders on the same dates as Martin's orders, which may not be the correct dates.

Option D: This option selects orders where the customer ID is the same as Martin's customer ID, but it does not specify a relationship between the orders and customers tables. Therefore, it may not retrieve the correct orders.

The Answer is: B. SELECT ord_id, cust_id, ord_total FROM orders WHERE ord_date IN (SELECT ord_date FROM orders WHERE cust_id = (SELECT cust_id FROM customers WHERE cust_name = 'Martin'));

Multiple choice technology databases
  1. MERGE INTO new_employees c USING employees e ON (c.employee_id = e.employee_id) WHEN MATCHED THEN UPDATE SET c.name = e.first_name ||','|| e.last_name WHEN NOT MATCHED THEN INSERT VALUES(e.employee_id, e.first_name ||', '||e.last_name);

  2. MERGE new_employees c USING employees e ON (c.employee_id = e.employee_id) WHEN EXISTS THEN UPDATE SET c.name = e.first_name ||','|| e.last_name WHEN NOT MATCHED THEN INSERT VALUES(e.employee_id, e.first_name ||', '||e.last_name);

  3. MERGE INTO new employees c USING employees e ON (c.employee_id = e.employee_id) WHEN EXISTS THEN UPDATE SET c.name = e.first_name ||','|| e.last_name WHEN NOT MATCHES THEN INSERT VALUES(e.employee_id, e.first_name ||', '||e.last_name);

  4. MERGE new_employees c FROM employees e ON (c.employee_id = e.employee_id) WHEN MATCHED THEN UPDATE SET c.name = e.first_name ||','|| e.last_name WHEN NOT MATCHED THEN INSERT INTO new_employees VALUES(e.employee_id, e.first_name ||'.'||e.last_name);

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

To determine which MERGE statement is valid, we need to examine the syntax of each option and compare it to the requirements of the question.

The question provides two tables: EMPLOYEES and NEW EMPLOYEES. Both tables have an EMPLOYEE_ID column as the primary key. The goal of the MERGE statement is to update the NAME column in the NEW EMPLOYEES table with the concatenated FIRST_NAME and LAST_NAME values from the EMPLOYEES table. If an employee exists in the NEW EMPLOYEES table, the NAME should be updated. If the employee does not exist, a new row should be inserted.

Now let's examine each option:

A. MERGE INTO new_employees c USING employees e ON (c.employee_id = e.employee_id) WHEN MATCHED THEN UPDATE SET c.name = e.first_name ||','|| e.last_name WHEN NOT MATCHED THEN INSERT VALUES(e.employee_id, e.first_name ||', '||e.last_name);

This option is valid. It uses the correct syntax for a MERGE statement and joins the two tables on the EMPLOYEE_ID column. It updates the NAME column in the NEW EMPLOYEES table with the concatenated FIRST_NAME and LAST_NAME values from the EMPLOYEES table. If a match is not found, a new row is inserted into the NEW EMPLOYEES table.

B. MERGE new_employees c USING employees e ON (c.employee_id = e.employee_id) WHEN EXISTS THEN UPDATE SET c.name = e.first_name ||','|| e.last_name WHEN NOT MATCHED THEN INSERT VALUES(e.employee_id, e.first_name ||', '||e.last_name);

This option is invalid. It uses the correct syntax for a MERGE statement, but the WHEN EXISTS clause is not valid. It should be WHEN MATCHED instead. Additionally, the syntax for the UPDATE clause is incorrect.

C. MERGE INTO new employees c USING employees e ON (c.employee_id = e.employee_id) WHEN EXISTS THEN UPDATE SET c.name = e.first_name ||','|| e.last_name WHEN NOT MATCHES THEN INSERT VALUES(e.employee_id, e.first_name ||', '||e.last_name);

This option is invalid. It has a syntax error in the WHEN NOT MATCHES clause, which should be WHEN NOT MATCHED. Additionally, the UPDATE clause syntax is incorrect.

D. MERGE new_employees c FROM employees e ON (c.employee_id = e.employee_id) WHEN MATCHED THEN UPDATE SET c.name = e.first_name ||','|| e.last_name WHEN NOT MATCHED THEN INSERT INTO new_employees VALUES(e.employee_id, e.first_name ||'.'||e.last_name);

This option is invalid. It does not use the correct syntax for a MERGE statement. Additionally, the INSERT INTO clause syntax is incorrect.

Thus, the valid MERGE statement is:

The Answer is: A. MERGE INTO new_employees c USING employees e ON (c.employee_id = e.employee_id) WHEN MATCHED THEN UPDATE SET c.name = e.first_name ||','|| e.last_name WHEN NOT MATCHED THEN INSERT VALUES(e.employee_id, e.first_name ||', '||e.last_name);

Multiple choice technology packaged enterprise solutions
  1. SQL cannot support object-orientation

  2. The same query can be written in many ways, each with vastly different execution plans.

  3. SQL syntax is too difficult for non-computer professionals to use

  4. SQL creates excessive locks within the database

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

SQL is declarative, meaning the same query can be written in multiple syntactically different ways that produce the same result but generate vastly different execution plans. This makes SQL optimization complex and critical for performance. The optimizer must choose the best execution plan from semantically equivalent alternatives.

Multiple choice technology packaged enterprise solutions
  1. Indexes are only used in special cases

  2. Indexes are used to make table storage more efficient

  3. Indexes rarely make a difference in SQL performance

  4. Indexes exist solely to improve query speed

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

Indexes are database structures created specifically to accelerate data retrieval operations. They exist solely to improve query performance by providing fast access paths to data, at the cost of additional storage and write overhead. Indexes are widely used and frequently significantly impact performance.

Multiple choice technology packaged enterprise solutions
  1. View

  2. Index

  3. Synonym

  4. Sequence

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

A synonym provides an alternative name for a database object like a table, allowing you to reference it without changing the actual table name. Views are virtual tables derived from queries, not simple aliases. Indexes improve query performance and sequences generate numeric values.

Multiple choice technology packaged enterprise solutions
  1. Column Constraint

  2. Table Constraint

  3. Both A. and B

  4. None of the Above

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

SQL allows defining constraints at both column level (like NOT NULL or CHECK on a single column) and table level (like PRIMARY KEY, FOREIGN KEY, or UNIQUE that may involve multiple columns). Column constraints apply to individual columns, while table constraints can span multiple columns and enforce relationships.

Multiple choice technology programming languages
  1. True

  2. False

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

TRUNCATE is faster than DELETE because it simply deallocates data pages without logging individual row deletions. DELETE logs each row deletion for rollback capability, making it slower. TRUNCATE is a DDL operation that resets the table, while DELETE is a DML operation that removes rows one by one.

Multiple choice technology programming languages
  1. DDL

  2. DML

  3. DCL

  4. None

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

GRANT and REVOKE are Data Control Language (DCL) commands used to manage database access permissions. DDL (Data Definition Language) defines structures, DML (Data Manipulation Language) manipulates data, and DCL controls access and permissions.