Viva Questions
Viva Questions
Viva Questions
1) What is SQL?
SQL stands for the Structured Query Language. It is the standard language used to maintain the relational database and
perform many different data manipulation operations on the data. SQL was initially invented in 1970. It is a database
language used for database creation, deletion, fetching and modifying rows, etc. sometimes, it is pronounced as 'sequel.' We
can also use it to handle organized data comprised of entities (variables) and relations between different entities of the data.
o Data definition language (DDL): It defines the data structure that consists of commands like CREATE, ALTER, DROP,
etc.
o Data manipulation language (DML): It is used to manipulate existing data in the database. The commands in this
category are SELECT, UPDATE, INSERT, etc.
o Data control language (DCL): It controls access to the data stored in the database. The commands in this category
include GRANT and REVOKE.
o Transaction Control Language (TCL): It is used to deal with the transaction operations in the database. The
commands in this category are COMMIT, ROLLBACK, SET TRANSACTION, SAVEPOINT, etc.
6) What is the purpose of DDL Language?
DDL stands for Data definition language. It is the subset of a database that defines the data structure of the database when
the database is created. For example, we can use the DDL commands to add, remove, or modify tables. It consists of the
following commands: CREATE, ALTER and DELETE database objects such as schema, tables, indexes, view, sequence, etc.
Example
Example
1. INSERT INTO Student VALUES (111, 'George', 'Computer Science')
GRANT: It enables system administrators to assign privileges and roles to the specific user accounts to perform specific
tasks on the database.
REVOKE: It enables system administrators to revoke privileges and roles from the user accounts so that they cannot use the
previously assigned permission on the database.
Example
Example
Table: Student
1. CONSTRAINT constraint_name]
2. FOREIGN KEY [foreign_key_name] (col_name, ...)
3. REFERENCES parent_tbl_name (col_name,...)
13) What is the difference between a primary key and a unique key?
The primary key and unique key both are essential constraints of the SQL. The main difference among them is that the
primary key identifies each record in the table. In contrast, the unique key prevents duplicate entries in a column except for
a NULL value. The following comparison chart explains it more clearly:
The primary key act as a unique identifier for The unique key is also a unique identifier for records when the
each record in the table. primary key is not present in the table.
We cannot store NULL values in the primary key We can store NULL value in the unique key column, but only one
column. NULL is allowed.
We cannot change or delete the primary key We can modify the unique key column values.
column values.
o Software
o Data
o Procedures
o Database Languages
o Query Processor
o Database Manager
o Database Engine
o Reporting
There are some rules of database normalization, which is commonly known as Normal From, and they are:
Using these steps, the redundancy, anomalies, inconsistency of the data in the database can be removed.
The occurrence of redundant terms in the database causes the waste of space in the disk.
Due to redundant terms, inconsistency may also occur. If any change is made in the data of one table but not made in the
same data of another table, then inconsistency will occur. This inconsistency will lead to the maintenance problem and
effects the ACID properties as well.
Denormalization doesn't mean that normalization will not be done. It is an optimization strategy that takes place after the
normalization process.
o Arithmetic operators: These operators are used to perform mathematical operations on numerical data. The
categories of this operators are addition (+), subtraction (-), multiplication (*), division (/), remainder/modulus (%),
etc.
o Logical operators: These operators evaluate the expressions and return their results in True or False. This operator
includes ALL, AND, ANY, ISNULL, EXISTS, BETWEEN, IN, LIKE, NOT, OR, UNIQUE.
o Comparison operators: These operators are used to perform comparisons of two values and check whether they are
the same or not. It includes equal to (=), not equal to (!= or <>), less than (<), greater than (>), less than or equal to (<=),
greater than or equal to (>=), not less than (!<), not greater than (!>), etc.
o Bitwise operators: It is used to do bit manipulations between two expressions of integer type. It first performs
conversion of integers into binary bits and then applied operators such as AND (& symbol), OR (|, ^), NOT (~), etc.
o Compound operators: These operators perform operations on a variable before setting the variable's result to the
operation's result. It includes Add equals (+=), subtract equals (-=), multiply equals (*=), divide equals (/=), modulo
equals (%=), etc.
o String operators: These operators are primarily used to perform concatenation and pattern matching of strings. It
includes + (String concatenation), += (String concatenation assignment), % (Wildcard), [] (Character(s) matches), [^]
(Character(s) not to match), _ (Wildcard match one character), etc.
For example: Suppose we have a book which carries the details of the countries. If you want to find out information about
India, why will you go through every page of that book? You could directly go to the index. Then from the index, you can go
to that particular page where all the information about India is given.
o Unique Index
o Clustered Index
o Non-Clustered Index
o Bit-Map Index
o Normal Index
o Composite Index
o B-Tree Index
o Function-Based Index
27) What is the unique index?
UNIQUE INDEX is used to enforce the uniqueness of values in single or multiple columns. We can create more than one
unique index in a single table. For creating a unique index, the user has to check the data in the column because the unique
indexes are used when any column of the table has unique values. This indexing does not allow the field to have duplicate
values if the column is unique indexed. A unique index can be applied automatically when a primary key is defined.
Example
Suppose we want to make a Phone column as a unique index. We can do this like below:
The purpose of creating a non-clustered index is for searching the data. Its best example is a book where the content is
written in one place, and the index is at a different place. We can create 0 to 249 non-clustered indexes in each table. The
non-clustered indexing improves the performance of the queries which use keys without assigning the primary key.
30) What are the differences between SQL, MySQL, and SQL Server?
The following comparison chart explains their main differences:
SQL or Structured Query Language is MySQL is the popular database SQL Server is an RDBMS database
useful for managing our relational management system used for system mainly developed for the
databases. It is used to query and managing the relational database. It is a Windows system to store, retrieve,
operate the database.
fast, scalable, and easy-to-use and access data requested by the
database. developer.
SQL first appeared in 1974. MySQL first appeared on May 23, 1995. SQL Server first appeared on April 24,
1989.
SQL was developed by IBM MySQL was developed by Oracle SQL Server was developed by
Corporation. Corporation. Microsoft Company.
SQL is a query language for MySQL is database software that uses SQL Server is also a software that uses
managing databases. SQL language to conduct with the SQL language to conduct with the
database. database.
SQL has no variables. MySQL can use variables constraints SQL Server can use variables
and data types. constraints and data types.
SQL is a programming language, so MySQL is software, so it gets frequent SQL Server is also software, so it gets
that it does not get any updates. Its updation. frequent updation.
commands are always fixed and
remain the same.
SQL PL/SQL
SQL is a database structured query language used PL/SQL or Procedural Language/Structured Query Language is a
to communicate with relational databases. It was dialect of SQL used to enhance the capabilities of SQL. Oracle
developed by IBM Corporations and first Corporation developed it in the early 90's. It uses SQL as its
appeared in 1974. database language.
SQL is a declarative and data-oriented language. PL/SQL is a procedural and application-oriented language.
SQL has no variables. PL/SQL can use variables constraints and data types.
SQL can execute only a single query at a time. PL/SQL can execute a whole block of code at once.
SQL query can be embedded in PL/SQL. PL/SQL cannot be embedded in SQL as SQL does not support any
programming language and keywords.
SQL can directly interact with the database server. PL/SQL cannot directly interact with the database server.
SQL is like the source of data that we need to PL/SQL provides a platform where SQL data will be shown.
display.
33) What is the difference between clustered and non-clustered indexes in SQL?
Indexing is a method to get the requested data very fast. There are mainly two types of indexes in SQL, clustered index and
non-clustered index. The differences between these two indexes are very important from an SQL performance perspective.
The following comparison chart explains their main differences:
Clustered Index Non-Clustered Index
A clustered index is a table or view where the data for the The indexes other than PRIMARY indexes (clustered
rows are stored. In a relational database, if the table column indexes) are called non-clustered indexes. It has a
contains a primary key, MySQL automatically creates a structure separate from the data row. The non-clustered
clustered index named PRIMARY. indexes are also known as secondary indexes.
Clustered indexes store the data information and the data Non-clustered indexes stores only the information, and
itself. then it will refer you to the data stored in clustered data.
There can only be one clustered index per table. There can be one or more non-clustered indexes in a
table.
A clustered index determines how data is stored physically It creates a logical ordering of data rows and uses pointers
in the table. Therefore, reading from a clustered index is for accessing the physical data files. Therefore, reading
faster. from a clustered index is slower.
A clustered index always contains an index id of 0. A non-clustered index always contains an index id>0.
35) Which are joins in SQL? Name the most commonly used SQL joins?
SQL joins are used to retrieve data from multiple tables into a meaningful result set. It is performed whenever you need to
fetch records from two or more tables. They are used with SELECT statement and join conditions.
o INNER JOIN
o LEFT OUTER JOIN
o RIGHT OUTER JOIN
o INNER JOIN
o SELF JOIN
o LEFT OUTER JOIN
o RIGHT OUTER JOIN
o FULL OUTER JOIN
o CROSS JOIN
37) What is INNER JOIN in SQL?
Inner join returns only those records from the tables that match the specified condition and hides other rows and columns.
In simple words, it fetches rows when there is at least one match of rows between the tables is found. INNER JOIN keyword
joins the matching records from two tables. It is assumed as a default join, so it is optional to use the INNER keyword with
the query.
1. SELECT column_lists
2. FROM table1
3. INNER JOIN table2 ON join_condition1
4. INNER JOIN table3 ON join_condition2
5. ...;
1. SELECT colum_lists
2. FROM table1
3. RIGHT JOIN table2
4. ON join_condition;
1. SELECT colum_lists
2. FROM table1
3. LEFT JOIN table2
4. ON join_condition;
40) What is Full Join in SQL?
The Full Join results from a combination of both left and right join that contains all the records from both tables. It fetches
rows when there are matching rows in any one of the tables. This means it returns all the rows from the left-hand side table
and all the rows from the right-hand side tables. If a match is not found, it puts NULL value. It is also known as FULL OUTER
JOIN.
SQL triggers have two main components one is action, and another is an event. When certain actions are taken, an event
occurs as a result of those actions.
We use the CREATE TRIGGER statement for creating a trigger in SQL. Here is the syntax:
1. SELECT column_lists
2. FROM table1 AS T1, table1 AS T2
3. WHERE join_conditions;
Example
If we want to get retrieve the student_id and name from the table where student_id is equal, and course_id is not equal, it
can be done by using the self-join:
B. UNION ALL: This operator is similar to the Union operator, but it does not remove the duplicate rows from the output of
the SELECT statements. The following syntax illustrates the UNION ALL operator:
C. INTERSECT: This operator returns the common records from two or more SELECT statements. It always retrieves unique
records and arranges them in ascending order by default. Here, the number of columns and data types should be the same.
The following syntax illustrates the INTERSECT operator:
D. MINUS: This operator returns the records from the first query, which is not found in the second query. It does not return
duplicate values. The following syntax illustrates the MINUS operator:
This operator is used to selects the range of data It is a logical operator to determine whether or not a specific value
between two values. The values can be numbers, exists within a set of values. This operator reduces the use of
text, and dates as well. multiple OR conditions with the query.
It returns records whose column value lies in It compares the specified column's value and returns the records
between the defined range. when the match exists in the set of values.
The following syntax illustrates this operator: The following syntax illustrates this operator:
SELECT * FROM table_name SELECT * FROM table_name
WHERE column_name BETWEEN 'value1' AND WHERE column_name IN ('value1','value 2');
'value2';
Column Level Constraints: These constraints are only applied to a single column and limit the type of data that can be stored
in that column.
Table Level Constraints: These constraints are applied to the entire table and limit the type of data that can be entered.
46) How to write an SQL query to find students' names start with 'A'?
We can write the following query to get the student details whose name starts with A:
Here is the demo example where we have a table named student that contains two names starting with the 'A' character.
47) Write the SQL query to get the third maximum salary of an employee from a table
named employees.
The following query is the simplest way to get the third maximum salary of an employee:
Here is the demo example that shows how to get the third maximum salary of an employee.
The following are the alternative way to get the third-highest salary of an employee:
1. SELECT salary
2. FROM
3. (SELECT salary
4. FROM employees
5. ORDER BY salary DESC
6. LIMIT 3) AS Temp
7. ORDER BY salary LIMIT 1;
48) What is the difference between DELETE and TRUNCATE statements in SQL?
The main difference between them is that the delete statement deletes data without resetting a table's identity, whereas the
truncate command resets a particular table's identity. The following comparison chart explains it more clearly:
3) We can use the WHERE clause in the DELETE We cannot use the WHERE clause with TRUNCATE.
command.
4) DELETE statement is used to delete a row from a TRUNCATE statement is used to remove all the rows from
table. a table.
5) DELETE is slower because it maintained the log. TRUNCATE statement is faster than DELETE statement as it
deletes entire data at a time without maintaining
transaction logs.
6) You can roll back data after using the DELETE It is not possible to roll back after using the TRUNCATE
statement. statement.
7) DELETE query takes more space. TRUNCATE query occupies less space.
The ACID property is an acronym for Atomicity, Consistency, Isolation, and Durability.
Atomicity: It ensures that all statements or operations within the transaction unit must be executed successfully. If one part
of the transaction fails, the entire transaction fails, and the database state is left unchanged. Its main features are COMMIT,
ROLLBACK, and AUTO-COMMIT.
Consistency: This property ensures that the data must meet all validation rules. In simple words, we can say that the
database changes state only when a transaction will be committed successfully. It also protects data from crashes.
Isolation: This property guarantees that the concurrent property of execution in the transaction unit must be operated
independently. It also ensures that statements are transparent to each other. The main goal of providing isolation is to
control concurrency in a database.
Durability: This property guarantees that once a transaction has been committed, it persists permanently even if the system
crashes, power loss, or failed.
o A NULL value is a value, which is 'unavailable, unassigned, unknown or not applicable.' It would be used in the
absence of any value. We can perform arithmetic operations on it. On the other hand, zero is a number, and a blank
space is treated as a character.
o The NULL value can be treated as an unknown and missing value, but zero and blank spaces differ from the NULL
value.
o We can compare a blank space or a zero to another blank space or a zero. On the other hand, one NULL may not be
the same as another NULL. NULL indicates that no data has been provided or that no data exists.
51) What are functions and their usage in SQL?
SQL functions are simple code snippets that are frequently used and re-used in database systems for data processing and
manipulation. Functions are the measured values. It always performs a specific task. The following rules should be
remembered while creating functions:
o A function should have a name, and the name cannot begin with a special character such as @, $, #, or other similar
characters.
o Functions can only work with the SELECT statements.
o Every time a function is called, it compiles.
o Functions must return value or result.
o Functions are always used with input parameters.
o User-Defined Function: Functions created by a user based on their needs are termed user-defined functions.
o System Defined Function: Functions whose definition is defined by the system are termed system-defined functions.
They are built-in database functions.
LOWER: This function is used to converts a given character into lowercase. The following example will return the 'STEPHEN'
as 'stephen':
UPPER: This function is used to converts a given character into uppercase. The following example will return the 'stephen' as
'STEPHEN':
INITCAP: This function is used to converts given character values to uppercase for the initials of each word. It means every
first letter of the word is converted into uppercase, and the rest is in lower case. The following example will return the 'hello
stephen' as 'Hello Stephen':
A) CONCAT: This function is used to join two or more values together. It always appends the second string into the end of
the first string. For example:
Output: Information-technology
B) SUBSTR: It is used to return the portion of the string from a specified start point to an endpoint. For example:
Output: Management
C) LENGTH: This function returns the string's length in numerical value, including the blank spaces. For example:
Output: 16
D) INSTR: This function finds the exact numeric position of a specified character or word in a given string. For example:
Output: 7
E) LPAD: It returns the padding of the left-side character value for right-justified value. For example:
Input: SELECT LPAD ('200', 6,'*');
Output: ***200
F) RPAD: It returns the padding of the right-side character value for left-justified value. For example:
Output: 200***
G) TRIM: This function is used to remove all the defined characters from the beginning, end, or both. It also trimmed extra
spaces. For example:
Output: BCDCB
H) REPLACE: This function is used to replace all occurrences of a word or portion of the string (substring) with the other
specified string value. For example:
Input: SELECT REPLACE ( 'It is the best coffee at the famous coffee shop.', 'coffee', 'tea');
56) What are the syntax and use of the COALESCE function?
The COALESCE() function evaluates the arguments in sequence and returns the first NON-NULL value in a specified number
of expressions. If it evaluates arguments as NULL or not found any NON-NULL value, it returns the NULL result.
Example:
Suppose we have a table 'customer' containing eight records in which the name column has some duplicate values.
If we want to get the name column without any duplicate values, the DISTINCT keyword is required. Executing the below
command will return a name column with unique values.
58) What is the default ordering of data using the ORDER BY clause? How could it be
changed?
The ORDER BY clause is used to sort the table data either in ascending or descending order. By default, it will sort the table
in ascending order. If we want to change its default behavior, we need to use the DESC keyword after the column name in
the ORDER BY clause.
We have taken a customer table in the previous example. Now, we will demonstrate the ORDER BY clause on them as well.
In the below output, we can see that the first query will sort the table data in ascending order based on the name column.
However, if we run the second query by specifying the DESC keyword, the table's order is changed in descending order.
59) Is the following query returns the output?
1. SELECT subject_code, AVG (marks)
2. FROM Students
3. WHERE AVG(marks) > 70
4. GROUP BY subject_code;
Answer: No. The above query does not return the output because we cannot use the WHERE clause to restrict the groups.
We need to use the HAVING clause instead of the WHERE clause to get the correct output.
60) What is the difference between the WHERE and HAVING clauses?
The main difference is that the WHERE clause is used to filter records before any groupings are established, whereas the
HAVING clause is used to filter values from a group. The below comparison chart explains the most common differences:
WHERE HAVING
This clause is implemented in row operations. This clause is implemented in column operations.
It does not allow to work with aggregate functions. It can work with aggregate functions.
This clause can be used with the SELECT, UPDATE, and DELETE This clause can only be used with the SELECT
statements. statement.
SQL provides seven (7) aggregate functions, which are given below:
o AVG(): This function is used to returns the average value from specified columns.
o COUNT(): This function is used to returns the number of table rows, including rows with null values.
o MAX(): This function is used to returns the largest value among the group.
o MIN(): This function is used to returns the smallest value among the group.
o SUM(): This function is used to returns the total summed values(non-null) of the specified column.
o FIRST(): This function is used to returns the first value of an expression.
o LAST(): This function is used to returns the last value of an expression