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. WHERE

  2. HAVING

  3. RESTRICT

  4. GROUP BY

  5. ORDER BY

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

HAVING is used to filter groups after GROUP BY aggregation. WHERE filters individual rows before grouping. RESTRICT is not a valid SQL clause. GROUP BY creates groups, and ORDER BY sorts results. Only HAVING can exclude group results based on aggregate conditions.

Multiple choice technology databases
  1. INSERT

  2. UPDATE

  3. SELECT

  4. DESCRIBE

  5. DELETE

  6. RENAME

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

DESCRIBE is a proprietary command specific to the SQL*Plus environment used to view table structures, whereas SELECT, INSERT, UPDATE, DELETE, and RENAME are standard ANSI SQL statements.

Multiple choice technology databases
  1. create groups of data

  2. sort data in a specific order

  3. convert data to a different format

  4. Retrieve data based on an unknown condition

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

To solve this question, the user needs to know the definition and purpose of a subquery.

A subquery is a query that is nested inside another query and returns data that will be used by the main query. It can be used to retrieve data based on an unknown condition and to filter data from a table.

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

A. create groups of data: This option is incorrect because creating groups of data is typically done using the GROUP BY clause, not a subquery.

B. sort data in a specific order: This option is incorrect because sorting data is typically done using the ORDER BY clause, not a subquery.

C. convert data to a different format: This option is incorrect because data conversion is typically done using functions like CAST or CONVERT, not a subquery.

D. Retrieve data based on an unknown condition: This option is correct. A subquery can be used to retrieve data based on an unknown condition, such as selecting all employees with salaries greater than the average salary.

Therefore, the answer is: D.

Multiple choice technology databases
  1. Removes the table

  2. Removes all rows from a table

  3. shortens the table to 10 rows

  4. Removes all columns from a table

  5. Removes foreign keys from a table

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

TRUNCATE removes all rows from a table quickly by deallocating data pages, unlike DELETE which logs individual row deletions. It does NOT remove the table structure (DROP does that), nor does it limit rows or remove columns/foreign keys. The table structure remains intact.

Multiple choice technology databases
  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 an ORDER BY clause in the SELECT statement

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

  5. A view must have aliases defined for the column names in the SELECT statement

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

Explanation: To answer this question, we need to understand the concept of views in the context of databases.

A. A view can be created as read-only - This statement is true. A view can be created with the SELECT statement that restricts the columns and rows that are returned. This can be used to ensure a user only has read access to certain data.

B. A view can be created as a join on two or more tables - This statement is true. A view can be created as a SELECT statement that combines data from two or more tables. This can be used to simplify queries by combining data that is frequently used together.

C. A view cannot have an ORDER BY clause in the SELECT statement - This statement is false. A view can have an ORDER BY clause in the SELECT statement to sort the data returned by the view.

D. A view cannot be created with a GROUP BY clause in the SELECT statement - This statement is false. A view can be created with a GROUP BY clause in the SELECT statement to group the data returned by the view.

E. A view must have aliases defined for the column names in the SELECT statement - This statement is false. A view does not require aliases for the column names in the SELECT statement, although it can be useful to provide more meaningful names for the columns.

Therefore, options A and B are both true statements, making the correct answer B.

Multiple choice technology databases
  1. SELECT last_name, 12*salary*commission_pct FROM emp;

  2. SELECT last_name, 12*salary* (commission_pct,0) FROM emp;

  3. SELECT last_name, 12*salary*(nvl(commission_pct,0)) FROM emp

  4. SELECT last_name, 12*salary*(decode(commission_pct,0)) FROM emp;

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

Since commission_pct can contain null values, any direct multiplication would result in null. Using NVL(commission_pct, 0) replaces any null value with 0, ensuring a valid numeric result is calculated and displayed for all rows.

Multiple choice technology databases
  1. SUM(start_date)

  2. AVG(start_date)

  3. COUNT(start_date)

  4. AVG(start_date, end_date)

  5. MIN(start_date)

  6. MAXIMUM(start_date)

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

COUNT works on any data type including dates - it counts non-null values. MIN and MAX can compare dates chronologically. SUM and AVG don't make sense on dates (can't add or average dates). MAXIMUM (F) is invalid - the correct function is MAX. Option D's AVG with two parameters is also invalid syntax.

Multiple choice technology databases
  1. The statement executes successfully and returns the student ID and sum of all marks for each student who obtained more than the average mark in each subject.

  2. The statement returns an error at the SELECT clause.

  3. The statement returns an error at the WHERE clause.

  4. The statement returns an error at the ORDER BY clause.

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

The SQL parser returns an error at the WHERE clause because group/aggregate functions like AVG cannot be used directly inside a WHERE clause; instead, a subquery or a HAVING clause must be used.

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

TO_CHAR format '$0,000.00' forces 4-digit with cents: $2,000.00. '$9,999.00' does the same (9 means optional digit, 0 means required). '$9,999.99' would also display $2,000.00 (the 99 format doesn't force extra cents if not present). Options A, E, F are invalid: E is literal, F uses N instead of 9, A uses # instead of 9.

Multiple choice technology databases
  1. The underlying tables must have data.

  2. You need SELECT privileges on the view.

  3. The underlying tables must be in the same schema.

  4. You need SELECT privileges only on the underlying tables.

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

To query a view, a user only needs the SELECT privilege on the view itself. They do not require direct privileges on the underlying base tables, nor do the tables need to reside in the same schema.

Multiple choice technology databases
  1. The two statements produce identical results.

  2. The second statement returns a syntax error.

  3. There is no need to specify DESC because the results are sorted in descending order by default.

  4. The two statements can be made to produce identical results by adding a column alias for the salary column in the second SQL statement.

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

To understand these SQL statements, the user needs to know how to query a database and sort the results in descending order based on a specific column.

The first SQL statement selects the last name, salary, and hire date columns from the EMPLOYEES table and orders the results by salary in descending order using the ORDER BY clause with the DESC keyword.

The second SQL statement is identical to the first one but uses the shorthand ORDER BY 2 DESC instead of explicitly specifying the salary column name.

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

A. The two statements produce identical results: This option is correct. Both SQL statements select the same columns and sort the results in the same descending order based on the salary column. The shorthand ORDER BY 2 DESC in the second statement refers to the second column in the SELECT clause, which is the salary column.

B. The second statement returns a syntax error: This option is incorrect. The second SQL statement is a valid syntax and produces the same results as the first one.

C. There is no need to specify DESC because the results are sorted in descending order by default: This option is incorrect. SQL sorts the results in ascending order by default, so the DESC keyword is needed to sort the results in descending order.

D. The two statements can be made to produce identical results by adding a column alias for the salary column in the second SQL statement: This option is incorrect. Adding a column alias does not change the way the ORDER BY clause works. It only renames the column in the result set.

The Answer is: A

Multiple choice technology databases
  1. INSTR returns the numeric position of a named character.

  2. NVL2 returns the first non-null expression in the expression list.

  3. TRUNCATE rounds the column, expression, or value to n decimal places.

  4. DECODE translates an expression after comparing it to each search value.

  5. TRIM trims the heading or trailing characters (or both) from a character string.

  6. NULLIF compares two expressions and returns null if they are equal, or the first expression if they are not equal.

Reveal answer Fill a bubble to check yourself
A,D,E,F Correct answer
Explanation

INTR finds the numeric position of a substring within a string, making option A correct. DECODE is a conditional function that compares an expression to search values and returns corresponding results, so D is correct. TRIM removes leading or trailing characters (or both) from strings, so E is correct. NULLIF compares two expressions and returns null if equal or the first expression if not equal, so F is correct. NVL2 does not return the first non-null expression (that's COALESCE) - NVL2 returns different values based on whether the first expression is null. TRUNC removes decimal places without rounding, while ROUND would round.

Multiple choice technology databases
  1. CREATE USER Susan;

  2. CREATE OR REPLACE USER Susan;

  3. CREATE NEW USER Susan DEFAULT;

  4. CREATE USER Susan IDENTIFIED BY blue;

  5. CREATE NEW USER Susan IDENTIFIED by blue;

  6. CREATE OR REPLACE USER Susan IDENTIFIED BY blue;

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

The correct syntax for creating a user in Oracle is CREATE USER username IDENTIFIED BY password. Option D shows this exact syntax. Options A and B are incomplete (missing IDENTIFIED BY clause). Options C and E incorrectly use CREATE NEW USER which is not valid Oracle syntax. Option F incorrectly uses CREATE OR REPLACE USER which is not a valid command for users (that syntax applies to objects like views or procedures).