0% found this document useful (0 votes)
31 views

DBMS Interview Questions

The document discusses various concepts related to database management systems (DBMS). It defines key terms like database, DBMS, RDBMS, SQL, tables, fields, constraints, joins and provides examples. The summary is: RDBMS stores data in tables where relations can be defined between common fields. SQL is used to retrieve and manipulate structured data in tables using queries. Tables contain rows and columns, with columns called fields and rows called records. Constraints like primary keys, unique keys and foreign keys enforce data rules. Joins combine data from multiple tables based on related columns.

Uploaded by

Krishna ganth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

DBMS Interview Questions

The document discusses various concepts related to database management systems (DBMS). It defines key terms like database, DBMS, RDBMS, SQL, tables, fields, constraints, joins and provides examples. The summary is: RDBMS stores data in tables where relations can be defined between common fields. SQL is used to retrieve and manipulate structured data in tables using queries. Tables contain rows and columns, with columns called fields and rows called records. Constraints like primary keys, unique keys and foreign keys enforce data rules. Joins combine data from multiple tables based on related columns.

Uploaded by

Krishna ganth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

DBMS – INTERVIEW QUESTIONS

1. What is Database?
A database is an organized collection of data, stored and retrieved digitally from a remote or local
computer system. Databases can be vast and complex, and such databases are developed using fixed
design and modeling approaches.

2. What is DBMS?
DBMS stands for Database Management System. DBMS is a system software responsible for the
creation, retrieval, updation and management of the database. It ensures that our data is consistent,
organized and is easily accessible by serving as an interface between the database and its end users or
application softwares.

3. What is RDBMS? How is it different from DBMS?


RDBMS stands for Relational Database Management System. The key difference here, compared to
DBMS, is that RDBMS stores data in the form of a collection of tables and relations can be defined
between the common fields of these tables. Most modern database management systems like MySQL,
Microsoft SQL Server, Oracle, IBM DB2 and Amazon Redshift are based on RDBMS.

4. What is SQL?
SQL stands for Structured Query Language. It is the standard language for relational database
management systems. It is especially useful in handling organized data comprised of entities
(variables) and relations between different entities of the data.

5. What is the difference between SQL and MySQL?


SQL is a standard language for retrieving and manipulating structured databases. On the contrary,
MySQL is a relational database management system, like SQL Server, Oracle or IBM DB2, that is
used to manage SQL databases.

6. What are Tables and Fields?


A table is an organized collection of data stored in the form of rows and columns. Columns can be
categorized as vertical and rows as horizontal. The columns in a table are called fields while the rows
can be referred to as records.
7. What are Constraints in SQL?
Constraints are used to specify the rules concerning data in the table. It can be applied for single or
multiple fields in an SQL table during creation of table or after creationg using the ALTER TABLE
command. The constraints are:
NOT NULL - Restricts NULL value from being inserted into a column.
CHECK - Verifies that all values in a field satisfy a condition.
DEFAULT - Automatically assigns a default value if no value has been specified for the field.
UNIQUE - Ensures unique values to be inserted into the field.
INDEX - Indexes a field providing faster retrieval of records.
PRIMARY KEY - Uniquely identifies each record in a table.
FOREIGN KEY - Ensures referential integrity for a record in another table.
CREATE TABLE Students ( /* Create table with a single field as primary key */
ID INT NOT NULL
Name VARCHAR(255)
PRIMARY KEY (ID)
);
CREATE TABLE Students ( /* Create table with multiple fields as primary key */
ID INT NOT NULL
LastName VARCHAR(255)
FirstName VARCHAR(255) NOT NULL,
CONSTRAINT PK_Student
PRIMARY KEY (ID, FirstName)
);
ALTER TABLE Students /* Set a column as primary key */
ADD PRIMARY KEY (ID);
ALTER TABLE Students /* Set multiple columns as primary key */
ADD CONSTRAINT PK_Student /*Naming a Primary Key*/
PRIMARY KEY (ID, FirstName);
8. What is a UNIQUE constraint?
A UNIQUE constraint ensures that all values in a column are different. This provides uniqueness for
the column(s) and helps identify each row uniquely. Unlike primary key, there can be multiple unique
constraints defined per table. The code syntax for UNIQUE is quite similar to that of PRIMARY KEY
and can be used interchangeably.
CREATE TABLE Students ( /* Create table with a single field as unique */
ID INT NOT NULL UNIQUE
Name VARCHAR(255)
);
CREATE TABLE Students ( /* Create table with multiple fields as unique */
ID INT NOT NULL
LastName VARCHAR(255)
FirstName VARCHAR(255) NOT NULL
CONSTRAINT PK_Student
UNIQUE (ID, FirstName)
);
ALTER TABLE Students /* Set a column as unique */
ADD UNIQUE (ID);

ALTER TABLE Students /* Set multiple columns as unique */


ADD CONSTRAINT PK_Student /* Naming a unique constraint */
UNIQUE (ID, FirstName);
9. What is a Foreign Key?
A FOREIGN KEY comprises of single or collection of fields in a table that essentially refer to the
PRIMARY KEY in another table. Foreign key constraint ensures referential integrity in the relation
between two tables.
The table with the foreign key constraint is labelled as the child table, and the table containing the
candidate key is labelled as the referenced or parent table.

CREATE TABLE Students ( /* Create table with foreign key - Way 1 */


ID INT NOT NULL
Name VARCHAR(255)
LibraryID INT
PRIMARY KEY (ID)
FOREIGN KEY (Library_ID) REFERENCES Library(LibraryID)
);

CREATE TABLE Students ( /* Create table with foreign key - Way 2 */


ID INT NOT NULL PRIMARY KEY
Name VARCHAR(255)
LibraryID INT FOREIGN KEY (Library_ID) REFERENCES Library(LibraryID)
);
ALTER TABLE Students /* Add a new foreign key */
ADD FOREIGN KEY (LibraryID)
REFERENCES Library (LibraryID);

10. What is a Join? List its different types.


The SQL Join clause is used to combine records (rows) from two or more tables in a SQL database
based on a related column between the two.
There are four different types of JOINs in SQL:
(INNER) JOIN: Retrieves records that have matching values in both tables involved in the join. This
is the widely used join for queries.
SELECT *
FROM Table_A
JOIN Table_B;
SELECT *
FROM Table_A
INNER JOIN Table_B;
LEFT (OUTER) JOIN: Retrieves all the records/rows from the left and the matched records/rows
from the right table.
SELECT *
FROM Table_A A
LEFT JOIN Table_B B
ON A.col = B.col;

RIGHT (OUTER) JOIN: Retrieves all the records/rows from the right and the matched records/rows
from the left table.
SELECT *
FROM Table_A A
RIGHT JOIN Table_B B
ON A.col = B.col;
FULL (OUTER) JOIN: Retrieves all the records where there is a match in either the left or right table.
SELECT *
FROM Table_A A
FULL JOIN Table_B B
ON A.col = B.col;

11. What is a Self-Join?


A self JOIN is a case of regular join where a table is joined to itself based on some relation between its
own column(s). Self-join uses the INNER JOIN or LEFT JOIN clause and a table alias is used to assign
different names to the table within the query.

SELECT A.emp_id AS "Emp_ID",A.emp_name AS "Employee",


B.emp_id AS "Sup_ID",B.emp_name AS "Supervisor"
FROM employee A, employee B
WHERE A.emp_sup = B.emp_id;
12. What is a Cross-Join?
Cross join can be defined as a cartesian product of the two tables included in the join. The table after
join contains the same number of rows as in the cross-product of number of rows in the two tables. If a
WHERE clause is used in cross join then the query will work like an INNER JOIN.

SELECT stu.name, sub.subject


FROM students AS stu
CROSS JOIN subjects AS sub;

13. What is an Index? Explain its different types.


A database index is a data structure that provides quick lookup of data in a column or columns of a
table. It enhances the speed of operations accessing data from a database table at the cost of additional
writes and memory to maintain the index data structure.
CREATE INDEX index_name /* Create Index */
ON table_name (column_1, column_2);
DROP INDEX index_name; /* Drop Index */

There are different types of indexes that can be created for different purposes:
Unique and Non-Unique Index:
Unique indexes are indexes that help maintain data integrity by ensuring that no two rows of data in a
table have identical key values. Once a unique index has been defined for a table, uniqueness is enforced
whenever keys are added or changed within the index.
CREATE UNIQUE INDEX myIndex
ON students (enroll_no);
Non-unique indexes, on the other hand, are not used to enforce constraints on the tables with which
they are associated. Instead, non-unique indexes are used solely to improve query performance by
maintaining a sorted order of data values that are used frequently.

14. What is Data Integrity?


Data Integrity is the assurance of accuracy and consistency of data over its entire life-cycle, and is a
critical aspect to the design, implementation and usage of any system which stores, processes, or
retrieves data. It also defines integrity constraints to enforce business rules on the data when it is entered
into an application or a database.

15. What is a Query?


A query is a request for data or information from a database table or combination of tables. A database
query can be either a select query or an action query.
SELECT fname, lname /* select query */
FROM myDb.students
WHERE student_id = 1;
UPDATE myDB.students /* action query */
SET fname = 'Captain', lname = 'America'
WHERE student_id = 1;

16. What is a Subquery? What are its types?


A subquery is a query within another query, also known as nested query or inner query . It is used to
restrict or enhance the data to be queried by the main query, thus restricting or enhancing the output of
the main query respectively. For example, here we fetch the contact information for students who have
enrolled for the maths subject:

SELECT name, email, mob, address


FROM myDb.contacts
WHERE roll_no IN (
SELECT roll_no
FROM myDb.students
WHERE subject = 'Maths');
There are two types of subquery - Correlated and Non-Correlated.
A correlated subquery cannot be considered as an independent query, but it can refer the column in a
table listed in the FROM of the main query.
A non-correlated subquery can be considered as an independent query and the output of subquery is
substituted in the main query.(Single row, Multiple row, Multiple column)

17. What is the SELECT statement?


SELECT operator in SQL is used to select data from a database. The data returned is stored in a result
table, called the result-set.
SELECT * FROM myDB.students;

18. What are some common clauses used with SELECT query in SQL?
Some common SQL clauses used in conjuction with a SELECT query are as follows:
WHERE clause in SQL is used to filter records that are necessary, based on specific conditions.
ORDER BY clause in SQL is used to sort the records based on some field(s) in ascending (ASC) or
descending order (DESC).
SELECT *
FROM myDB.students
WHERE graduation_year = 2019
ORDER BY studentID DESC;
GROUP BY clause in SQL is used to group records with identical data and can be used in conjuction
with some aggregation functions to produce summarized results from the database.
HAVING clause in SQL is used to filter records in combination with the GROUP BY clause. It is
different from WHERE, since WHERE clause cannot filter aggregated records.
SELECT COUNT(studentId), country
FROM myDB.students
WHERE country != "INDIA"
GROUP BY country
HAVING COUNT(studentID) > 5;

19. What are UNION, MINUS and INTERSECT commands?


The UNION operator combines and returns the result-set retrieved by two or more SELECT statements.
The MINUS operator in SQL is used to remove duplicates from the result-set obtained by the second
SELECT query from the result-set obtained by the first SELECT query and then return the filtered
results from the first.
The INTERSECT clause in SQL combines the result-set fetched by the two SELECT statements where
records from one match the other and then returns this intersection of result-sets.
Certain conditions need to be met before executing either of the above statements in SQL -
Each SELECT statement within the clause must have the same number of columns
The columns must also have similar data types
The columns in each SELECT statement should necessarily have the same order
SELECT name FROM Students /* Fetch the union of queries */
UNION
SELECT name FROM Contacts;

SELECT name FROM Students /* Fetch the union of queries with duplicates*/
UNION ALL
SELECT name FROM Contacts;

SELECT name FROM Students /* Fetch names from students */


MINUS /* that aren't present in contacts */
SELECT name FROM Contacts;

SELECT name FROM Students /* Fetch names from students */


INTERSECT /* that are present in contacts as well */
SELECT name FROM Contacts;

20. What are Entities and Relationships?


Entity: An entity can be a real-world object, either tangible or intangible, that can be easily identifiable.
For example, in a college database, students, professors, workers, departments, and projects can be
referred to as entities. Each entity has some associated properties that provide it an identity.

Relationships: Relations or links between entities that have something to do with each other. For
example - The employees table in a company's database can be associated with the salary table in the
same database.

21. List the different types of relationships in SQL.


One-to-One - This can be defined as the relationship between two tables where each record in one table
is associated with the maximum of one record in the other table.
One-to-Many & Many-to-One - This is the most commonly used relationship where a record in a table
is associated with multiple records in the other table.
Many-to-Many - This is used in cases when multiple instances on both sides are needed for defining a
relationship.
Self Referencing Relationships - This is used when a table needs to define a relationship with itself.

22. What is an Alias in SQL?


An alias is a feature of SQL that is supported by most, if not all, RDBMSs. It is a temporary name
assigned to the table or table column for the purpose of a particular SQL query. In addition, aliasing
can be employed as an obfuscation technique to secure the real names of database fields. A table alias
is also called a correlation name .
An alias is represented explicitly by the AS keyword but in some cases the same can be performed
without it as well. Nevertheless, using the AS keyword is always a good practice.
SELECT A.emp_name AS "Employee" /* Alias using AS keyword */
B.emp_name AS "Supervisor"
FROM employee A, employee B /* Alias without AS keyword */
WHERE A.emp_sup = B.emp_id;

23. What is a View?


A view in SQL is a virtual table based on the result-set of an SQL statement. A view contains rows and
columns, just like a real table. The fields in a view are fields from one or more real tables in the database.

24. What is Normalization?


Normalization represents the way of organizing structured data in the database efficiently. It includes
creation of tables, establishing relationships between them, and defining rules for those relationships.
Inconsistency and redundancy can be kept in check based on these rules, hence, adding flexibility to
the database.

25. What is Denormalization?


Denormalization is the inverse process of normalization, where the normalized schema is converted
into a schema which has redundant information. The performance is improved by using redundancy
and keeping the redundant data consistent. The reason for performing denormalization is the overheads
produced in query processor by an over-normalized structure.

26. What are the various forms of Normalization?


Normal Forms are used to eliminate or reduce redundancy in database tables. The different forms are
as follows:
First Normal Form
A relation is in first normal form if every attribute in that relation is a single-valued attribute. If a relation
contains composite or multi-valued attribute, it violates the first normal form. Let's consider the
following students table. Each student in the table, has a name, his/her address and the books they issued
from the public library -
Students Table

As we can observe, the Books Issued field has more than one values per record and to convert it into
1NF, this has to be resolved into separate individual records for each book issued. Check the
following table in 1NF form –
Students Table (1st Normal Form)

Second Normal Form


A relation is in second normal form if it satisfies the conditions for first normal form and does not
contain any partial dependency. A relation in 2NF has no partial dependency, i.e., it has no non-prime
attribute that depends on any proper subset of any candidate key of the table. Often, specifying a single
column Primary Key is the solution to the problem. Examples -
Example 1 - Consider the above example. As we can observe, Students Table in 1NF form has a
candidate key in the form of [Student, Address] that can uniquely identify all records in the table. The
field Books Issued (non-prime attribute) depends partially on the Student field. Hence, the table is not
in 2NF. To convert it into 2nd Normal Form, we will partition the tables into two while specifying a
new Primary Key attribute to identify the individual records in the Students table. The Foreign Key
constraint will be set on the other table to ensure referential integrity.
Students Table (2nd Normal Form)
Books Table (2nd Normal Form)

Third Normal Form


A relation is said to be in the third normal form, if it satisfies the conditions for second normal form
and there is no transitive dependency between the non-prime attributes, i.e.,all non-prime attributes are
determined only by the candidate keys of the relation and not by any other non-prime attribute.

Example 1 - Consider the Students Table in the above example. As we can observe, Students Table in
2NF form has a single candidate key Student_ID (primary key) that can uniquely identify all records in
the table. The field Salutation (non-prime attribute), however, depends on the Student Field rather than
the candidate key. Hence, the table is not in 3NF. To convert it into 3rd Normal Form, we will once
again partition the tables into two while specifying a new Foreign Key constraint to identify the
salutations for individual records in the Students table. The Primary Key constraint for the same will be
set on the Salutations table to identify each record uniquely.
Students Table (3rd Normal Form)
Books Table (3rd Normal Form)

Salutations Table (3rd Normal Form)

Boyce-Codd Normal Form


A relation is in Boyce-Codd Normal Form if satisfies the conditions for third normal form and for every
functional dependency, Left-Hand-Side is super key. In other words, a relation in BCNF has non-trivial
functional dependencies in the form X –> Y, such that X is always a super key. For example - In the
above example, Student_ID serves as the sole unique identifier for the Students Table and Salutation_ID
for the Salutations Table, thus these tables exist in BCNF. Same cannot be said for the Books Table and
there can be several books with common Book Names and same Student_ID.
27. What are the TRUNCATE, DELETE and DROP statements?
DELETE statement is used to delete rows from a table.
DELETE FROM Candidates
WHERE CandidateId > 1000;
TRUNCATE command is used to delete all the rows from the table and free the space containing the
table.
TRUNCATE TABLE Candidates;
DROP command is used to remove an object from the database. If you drop a table, all the rows in the
table is deleted and the table structure is removed from the database.
DROP TABLE Candidates;
28. What is the difference between DROP and TRUNCATE statements?
If a table is dropped, all things associated with the tables are dropped as well. This includes - the
relationships defined on the table with other tables, the integrity checks and constraints, access
privileges and other grants that the table has. To create and use the table again in its original form, all
these relations, checks, constraints, privileges and relationships need to be redefined. However, if a
table is truncated, none of the above problems exist and the table retains its original structure.

29. What is the difference between DELETE and TRUNCATE statements?


The TRUNCATE command is used to delete all the rows from the table and free the space containing
the table.
The DELETE command deletes only the rows from the table based on the condition given in the where
clause or deletes all the rows from the table if no condition is specified. But it does not free the space
containing the table.

30. What are Aggregate functions?


An aggregate function performs operations on a collection of values to return a single scalar value.
Aggregate functions are often used with the GROUP BY and HAVING clauses of the SELECT
statement. Following are the widely used SQL aggregate functions:
AVG() - Calculates the mean of a collection of values.
COUNT() - Counts the total number of records in a specific table or view.
MIN() - Calculates the minimum of a collection of values.
MAX() - Calculates the maximum of a collection of values.
SUM() - Calculates the sum of a collection of values.
Note: All aggregate functions described above ignore NULL values except for the COUNT function.
Additional frequently used functions:
LEN() - Calculates the total length of the given field (column).
UCASE() - Converts a collection of string values to uppercase characters.
LCASE() - Converts a collection of string values to lowercase characters.
MID() - Extracts substrings from a collection of string values in a table.
CONCAT() - Concatenates two or more strings.
RAND() - Generates a random collection of numbers of given length.
ROUND() - Calculates the round off integer value for a numeric field (or decimal point values).
NOW() - Returns the current data & time.
FORMAT() - Sets the format to display a collection of values.

31. What are the differences between OLTP and OLAP?


OLTP stands for Online Transaction Processing, is a class of software applications capable of
supporting transaction-oriented programs. An important attribute of an OLTP system is its ability to
maintain concurrency. OLTP systems often follow a decentralized architecture to avoid single points
of failure. These systems are generally designed for a large audience of end users who conduct short
transactions. Queries involved in such databases are generally simple, need fast response times and
return relatively few records. Number of transactions per second acts as an effective measure for such
systems.
OLAP stands for Online Analytical Processing, a class of software programs which are characterized
by relatively low frequency of online transactions. Queries are often too complex and involve a bunch
of aggregations. For OLAP systems, the effectiveness measure relies highly on response time. Such
systems are widely used for data mining or maintaining aggregated, historical data, usually in multi-
dimensional schemas.

32. What is a Stored Procedure?


A stored procedure is a subroutine available to applications that access a relational database
management system (RDBMS). Such procedures are stored in the database data dictionary. The sole
disadvantage of stored procedure is that it can be executed nowhere except in the database and occupies
more memory in the database server. It also provides a sense of security and functionality as users who
can't access the data directly can be granted access via stored procedures.
DELIMITER $$
CREATE PROCEDURE FetchAllStudents()
BEGIN
SELECT * FROM myDB.students;
END $$
DELIMITER ;
33. How to create empty tables with the same structure as another table?
Creating empty tables with the same structure can be done smartly by fetching the records of one table
into a new table using the INTO operator while fixing a WHERE clause to be false for all records.
Hence, SQL prepares the new table with a duplicate structure to accept the fetched records but since no
records get fetched due to the WHERE clause in action, nothing is inserted into the new table.
SELECT * INTO Students_copy
FROM Students WHERE 1 = 2;

34. What is Pattern Matching in SQL?


SQL pattern matching provides for pattern search in data if you have no clue as to what that word should
be. This kind of SQL query uses wildcards to match a string pattern, rather than writing the exact word.
The LIKE operator is used in conjunction with SQL Wildcards to fetch the required information.
Using the % wildcard to perform a simple search
The % wildcard matches zero or more characters of any type and can be used to define wildcards both
before and after the pattern. Search a student in your database with first name beginning with the letter
K:
SELECT *
FROM students
WHERE first_name LIKE 'K%'
Omitting the patterns using the NOT keyword
Use the NOT keyword to select records that don't match the pattern. This query returns all students
whose first name does not begin with K.
SELECT *
FROM students
WHERE first_name NOT LIKE 'K%'
Matching a pattern anywhere using the % wildcard twice
Search for a student in the database where he/she has a K in his/her first name.
SELECT *
FROM students
WHERE first_name LIKE '%Q%'
Using the _ wildcard to match pattern at a specific position
The _ wildcard matches exactly one character of any type. It can be used in conjunction with %
wildcard. This query fetches all students with letter K at the third position in their first name.
SELECT *
FROM students
WHERE first_name LIKE '__K%'
Matching patterns for specific length
The _ wildcard plays an important role as a limitation when it matches exactly one character. It limits
the length and position of the matched results. For example -
SELECT * /* Matches first names with three or more letters */
FROM students
WHERE first_name LIKE '___%'
SELECT * /* Matches first names with exactly four characters */
FROM students
WHERE first_name LIKE '____'
35. What are advantages of DBMS over traditional file based systems?
Database management systems were developed to handle the following difficulties of typical Fille-
processing systems supported by conventional operating systems.
1. Data redundancy and inconsistency
2. Difficulty in accessing data
3. Data isolation – multiple files and formats
4. Integrity problems
5. Atomicity of updates
6. Concurrent access by multiple users
7. Security problems
36. What is the difference between primary key and unique constraints?
Primary key cannot have NULL value, the unique constraints can have NULL values. There is only one
primary key in a table, but there can be multiple unique constraints.
37. What is SQL?
SQL is Structured Query Language designed for inserting and modifying in a relational database
system.
38. What are the differences between DDL, DML and DCL in SQL?
DDL stands for Data Definition Language. - CREATE, ALTER, DROP and RENAME.
DML stands for Data Manipulation Language. - SELECT, INSERT and UPDATE.
DCL stands for Data Control Language. - GRANT and REVOKE.
39. What is the difference between having and where clause?
HAVING is used to specify a condition for a group or an aggregate function used in select statement.
The WHERE clause selects before grouping. The HAVING clause selects rows after grouping. Unlike
HAVING clause, the WHERE clause cannot contain aggregate functions.
40. What is a Trigger?
A Trigger is a code that associated with insert, update or delete operations. The code is executed
automatically whenever the associated query is executed on a table. Triggers can be useful to maintain
integrity in database.

41. What is a stored procedure?


A stored procedure is like a function that contains a set of operations compiled together. It contains a
set of operations that are commonly used in an application to do some common database tasks.

42. What is the difference between Trigger and Stored Procedure?


Unlike Stored Procedures, Triggers cannot be called directly. They can only be associated with queries.

43. What is a transaction? What are ACID properties?


A Database Transaction is a set of database operations that must be treated as whole, means either all
operations are executed or none of them.
An example can be bank transaction from one account to another account. Either both debit and credit
operations must be executed or none of them.
ACID (Atomicity, Consistency, Isolation, Durability) is a set of properties that guarantee that database
transactions are processed reliably.
44. What is the difference between CHAR and VARCHAR?
• CHAR and VARCHAR are differ in storage and retrieval.
• CHAR column length is fixed while VARCHAR length is variable.
• The maximum no. of character CHAR data type can hold is 255 character while VARCHAR
can hold up to 4000 character.
• CHAR is 50% faster than VARCHAR.
• CHAR uses static memory allocation while VARCHAR uses dynamic memory allocation.

45. Are NULL values same as that of zero or a blank space?


A NULL value is not at all same as that of zero or a blank space. NULL value represents a value which
is unavailable, unknown, assigned or not applicable whereas a zero is a number and blank space is a
character.
46. What is the difference between cross join and natural join?
The cross join produces the cross product or Cartesian product of two tables whereas the natural join is
based on all the columns having the same name and data types in both the tables.
47. How can you insert NULL values in a column while inserting the data?
NULL values in SQL can be inserted in the following ways:

• Implicitly by omitting column from column list.


• Explicitly by specifying NULL keyword in the VALUES clause
48. What is the main difference between ‘BETWEEN’ and ‘IN’ condition operators?
BETWEEN operator is used to display rows based on a range of values in a row whereas the IN
condition operator is used to check for values contained in a specific set of values.
Example of BETWEEN:
SELECT * FROM Students where ROLL_NO BETWEEN 10 AND 50;
Example of IN:
SELECT * FROM students where ROLL_NO IN (8,15,25);

49. How can you select unique records from a table?


You can select unique records from a table by using the DISTINCT keyword.
Select DISTINCT studentID from Student
Using this command, it will print unique student id from the table Student.

50. How can you fetch first 5 characters of the string?


There are a lot of ways to fetch characters from a string. For example:
Select SUBSTRING(StudentName,1,5) as studentname from student

51. What is the main difference between SQL and PL/SQL?


SQL is a query language that allows you to issue a single query or execute a single insert/update/delete
whereas PL/SQL is Oracle’s “Procedural Language” SQL, which allows you to write a full program
(loops, variables, etc.) to accomplish multiple operations such as selects/inserts/updates/deletes.

52. What is Auto Increment in SQL?


Autoincrement keyword allows the user to create a unique number to get generated whenever a new
record is inserted into the table.
This keyword is usually required whenever PRIMARY KEY in SQL is used.
AUTO INCREMENT keyword can be used in Oracle and IDENTITY keyword can be used in SQL
SERVER.

53. How to change a table name in SQL?


ALTER TABLE table_name RENAME TO new_table_name;

54. What do you understand by the E-R model?


E-R model is an Entity-Relationship model which defines the conceptual view of the database.
The E-R model basically shows the real-world entities and their association/relations. Entities here
represent the set of attributes in the database.

55. Define Entity, Entity type, and Entity set.


Entity can be anything, be it a place, class or object which has an independent existence in the real
world.
Entity Type represents a set of entities that have similar attributes.
Entity Set in the database represents a collection of entities having a particular entity type.

56. Define a Weak Entity set.


Weak Entity set is the one whose primary key comprises its partial key as well as the primary key of its
parent entity. This is the case because the entity set may not have sufficient attributes to form a primary
key.

57. Explain the terms ‘Attribute’ and ‘Relations’

Attribute is described as the properties or characteristics of an entity. For Example, Employee ID,
Employee Name, Age, etc., can be attributes of the entity Employee.

Relation is a two-dimensional table containing a number of rows and columns where every row
represents a record of the relation. Here, rows are also known as ‘Tuples’ and columns are known
as ‘Attributes’.

58. What are ACID properties in a transaction?


In order to maintain consistency in a database ‘before and after’ transactions, certain properties are
followed. They are
Atomicity: This means the transaction must happen fully and cannot be left midway.
Consistency: To maintain integrity constraints hence valid data enters the database
Isolation: Controls Concurrency
Durability: Once a transaction is committed it remains committed

59. Can we use TRUNCATE with a WHERE clause?


No, we cannot use TRUNCATE with the WHERE clause.

60. What is a schema?


Answer: A schema is a collection of database objects in a database for a particular user/owner.
Objects can be tables, views, indices and so on.

61. What is the role of GRANT and REVOKE commands?


Answer: The GRANT command enables privileges on the database objects and the REVOKE
command removes them.

62. Write the SQL query to get the current date.

SELECT CURDATE();
63. What is a composite key?
When more than one column is used to define the primary key, it is called a composite key.

Here is a SQL syntax to create a composite key in MySQL

CREATE TABLE SAMPLE_TABLE


(COL1 integer,
COL2 varchar(30),
COL3 varchar(50),
PRIMARY KEY (COL1, COL2));

64. Explain the different types of database keys?

Primary Key: column or set of columns that identify a particular row in a table.
Super Key: single key or set of multiple keys that identifies rows of a table.
Candidate Key: A set of attributes which identify tuples of a table uniquely. It is essentially a super
key without repeated attributes.
Alternate Key: One or more columns of a table that collectively identify each of a table uniquely.
Foreign Key: A common column that defines the relationship between two tables. maintains data
integrity.
Compound Key: Consists of two more attributes that uniquely identify a record, even when the
column is not unique by itself
Composite Key: Primary key that has two or more attributes like {customer_id + mobile_number}
Surrogate Key: Created when there is no naturally available primary key for a table. provides a
unique identity to a row in the table.

65. How do you communicate with an RDBMS?

You have to use Structured Query Language (SQL) to communicate with the RDBMS. Using
queries of SQL, we can give the input to the database and then after processing of the queries
database will provide us the required output.

You might also like