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. SELECT DISTINCT

  2. SELECT DIFFERENT

  3. SELECT UNIQUE

  4. SELECT ALL

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

SELECT DISTINCT eliminates duplicate rows from the result set, returning only unique values. The other options (DIFFERENT, UNIQUE, ALL) are not valid SQL keywords for this purpose - SELECT UNIQUE exists in Oracle but is deprecated, while SELECT DIFFERENT is not standard SQL.

Multiple choice technology databases
  1. SELECT * FROM Persons ORDER FirstName DESC

  2. SELECT * FROM Persons ORDER BY FirstName DESC

  3. SELECT * FROM Persons SORT BY 'FirstName' DESC

  4. SELECT * FROM Persons SORT 'FirstName' DESC

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

To solve this question, the user needs to know SQL syntax and the concept of sorting query results in a descending order based on a specific column.

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

A. SELECT * FROM Persons ORDER FirstName DESC This option is incorrect because the correct syntax for sorting query results in SQL requires the use of the "ORDER BY" clause followed by the column name to sort by, and then the keyword "DESC" to specify descending order. Therefore, the correct syntax should be "ORDER BY FirstName DESC" instead of "ORDER FirstName DESC".

B. SELECT * FROM Persons ORDER BY FirstName DESC This option is correct. The "ORDER BY" clause is used to sort the query results based on a specific column. In this case, we want to sort the results in descending order based on the "FirstName" column. Therefore, the correct syntax is "ORDER BY FirstName DESC".

C. SELECT * FROM Persons SORT BY 'FirstName' DESC This option is incorrect because there is no "SORT" keyword in SQL. The correct keyword to use is "ORDER BY". Additionally, the column name should not be enclosed in single quotes. Therefore, the correct syntax should be "ORDER BY FirstName DESC" instead of "SORT BY 'FirstName' DESC".

D. SELECT * FROM Persons SORT 'FirstName' DESC This option is incorrect because the correct keyword to use for sorting query results in SQL is "ORDER BY" rather than "SORT". Additionally, the column name should not be enclosed in single quotes. Therefore, the correct syntax should be "ORDER BY FirstName DESC" instead of "SORT 'FirstName' DESC".

The Answer is: B

Multiple choice technology programming languages
  1. SELECT * FROM Contest WHERE ContestDate < '05/25/2006'

  2. SELECT * FROM Contest HAVING ContestDate >= '05/25/2006'

  3. SELECT * FROM Contest WHERE ContestDate >= '05/25/2006'

  4. None of the above

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

SELECT with WHERE clause filters rows before returning them. Option C correctly uses WHERE with >= operator. Option B incorrectly uses HAVING (for aggregate filtering), and Option A has the wrong operator direction.

Multiple choice technology programming languages
  1. To retrieve data.

  2. To insert data.

  3. To modify data.

  4. All the above

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

The INSERT command in SQL is specifically designed to add new rows of data into a table. It is not used for retrieving (SELECT) or modifying (UPDATE) existing data - those are separate SQL commands.

Multiple choice technology programming languages
  1. checks if the table has primary key specified

  2. deletes the table

  3. deletes all rows from a table

  4. None of the above

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

The TRUNCATE TABLE command is a DDL operation that removes all rows from a table. It is faster than DELETE because it deallocates the data pages instead of logging individual row deletions, but it keeps the table structure intact.

Multiple choice technology databases
  1. 123

  2. 12

  3. 123456

  4. 456

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

In T-SQL, SUBSTRING(string, start, length) uses 1-based indexing. However, if start is 0, it starts 'before' the first character. For SUBSTRING('123456', 0, 3), it takes the character at index 0 (nothing), index 1 ('1'), and index 2 ('2'), resulting in '12'.

Multiple choice technology databases
  1. 09/09/2008

  2. 09-09-2008 00:00:00:000

  3. Error Message

  4. None of the above

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

The DATE data type was introduced in SQL Server 2008. In SQL Server 2005 and earlier versions, attempting to use 'declare @d date' will result in an error because the DATE type doesn't exist in those versions. The code will fail at the declaration stage itself, never reaching the assignment statement. This is a common compatibility issue when migrating code between SQL Server versions.

Multiple choice technology databases
  1. 8000

  2. 4000

  3. 4005

  4. 5

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

The unicode prefix N'hello' makes the string a unicode string (nvarchar), while replicate('-', 8000) produces a regular varchar string. When concatenating unicode and non-unicode strings, SQL Server implicitly converts to unicode (nvarchar), but the variable @c is declared as varchar(8000), not nvarchar. The implicit conversion truncates to 4000 characters because unicode strings require 2 bytes per character. Therefore, len(@c) returns 4000, not 8000.

Multiple choice technology databases
  1. 123

  2. 12

  3. 123456

  4. 456

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

SQL Server's SUBSTRING function uses 1-based indexing, not 0-based. When you call SUBSTRING('123456', 0, 3), it starts from position 0, which is treated as position 1. It then returns 3 characters starting from that position, giving you '12'. This is different from many programming languages that use 0-based indexing and is a common source of confusion for developers new to SQL Server.

Multiple choice technology databases
  1. Primary key access

  2. Full table scan

  3. Access via unique index

  4. Table access by ROWID

  5. None of the above

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

Primary key access is generally the fastest method for Oracle to retrieve a single row because primary keys are unique and have mandatory indexes. When you query by primary key, Oracle can use the index to quickly locate the exact row. Access by ROWID is equally fast when you already have the ROWID, but primary key is typically the standard fastest method. Full table scans are slow for single row retrieval, and unique index access is similar but secondary to primary key access in most scenarios.