SQL Interview Questions

62%

Question 1

What is the difference between DELETE and TRUNC



Delete command is used to delete one row or rows in a table
You can roll back after using the delete command
It is DML command (Data manipulation language)
It is slower than TRUNCATE

TRUNCATE is used to delete all the rows in a table
You can not rollback data
It is a DDL command (Data Definition Language)
It is faster

Question 2

What are meanings of DQL, DDL, DML, DCL, TCL? Whats are the differen



DQL: Data Query Language statements are used for performing queries on the data within schema objects.

List of DQL:
SELECT: It is used to retrieve data from the database.

DDL: Data Definition Language is a subset of SQL. It is a syntax for creating and modifying database objects such as tables, indices, and users.

List of DDL commands:
CREATE: This command is used to create the database or its objects (like table, index, function, views, store procedure, and triggers).
DROP: This command is used to delete objects from the database.
ALTER: This is used to alter the structure of the database.
TRUNCATE: This is used to remove all records from a table, including all spaces allocated for the records are removed.
COMMENT: This is used to add comments to the data dictionary.
RENAME: This is used to rename an object existing in the database.

DML: A data manipulation language is a computer programming language used for adding, deleting, and modifying data in a database.

List of DML commands:
INSERT: It is used to insert data into a table.
UPDATE: It is used to update existing data within a table.
DELETE: It is used to delete records from a database table.
LOCK: Table control concurrency.
CALL: Call a PL/SQL or JAVA subprogram.
EXPLAIN PLAN: It describes the access path to data.

DCL: Data Control Language includes commands such as GRANT and REVOKE which mainly deal with the rights, permissions, and other controls of the database system.

List of DCL commands:
GRANT: This command gives users access privileges to the database.
REVOKE: This command withdraws the user’s access privileges given by using the GRANT command.

TCL: Transaction Control Language commands are used to manage transactions in the database. These are used to manage the changes made by DML-statements. It also allows statements to be grouped together into logical transactions.

Examples of TCL commands:
COMMIT: Commit command is used to permanently save any transaction into the database.
ROLLBACK: This command restores the database to last committed state.
It is also used with savepoint command to jump to a savepoint in a transaction.
SAVEPOINT: Savepoint command is used to temporarily save a transaction so that you can rollback to that point whenever necessary.

Question 3

What do you mean by DBMS, what are its different ty



A database management system is a software package for creating and managing databases. Many different types of database systems exist based on how they manage the database structure.

A relational database contains multiple tables of data with rows and columns that relate to each other through special key fields. These databases are more flexible than flat file structures, and provide functionality for reading, creating, updating, and deleting data. Relational databases use Structured Query Language (SQL) - a standard user application that provides an easy programming interface for database interaction.

Hierarchical database model resembles a tree structure, similar to a folder architecture in your computer system. The relationships between records are pre-defined in a one to one manner, between 'parent and child' nodes. They require the user to pass a hierarchy in order to access needed data. Due to limitations, such databases may be confined to specific uses.

Network database models also have a hierarchical structure. However, instead of using a single-parent tree hierarchy, this model supports many to many relationships, as child tables can have more than one parent.

In object-oriented databases, the information is represented as objects, with different types of relationships possible between two or more objects. Such databases use an object-oriented programming language for development.

A popular alternative to relational databases, NoSQL databases take a variety of forms and allow you to store and manipulate large amounts of unstructured and semi-structured data. Examples include key-value stores, document stores and graph databases.

Question 4

What are joins in SQL?



A JOIN clause is used to combine rows from two or more tables, based on a related column between them. It is used to merge two tables or retrieve data from there.

There are 4 joins in SQL namely;
- LEFT JOIN
- INNER JOIN
- FULL JOIN
- RIGHT JOIN

Question 5

What is the difference between CHAR and VARCHAR2 datatypes?



Char is used for strings of fixed length. For example; char(10) can only store 10 characters and will not be able to store a string of any other lenght.

Varchar2 is used for character strings of variable length. For example; varchar2(10) can store any lenght i.e. 6,8,2 ,n this variable.

Question 6

What is a Primary key?



A set of attributes that can be used to uniquely identify every tuple is a primary key. So, if there are 3-4 candidate keys present in a relationship, then out of those, one can be chosen as a pirmary key.


Question 7

What are constraints?



Constraints are used to specify the limit on the data type of the table. It can be specified while creating or altering the table statement.

• NOT NULL Ensures that a NULL value can not be stored in a column.
• UNIQUE This contraint makes sure that all the values in a column are different.
• CHECK This contraint ensures that all the values in a column satify a specific condition.
• DEFAULT This contraint consists of a set of default values for a column when no value is specified.
• INDEX This contraint is used to create and retrieve data from the database very quickly.

Question 8

What is a UNIQUE key?



A unique key is a set of one or more than one fields/columns of a table that uniquely identify a record in a database table.

Dublicate values are not allowed. But NULL values are allowed.

You can say that it is little like primary key but it can accept only "one" null value and it cannot have duplicate values.

Question 9

What is a FOREIGN key?



A foreign key is a column or group of columns in a relational database table that provides a link between data in two tables. It acts as a cross-reference between tables because it references the primary key of another table, thereby establishing a link between them.

The table with the foreign key is called the child table, and the table with the primary key is called the referenced or parent table.

The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables.

Question 10

What is the difference between Clustered and Non-Clustered Indexes?



A clustered index defines the order in which data is physically stored in a table. Table data can be sorted in only way, therefore, there can be only one clustered index per table. In SQL Server, the primary key constraint automatically creates a clustered index on that particular column.

A non-clustered index doesn’t sort the physical data inside the table. In fact, a non-clustered index is stored at one place and table data is stored in another place. This is similar to a textbook where the book content is located in one place and the index is located in another. This allows for more than one non-clustered index per table.

Clustered indexes are faster than non-clustered indexes since they don’t involve any extra lookup step.

There can be only one clustered index per table. However, you can create multiple non-clustered indexes on a single table.

Clustered indexes only sort tables. Therefore, they do not consume extra storage. Non-clustered indexes are stored in a separate place from the actual table claiming more storage space.

Next Page >

Quick access to all questions in this exam