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. EXTRACT FirstName FROM Persons

  2. SELECT FirstName FROM Persons

  3. SELECT Persons.FirstName

  4. SELECT * FROM Persons

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

SELECT column_name FROM table_name retrieves a specific column. Option B shows the correct syntax. Options A (EXTRACT) and D (SELECT *) are wrong - EXTRACT is for dates/time parts, and * selects all columns not just one. Option C would work but is less standard than B.

Multiple choice technology databases
  1. SELECT * FROM Persons WHERE FirstName='Peter' AND LastName='Jackson'

  2. SELECT FirstName='Peter', LastName='Jackson' FROM Persons

  3. SELECT * FROM Persons WHERE FirstName<>'Peter' AND LastName<>'Jackson'

  4. SELECT * FROM Persons WHERE FirstName='Jackson' AND LastName='Peter'

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

The standard SQL syntax to filter records based on multiple conditions is to use the WHERE clause combined with the AND operator. The correct query specifies both FirstName and LastName conditions correctly, whereas the other options use incorrect syntax or swap the values.

Multiple choice technology databases
  1. SELECT * FROM Persons WHERE LastName>'Hansen' AND LastName<'Pettersen'

  2. SELECT * FROM Persons WHERE LastName BETWEEN 'Hansen' AND 'Pettersen'

  3. SELECT LastName>'Hansen' AND LastName<'Pettersen' FROM Persons

  4. SELECT LastName<>'Hansen' AND LastName<>'Pettersen' FROM Persons

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

To solve this question, the user needs to know the syntax for the SELECT statement in SQL and how to use the WHERE clause to filter records based on a specific condition. The user must also understand the BETWEEN operator in SQL that is used to select values within a range.

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

A. SELECT * FROM Persons WHERE LastName>'Hansen' AND LastName<'Pettersen' This option is incorrect. The query selects records whose last name is greater than "Hansen" and less than "Pettersen," but does not include the records with last name "Hansen" and "Pettersen." We need to include the "Hansen" and "Pettersen" records in the query.

B. SELECT * FROM Persons WHERE LastName BETWEEN 'Hansen' AND 'Pettersen' This option is correct. The query selects all records from the "Persons" table where the "LastName" is alphabetically between (and including) "Hansen" and "Pettersen". The BETWEEN operator is used to select values within a range.

C. SELECT LastName>'Hansen' AND LastName<'Pettersen' FROM Persons This option is incorrect. The syntax is incorrect. We need to use the SELECT statement to select columns, not conditions. Also, we need to use the WHERE clause to filter records, not the SELECT statement.

D. SELECT LastName<>'Hansen' AND LastName<>'Pettersen' FROM Persons This option is incorrect. The query selects all the records except the records with the last name "Hansen" and "Pettersen" from the "Persons" table, which is not what we want. We need to select records with last name between (and including) "Hansen" and "Pettersen".

The Answer is: B

Multiple choice technology programming languages
  1. UPDATE, DELETE, INSERT and SELECT statements.

  2. INSERT statements only.

  3. UPDATE statements only.

  4. DELETE statements only.

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

Subqueries can indeed be nested within SELECT, INSERT, UPDATE, and DELETE statements in SQL. They allow you to use the result of one query to filter or provide values for another query. Options B, C, and D are incorrect because they are overly restrictive - subqueries work in all four types of statements.

Multiple choice technology programming languages
  1. A view is a database diagram.

  2. A view is a virtual table which results of executing a pre-compiled query. A view is not part of the physical database schema, while the regular tables are.

  3. A view is a special stored procedure executed when certain event occurs.

  4. None of the above

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

To answer this question, the user needs to understand the concept of views in databases.

Option A is incorrect. A view is not a database diagram. A database diagram is a graphical representation of the database schema.

Option B is correct. A view is a virtual table that is generated from the result of a pre-compiled query. It is not part of the physical database schema, but it can be used in the same way as a regular table. Views are often used to simplify complex queries or to provide an additional level of security by limiting access to certain data.

Option C is incorrect. A view is not a stored procedure. A stored procedure is a set of SQL statements that are stored in the database and can be executed later.

Therefore, the correct answer is:

The Answer is: B

Multiple choice technology programming languages
  1. A blinking vertical line that indicates the location of the next input on the display screen.

  2. A cursor is SQL keyword specifying a retrieved data order.

  3. Cursor is acronym for Current Set Of Records and is a database object pointing to a currently selected set of records.

  4. None of the above

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

A database cursor is a database object that allows traversal over the rows in a result set row-by-row. It represents the 'Current Set Of Records' and is commonly used in stored procedures and application code. Option A describes a UI cursor (blinking line), not a database cursor.

Multiple choice technology programming languages
  1. A foreign key is a key field (column) in a database table, which relates the table to another table where the key is a primary key. The primary - foreign key relations are used to cross-reference database tables.

  2. The foreign key is a SQL locking mechanism.

  3. The foreign key is a column that can have NULL values.

  4. None of the above

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

A foreign key is a column (or set of columns) in one table that references the primary key of another table, creating a relationship between the two tables. This referential integrity constraint ensures that data remains consistent across related tables. Option B incorrectly describes it as a locking mechanism.

Multiple choice technology programming languages
  1. Derived Table

  2. Volatile Table

  3. Global Temp Table

  4. All of the above

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

Volatile tables are temporary session-specific tables ideal for ad-hoc queries. They exist only during the session and are automatically dropped, perfect for one-time analysis without cluttering the database. Derived tables are query-scoped, while global temp tables persist for future use.

Multiple choice technology programming languages
  1. Global Temporary Table

  2. Derived Table

  3. Volatile Table

  4. All of the above

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

Derived tables (subqueries in FROM clause) are ideal for one-time transformations because they exist only during query execution. For a single denormalization task, creating temporary tables adds unnecessary overhead and object management.

Multiple choice technology programming languages
  1. True

  2. False

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

The CREATE TABLE statement has invalid syntax. The AS clause requires proper column specification (either defining columns first or using AS (SELECT) WITH DATA without declaring columns in the CREATE clause). This syntax mix creates a malformed statement.

Multiple choice technology programming languages
  1. By Using a Self Join.

  2. By Using a Nested Join.

  3. By using a Volatile Temporary Table.

  4. By using a View.

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

A self-join can update a table by joining it to itself and setting values based on correlated data. A view can also be used indirectly - some databases allow updating through simple views, or you can use INSTEAD OF triggers to perform updates through views. Nested joins don't perform updates, and volatile tables are temporary storage, not update mechanisms.

Multiple choice technology databases
  1. CREATE TABLE EMP (empno NUMBER(4), ename VARCHAR2(35), deptno NUMBER(7,2) NOT NULL, CONSTRAINT emp_deptno_fk FOREIGN KEY deptno REFERENCES dept deptno);

  2. CREATE TABLE EMP (empno NUMBER(4), ename VARCHAR2(35), deptno NUMBER(7,2) CONSTRAINT emp_deptno_fk REFERENCES dept (deptno));

  3. CRETE TABLE EM (empno NUMBER(4), ename VARCHAR2(35) deptno NUMBER (7,2) NOT NULL, CONSTRAINT em_deptno_fk REFERENCES dept (deptno) FOREIGN KEY (deptno));

  4. CREATE TABLE EMP (empno NUMBER (4), ename VARCHAR2(35), deptno NUMBER(7,2) FOREIGN KEY CONSTRAINT emp deptno fk REFERENCES dept (deptno));

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

To understand this question, the user needs to know how to define foreign key constraints in SQL. A foreign key is a column or combination of columns that is used to establish a link between the data in two tables. The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables.

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

A. CREATE TABLE EMP (empno NUMBER(4), ename VARCHAR2(35), deptno NUMBER(7,2) NOT NULL, CONSTRAINT emp_deptno_fk FOREIGN KEY deptno REFERENCES dept deptno);

This option is incorrect because it has a syntax error. The FOREIGN KEY constraint is not properly defined. It is missing parentheses around the column name, and the table name should be followed by the column name in parentheses.

B. CREATE TABLE EMP (empno NUMBER(4), ename VARCHAR2(35), deptno NUMBER(7,2) CONSTRAINT emp_deptno_fk REFERENCES dept (deptno));

This option is correct. It defines a FOREIGN KEY constraint called emp_deptno_fk on the deptno column of the EMP table. The constraint references the DEPTNO column of the DEPT table.

C. CRETE TABLE EM (empno NUMBER(4), ename VARCHAR2(35) deptno NUMBER (7,2) NOT NULL, CONSTRAINT em_deptno_fk REFERENCES dept (deptno) FOREIGN KEY (deptno));

This option is incorrect because it has a syntax error. The table name is misspelled, and the FOREIGN KEY constraint is not properly defined. It should be defined before the REFERENCES keyword.

D. CREATE TABLE EMP (empno NUMBER (4), ename VARCHAR2(35), deptno NUMBER(7,2) FOREIGN KEY CONSTRAINT emp deptno fk REFERENCES dept (deptno));

This option is incorrect because it has a syntax error. The FOREIGN KEY constraint is not properly defined. It is missing parentheses around the column name, and the table name should be followed by the column name in parentheses.

Therefore, the correct answer is:

The Answer is: B

Multiple choice technology databases
  1. The DESCRIBE DEPT statement displays the structure of the DEPT table

  2. The ROLLBACK statement frees the storage space occupied by the DEPT table.

  3. The DESCRIBE DEPT statement returns an error ORA-04043: object DEPT does not exist

  4. The DESCRIBE DEPT statement displays the structure of the DEPT table only if there is a COMMIT statement introduced before the ROLLBACK statement.

Reveal answer Fill a bubble to check yourself
A Correct answer