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
-
Change it to CHAR(10)
-
Change it to VARCHAR(50)
-
Change it to NVARCHAR(50)
-
Change it to CHAR(100)
B
Correct answer
Explanation
VARCHAR(50) is more efficient than CHAR(50) for state names because VARCHAR only uses storage space equal to the actual length of the data, while CHAR always allocates the full 50 bytes regardless of content length. For variable-length data like state names, VARCHAR reduces storage and improves cache efficiency. CHAR(10) is insufficient (some state names exceed 10 characters). NVARCHAR adds Unicode overhead unnecessarily for plain text. CHAR(100) wastes even more space.
A
Correct answer
Explanation
Numeric columns generally make good index candidates because they are typically compact, fixed-width, and allow efficient comparisons. Numeric indexes enable fast range scans and sorting operations. However, this is not universally true - the selectivity of the column and query patterns matter more. Low-cardinality numeric columns like boolean flags may not benefit from indexing. But as a general principle, numeric columns are well-suited for indexes.
-
There needs to be a Foreign Key relationship between the two tables.
-
Table_B.Column_1 shouldn't have NULL values.
-
There should be a clustered index on Table_A.Column_1 and a non-clustered index on Table_B.Column_1.
-
Table_B.Column_1 should be unqiue.
D
Correct answer
Explanation
In a parent-child one-to-many relationship where the child column must always have a value, the child column (Table_B.Column_1) is a foreign key referencing the parent, must NOT allow NULLs, and is NOT unique (multiple child rows can reference same parent). Option D states 'Table_B.Column_1 should be unique' which is FALSE for a one-to-many relationship. Options A, B, and C all correctly describe the relationship: foreign key constraint, non-nullable child column, and proper indexing strategy.
-
Defining a clustered index on Account_ID
-
Defining a clustered index on Account_ID, Trans_Date
-
Defining a non-clustered index on Account_ID, Trans_Date
-
Don't define any indexes
B
Correct answer
Explanation
Because the query frequently needs the most recent transaction per account, a clustered index on (Account_ID, Trans_Date) stores rows in that order, enabling fast retrieval of the latest date for a given account without additional sorting. A single‑column index or a non‑clustered index would be less efficient.
-
Use UNION between the 10 SELECT statements
-
Execute separate SQL statements and let the front-end merge the results
-
Use UNION ALL between the 10 SELECT statements
-
Define a temporary table and populate it with data from the 10 SELECT statements and then do a single SELECT from this table
C
Correct answer
Explanation
UNION ALL combines results from multiple tables without duplicate checking. Since the tables have mutually exclusive records, no duplicates exist, so the overhead of duplicate elimination in UNION is unnecessary. UNION is slower because it must sort and compare rows to remove duplicates. Separate SQL statements with front-end merging adds application complexity. A temporary table introduces unnecessary I/O and complexity. UNION ALL is the most efficient approach for mutually exclusive data.
B
Correct answer
Explanation
Query hints should only be used as a last resort when the optimizer fails to choose an efficient plan. Hints like NOLOCK can return uncommitted data (dirty reads). UPDATE locks can cause unintended blocking. Once hints are added, they persist even if data distribution changes and the optimizer would have chosen a better plan. Best practice is to trust the query optimizer unless there's a proven performance issue. The statement is false.
-
It will describe the table.
-
It will return ar error:'Table or view does not exist'
-
The query is not correct.
-
None of the above
A
Correct answer
Explanation
In Oracle and most relational databases, DDL statements like CREATE TABLE commit automatically and implicitly. Because of this implicit commit, a subsequent ROLLBACK command has no effect on the table's existence. Therefore, the DESC command will successfully describe the table structure.
-
Non aggregate functions only
-
Aggregate functions only
-
Both 1 & 2
-
None of the above
B
Correct answer
Explanation
GROUP BY is used with aggregate functions like COUNT, SUM, AVG, MAX, MIN to perform calculations on groups of rows. The columns in GROUP BY determine how rows are grouped, and aggregates compute summary values for each group. Non-aggregate columns in the SELECT must appear in the GROUP BY clause itself.
-
update table TABLE_NAME set COMLUMN_NAME=<new values> where <conditions>
-
update TABLE_NAME set COMLUMN_NAME=<new values> where <conditions>
-
Both 1 & 2 are correct
-
None of the above
B
Correct answer
Explanation
The UPDATE statement syntax requires specifying the table name directly after UPDATE, not using the TABLE keyword. Option B correctly shows 'UPDATE TABLE_NAME SET column_name=value WHERE condition'. Option A incorrectly includes 'TABLE' as a keyword before the table name.
A
Correct answer
Explanation
UNION eliminates duplicate rows from the combined result set, effectively performing a DISTINCT operation. UNION ALL preserves all rows including duplicates, which makes it faster and useful when you know there are no duplicates or want to keep them. The statement correctly describes this key difference.
B
Correct answer
Explanation
The SPLIT string function was NOT available in SQL Server 2005. It was introduced in SQL Server 2016. Earlier versions required workarounds using CHARINDEX, SUBSTRING, XML methods, or custom user-defined functions to split strings.
-
DBCC CHECKIDENT
-
DBCC CHECKTABLE
-
DBCC CHECKALLOC
-
DBCC CHECKINTEG
B
Correct answer
Explanation
DBCC CHECKTABLE checks the integrity of a table or indexed view, verifying page linking, structural integrity, and performing internal consistency checks. CHECKIDENT checks identity values, CHECKALLOC verifies allocation structures, and CHECKINTEG isn't a valid DBCC command.
-
DatabaseName.fun.FunctionName()
-
DatabaseName.dbo.FunctionName()
-
Exercise.fun.GetFullName();
-
Exercise.dbo.GetFullName();
B
Correct answer
Explanation
SQL Server functions are called using schema qualification: DatabaseName.SchemaOwner.FunctionName(). The default schema owner is dbo, so DatabaseName.dbo.FunctionName() is correct syntax. The schema qualification is required for UDFs (User-Defined Functions) when calling from outside the function's database.
-
datetime
-
hierarchyid
-
status
-
views
-
value
A,B,C
Correct answer
Explanation
Transact-SQL reserves certain keywords that have special meaning in the language. datetime is a data type keyword, hierarchyid is a data type for hierarchical data, and status is a reserved keyword. views and value are NOT reserved keywords in T-SQL. Knowing reserved words is important to avoid naming conflicts.
-
sp_rename TableNewName, ExistingTableName;
-
sp_renametable ExistingTableName, TableNewName;
-
sp_rename ExistingTableName, TableNewName;
-
sp_renametable TableNewName, ExistingTableName;
C
Correct answer
Explanation
sp_rename 'OldName', 'NewName' is the correct system stored procedure to rename a table (or other database objects). The first parameter is the current name, the second is the new name. Options A and D reverse the order, and sp_renametable doesn't exist - only sp_rename.