0% found this document useful (0 votes)
498 views521 pages

Oracle Quest

This document provides an overview of 50 common Oracle interview questions. It is intended to help job candidates prepare for an Oracle interview by reviewing frequently asked questions about SQL, PL/SQL, Oracle database concepts, and more. The document lists questions in three categories - Oracle Basic Interview Questions, SQL Interview Questions, and PL/SQL Interview Questions - and provides answers for 10 sample questions from the Oracle Basic Interview Questions section to help explain common Oracle concepts and terminology.

Uploaded by

prasanna ghare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
498 views521 pages

Oracle Quest

This document provides an overview of 50 common Oracle interview questions. It is intended to help job candidates prepare for an Oracle interview by reviewing frequently asked questions about SQL, PL/SQL, Oracle database concepts, and more. The document lists questions in three categories - Oracle Basic Interview Questions, SQL Interview Questions, and PL/SQL Interview Questions - and provides answers for 10 sample questions from the Oracle Basic Interview Questions section to help explain common Oracle concepts and terminology.

Uploaded by

prasanna ghare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 521

Top 50 Oracle Interview Questions

You Should Master

Knowledge of SQL is a must because the demand for SQL-expertise is high and is


valued in the market. Oracle is a very popular secured database that is widely
used across multinational companies. So, this article on Oracle interview
questions will cover the most frequently asked interview questions and help you
to brush up your knowledge before the interview.

If you are a fresher or an experienced, this is the right platform for you which will
help you to start your preparation.

Let’s begin by taking a look at the most frequently asked questions.

 Oracle Basic Interview Questions


 SQL Interview Questions
 PL/SQL Interview Questions

So, let’s begin!

Oracle Basic Interview Questions


Q1. How will you differentiate between varchar & varchar2
Q2. What are the components of logical database structure in Oracle database?
Q3. Describe an Oracle table
Q4. Explain the relationship among database, tablespace and data file?
Q5. What are the various Oracle database objects?
Q6. Explain about the ANALYZE command in Oracle?
Q7. What types of joins are used in writing subqueries?
Q8. RAW datatype in Oracle
Q9. What is the use of Aggregate functions in Oracle?
Q10. Explain Temporal data types in Oracle

Q1. How will you differentiate between Varchar & Varchar2?

Both Varchar & Varchar2 are the Oracle data types which are used to store
character strings of variable length. To point out the major differences between
these,

Q2. What are the components of logical database structure in Oracle database?

The components of the logical database structure in Oracle database are:

 Tablespaces: A database mainly contains the Logical Storage Unit


called tablespaces. This tablespace is a set of related logical
structures. To be precise, tablespace groups are related to logical
structures together.
 Database schema objects: A schema is a collection of database
objects owned by a specific user. The objects include tables,
indexes, views, stored procedures, etc. And in Oracle, the user is the
account and the schema is the object. It is also possible in the
database platforms to have a schema without a user-specified.
Q3. Describe an Oracle table

A table is a basic unit of data storage in the Oracle database. A table basically
contains all the accessible information of a user in rows and columns.

To create a new table in the database, use the “CREATE TABLE” statement.
First, you have to name that table and define its columns and datatype for each
column.

CREATE TABLE table_name


(
column1 datatype [ NULL | NOT NULL ],
column2 datatype [ NULL | NOT NULL ],

column_n datatype [ NULL | NOT NULL ]
);

Here,

 table_name: This specifies the name of the table that you want to


create.
 column..n: It specifies the number of columns which you want to
add in the table. Here, every column must have a datatype and
should either be defined as “NULL” or “NOT NULL”. If in case, the
value is left blank, it is treated as “NULL” as default.

Q4. Explain the relationship among database, tablespace and data file?

An Oracle database possesses one or more logical storage units


called tablespaces. Each tablespace in Oracle database consists of one or more
files called the datafiles. These tablespaces collectively store the entire data of
databases. Talking about the data files, these are the physical structure that
confirms with the operating system as to which Oracle program is running.

Q5. What are the various Oracle database objects?

These are the Oracle Database Objects:

 Tables: This is a set of elements organized in a vertical and


horizontal manner.
 Tablespaces: It is a logical storage unit in Oracle.
 Views: Views are a virtual table derived from one or more tables.
 Indexes: This is a performance tuning method to process the
records.
 Synonyms: It is a name for tables.

Q6. Explain about the ANALYZE command in Oracle?

This “Analyze” command is used to perform various functions on index, table, or


cluster. The following list specifies the usage of ANALYZE command in Oracle:

 Analyze command is used to identify migrated and chained rows of


the table or a cluster.
 It is used to validate the structure of an object.
 This helps in collecting the statistics about the object used by the
user and are then stored on to the data dictionary.
 It also helps in deleting statistics that are used by an object from
the data dictionary.

Q7. What types of joins are used in writing subqueries?

A Join is used to compare and combine, this means literally join and return
specific rows of data from two or more tables in a database.
There are three types of joins in SQL that are used to write the subqueries.

 Self Join: This is a join in which a table is joined with itself,


especially when the table has a foreign key which references its own
primary key.
 Outer Join: An outer join helps to find and returns matching data
and some dissimilar data from tables.
 Equi-join: An equijoin is a join with a join condition containing
an equality operator. An equijoin returns only the rows that have
equivalent values for the specified columns.

Q8. RAW datatype in Oracle

The RAW datatype in Oracle is used to store variable-length binary data or byte
string values. The maximum size for a raw in a given table in 32767 bytes.

You might get confused as to when to use RAW, varchar, and varchar2. Let me
point out the major differences between them. PL/SQL does not recognize the
data type and hence, it cannot have any conversions when RAW data is
transferred to different systems. This data type can only be queried or can be
inserted in a table.

Q9. What is the use of Aggregate functions in Oracle?

An aggregate function in Oracle is a function where values of multiple rows or


records are joined together to get a single value output. It performs the summary
operations on a set of values in order to provide a single value. There are several
aggregate functions that you can use in your code to perform calculations.
Some common Aggregate functions are:

 Average
 Count
 Sum

Q10. Explain Temporal data types in Oracle

Oracle mainly provides these following temporal data types:

 Date Data Type: Different formats of Dates.


 TimeStamp Data Type: Has different formats of Time Stamp.
 Interval Data Type: Interval between dates and time.

Q11. What is a View?

A view is a logical table based on one or more tables or views. A View is also
referred to as a user-defined database object that is used to store the results of a
SQL query, that can be referenced later in the course of time. Views do not store
the data physically but as a virtual table, hence it can be referred to as a logical
table. The corresponding tables upon which the views are signified are
called Base Tables and this doesn’t contain data.

Q12. How to store pictures on to the database?

It is possible to store pictures on to the database by using Long Raw Data type.
This data type is used to store binary data of length 2GB. Although, the table can
have only on Long Raw data type.

Q13. Where do you use DECODE and CASE Statements?

Both these statements Decode and Case will work similar to the if-then-else
statement and also they are the alternatives for each of them. These functions are
used in Oracle for data value transformation.

Example:
Decode function

Select OrderNum,
DECODE (Status,’O’, ‘Ordered’,’P’, ‘Packed,’ S’,’ Shipped’, ’A’,’Arrived’)
FROM Orders;

Case function

Select OrderNum
, Case(When Status=’O’ then ‘Ordered’
When Status =’P’ then Packed
When Status=’ S’ then ’Shipped’
else ’Arrived’) end
FROM Orders;

Both these commands will display Order Numbers with their respective Statuses
like this,

Status O= Ordered
Status P= Packed
Status S= Shipped
Status A= Arrived

Q14. What do you mean by Merge in Oracle and how can you merge two
tables?

Merge statement is used to merge the data from two tables subsequently. It


selects the data from the source table and then inserts/updates it in the other
table based on the condition provided in the query. It is also useful in data
warehousing applications.
Q15. What is the data type of DUAL table?

The Dual table is basically a one-column table that is present in the Oracle
database. This table has a single Varchar2(1) column called Dummy which has a
value of ‘X’.

SQL Interview Questions

Q16. Explain about integrity constraint?

An integrity constraint is actually a declaration that is defined as a business rule


for a table column. They are used to ensure accuracy and consistency of data in
the database. It can also be called as a declarative way to define a business rule
for a table’s column. There are a few types, namely:

 Domain Integrity
 Referential Integrity
 Domain Integrity

Q17. What is SQL and also describe types of SQL statements?

SQL stands for Structured Query Language. SQL is used to communicate with


the server in order to access, manipulate and control data. There are 5 different
types of SQL statements available. They are:

1. Select: Data Retrieval
2. Insert, Update, Delete, Merge: Data Manipulation Language
(DML)
3. Create, Alter, Drop, Rename, Truncate: Data Definition Language
(DDL)
4. Commit, Rollback, Savepoint: Transaction Control Statements
5. Grant, Revoke: Data Control Language (DCL)
Q18. Briefly explain what is Literal? Give an example where it can be used?

A Literal is a string that contains a character, a number, or a date that is included


in the Select list and which is not a column name or a column alias.

Also note that Date and character literals must be enclosed within single quotes
(‘ ‘), whereas you don’t have to do that for the number literals.

For example: Select last_name||’is a’||job_id As “emp details” from employee;

In this case, “is a” is literal.

Q19. How to display row numbers with the records?

In order to display row numbers along with their records numbers you can do
this:

Select rownum <fieldnames> from table;

The above query will display the row numbers and the field values from the given
table. This query will display row numbers and the field values from the given
table.

Q20. What is the difference between SQL and iSQL*Plus?


Q21. What are SQL functions? Describe in brief different types of SQL
functions?

SQL Functions are a very powerful feature of SQL. These functions can take
arguments but always return some value. There are two distinct types of SQL
functions available. They are:

 Single-Row functions: These functions operate on a single row


to give one result per row.

Types of Single-Row functions are:

1. Character
2. Number
3. Date
4. Conversion
5. General

 Multiple-Row functions: These functions operate on groups of


rows to give one result per group of rows.

Types of Multiple-Row functions:

1. avg
2. count
3. max
4. min
5. sum
6. stddev
7. variance
Q22. Describe different types of General Function used in SQL?

General functions are of following types:

1. NVL: Converts a null value to an actual value. NVL (exp1, exp2) .If


exp1 is null then NVL function return value of exp2.
2. NVL2: If exp1 is not null, nvl2 returns exp2, if exp1 is null, nvl2
returns exp3. The argument exp1 can have any data type. NVL2
(exp1, exp2, exp3)
3. NULLIF: Compares two expressions and returns null if they are
equal or the first expression if they are not equal. NULLIF (exp1,
exp2)
4. COALESCE: Returns the first non-null expression in the
expression list. COALESCE (exp1, exp2… expn). The advantage of
the COALESCE function over NVL function is that the COALESCE
function can take multiple alternative values.
5. Conditional Expressions: Provide the use of IF-THEN-ELSE
logic within a SQL statement. Example: CASE Expression and
DECODE Function.

Q23. What is a Sub Query? Describe its Types?

A subquery is a SELECT statement that is embedded in a clause of


another SELECT statement. A subquery can be placed in where
having and from clause.

Guidelines for using subqueries:

1. You should enclose the sub-queries within parenthesis.


2. Place these subqueries on the right side of the comparison
condition.
3. Use Single-row operators with single-row subqueries.
4. Use Multiple-row operators with multiple-row subqueries.

Types of subqueries:

1. Single-Row Subquery: Queries that return only one row from


the inner select statement. Single-row comparison operators are: =,
>, >=, <, <=, <>
2. Multiple-Row Subquery: These are queries that return more
than one row from the inner Select statement. You will also find
multiple-column subqueries that return more than one column
from the inner select statement. Operators include: IN, ANY, ALL.

Q24. What is the use of Double Ampersand (&&) in SQL Queries? Give an
example

You can use && if you want to reuse the variable value without prompting the
user each time.

For example: Select empno, ename, &&column_name from employee order by


&column_name;

Q25. Describe VArray

VArray is basically an Oracle data type used to have columns containing


multivalued attributes and it can hold a bounded array of values. All Varrays
consist of contiguous memory locations. The lowest address corresponds to the
first element and the highest address to the last element.
Each element in a Varray has an index associated with it. It has a maximum size
(max_size) that can be changed dynamically.

Q26. What are the attributes of the Cursor?

Each Cursor in Oracle has a set of attributes that enables an application program
to test the state of the Cursor. The attributes can be used to check whether the
cursor is opened or closed, found or not found and also find row count.

Q27. Name the various constraints used in Oracle

These are the following constraints used:

 NULL: It is to indicate that a particular column can contain NULL


values.
 NOT NULL: It is to indicate that a particular column cannot
contain NULL values.
 CHECK: Validate that values in the given column to meet the
specific criteria.
 DEFAULT: It is to indicate the value is assigned to a default value.

Q28. What is the fastest query method to fetch data from the table?

The fastest query method to fetch data from the table is by using the Row ID. A
row can be fetched from a table by using RowID.

Q29. Difference between Cartesian Join and Cross Join?

There are no such differences between these Joins. Cartesian and Cross join are
the same.

Cross join gives a cartesian product of two tables i.e., the rows from the first table
is multiplied with another table that is called cartesian product.
Cross join without the where clause gives a Cartesian product.

Q30. How does the ON-DELETE-CASCADE statement work?

Using this On Delete Cascade you can automatically delete a record in the child
table when the same record is deleted from the parent table. This statement can
be used with Foreign Keys as well.

You can add this On Delete Cascade option on an existing table.

Syntax:

Alter Table Child_T1 ADD Constraint Child_Parent_FK References


Parent_T1(Column1) ON DELETE CASCADE;

Now let’s move on to the next part of this Oracle Interview Questions article.

Oracle PL/SQL Interview Questions

Q31. What is PL SQL?

PL/SQL is an extension of Structured Query Language (SQL) that is used in


Oracle. It combines the data manipulation power of SQL with the processing
power of procedural language in order to create super-powerful SQL queries. PL
SQL means instructing the compiler what to do through SQL and how to do
it through its procedural way.

Q32. Enlist the characteristics of PL/SQL?

There are a lot of characteristics of PL/SQL. Some notable ones among them are:

 PL/SQL is a block-structured language.


 It is portable to all environments that support Oracle.
 PL/SQL is integrated with the Oracle data dictionary.
 Stored procedures help better sharing of application.

Q33. What are the data types available in PL/SQL?

There are two data types available in PL/SQL. They are namely:

 Scalar data types

Example: Char, Varchar, Boolean, etc.

 Composite datatypes

Example: Record, table etc.

Q34. What are the uses of a database trigger

Triggers are the programs which are automatically executed when some events
occur:

 Implement complex security authorizations.


 Drive column values.
 Maintain duplicate tables.
 Implement complex business rules.
 Bring transparency in log events.

Q35. Show how functions and procedures are called in a PL SQL block

A Procedure can have a return statement to return the control to the calling


block, but, it cannot return any values through the return statement. They cannot
be called directly from Select statements but they can be called from another
block or through EXEC keyword.

The procedure can be called in the following ways:


a) CALL <procedure name> direc
b) EXECUTE <procedure name> from calling environment
c) <Procedure name> from other procedures or functions or packages

Functions can be called in the following ways


a) Execute<Function name> from calling environment. Always use a variable to
get the return value.
b) As part of an SQL/PL SQL Expression

Q36. What are the two virtual tables available at the time of database trigger
execution?

Columns are referred as Then.column_name and Now.column_name.

 INSERT related triggers, Now.column_name values are available


only.
 DELETE related triggers, Then.column_name values are available
only.
 UPDATE related triggers, both Table columns are available.

Q37. What are the differences between Primary Key and Unique Key?
Q38. Explain the purpose of %TYPE and %ROWTYPE data types with the
example?

%ROWTYPE and %TYPE are the attributes in PL/SQL which can inherit the
datatypes of a table that are defined in a database. The main purpose of using
these attributes in Oracle is to provide data independence and integrity. Also
note that, if any of the datatypes gets changed in the database, PL/SQL code gets
updated automatically including the change in the datatypes.

%TYPE: This is used for declaring a variable that needs to have the same data
type as of a table column.
%ROWTYPE: This is used to define a complete row of record having a
structure similar to the structure of a table.

Q39. Explain the difference between Triggers and Constraints?

Triggers are very different from Constraints in the following ways:

Q40. Exception handling in PL/SQL

When an error occurs in PL/SQL, the corresponding exception is raised. This


also means, to handle undesired situations where PL/SQL scripts gets
terminated unexpectedly, error-handling code is included in the program. In
PL/SQL, all exception handling code is placed in the Exception section.

There are 3 types of Exceptions:


 Predefined Exceptions: Common errors with predefined
names.
 Undefined Exceptions: Less common errors with no predefined
names.
 User-defined Exceptions: Do not cause runtime error but
violate business rules.

Comparison based Interview Questions

Q41. What is the difference between COUNT (*), COUNT (expression), COUNT
(distinct expression)?

COUNT (*): This returns a number of rows in a table including the duplicates


rows and the rows containing null values in the columns.
COUNT (EXP): This returns the number of non-null values in the column
identified by an expression.
COUNT (DISTINCT EXP): It returns the number of unique, non-null values
in the column identified by an expression.

Q42. Difference between the “VERIFY” and “FEEDBACK” command?

The major differences between Verify and Feedback commands are:

 Verify Command: You can use this command to confirm the


changes in the SQL statement which can have old and new values
that are defined with Set Verify On/OFF.
 Feedback Command: It displays the number of records that are
returned by a query.

Q43. List out the difference between Commit, Rollback, and Savepoint?

The major differences between these are listed below:


 Commit: This ends the current transaction by ensuring that all
pending data changes are made permanent.
 Rollback: This ends the current transaction by discarding or
deleting all pending data changes.
 Savepoint: It divides a transaction into smaller parts. You can
rollback the transaction until you find a particular named
savepoint.

Q44. What is the difference between SUBSTR and INSTR?

SUBSTR returns a specific portion of a string whereas INSTR provides the


character position in which a pattern is found in a string. SUBSTR returns string
whereas INSTR returns numeric values.

Q45. Point out the difference between USER TABLES and DATA DICTIONARY?

User Tables: This is a collection of tables created and maintained by the user.


It also contains user information.
Data dictionary: This is a collection of tables that are created and maintained
by the Oracle Server. It contains database information. All data dictionary tables
are owned by the SYS user.

Q46. Major difference between Truncate and Delete?


Q47. Point the difference between TRANSLATE and REPLACE?

Translate is used for character by character substitution whereas Replace is used


to substitute a single character with a word.

Q48. What is the difference between $ORACLE_BASE and $ORACLE_HOME?

$Oracle_base is the main or root directory of Oracle whereas Oracle_Home is


located beneath the base folder in which all Oracle products reside.

Q49. What do you understand by Redo Log file mirroring?

Mirroring is a process of having a copy of Redo log files. This is done by creating
a group of log files altogether. It ensures that the LGWR automatically writes it
to all the members of the current on-line redo log group. If the group fails, the
database automatically switches over to the next group and it diminishes the
performance of the database.

Q50. What is the difference between a hot backup and a cold backup in Oracle?
Explain about their benefits as well

Hot backup (Online Backup): A hot backup is also known as an online


backup because it is done while the database is active. Some sites can’t shut
down their database while making a backup copy and they are used 24*7.

Cold backup (Offline Backup): A cold backup is also known as an offline


backup because it is done while the database has been shut down using the
SHUTDOWN command. If the database is suddenly shutdown with an uncertain
condition, it should be restarted with RESTRICT mode and then shutdown with
the NORMAL option. For a complete cold backup, the corresponding files must
be backed up i.e., all data files, All control files, All online redo log files and the
init.ora file (you can recreate it manually).
I hope this set of Oracle Interview Questions will help you in preparing for your
interviews. All the best!

Now with this, we come to an end of this comparison on SQL vs NoSQL. I hope
you guys enjoyed this article and understood all the differences. If you wish to
check out more articles on the market’s most trending technologies like Artificial
Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official
site.

Do look out for other articles in this series which will explain the various other
aspects of SQL.

Top SQL Interview Questions - Beginners


1. What do you understand by database, and what does it have?

A database can be defined as the structured form of data storage from which data can be retrieved
and managed based on requirements. Basically, a database consists of tables where data is stored
in an organized manner. Each table consists of rows and columns to store data. Data can be stored,
modified, updated, and accessed easily in a database. For instance, a bank management database
or school management database are a few examples of databases.

2. What are DBMS and RDBMS?

DBMS – Database Management System.

DBMS is the software that allows storing, modifying, and retrieving data from a database. And it is a
group of programs that act as the interface between data and applications. DBMS supports
receiving queries from applications and retrieving data from the database.

RDBMS – Relational Database Management System

Like DBMS, RDBMS is also the software that allows storing, modifying, and retrieving data from a
database but a RELATIONAL database. In a relational database, the data in the tables have a
relationship. Besides, RDBMS is useful when data in tables are being managed securely and
consistently.
3. What are Query and Query language?

A query is nothing but a request sent to a database to retrieve data or information. The required
data can be retrieved from a table or many tables in the database.

Query languages use various types of queries to retrieve data from databases. SQL, Datalog, and
AQL are a few examples of query languages; however, SQL is known to be the widely used query
language. SQL returns data as columns and rows in a table, whereas other languages return data in
other forms, like graphs, charts, etc.

4. What do you mean by subquery?

It is a query that exists inside the statements such as SELECT, INSERT, UPDATE, and DELETE. It may
exist inside a subquery too. A subquery is also known as an inner query or inner select. The
statement with a subquery is an outer query or outer select.

Let’s see the example shown below in which the maximum unit price is the result that will be
returned by the subquery using the SELECT statement. Also, orders is the value that will be
returned by the outer query using the SELECT statement.

5. What is SQL, and mention its uses?

SQL – Structured Query Language

SQL is known as the query programming language. It uses SQL queries to store, modify and
retrieve data into and from databases. Briefly, SQL inserts, updates, and deletes data in databases;
creates new databases and new tables; creates views and stored procedures; and sets permissions
on the database objects.

6. What is Dynamic SQL, and when can you use it?

Dynamic SQL is the programming method that allows building SQL statements during runtime. You
can use dynamic SQL when you do not know the full text of the SQL statements used in the
program until runtime. Moreover, dynamic SQL can execute SQL statements that are not supported
by static SQL programs. So, Dynamic SQL helps to build more flexible applications.

7. What do you understand by tables and fields in a database?

Tables are the database objects where data is stored logically. Like a spreadsheet, data is stored in
the form of rows and columns in a database table. A row in a table represents a record, and
columns represent the different fields. Fields have the data types such as text, dates, numbers, and
links.

For example, consider the below customer database in which rows consist of the company names
and columns consist of the various details of customers like first name, last name, age, location,
etc. Here, number 1 indicates a record, number 2 indicates a field, and number 3 indicates the field
value. 

8. What are the different types of tables used in SQL?

The following are the table types used in SQL:

 Partitioned tables
 Temporary tables
 System tables
 Wide tables

9. What are temporary tables?

Temporary tables only store data during the current session, and they will be dropped once the
session is over. With temporary tables, you can create, read, update and delete records like
permanent tables. Know that there are two types of temporary tables: local and global temporary
tables.

Local temporary tables are only visible to the user who created them, and they are deleted the
moment the user disconnects from the instance of the SQL server.

On the contrary, global temporary tables are visible to all users, and they are deleted only when all
the users who reference the tables get disconnected.

10. What do you mean by Primary Key and Foreign Key in SQL?

Primary Key: A primary is a field or combination of many fields that help identify records in a
table. Note that there can be only one primary key for a table. The table that has the primary key is
known as the parent table.

Foreign Key: A foreign key is the field or combination of fields of a table that links the primary key
of another table. A foreign key is used to create a connection between two tables. Unlike a primary
key, a table can have one or many foreign keys. The table that has a foreign key is known as the
child table.

For example, customer ID (1) is the primary key of the Customers table, and customer ID (2) in the
orders table is identified as the foreign key to the customer's table. 

11. What are Superkey and candidate key?

A super key may be a single or a combination of keys that help to identify a record in a table. Know
that Super keys can have one or more attributes, even though all the attributes are not necessary
to identify the records.
A candidate key is the subset of Superkey, which can have one or more than one attributes to
identify records in a table. Unlike Superkey, all the attributes of the candidate key must be helpful
to identify the records.

Note that all the candidate keys can be Super keys, but all the super keys cannot be candidate keys.

12. What are composite keys?

A composite key is the combination of two or more columns in a table used to identify a row in a
table. Know that a combination of columns is essential in creating composite keys because a single
column in a composite key cannot identify a row in a table. We can say that the composite key is
the primary key with a few more attributes or columns. Also, a composite key can be a combination
of candidate keys.

13. What is JOIN operation in SQL, and mention their types?

JOIN is the logical operation used to retrieve data from two or more tables. It can be applied only
when there is a logical relationship between two tables. Moreover, the JOIN operator uses the data
of one table to retrieve data from another table.

Following are the different types of logical operations:

 INNER JOIN
 LEFT (OUTER) JOIN
 RIGHT (OUTER) JOIN
 FULL (OUTER) JOIN
 CROSS JOIN

14. What do you mean by Self Join?

In self-join operation, a table is joined with itself to retrieve the desired data. Every join operation
needs two tables as a basic rule. Therefore, in self-join, a table is joined with an instance of the
same table. By doing this, values of the two table columns are compared with each other, and the
desired data is retrieved as the result set.

[Related Article: Tech Mahindra Interview Questions]

15. What do you mean by Cross Join?

Cross Join is basically the Cartesian product type in which each row in a table is paired with all the
rows of another table. So, the result set will be the paired combinations of the rows of two tables.
Generally, cross join is not preferred by developers as it increases complexity in programs when
there are many rows in tables. But, it can be used in queries if you identify normal join operation
won’t be effective for your query.
16. What are the SQL constraints?

 SQL constraints specify conditions for a column or table to manage the data stored in tables
effectively.

 The following are the commonly used SQL constraints.

 NOT NULL - This condition ensures columns won’t accept a NULL value.
 UNIQUE - It ensures that all the values in a column must be unique.
 CHECK - It ensures that all the column fields obey a specific condition.
 DEFAULT - It provides a default value for the fields of a column unless no value is specified
for the fields
 CREATE INDEX - It ensures creating an index for tables so that retrieving data from the tables
becomes easier
 PRIMARY KEY - It must identify every row of a table
 FOREIGN KEY -  It must link tables based on common attributes

17. What are local and global variables?

Local variables are declared inside a function so that only that function can call them. They only
exist until the execution of that specific function. Generally, local variables are stored in stack
memory and cleaned up automatically.

Global variables are declared outside of a function. They are available until the execution of the
entire program. Unlike local variables, global variables are stored in fixed memory and not cleaned
up automatically.

18. What is an index in SQL, and mention its types?

An index is used to retrieve data from a database quickly. Generally, indexes have keys taken from
the columns of tables and views. We can say, SQL indexes are similar to the indexes in books that
help to identify pages in the books quickly.

There are two types of indexes:

 Clustered indexes
 Non-clustered indexes

19. Mention the different types of SQL commands or SQL subsets?

There are five types of SQL commands offered in SQL. They are given as follows;

 DDL - Data Definition Languages


 DML - Data Manipulation Languages
 DCL - Data Control Language
 TCL - Transaction Control Language
 DQL - Data Query Language

20. What are the Various Commands used in SQL Subsets?


DDL CREATE, DROP, ALTER, TRUNCATE, ADD COLUMN, and DROP COLUMN

DML INSERT, DELETE, and UPDATE

DCL GRANT and REVOKE

TCL COMMIT, ROLLBACK, SAVEPOINT, and SET TRANSACTION

DQL SELECT

21. Can you brief me on a few DDL Commands?


ALTER This command allows changing the structure of a table

CREATE It allows the creation of database objects such as tables, views, and indexes.

DROP This command allows removing database objects from a database

TRUNCATE This command helps to delete all the rows of a table permanently.

22. Can you brief the DML Commands?


INSERT This command allows inserting a data into a table of a database

DELETE This command allows deleting specific rows from a table

UPDATE This command allows modifying a data in a table

23. Can you brief me on a few DCL Commands?


GRANT This command can be used to share a database with other users. All the
database objects can be granted access with certain rights to users.

This command can be applied if you want to restrict the access of database
REVOKE
objects by other users.

24. Can you brief me about TCL commands?


COMMIT This command allows for saving the transactions made in a database.

This command helps undo the transactions made in a database with the
ROLLBACK
condition that the transactions shouldn't be saved yet.

This command helps to roll the transactions up to a certain point but not the
SAVEPOINT
entire transaction.

25. What are Stored Procedures?

It is a function that consists of a group of statements that can be stored and executed whenever it
is required. Know that stored procedures are compiled only once. They are stored as ‘Named
Object’ in the SQL server database. Stored procedures can be called at any time during program
execution. Moreover, a stored procedure can be called another stored procedure.

Explore Oracle PL SQL Interview Questions 

26. What are the SQL database functions?

SQL offers the flexibility to developers to use built-in functions as well as user-defined functions.

The functions are categorized as follows:

 Aggregate functions: They process a group of values and return a single value. They can
combine with GROUP BY, OVER, HAVING clauses and return values. They are deterministic
functions.
 Analytic functions: They are similar to aggregate functions but return multiple rows as
result set after processing a group of values. They help calculate moving averages, running
totals, Top-N results, percentages, etc.
 Ranking functions: They return ranking values for rows in a table based on the given
conditions. Here, the results are non-deterministic.
 Rowset functions: They return an object used as the table reference.
 Scalar functions: They operate on a single value and return a single value.
27. Mention the different types of operators used in SQL?

There are six types of operators used in SQL. They are given as follows:

Arithmetic Operators Addition, Subtraction, Multiplication, Division, and Remainder/Modulus

Bitwise Operators Bitwise AND, Bitwise OR, Bitwise XOR, etc.

Comparison Equal to, Not equal to, Greater than, Not greater than, Less than, Not less than,
Operators Not equal to, etc.

Compound Operators Add equals, Multiply equals, Subtract equals, Divide equals, and Modulo equals

Logical Operators ALL, ANY/SOME, AND, BETWEEN, NOT, EXISTS, OR, IN, LIKE, and ISNULL

String Operators String concatenation, wildcard, character matches, etc.

28. What are the Set Operators?

There are four types of set operators available in SQL. They are given as follows:

Union This operator allows combining result sets of two or more SELECT statements.

This operator allows combining result sets of two or more SELECT statements
Union All
along with duplicates.

This operator returns the common records of the result sets of two or more
Intersect
SELECT statements.

This operator returns the exclusive records of the first table when two tables
Minus
undergo this operation.

29. What do you mean by buffer pool and mention its benefits?

A buffer pool in SQL is also known as a buffer cache. All the resources can store their cached data
pages in a buffer pool. The size of the buffer pool can be defined during the configuration of an
instance of SQL Server. The number of pages that can be stored in a buffer pool depends on its
size.

The following are the benefits of a buffer pool:

  Increase in I/O performance


  Reduction in I/O latency
 Increase in transaction throughput
  Increase in reading performance

30. What are Tuple and tuple functions?

A tuple is a single row in a table that represents a single record of a relation. A tuple contains all the
data that belongs to a record. At the same time, tuple functions allow retrieving tuples from a
database table. They are extensively used in analysis services that have multidimensional
structures.

For example, the highlighted row in the below table shows all the data belonging to a customer,
which is nothing but a tuple.

Customer Name Phone Number Email Address Postal Address

Naren 123 -456 -789 [email protected] PO No:123, New Delhi

Raman 234 -567 -891 [email protected] PO No:143, Mumbai

Krishna 345 -678 -912 [email protected] PO No:443, Hyderabad

31. What do you mean by dependency and mention the different dependencies?

Dependency is the relation between the attributes of a table. The following are the different types
of dependencies in SQL.

 Functional dependency
 Fully-functional dependency
 Multivalued dependency
 Transitive dependency
 Partial dependency

32. What do you mean by Data Integrity?

Data integrity ensures the accuracy and consistency of data stored in a database. Data integrity, in
a way, represents the data quality. So, the data characteristics defined for a column should be
satisfied while storing data in the columns. For instance, if a column in a table is supposed to store
numeric values, then it should not accept Alphabetic values; otherwise, you can mean that data
integrity is lost in the table.
33. What is Database Cardinality?

Database Cardinality denotes the uniqueness of values in the tables. It supports optimizing query
plans and hence improves query performance. There are three types of database cardinalities in
SQL, as given below:

 Higher Cardinality
 Normal Cardinality
  Lower Cardinality

34. What are database Normalisation and various forms of Normalisation?

It is the process that reduces data redundancy and improves data integrity by restructuring the
relational database.

The following are the different forms of normalization:

 First normal form – 1NF


 Second normal form – 2 NF
 Third normal form – 3 NF
 Boyce Codd Normal Form/Fourth Normal form – BCNF/4NF

35. What is Cursor, and how to use it?

In general, the result set of a SQL statement is a set of rows. If we need to manipulate the result set,
we can act on a single row of the result set at a time. Cursors are the extensions to the result set
and help point a row in the result set. Here, the pointed row is known as the current row.

Cursors can be used in the following ways:

 Positions a row in a result set


 Supports retrieving the current row from the result set
 Supports data modifications in the current row
 Allowing  SQL statements in stored procedures, scripts, and triggers to access the result set

36. Mention the different types of Cursors?

 Forward Only: It is known as the firehose cursor that can make only a forward movement.
The modification made by the current user and other users is visible while using this cursor.
As it is the forward-moving cursor, it fetches rows of the result set from the start to end
serially.
 Static: This cursor can move forward and backward on the result set. Here, only the same
result set is visible throughout the lifetime of the cursor. In other words, once the cursor is
open, it doesn’t show any changes made in the database that is the source for the result set.
 Keyset: This cursor is managed by a set of identifiers known as keys or keysets. Here, the
keysets are built by the columns that derive the rows of a result set. When we use this
cursor, we can’t view the records created by other users. Similarly, if any user deletes a
record, we can’t access that record too.
 Dynamic: Unlike static cursors, once the cursor is open, all the modifications performed in
the database are reflected in the result set. The UPDATE, INSERT and DELETE operations
made by other users can be viewed while scrolling the cursor.

37. What are Entity and Relationship?

Entities are real-world objects that are individualistic and independent. Rows of a table represent
the members of the entity, and columns represent the attributes of the entity. For instance, a ‘list of
employees of a company is an entity where employee name, ID, address, etc., are the attributes of
the entity.

 A relationship indicates how entities in a database are related to each other. Simply put, how a row
in a table is related to row(s) of another table in a database. The relationship is made using the
primary key and the foreign key primarily.

There are three types of relationships in DBMS, as mentioned below:

 One-to-one relationship
  One-to-many relationship
 Many-to-many relationship

38. What is a Trigger, and mention its various types?

Triggers are nothing but they are special stored procedures. When there is an event in the SQL
server, triggers will be fired automatically.

There are three types of triggers – LOGON, DDL, and DML.

             LOGON triggers: They get fired when a user starts a Logon event

           DDL triggers: They get fired when there is a DDL event

          DML Triggers: They get fired when there is a modification in data due to DML

39. What is Schema in SQL, and mention its advantages?

The schema represents the logical structures of data. Using schemas, the database objects can be
grouped logically in a database. Schema is useful for segregating database objects based on
different applications, controlling access permissions, and managing a database's security aspects.
Simply out, Schemas ensure database security and consistency.

Advantages:

 Schemas can be easily transferred


 You can transfer database objects between schemas
 It protects database objects and achieves effective access control

40. What are the types of UDFs?

There are three types of UDFs. They are defined as follows:

 User-defined scalar functions


 Table-valued functions
 System functions

41. What is the difference between char and varchar data types?

Char data type is a fixed-length data type in which the length of the character cannot be changed
during execution. It supports storing normal and alphanumeric characters.

 On the other hand, varchar is the variable-length data type in which the length of the character can
be changed during execution. That's why, it is known as a dynamic data type.

42. Mention the Aggregate Functions used in SQL?

The following aggregate functions are used in SQL.

 COUNT
  SUM
 AVG
 MAX
 MIN

43. What are Case Manipulation Functions used in SQL?

The following are the case manipulation functions used in SQL.

 LOWER
 UPPER
 INITCAP

44. What are Character Manipulation Functions used in SQL?

The following are the character manipulation functions used in SQL.

 CONCAT
 SUBSTR
 LENGTH
 INSTR
 LPAD
 RPAD
 TRIM
 REPLACE

45. How would you differentiate single-row functions from multiple-row functions?

Single row functions can act on a single row of a table at a time. They return only one result after
executing a row. Length and case conversions are known to be single-row functions.

Multiple row functions can act on multiple rows of a table at a time. They are also called group
functions and return a single output after executing multiple rows.

Experienced:

46. What is the difference between SQL and NoSQL?

SQL No SQL

Works on relational databases Works on non-relational databases

Stores data in tables based on schemas so that No specific method is followed for data storage, so
data are organized and structured it offers flexibility in storing data.

Easy to execute complex queries Difficult to execute complex queries

Scaling is performed vertically increasing the Scaling is performed horizontally adding more
processing power of servers servers and nodes

Follows CAP theory – according to this, any two of


SQL satisfies ACID Properties such as atomicity,
the following need to be satisfied – Consistency,
consistency, isolation, and durability.
Availability, and Partition tolerance.

 47. What is the difference between SQL and MySQL?

SQL MySQL

It is the RDMS – Relational Database Management


It is the programming language
System

It is used to store, modify and delete data in a


It is used for querying relational database systems
database in an organized way.
It is an open-source platform managed by Oracle
It is a licensed product of Microsoft.
corporation

It provides adequate protection to SQL servers As it is an open-source platform, security cannot be


against intruders reliable

Support connectors such as the Workbench tool to


It doesn’t support any connectors
build databases

48. What is the difference between Index and View?

Generally, an index is created in a separate table. They are the pointers that indicate the address of
data in a database table. An index helps speed up querying and the data retrieval process in a
database.

On the other hand, a view is a virtual table created from the rows and columns of one or more
tables. The main thing about a view is that the rows and columns are grouped logically. With the
support of views, you can restrict access to the entire data in a database.

49. What is the use of Views, and mention its types in SQL?

Views are the virtual database tables created by selecting rows and columns from one or more
tables in a database. They support developers in multiple ways, such as simplifying complex
queries, restricting access to queries, and summarising data from many tables.

There are two types of views, as mentioned below:

 System-defined views: They can be used for specific purposes and perform specific actions
only. It provides all the information and properties of databases and tables.
 User-defined views: They are created as per the requirements of users. They are routines
that accept parameters, perform complex functions, and return a value.

50. Compare: LONG and LOB Data types

LONG Datatype LOB Datatype

helps store large scale semi-structured and Known as Large Objects. It is used to store large
unstructured data size data

Stores up to 2GB of data Can store up to 4GB  of data

Difficult to maintain Supports manipulating and accessing data easily


A table can have multiple LOB columns where LOB
A table can have only one LONG column
type data is stored

Subqueries cannot select LONG data types Subqueries can select LOB datatypes

Access data only sequentially Access data randomly

51. What is the difference between Zero and NULL values in SQL?

When a field in a column doesn’t have any value, it is said to be having a NULL value. Simply put,
NULL is the blank field in a table. It can be considered as an unassigned, unknown, or unavailable
value. On the contrary, zero is a number, and it is an available, assigned, and known value.

52. What is the difference between INNER JOIN and OUTER JOIN?

INNER JOIN OUTER JOIN

It is the intersection of two tables It is the union of two tables

Retrieves the rows common to two tables and all


Only retrieves rows that are common to two tables
the values of one table

53. What are Database Testing and its benefits?

Database testing is also known as back-end testing. It consists of the SQL queries executed to
validate database operations, data structures, and attributes of a database. It helps to ensure the
data integrity by eliminating duplicate entries of data in a database, failing which will create many
problems while managing the database. Besides, it deals with testable items hidden and not visible
to users.

54. What is Database Black box testing?

Blackbox testing helps to examine the functionality of a database. It is performed by validating the
integration level of a database. The incoming and outgoing data are verified by various test cases
such as the cause-effect graphing technique, equivalence partitioning, and boundary value analysis.
This kind of testing can be performed at the early stages of development to ensure better
performance.

55. What is the use of Defaults in SQL?

In a database, default values are substituted when no value is assigned to a field in a table column.
Basically, each column can be specified with a default value. In this way, SQL server management
studio specifies default values, which can be created only for the current databases. Note that if the
default value exceeds the size of the column field, it can be truncated.
56. What is SQL Injection, and how to avoid it?

SQL injection is a malicious attack sent targeting an SQL server instance. It is usually sent through
strings of statements and passed into the SQL server for execution. To avoid SQL injection, all
statements must be verified for malicious vulnerabilities before allowing for execution.

In addition to that, the following methods can be applied to avoid SQL injections. They are given as
follows:

 Using type-safe SQL parameters


 Using parameterized input with stored procedures
 Filtering inputs
 Reviewing codes
 Wrapping parameters

57. What do you mean by Autonomous Transaction?

An autonomous transaction is an independent transaction initiated by a transaction that is the


main transaction. Autonomous transaction holds the main transaction, performs SQL operations,
and commits or rolls back. After that, it resumes the main transaction. Note that autonomous
transaction doesn’t share locks and resources with the main transaction.

58. Write the SQL statements that can be used to return even number records and odd number
records?

You can use the following statement to retrieve even number records from a table.

SELECT * from table where id % 2 = 0

You can use the following statement to retrieve odd number records from a table.

SELECT * from table where id % 2 ! = 0

59. What is Alias in SQL?

SQL aliases help to assign temporary names for a table or column. It is used to simplify table or
column names. And aliases can exist only for that query period. It can be created using the ‘AS’
keyword. Know that creation of an alias is in no way affecting the column names in the database. It
can be applied when more than one table is involved in a query.
60. What is the difference between OLAP and OLTP?

OLAP is known as Online Analytical Processing. It consists of tools used for data analysis that will be
used for making better decisions. It can work on multiple database systems' historical data and
provide valuable insights. For example, NETFLIX and SPOTIFY generate insights from past data.

On the other hand, OLTP is known as Online Transaction Processing, and it works on operational
data. OLTP manages ACID properties during transactions. Specifically, it performs faster than OLAP
so that it can be used in online ticket booking, messaging services, etc.

61. What do you mean by Data Inconsistency?

Data inconsistency occurs when the same data exists in many tables in different formats. In other
words, the same information about an object or person may be spread across the database in
various places creating duplication. It decreases the reliability of the data and decreases the query
performance significantly. To overcome this drawback, we can use constraints on the database.

62. What do you mean by Collation in SQL?

Collation allows to sort and compare data with pre-defined rules. These rules help to store, access
and compare data effectively. The collation rules are applied while executing insert, select, update
and delete operations. SQL servers can store objects that have different collations in a single
database. Note that collation offers case-sensitivity and accent sensitivity for datasets.

63. How to create a table from an existing table?

A copy of a table can be created from an existing table using the combination of CREATE and
SELECT statements. Using these statements, you can select all the columns or specific columns
from an existing table. As a result, the new table will be replaced with all the values of the existing
table. Here, the WHERE clause can select the specific columns from the table.

The syntax for this type of table creation is given below:

CREATE TABLE NEW_TABLE_NAME1 AS

                SELECT [column1,column2,…..columnN]

                FROM EXISTING_TABLE_NAME1

                [WHERE]

64. How to fetch common records from two tables?

We can fetch common records using INTERSECT commands in SQL. The main thing about this
statement is that it returns only the common records. It means that this statement helps to
eliminate duplication of data.
The syntax for this statement is given as below:

SELECT CustomerID

             FROM Sales. customers

INTERSECT

              SELECT CustomerID

              FROM Sales. Orders

              WHERE Month (Orderdate) = December;

65. What are the common clauses used with SELECT Statements?

The common clauses such as FOR, ORDER BY, GROUP BY, and HAVING are used with SELECT
statements.

 FOR Clause - it specifies the different formats for viewing result sets such as browser mode
cursor, XML, and JSON file.
 ORDER BY Clause - It sorts the data returned by a query in a specific order. It helps to
determine the order for ranking functions.
 GROUP BY Clause - It groups the result set of the SELECT statement. It returns one row per
group.
 HAVING Clause – It is used with the GROUP BY clause and specifies a search condition for a
group.

66. What is COALESCE and describe any two properties of COALESCE functions?

COALESCE is an expression that evaluates arguments in a list and only returns the non-NULL value.

For example, consider the following statement:

SELECT COALESCE (NULL, 14, 15);

This statement will return 14 after the execution since the first value is the NULL in this argument
list.

                        Properties of COALESCE function:

 The datatype must be the same


 It functions as a syntactic shortcut for the case expression
67. What is the use of the MERGE statement?

MERGE allows combining the INSERT, DELETE and UPDATE functions altogether. This statement can
be applied when two statements have complex matching characteristics. Though the MERGE
statement seems to be complex, it provides much more advantages to developers when they get
familiar with this statement. It reduces I/O operations significantly and allows to read data only
from the source.

68. What is the use of CLAUSE in SQL?

Clauses are nothing but they are the built-in functions of SQL. They help to retrieve data very
quickly and efficiently. Clauses are much-needed for developers when there is a large volume of
data in a database. The result set of clauses would be a pattern, group, or an ordered format.

The following are the various clauses used in SQL:

 WHERE Clause
 OR Clause
 And Clause
 Like Clause
 Limit Clause
 Order By
 Group By

69. How to change a table name in SQL?

If you need to rename a table name in SQL, you can use the RENAME OBJECT statement to achieve
the same.

You have to execute the following steps to change a table name using SQL.

 First, connect to a database engine in Object Explorer


 Select a new query on the standard bar
 Then, write the query and execute it.

The following example will show the use of rename query.

Advanced SQL Interview Questions & Answers


70. What are the differences between SQL and PL/SQL?

SQL PL/SQL

It is a Procedural Language where SQL statements


It is a Structured Query Language
are processed effectively
A Group of operations as a single block can be
Only a single operation can be performed at a time
performed at a time

SQL executes the queries such as creating tables, It is used to write program blocks, functions,
deleting tables, and inserting into tables. procedures, triggers, packages, and cursors.

Mainly, it is used to retrieve data from databases Used for creating web applications and server
and modify tables. pages

Processing speed is low It has the excellent processing speed

71. What are the advantages of PL/SQL functions?

 It has tight interaction with SQL.


 It has high performance and productivity.
 It has high portability and scalability.
  It is highly flexible and secure.
 Supports developing web applications and server pages

72. Differentiate: CHAR and VARCHAR data types in SQL?

CHAR VARCHAR

It is a fixed-length character string data type It is a variable-length character string data type.

The data type can be a single byte or multiple-byte  It can accept character strings up to 255 bytes

This data type can be used when the character This data type is used when the character length is
length is known not clear

It uses static memory location It uses dynamic memory location

This is used when the character length of the data This is used when the character length of the data
is the same. is variable.

73. How can you avoid Duplicate Keys in SQL?

We can eliminate duplicate keys in SQL by using the following methods:

 Using INSERT INTO SELECT


  Using WHERE NOT IN
 Using WHERE NOT EXISTS
 Using IF NOT EXISTS
 Using COUNT(*)=0

74. Brief the factors that affect the functionalities of databases?

The following five factors affect the functionalities of databases.

 Workload
 Throughput
 Resources
 Optimization
 Contention

75. List out the factors that affect the query performance?

The following are the factors that affect the performance of queries.

 Number of nodes, processors, or slices


 Node types
 Data distribution
 Data sort order
 Dataset size
 Concurrent operations
 Query structure
 Code compilation

76. Differentiate: UNION and INTERSECT statements?

UNION: It is the operator that returns a single result set for two separate queries. And this
operator functions based on specific conditions.

Syntax: query 1 UNION query2

INTERSECT: It is the operator that returns only the distinct rows from two separate queries.

Syntax: query 1 INTERSECT query2

77. What is the difference between DROP and TRUNCATE statements?

DROP TRUNCATE

It removes a whole database It removes a table or data or index

All the constraints will be removed after the Constraints don’t get affected because of the
execution of the DROP function. execution of this statement
The structure of the data also will be removed The structure of the data won’t get affected

It is a slow process It is faster than the DROP statement

78. What is the use of the SELECT DISTINCT statement?

This statement is used to select distinct values from a table. The table might consist of many
duplicate records, whereas this statement helps to return only the distinct values.

The syntax for the statement is given as follows;

SELECT DISTINCT column1, column2,

FROM table_name1;

79. How can you differentiate the RANK and DENSE_RANK functions?

Both RANK and DENSE_RANK are used as the ranking functions, which perform ranking of data
based on specific conditions. When the RANK statement is executed, it returns a ranking of values
of a table based on specific conditions. At the same time, the result set up skip positions in the
ranking if there are the same values. Simply put, there will be a discontinuity in the numbering of
ranking. On the other hand, when the RANK_DENSE function is executed, it doesn’t skip any
position in the ranking of values even though there are the same values present in the table. It
returns continuous numbering of ranking.

The following example will explain the use of the RANK and DENSE_RANK functions.

80. What is the difference between IN and BETWEEN operators?

Both IN and BETWEEN operators are used to return records for multiple values from a table. The IN
operator is used to return records from a table for the multiple values specified in the statement.
On the other side, BETWEEN operator is used to return records within a range of values specified in
the statement.

             Syntax for the IN statement is given as:

SELECT * FROM table_name1

WHERE column_name1 IN (value 1,value2)

The syntax for the BETWEEN statement is given as:

SELECT * FROM table_name1


WHERE column_name1 BETWEEN ‘value 1’ AND ‘value2’

81. Compare: STUFF and REPLACE statements?

Both STUFF and REPLACE statements are used to replace characters in a string. The STUFF
statement inserts the specific characters in a string replacing existing characters. In comparison,
the REPLACE statement replaces existing characters with specific characters throughout the string.

For example, consider the following examples:

For the STUFF statement;

SELECT STUFF (‘raman’,2,3,’aja’)

Output: rajan

For the REPLACE statement;

SELECT REPLACE (‘ramanathan’,’an’,’ar’)

Output: ramarathar

82. What do you mean by COMMIT in SQL?

COMMIT statement allows saving the changes made in a transaction permanently. Once a
transaction is committed, the previous values cannot be retrieved.

The following syntax is used for this operation:

SELECT *

FROM Staff

WHERE incentive = 1000;

sql>COMMIT;

83. What is the use of the GRANT Statement?

This statement grants permissions for users to perform operations such as SELECT, UPDATE,
INSERT, DELETE, or any other operations on tables and views.

For example, if you would like to provide access to a user for updating tables, then the following
statement must be used. In addition, the user too can grant permissions to other users.
GRANT UPDATE ON table_name TO user_name WITH GRANT OPTION

84. What is the difference between White Box Testing and Black Box Testing?

Black Box Testing White Box Testing

The internal structure of the program is hidden


Testers know the internal structure of the program
from testers

It is performed by software testers It is performed by software developers

Testing is known as outer or external software Testing is known as inner or internal software
testing testing

Programming knowledge is not required for testers Programming knowledge is a must for testers

Functional testing, non-functional testing, and


Path testing, loop testing, and condition testing are
regression testing are the types of black-box
types of white box testing.
testing.

85. What do you mean by ETL in SQL?

ETL in SQL represents Extract, Transform and Load.

Extracting – It is about extracting data from the source, which can be a data warehouse, CRMs,
databases, etc.

Transforming – It includes many processes such as cleansing, standardization, deduplication,


verification, and sorting.

Loading – It is the process of loading the transformed data into the new destination. There are two
types of loading data: full loading and incremental loading.

86. What do you mean by NESTED triggers?

If a trigger fires another trigger while being executed, it is known as a NESTED trigger. Nested
triggers can be fired while executing DDL and DML operations such as INSERT, DROP and UPDATE.
Nested triggers help to back up the rows affected by the previous trigger. There are two types of
nested triggers: AFTER triggers and INSTEAD OF triggers.

87. How to insert multiple rows in a database table in SQL?

We can use the INSERT INTO statement to insert multiple rows in a database table in SQL.
The following syntax can be used for this case:

INSERT INTO table_name VALUES (value1, value), (value3, value4)…;

The inserted data can be selected using the following syntax:

SELECT * FROM table_name;

88. What do you mean by live-lock in SQL?

When two processes repeat the same type of interaction continually without making any progress
in the query processing, it leads to a live-lock situation in the SQL server. There is no waiting state in
live-lock, but the processes are happening concurrently, forming a closed loop.

 For example, let us assume process A holds a resource D1 and requests resource D2. At the same
time, assume that process B holds a resource D2 and requests resource D1. This situation won’t
progress any further until any of the processes should either drop holding a resource or drop
requesting a resource.

89. What do you mean by Equi-JOIN and non-Equi-JOIN?

Equi-join creates a join operation to match the values of the relative tables. The syntax for this
operation can be given as follows:

SELECT column_list

FROM table1, table2,…..

WHERE table1.column_name = table.2column_name;

On the other side, Non-Equi join performs join operations except equal. This operator works with
<,>,>=, <= with conditions.

SELECT *

FROM table_name1,table_name2

WHERE table_name1.column[>|<|>=|<=] table_name2.column;

90. What are the different types of SQL sandboxes?

There are three types of SQL sandboxes. They are given as follows:
 Safe access sandbox
 Unsafe access sandbox
 External access sandbox

91. What do you mean by lock escalation?

It is the process of converting row and page locks into table locks. Know that Reduction of lock
escalation would increase the server performance. To improve performance, we need to keep
transactions short and reduce lock footprints in queries as low as possible. Besides, we can disable
lock escalation at the table and instance levels, but it is not recommended.

92. How can you update a table using SQL?

The UPDATE statement allows you to update a database table in SQL. After the execution, one or
more columns in a table will be replaced by new values.

The syntax for the UPDATE statement is given as follows:

UPDATE table_name

SET

  Column1 = new_value1,

  Column2 = new_value2,

  ..…..

WHERE

   Condition;

This statement requires a table name, new values, and conditions to select the rows. Here, the
WHERE statement is not mandatory. Suppose the WHERE clause is used, all the rows in a table will
be updated by the new values.

93. How to create a Stored Procedure using T-SQL?

 Connect to the instance of a database engine in ‘object explorer’


 Click ‘new query’ from the ‘files menu’
 Copy and paste the following sample codes in the query window

USE AdventureWorks2012; 
GO 

CREATE PROCEDURE HR.GetEmployeesTest2  

    @LastName nvarchar(25),  

    @FirstName nvarchar(25)  

AS 

     SET NOCOUNT ON

     SELECT FirstName, LastName, Division

     FROM HR.vEmployeeDivisionHistory 

     WHERE FirstName = @FirstName AND LastName = @LastName 

     AND EndDate IS NULL; 

GO

 Now, execute the codes

You can use the following statement to run the newly created stored procedure.

EXECUTE HR.GetEmployeesTest2 N'Ackerman', N'Pilar';

94. What do you mean by DELETE CASCADE constraint?

When a foreign key is created under this option, and if a referenced row in the parent table is
deleted, the referencing row(s) in a child table also gets deleted.

On similar tracks, when a referenced row is updated in a parent table, the referencing row(s) in a
child table is also updated.

95. Explain the different types of indexes in SQL?

The following are the different types of indexes in SQL.

 Single-column indexes
 Unique indexes
 Composite indexes
 Implicit indexes
96. What do you mean by auto-increment?

It is a unique number that will be generated when a new record is inserted into a table. Mainly, it
acts as the primary key for a table.

The following syntax is used for this purpose:

IDENTITY (starting_value, increment_value)

97. What do you mean by Pattern Matching?

We can use the LIKE command in SQL to identify patterns in a database using character strings.
Generally, a pattern may be identified using wildcard characters or regular characters. So, pattern
matching can be performed using both wildcard characters and string comparison characters as
well. However, pattern matching through wildcard characters is more flexible than using string
comparison characters.

98. What is the difference between blocking and deadlocking?

Blocking is a phenomenon that occurs when a process locks a resource ‘A’, and the same resource
is requested by another process ‘B’. Now, process ‘B’ can access the resource ‘A’ only when process
‘A’ releases the lock. The process ‘B’ has to wait until the process ‘A’ releases the lock. The SQL
server doesn't interfere and stops any process in this scenario.

On the contrary, deadlocking is the phenomenon that occurs when a resource 'A' is locked by a
process 'A' and the same resource is requested by another process 'B'. Similarly, a resource 'B' is
locked by process 'B' and requested by process A. This scenario causes a deadlock situation, and it
is a never-ending process. So, the SQL server interferes and voluntarily stops any one of the
processes to remove the deadlock.

99. What is the difference between COALESCE ( ) and ISNULL ( )?

COALESCE function returns the first value that is non-NULL in the expression, whereas ISNULL is
used to replace the non-NULL values in the expression.

Syntax for COALESCE function is given as:

SELECT column(s),COALESCE (exp_1,…..,exp_n)

FROM table_name;

Syntax for ISNULL is given as:

SELECT column(s),ISNULL(column_name,value_to_replace)
FROM table_name;

100. What is the difference between NVL and the NVL (2) functions in SQL?

Both the functions are used to find whether the first argument in the expression is NULL. The NVL
function in the SQL query returns the second argument if the first argument is NULL. Otherwise, it
returns the first argument.

The NVL2 function in SQL query returns the third argument if the first argument is NULL.
Otherwise, the second argument is returned.

Conclusion

All of us know that knowledge is power. After reading this blog, we hope you might have gathered
good knowledge about SQL and understood it in depth. Keep reading the Q&A questions for few
more times. It will help you get familiar with the terminologies and syntaxes used in this blog.

Basic PL/SQL Interview Questions for Freshers

1. Compare SQL and PL/SQL.


Criteria SQL PL/SQL
What is it? A single query or command A full programming language
execution
What does it A data source for reports, web An application language to build, format, and display
comprise? pages, etc. reports, web pages, etc.
Characteristic Declarative in nature Procedural in nature
Used for Manipulating data Creating applications

2. What is PL/SQL?

Oracle PL/SQL is a procedural language that has both interactive SQL and procedural
programming language constructs such as iteration and conditional branching.

Go through this  PL/SQL Tutorial  to learn what is PL/SQL?


3. What is the Basic Structure of PL/SQL?

PL/SQL uses a block structure as its basic structure. Anonymous blocks or nested blocks can be
used in PL/SQL.

4. What is a Trigger and what are its uses?

A trigger is a database object that automatically executes in response to some events on the
tables or views. It is used to apply the integrity constraint to database objects.

A PL/SQL program unit associated with a particular database table is called a database trigger.
It is used for:

 Audit data modifications


 Log events transparently
 Enforce complex business rules
 Maintain replica tables
 Derive column values
 Implement complex security authorizations
Any constant, variable, or parameter has a data type depending on which the storage
constraints, format, and range of values and operations are determined.

5. What Data Types are Present in PL/SQL?

There are various kinds of data types present in PL/SQL. They are:

1. Scalar: The scalar data type is a one-dimensional data type with no internal components.
CHAR, DATE, LONG, VARCHAR2, NUMBER, and BOOLEAN are some examples of the scalar
data type.
2. Composite: The composite data type is made up of different data types that are easy to
update and have internal components that can be utilized and modified together. For
instance, RECORD, TABLE, VARRAY, and so on.
3. Reference: The reference data type stores pointers, which are values that relate to other
programs or data elements. REF CURSOR is an example of the reference data type.
4. Large Object: The large object data type stores locators, which define the location of large
items stored out of line such as video clips, graphic images, and so on. BLOB, BFILE, CLOB,
and NCLOB are examples of the large object data type.

Get 100% Hike!


Master Most in Demand Skills Now !

Submit
6. What are the Basic Parts of a Trigger?

There are three basic parts of a trigger. They are:

 A triggering statement or event


 A restriction
 An action

7. How is the Process of PL/SQL Compiled?

Syntax checking, binding, and P-code generation are all part of the compilation process. Syntax
checking looks for compilation issues in PL/SQL code. After all mistakes have been fixed, the
data holding variables are given a storage address. This process is referred to as binding. The
PL/SQL engine’s P-code is a set of instructions. For named blocks, P-code is saved in the
database and used the next time it is run.

Go through the  Handling PL/SQL Errors  tutorial page to know how error handling is done in
PL/SQL!

8. What is a Join?

A join is a query that combines rows from two or more tables, views, or materialized views. A
join is performed by the Oracle Database whenever there are multiple tables in the FROM
clause of the query. Most of these queries contain at least one join condition, either in the
FROM or WHERE clause.

9. What is a View?

A view is created by joining one or more tables. It is a virtual table that is based on the result
set of an SQL statement; it contains rows and columns, just like a real table. A view can be
created with the CREATE VIEW statement.
Intermediate PL SQL Interview Questions

10. What Does a PL/SQL Package Consist Of?

A PL/SQL package consists of:

 PL/SQL table and record type statements


 Procedures and functions
 Cursors
 Variables, such as tables, scalars, records, etc., and constants
 Exception names and pragmas for relating an error number with an exception
 Cursors

Check out this insightful PL/SQL tutorial to learn more  about Pl/SQL Packages!

11. What do you know about the Commands COMMIT,


ROLLBACK, and SAVEPOINT?

COMMIT: The COMMIT command saves changes to a database permanently during the current
transaction.

ROLLBACK: The ROLLBACK command is used at the end of a transaction to undo any


modifications made since the start of the transaction.

SAVEPOINT: The SAVEPOINT command saves the current point with a unique name during the
processing of a transaction.
Career Transition



12. What are the Benefits of PL/SQL Packages?

PL/SQL packages provide several benefits. Some of them are as follows:


 Enforced Information Hiding: It offers the liberty to choose whether to keep the data
private or public.
 Top-down Design: We can design the interface to the code hidden in the package before we
actually implement the modules.
 Object Persistence: Objects declared in a package specification behave like global data for
all PL/SQL objects in the application. We can modify the package in one module and then
reference those changes in another
 Object-oriented Design: The package gives developers a stronghold over how the modules
and data structures inside the package can be used.
 Guaranteeing Transaction Integrity: It provides a certain level of transaction integrity.
 Performance Improvement: The RDBMS automatically tracks the validity of all program
objects stored in the database. It also enhances the performance of packages.

Master PL/SQL by enrolling in this top-rated  PL/SQL Certification Training!

13. What is Exception Handling?

Exception handling is a mechanism that is implemented to deal with runtime errors. It can be
adjusted in PL/SQL. PL/SQL provides the exception block that raises the exception, thus helping
the programmer to find the fault and resolve it. When an error occurs, the program’s error
handling code is included. There are two different types of exceptions defined in PL/SQL:
 User-defined exception
 System-defined exception

14. Mention a Few Predefined Exceptions

The following are some examples of predefined exceptions:

 NO DATA FOUND: A single-row SELECT statement that returns no data


 TOO MANY ROWS: A single row SELECT statement that returns many rows
 INVALID CURSOR: An incorrect cursor operation is performed
 ZERO DIVIDE: An attempt at zero division

15. What are the Various Types of SQL Statements?

The five types of SQL statements are as follows:

 DDL: Data definition language (DDL) helps in the creation of a database structure or schema.
CREATE, DROP, ALTER, RENAME, and TRUNCATE are the five types of DDL commands in SQL.
 DML: Data manipulation language (DML) allows you to insert, change, and delete data from
a database instance. DML is in charge of making all kinds of changes to a database’s data.
The database application and the user can insert data and information using three basic
commands—INSERT, UPDATE, and DELETE.
 DCL: GRANT and REVOKE are the commands in the data control language (DCL) that can be
used to grant rights and permissions. The database system’s parameters are controlled by
other permissions.
 TCL: Transaction control language (TCL) commands deal with database transactions. Some
of the TCL commands are COMMIT, ROLLBACK, and SAVEPOINT.
 DQL: Data query language (DQL) is used to retrieve data from the database. It just has one
command, which is SELECT.

16. What are the Different Methods to Trace the PL/SQL Code?

Tracing the code is a crucial technique to measure its performance during the runtime.
The different methods of tracing the code include:
 DBMS_APPLICATION_INFO
 DBMS_TRACE
 DBMS_SESSION and DBMS_MONITOR
 trcsess and tkprof utilities

17. What are the Various Types of Parameters in PL/SQL?

There are three types of parameters in PL/SQL. They are as follows:

 IN: The IN parameter allows you to send values to the procedure that is being called. The IN
parameter can be set to default values. It behaves as a constant and cannot be changed.
 OUT: The OUT parameter returns a value to the caller. The OUT parameter is an uninitialized
variable that cannot be used in expressions.
 IN OUT: The IN OUT parameter sends starting values to a procedure and returns the
updated values to the caller. This parameter should be treated as an initialized variable and
given a value.

18. What are PL/SQL Records?

PL/SQL records are a collection of values. To put it another way, PL/SQL records are a collection
of many pieces of information, each of which is of a simpler type and can be associated with
one another as fields.

Three types of records are supported in PL/SQL:

 Records based on tables


 Records created by programmers
 Records that are based on a cursor
19. Why do we use Index in a Table?

We use an index in a table to allow quick access to rows. For procedures that return a small
percentage of a table’s rows, an index allows quicker access to data.

20. What is the Difference among Functions, Procedures, and


Packages in PL/SQL?

 Functions: The main purpose of PL/SQL functions is to compute and return a single value.
The functions have a return type in their specifications and must return a specified value in
that type.
 Procedures: Procedures do not have a return type and should not return any value, but
they can have a return statement that simply stops its execution and returns to the caller.
Procedures are used to return multiple values; otherwise, they are generally similar to
functions.
 Packages: Packages are schema objects that group logically related PL/SQL types, items,
and subprograms. You can also say that packages are a group of functions, procedures,
variables, and record TYPE statements. Packages provide modularity, which aids in
application development. Packages are used to hide information from unauthorized users.

21. Why are Database Links Used?

Database links are used to establish communication across multiple databases or


environments such as test, development, and production. Other information can also be
accessed using database links, which are read-only.
Courses you may like
22. What is a Stored Procedure?

A stored procedure is a sequence of statements or a named PL/SQL block that performs one or
more specific functions. It is similar to a procedure in other programming languages. It is
stored in the database and can be repeatedly executed. It is stored as a schema object and can
be nested, invoked, and parameterized.

23. What is the Overloading of a Procedure?

When the name of the same procedure is repeated with the parameters of different data types
and parameters in different places, then that is referred to as procedure overloading.

24. What is meant by Expressions?

Expressions are made up of a series of literals and variables that are separated by operators.
Operators are used in PL/SQL to manipulate, compare, and calculate data. Expressions are
made up of two parts, operators and operands.
25. Which Cursor Attributes are the Result of a Saved DML
Statement, when it is Executed?

The statement’s result is saved in four cursor attributes. The four attributes are:

 SQL% FOUND
 SQL% NOTFOUND
 SQL% ROWCOUNT
 SQL% ISOPEN

26. What is a Cursor? Why is it Required?

A cursor is a temporary work area that is created in system memory when an SQL statement is
executed. A cursor contains information on a select statement and the row of data accessed by
it. This temporary work area stores the data, which is retrieved from the database, to
manipulate it. A cursor can hold more than one row but can process only one row at a time. A
cursor is required to process rows individually for queries.

27. What are the Types of Cursors?

There are two types of cursors:

 Implicit Cursor: When PL/SQL executes an SQL statement, it automatically constructs a


cursor without specifying one; these cursors are known as implicit PL/SQL uses implicit
cursors for the following statements:
o INSERT
o UPDATE
o DELETE
o SELECT

 Explicit Cursor: A programmer declares and names an explicit cursor for the queries that
return more than one row. An explicit cursor is a SELECT statement that is declared explicitly
in the current block’s declaration section or in a package definition. The following are the
commands that are used for explicit cursors in PL/SQL:
o OPEN
o FETCH
o CLOSE

28. What is the Open Cursor Command Function?

When the OPEN cursor command is used to open a cursor, it performs the following
operations:

 Set aside a processing memory region


 Paese the statement SELECT
 Use the memory addresses to assign values to input variables
 Recognize the active set of rows that meet the selection criteria
 Place the pointer exactly before the active set’s first row

29. How to Delete a Trigger?

To delete a trigger, you need to use the command DROP TRIGGER.

30. What are the Advantages of Stored Procedures?

Stored procedures have various advantages to help you design sophisticated database
systems. Some of the advantages of stored procedures as listed below:

 Better performance
 Higher productivity
 Ease of use
 Increased scalability
 Interoperability
 Advance security
 Replication
31. What are the Various Types of Schema Objects that can be
Created by PL/SQL?

There are various types of schema objects that are created by PL/SQL. Some of them are
mentioned below:

 Stored procedures, packages, and functions


 Object tables, object types, and object views
 Database triggers
 Database links
 Cursors
 Table
 View

32. What is the Advantage of Implicit Records?

Implicit records are handy since they do not require hard-coded descriptions. Because implicit
records are based on database table records, any changes to the database table records will be
reflected in the implicit records automatically.

33. What are the Ways of Commenting in PL/SQL?

In PL/SQL, comments help readability by describing the purpose and function of code
portions. Two types of comments are available in PL/SQL. They are as follows:

 Single-line Comments: Single-line comments start with a double hyphen (- -) at the


beginning of a line and go all the way to the conclusion.
 Multi-line Comments: Multi-line comments start with a slash-asterisk (/*) and terminate
with an asterisk-slash (*/), and they can span across several lines.

34. What is %TYPE?

The %TYPE property is used to declare a column in a table that includes the value of that
column. The variable’s data type is the same as the table’s column.
35. What is %ROWTYPE?

The %ROWTYPE property is used to declare a variable that contains the structure of the records
in a table. The variable’s data type is the same as the table’s columns.

36. Differentiate between a Temporary Tablespace and a


Permanent Tablespace

A temporary tablespace is used to store temporary items such as sort structures, while a
permanent tablespace is used to store things that will be used as the database’s genuine
objects.

37. How Many Triggers can be Applied to a Table?

A maximum of 12 triggers can be added to a table.

38. What is a Mutating Table Error?

A mutating table error occurs when a trigger tries to update a row that is currently in use. It can
be fixed by using views or temporary tables so that the database selects one and updates the
other.

39. What does the PLVtab Enable you to do when you Show the
Contents of PL/SQL Tables?

PLVtab enables you to do following when you show the contents of PL/SQL tables:

 Display or suppress the row numbers for the table values


 Show a prefix before each row of the table
 Display or suppress a header for the table
40. How can you Save or Place your msg in a Table?

To save a msg in a table, you either load the individual messages with calls to the add_text
procedure or load sets of messages from a database table using the load_from_dbms
procedure.

Wish to learn more? Visit the  PL/SQL Collections and Records  tutorial page!

Advanced PL/SQL Interview Questions and Answers for


Experienced Professionals

41. What are Pseudocolumns and how do they work? How can
Pseudocolumns be used in Procedure Statements?

Pseudocolumns aren’t genuine table columns but they behave like them. Pseudocolumns are
used to retrieve specific information in SQL statements. Although pseudocolumns are
recognized by PL/SQL as part of SQL statements, they cannot be used directly in a procedural
language. The following are the pseudocolumns that are used:

 CURRVAL and NEXTVAL


 LEVEL
 ROWID
 ROWNUM

42. What is the use of the || Operator?

The strings are concatenated using the || operator. The || operator is employed by both
DBMS_OUTPUT.put line and select statements.

43. What is the difference between Sqlcode and Sqlerrm, and


why are they important for Pl/SQL Developers?

The value of the error number for the most recent error detected is returned by SQLCODE. The
SQLERRM function returns the actual error message for the most recent issue. They can be
used in exception handling to report or save the error that happened in the code in the error
log database. These are especially important for the exception WHEN OTHERS.

44. What is raise_application_error?

This procedure can be used to send user-defined error messages from stored subprograms.
You can prevent returning unhandled exceptions by reporting failures to your application. It
appears in two places, the executable section and the exceptional section.

45. In PL/SQL, how can you verify whether an Update Statement


is Executed or not?

The SQL % NOTFOUND attribute can be used to determine whether or not the UPDATE
statement successfully changed any records. If the last SQL statement run had no effect on any
rows, this variable returns TRUE.

46. Explain the Day-to-day Activities in PL/SQL.

 Create database objects—tables, synonyms, sequences, etc.


 Implement business rules, create procedures, functions, etc.
 Impose business rules, create constraints, triggers, etc.
 Create cursors for data manipulation

47. How can you locate a PL/SQL Block when a Cursor is Open?

The %ISOPEN variable cursor status can be used to find the PL/SQL block.

48. What do you know about pragma_exception_init in PL/SQL?

The pragma_exception_init command in PL/SQL instructs the compiler to associate an


exception name with an Oracle error number. This enables one to refer to any internal
exception by name and create a custom handler for it.
49. In PL/SQL, what are the differences between Stored
Procedure and Stored Function?

The key differences between stored procedure and stored function are:

 Returning the value in a stored procedure is optional, while returning the value in a stored
function is required.
 A stored procedure can have both input and output parameters, while a stored function can
only have either an input parameter or an output parameter.
 Exception handling is possible in a stored procedure, whereas it is not possible in a stored
function.

50. How to Display Records having the Maximum Salary from an


Employee Table?
Select * from emp where sal= (select max(sal) from emp)

If you have any doubts or queries related to PL/SQL, get them clarified from our PL/SQL
experts on our  SQL Community!

51. What is the Syntax to Disable a Trigger?


ALTER TRIGGER TRIGGER_NAME DISABLE;

52. How to Display the Highest Salary from an Employee Table?

Use the following code to display the highest salary from an employee table:
Select max(sal) from emp;

53. Which Command is used for Deleting a Package?

The command used for deleting a package is DROP PACKAGE

54. How to Display the Second Highest Salary from an Employee


Table?
Select max(sal) from emp where sal not in ( select max(sal) from emp
55. How can you view the User-defined Functions and
Procedures in PL/SQL?

The table USER SOURCE is used to store user-defined functions and procedures. To examine
them, the function and procedure names should be specified in uppercase (in select
command). The following command is used to inspect the source code of a user-defined
function or method:
Select text from user_source where name=’PROCEDURE_NAME’;

56. What is a Join?

Join is a keyword that is used to query data from multiple tables based on the relationship
between the fields of tables. Keys play a major role in Joins.

57. In PL/SQL, what is the Purpose of the DBMS_OUTPUT


Package?

The PL/SQL output is shown on the screen using the DMS_OUTPUT package. get_line, put_Line,
new_line, and many more are found in DBMS_OUTPUT. The put_line procedure, which is a part
of the DBMS_OUPUT package, is used to display the information in the line.

58. What is a View?

 A view is a virtual table consisting of the data contained in a table.


 A view does not need any memory space.
 A view can be created on multiple tables.
59. How can you Execute a Stored Procedure?

There are two steps to execute a stored procedure: 

 Use the EXECUTE keyword. The EXEC keyword can also be used.
 Call the name of the procedure from a PL/SQL block.

Syntax
EXECUTE procedure_name;
Or
Exec  procedure_name;

60. What are the differences between ANY and ALL operators?

ALL Operator: Value is compared to every value returned by the subquery using the ALL
operator.

o > ALL denotes greater than the maximum


o < ALL denotes less than the minimum
o <> ALL is the same as NOT IN condition

ANY Operator: Value is compared to each value returned by the subquery using the ANY
operator. SOME is a synonym for ANY operator.

o > ANY denotes something more than the bare minimum


o < ANY denotes a value lower than the maximum
o = ANY is the same as the IN operator

61. How to Create a Function?

The syntax to create a CREATE function is below:


CREATE function_name
RETURN return_datatype
{IS | AS}
DECLARE
VARIABLE DATATYPE;
BEGIN
function_body
END function_name;

62. How can you Switch from an Init.ora File to Spfile?

One can switch from Init.ora file to Spfile by creating a spfile from the pfile command.

63. What is a Subquery? What are Its Types?

A subquery is a query within another query. The outer query is known as the main query and
the inner query is called the subquery. A subquery is executed first, and the result of the
subquery is passed to the main query.

There are two types of subqueries:

 Correlated
 Non-correlated
64. How can you Read/Write Files in PL/SQL?

One can read/write operating system text files by using the UTL_FILE package. It provides a
restricted version of the operating system stream file I/O, and it is available for both client-side
and server-side PL/SQL.
DECLARE
fileHandler UTL_FILE.FILE_TYPE;
BEGIN
fileHandler := UTL_FILE.FOPEN('/home/oracle/tmp','myoutput','z');
UTL_FILE.PUTF(file, 'Value of func1 is %sn',func1(2));
UTL_FILE.FCLOSE(file;
END;

65. How do you Create Nested Tables in PL/SQL?

In PL/SQL, one of the collection types is nested tables. They can be made in a PL/SQL block or
at the schema level. These are similar to a 1D array, except their size can be dynamically
extended.

The syntax to create a nested table is as follows:


TYPE type_name IS TABLE OF element_type [NOT NULL];
name_of_table type_name;
An example for creating nested tables in PL/SQL is as follows:
DECLARE
TYPE deptname IS TABLE OF VARCHAR2(10);
TYPE budget IS TABLE OF INTEGER;
names deptname;
deptbudget budget;
BEGIN
names := deptname ('Finance', 'Sales', 'Marketing');
deptbudget := budget (89899, 67879, 98999);
FOR i IN 1 .. names.count LOOP
dbms_output.put_line('Department = '||names(i)||', Budget = ' || deptbudget(i));
end loop;
END;
/
Oracle PL SQL Interview Questions for Beginners
1) What is the difference between PL SQL and SQL?

Comparison SQL PL/SQL

Execution Single command at a time Block of code

Application created by data


Application Source of data to be displayed
acquired by SQL

DDL and DML based queries and


Structures include Includes procedures, functions, etc
commands

Creating applications to display data


Recommended while Performing CRUD operations on data
obtained using SQL

Compatibility with each


SQL can be embedded into PL/SQL PL/SQL cant be embedded in SQL
other

2) What is SQL and also describe types of SQL statements?

SQL stands for Structured Query Language. SQL is a language used to communicate with the server
to access, manipulate, and control data.

There are 5 different types of SQL statements.

1. Data Retrieval: SELECT

2. Data Manipulation Language (DML): INSERT, UPDATE, DELETE, MERGE

3. Data Definition Language (DDL): CREATE, ALTER, DROP, RENAME, TRUNCATE.

4. Transaction Control Statements: COMMIT, ROLLBACK, SAVEPOINT

5. Data Manipulation Language (DCL): GRANT, REVOKE

3) What is an alias in SQL statements?

Alias is a user-defined alternative name given to the column or table. By default column, alias
headings appear in upper case. Enclose the alias in double quotation marks (“ “) to make it case-
sensitive. “AS” Keyword before the alias name makes the SELECT clause easier to read.
For example Select emp_name AS name from employee; (Here AS is a keyword and “name” is an
alias).

If you want to enrich your career and become a Professional in Oracle PL SQL, then enroll in "Oracle PL SQL
Training" - This course will help you to achieve excellence in this domain.

4) What is a Literal? Give an example of where it can be used?

A Literal is a string that can contain a character, a number, or a date that is included in the SELECT
list and that is not a column name or a column alias. Date and character literals must be enclosed
within single quotation marks (‘ ‘), number literals need not.

For exp: Select last_name||’ is a’||job_id As “emp details” from the employee; (Here “is a” is a
literal).

5) What is the difference between SQL and iSQL*Plus?

SQL iSQL*Plus

Is a Language Is an Environment

Character and date column headings are left-


justified and number column headings are right- Default heading justification is in the Centre.
justified.

Cannot be Abbreviated (short forms) Can be Abbreviated

Has a dash (-) as a continuation character if the


Does not have a continuation character
command is longer than one line

Use Functions to perform some formatting Use commands to format data

6) Define the order of Precedence used in executing SQL statements?

Order Evaluated Operator


1 Arithmetic operators (*, /, +, -)

2 Concatenation operators (||)

3 Comparison conditions

4 Is[NOT] NULL, LIKE, [NOT] IN

5 [NOT] BETWEEN

6 NOT Logical condition

7 AND logical condition

8 OR logical condition

7) What are SQL functions? Describe in brief different types of SQL functions?

SQL Functions are a very powerful feature of SQL. SQL functions can take arguments but always
return some value.1
There are two distinct types of SQL functions:

1) Single-Row functions: These functions operate on a single row to give one result per row.

Types of Single-Row functions:

1. Character
2. Number
3. Date
4. Conversion
5. General

2) Multiple-Row functions: These functions operate on groups of rows to give one result per
group of rows.

Types of Multiple-Row functions:

1. AVG
2. COUNT
3. MAX
4. MIN
5. SUM
6. STDDEV
7. VARIANCE

8) Explain the character, number, and date function in detail?

Character functions: accept character input and return both character and number values. Types
of character function are:

 Case-Manipulation Functions: LOWER, UPPER, INITCAP


 Character-Manipulation Functions: CONCAT, SUBSTR, LENGTH, INSTR, LPAD/RPAD, TRIM,
REPLACE

Number Functions: accept Numeric input and return numeric values. Number Functions are:
ROUND, TRUNC, and MOD

Date Functions: operates on values of the Date data type. (All date functions return a value of
DATE data type except the MONTHS_BETWEEN Function, which returns a number. Date Functions
are MONTHS_BETWEEN, ADD_MONTHS, NEXT_DAY, LAST_DAY, ROUND, TRUNC. 

9) What is a Dual Table?

The dual table is owned by the user SYS and can be accessed by all users. It contains one
columnDummy and one row with the value X. The Dual Table is useful when you want to return a
value only once. The value can be a constant, pseudocolumn, or expression that is not derived from
a table with user data.

10) Explain the Conversion function in detail?

Conversion Functions convert a value from one data type to another. Conversion functions are of
two types:

Implicit Data type conversion:

1. VARCHAR2 or CHAR To NUMBER, DATE


2. NUMBER To VARCHAR2
3. DATE To VARCHAR2

Explicit data type conversion:

1. TO_NUMBER
2. TO_CHAR
3. TO_DATE

TO_NUMBER function is used to convert a Character string to Number format. TO_NUMBER


function use fx modifier. Format: TO_NUMBER ( char[, ‘ format_model’] ). fx modifier specifies the
exact matching for the character argument and number format model of the TO_NUMBER function.
TO_CHAR function is used to convert NUMBER or DATE data type to CHARACTER format. TO_CHAR
Function uses fm element to remove padded blanks or suppress leading zeros. TO_CHAR Function
formats:TO_CHAR (date, ‘format_model’).The format model must be enclosed in single quotation
marks and is case sensitive.

For exp: Select TO_CHAR (hire date, ‘MM/YY’) from the employee.

TO_DATE function is used to convert a Character string to date format. TO_DATE function use fx
modifier which specifies the exact matching for the character argument and date format model of
TO_DATE function. TO_DATE function format: TO_DATE ( char[, ‘ format_model’] ).

For exp: Select TO_DATE (‘May 24, 2007’,’ mon dd RR’) from dual;

Read these latest  SQL Interview Questions and Answers  that help you grab high-paying jobs

11) Describe different types of General Function used in SQL?

General functions are of the following types:

NVL: Converts a null value to an actual value. NVL (exp1, exp2) .If exp1 is null then the NVL function
returns the value of exp2.

NVL2: If exp1 is not null, nvl2 returns exp2, if exp1 is null, nvl2 returns exp3. The argument exp1
can have any data type. NVL2 (exp1, exp2, exp3)

NULLIF: Compares two expressions and returns null if they are equal or the first expression if they
are not equal. NULLIF (exp1, exp2)

COALESCE: Returns the first non-null expression in the expression list. COALESCE (exp1, exp2…
expn). The advantage of the COALESCE function over the NVL function is that the COALESCE
function can take multiple alternative values.

Conditional Expressions: Provide the use of IF-THEN-ELSE logic within a SQL statement. Example:
CASE Expression and DECODE Function. 

12) What is the difference between COUNT (*), COUNT (expression), COUNT (distinct expression)?
(Where expression is any column name of Table)?

 COUNT (*): Returns a number of rows in a table including duplicates rows and rows
containing null values in any of the columns.
 COUNT (EXP): Returns the number of non-null values in the column identified by expression.
 COUNT (DISTINCT EXP): Returns the number of unique, non-null values in the column
identified by expression.
13) What is a Sub Query? Describe its Types?

A subquery is a SELECT statement that is embedded in a clause of another SELECT statement. A


subquery can be placed in WHERE HAVING and FROM clause.

Guidelines for using subqueries:

1. Enclose sub queries within parenthesis


2. Place subqueries on the right side of the comparison condition.
3. Use Single-row operators with single-row subqueries and Multiple-row operators with
multiple-row subqueries.

Types of subqueries:

 Single-Row Subquery: Queries that return only one row from the inner select statement.
Single-row comparison operators are: =, >, >=, <, <=, <>
 Multiple-Row Subquery: Queries that return more than one row from the inner Select
statement. There are also multiple-column subqueries that return more than one column
from the inner select statement. Operators include: IN, ANY, ALL.

14) What is the difference between ANY and ALL operators?

ANY Operator compares value to each value returned by the subquery. ANY operator has a
synonym SOME operator.

 > ANY means more than the minimum.


 < ANY means less than the maximum
 = ANY is equivalent to IN operator.

ALL Operator compares value to every value returned by the subquery.

 > ALL means more than the maximum


 < ALL means less than the minimum
 <> ALL is equivalent to NOT IN condition. 

15) What is a MERGE statement?

The MERGE statement inserts or updates rows in one table, using data from another table. It is
useful in data warehousing applications. 

16) What is the difference between the “VERIFY” and the “FEEDBACK” command?

 VERIFY Command: Use VERIFY Command to confirm the changes in the SQL statement (Old
and New values). Defined with SET VERIFY ON/OFF.
 Feedback Command: Displays the number of records returned by a query.
17) What is the use of Double Ampersand (&&) in SQL Queries? Give an example?

Use “&&” if you want to reuse the variable value without prompting the user each time.

For ex: Select empno, ename, &&column_name from employee order by &column_name;

18) What are Joins and how many types of Joins are there?

Joins are used to retrieve data from more than one table.

There are 5 different types of joins.

Oracle 8i and Prior SQL: 1999 (9i)

Equi Join Natural/Inner Join

Outer Join Left Outer/ Right Outer/ Full Outer Join

Self Join  Join ON

Non-Equi Join Join USING

Cartesian Product Cross Join

19) Explain all Joins used in Oracle 8i?

Cartesian Join: When a Join condition is invalid or omitted completely, the result is a Cartesian
product, in which all combinations of rows are displayed. To avoid a Cartesian product, always
include a valid join condition in a “where” clause. To Join ‘N’ tables together, you need a minimum of
N-1 Join conditions.

For exp: to join four tables, a minimum of three joins is required. This rule may not apply if the
table has a concatenated primary key, in which case more than one column is required to uniquely
identify each row.

Equi Join: This type of Join involves primary and foreign key relations. Equi Join is also called Simple
or Inner Joins.

Non-Equi Joins A Non-Equi Join condition containing something other than an equality operator.
The relationship is obtained using an operator other than an equal operator (=). The conditions
such as <= and >= can be used, but BETWEEN is the simplest to represent Non-Equi Joins.

Outer Joins: Outer Join is used to fetch rows that do not meet the join condition. The outer join
operator is the plus sign (+), and it is placed on the side of the join that is deficient in information.
The Outer Join operator can appear on only one side of the expression, the side that has
information missing. It returns those rows from one table that has no direct match in the other
table. A condition involving an Outer Join cannot use IN and OR operators.

Self Join: Joining a table to itself.

20) Explain all Joins used in Oracle 9i and later release?

Cross Join:

The Cross Join clause produces the cross-product of two tables. This is the same as a Cartesian
product between the two tables.

Natural Joins:

This is used to join two tables automatically based on the columns which have matching data types
and names, using the keyword NATURAL JOIN. It is equal to the Equi-Join. If the columns have the
same names but different data types, then the Natural Join syntax causes an error.

Join with the USING clause:

If several columns have the same names but the data types do not match, then the NATURAL JOIN
clause can be modified with the USING clause to specify the columns that should be used for an
equi Join. Use the USING clause to match only one column when more than one column matches.
Do not use a table name or alias in the referenced columns. The NATURAL JOIN clause and USING
clause are mutually exclusive.

For ex: Select a.city, b.dept_name from loc a Join dept b USING (loc_id) where loc_id=10;

Joins with the ON clause:

Use the ON clause to specify a join condition. The ON clause makes the code easy to understand.
ON clause is equals to Self Joins. The ON clause can also be used to join columns that have different
names.

Left/ Right/ Full Outer Joins:

Left Outer Join displays all rows from the table that is Left to the LEFT OUTER JOIN clause, right
outer join displays all rows from the table that is right to the RIGHT OUTER JOIN clause, and full
outer join displays all rows from both the tables either left or right to the FULL OUTER JOIN clause.

Read these latest  SQL Interview Questions and Answers for Experienced  that help you grab high-paying jobs
PlSQL Developer Interview Questions
21) What is the difference between Entity, Attribute, and Tuple?

Entity: A significant thing about which some information is required.  For exp: EMPLOYEE (table).
Attribute: Something that describes the entity. For exp: empno, emp name, emp address
(columns). Tuple: A row in a relation is called Tuple.

22) What is a Transaction? Describe common errors that can occur while executing any
Transaction?

Transaction consists of a collection of DML statements that forms a logical unit of work.

The common errors that can occur while executing any transaction are:

The violation of constraints.

1. Data type mismatch.


2. Value too wide to fit in the column.
3. The system crashes or the Server gets down.
4. The session Killed.
5. Locking takes place. Etc.

23) What is locking in SQL? Describe its types?

Locking prevents destructive interaction between concurrent transactions. Locks held until Commit
or Rollback. Types of locking are: 

 Implicit Locking: This occurs for all SQL statements except SELECT.


 Explicit Locking: This can be done by the user manually.

Further, there are two locking methods:

1. Exclusive: Locks out other users


2. Share: Allows other users to access

24) What is the difference between Commit, Rollback, and Savepoint?

 COMMIT: Ends the current transaction by making all pending data changes permanent.
 ROLLBACK: Ends the current transaction by discarding all pending data changes.
 SAVEPOINT: Divides a transaction into smaller parts. You can roll back the transaction to a
particular named savepoint.

25) What are the advantages of COMMIT and ROLLBACK statements?

Advantages of COMMIT and ROLLBACK statements are:


 Ensure data consistency
 Can preview data changes before making changes permanent.
 Group logically related operations.

26) Describe naming rules for creating a Table?

Naming rules to be considered for creating a table is:

1. The table name must begin with a letter,


2. The table name can be 1-30 characters long,
3. Table name can contain only A-Z, a-z, 0-9,_, $, #.
4. The table name cannot duplicate the name of another object owned by the same user.
5. The table name cannot be an oracle server reserved word.

27) What is a DEFAULT option in a table?

A column can be given a default value by using the DEFAULT option. This option prevents null
values from entering the column if a row is inserted without a value for that column. The DEFAULT
value can be a literal, an expression, or a SQL function such as SYSDATE and USER but the value
cannot be the name of another column or a pseudo column such as NEXTVAL or CURRVAL.

28)  What is the difference between USER TABLES and DATA DICTIONARY?

 USER TABLES: This is a collection of tables created and maintained by the user. Contain USER
information.
 DATA DICTIONARY: This is a collection of tables created and maintained by the Oracle
Server. It contains database information. All data dictionary tables are owned by the SYS
user.

29)  Describe a few Data Types used in SQL?

Data Types is a specific storage format used to store column values. Few data types used in SQL
are:

1. VARCHAR2(size): Minimum size is ‘1’ and Maximum size is ‘4000’


2. CHAR(size): Minimum size is ‘1’and Maximum size is ‘2000’
3. NUMBER(P,S): " Precision" can range from 1 to 38 and the “Scale” can range from -84 to 127.
4. DATE
5. LONG: 2GB
6. CLOB: 4GB
7. RAW (size): Maximum size is 2000
8. LONG RAW: 2GB
9. BLOB: 4GB
10. BFILE: 4GB
11. ROWID: A 64 base number system representing the unique address of a row in the table.
30)  In what scenario you can modify a column in a table?

During modifying a column:

1. You can increase the width or precision of a numeric column.


2. You can increase the width of numeric or character columns.
3. You can decrease the width of a column only if the column contains null values or if the table
has no rows.
4. You can change the data type only if the column contains null values.
5. You can convert a CHAR column to the VARCHAR2 data type or convert a VARCHAR2 column
to the CHAR data type only if the column contains null values or if you do not change the
size. 

31) Describe a few restrictions on using the “LONG” data type?

A LONG column is not copied when a table is created using a subquery. A LONG column cannot be
included in a GROUP BY or an ORDER BY clause. Only one LONG column can be used per table. No
constraint can be defined on a LONG column.

32) What is a SET UNUSED option?

SET UNUSED option marks one or more columns as unused so that they can be dropped when the
demand on system resources is lower. Unused columns are treated as if they were dropped, even
though their column data remains in the table’s rows. After a column has been marked as unused,
you have no access to that column.

A select * query will not retrieve data from unused columns. In addition, the names and types of
columns marked unused will not be displayed during a DESCRIBE, and you can add to the table a
new column with the same name as an unused column. The SET UNUSED information is stored in
the USER_UNUSED_COL_TABS dictionary view.

33) What is the difference between Truncate and Delete?

The main difference between Truncate and Delete is as below:

SQL Truncate SQL Delete

TRUNCATE DELETE

Removes all rows from a table and releases storage Removes all rows from a table but does not release
space used by that table. storage space used by that table.

TRUNCATE Command is faster.  DELETE command is slower.


 
Is a DDL statement and cannot be Rollback.
Is a DDL statement and can be Rollback.

 
Database Triggers do not fire on TRUNCATE. Database Triggers fire on DELETE.

34) What is the main difference between CHAR and VARCHAR2?

CHAR pads blank spaces to a maximum length, whereas VARCHAR2 does not pad blank spaces.

35) What are Constraints? How many types of constraints are there?

Constraints are used to prevent invalid data entry or deletion if there are dependencies.
Constraints enforce rules at the table level. Constraints can be created either at the same time as
the table is created or after the table has been created. Constraints can be defined at the column or
table level. Constraint defined for a specific table can be viewed by looking at the USER-
CONSTRAINTS data dictionary table. You can define any constraint at the table level except NOT
NULL which is defined only at the column level. There are 5 types of constraints:

 Not Null Constraint


 Unique Key Constraint
 Primary Key Constraint
 Foreign Key Constraint
 Check Key Constraint.

36) Describe types of Constraints in brief?

 NOT NULL: NOT NULL Constraint ensures that the column contains no null values.
 UNIQUE KEY: UNIQUE Key Constraint ensures that every value in a column or set of columns
must be unique, that is, no two rows of a table can have duplicate values in a specified
column or set of columns. If the UNIQUE constraint comprises more than one column, that
group of columns is called a Composite Unique Key. There can be more than one Unique key
on a table. Unique Key Constraint allows the input of Null values. Unique Key automatically
creates an index on the column it is created.
 PRIMARY KEY: Uniquely identifies each row in the Table. Only one PRIMARY KEY can be
created for each table but can have several UNIQUE constraints. PRIMARY KEY ensures that
no column can contain a NULL value. A Unique Index is automatically created for a PRIMARY
KEY column. PRIMARY KEY is called a Parent key.
 FOREIGN KEY: This is also called Referential Integrity Constraint. FOREIGN KEY is one in
which a column or set of columns take references of the Primary/Unique key of the same or
another table. FOREIGN KEY is called a child key. A FOREIGN KEY value must match an
existing value in the parent table or be null.
 CHECK KEY: Defines a condition that each row must satisfy. A single column can have
multiple CHECK Constraints. During CHECK constraint following expressions is not allowed:

         1. References to CURRVAL, NEXTVAL, LEVEL, and ROWNUM Pseudo columns.

         2. Calls to SYSDATE, UID, USER, and USERENV Functions

37) What is the main difference between Unique Key and Primary Key?

The main difference between Unique Key and Primary Key is:

Unique Key Primary Key

A table can have more than one Unique Key. A table can have only one Primary Key.

The unique key column can store NULL values. The primary key column cannot store NULL values.

Uniquely identify each value in a column.  Uniquely identify each row in a table.

38) What is the difference between ON DELETE CASCADE and ON DELETE SET NULL?

ON DELETE CASCADE Indicates that when the row in the parent table is deleted, the dependent
rows in the child table will also be deleted. ON DELETE SET NULL Covert foreign key values to null
when the parent value is removed. Without the ON DELETE CASCADE or the ON DELETE SET NULL
options, the row in the parent table cannot be deleted if it is referenced in the child table.

39) What is a Candidate Key?

The columns in a table that can act as a Primary Key are called Candidate Key.

40)  What are Views and why they are used?

A View logically represents subsets of data from one or more tables. A View is a logical table based
on a table or another view. A View contains no data of its own but is like a window through which
data from tables can be viewed or changed. The tables on which a view is based are called Base
Tables. The View is stored as a SELECT statement in the data dictionary. View definitions can be
retrieved from the data dictionary table: USER_VIEWS.

Views are used:

 To restrict data access


 To make complex queries easy
 To provide data Independence
 Views provide groups of users to access data according to their requirements.
Pl SQL Interview Questions For Experienced
41) What is the difference between Simple and Complex Views?

The main differences between the two views are:

Simple View Complex View

Derives data from many tables.  Contain functions


Derives data from only one table.
or groups of data.

Derives data from many tables.  Contain functions


Contains no functions or group of data
or groups of data.

Does not always allow DML operations through the


Can perform DML operations through the view.
view

42) What are the restrictions of DML operations on Views?

Few restrictions of DML operations on Views are:

You cannot DELETE a row if the View contains the following:

1. Group Functions
2. A Group By clause
3. The Distinct Keyword
4. The Pseudo column ROWNUM Keyword.

You cannot MODIFY data in a View if it contains the following:

1. Group Functions
2. A Group By clause
3. The Distinct Keyword
4. The Pseudo column ROWNUM Keyword.
5. Columns defined by expressions (Ex; Salary * 12)

You cannot INSERT data through a view if it contains the following:

43)  What is PL/SQL, Why do we need PL/SQL instead of SQL, Describe your experience working
with PLSQL and What are the difficulties faced while working with PL SQL and How did you
overcome them?

1. PL/SQL is a procedural language extension with SQL Language.


2. Oracle introduced PL/SQL
3. It is a combination of SQL and Procedural Statements and used for creating applications.
4. Basically, PL/SQL is a block-structured programming language whenever we are submitting
PL/SQL
5. Blocks then all SQL statements are executing separately by using SQL engine and also all
procedure statements are executed separately.
6. Explain your current and previous projects along with your roles and responsibilities,
mention some of the challenging difficulties you've faced in your project while working with
PL/SQL. 

44) What are the different functionalities of a Trigger?

Trigger is also the same as stored procedure & also it will automatically be invoked whenever DML
operation performed against table or view.

There are two types of triggers supported by PL/SQL

1. Statement Level Trigger.


2. Row Level Trigger

Statement Level Trigger: In a statement-level trigger, the trigger body is executed only once for
the DML statement.

Row Level Trigger: In a row-level trigger, the trigger body is executed for each row DML
statement. It is the reason, we are employing each row clause and internally stored DML
transaction in trigger specification, these qualifiers: old, new, are also called records type variables.
These qualifiers are used in trigger specification & trigger body.

Syntax

old.column_name

Syntax            

new column_name

When we use these qualifiers in trigger specification then we are not allowed to use “:” in form of
the names of the qualifiers.

45) Write a PL/SQL Program that raises a user-defined exception on Thursday?

declare
a exception
begin
If to_char(sysdate, ‘DY)=’THU’
then
raise a;
end if;
exception
when a then
dbms_output.put_line(‘my exception raised on thursday’);
end
;

Output: my exception raised on Thursday

46) Write a PL/SQL program to retrieve the emp table and then display the salary?

declare
v_sal number(10);
begin select max(sal)intr v_sal;
from emp;
dbms_output.put_line(v.sal);
end;
/

(or)

declare
A number(10);
B number(10);
C number(10);
begin
a:=70;
b:=30;
c:=greatest+(a,b);
dbms_output.put_line(c);
end;
/

Output:70

47) Write a PL/SQL cursor program that is used to calculate total salary from emp table without
using sum() function?

Declare
cursor c1 is select sal from emp;
v_sal number(10);
n.number(10):=0;
begin
open c1;
loop
fetch c1 into v_sal;
exit when c1%not found;
n:=n+v_sal;
end loop;
dbms_output.put_line(‘tool salary is’||’ ‘ ||n);
close c1;
end;
/

Output: total salary is: 36975

48) Write a PL/SQL cursor program to display all employee names and their salary from the emp
table by using % not found attributes?

Declare
Cursor c1 is select ename, sal from emp;
v_ename varchar2(10);
v_sal number(10);
begin
open c1;
loop
fetch c1 into v_ename, v_sal;
exist when c1 % notfound;
dbms_output.put_line(v_name ||’ ‘||v_sal);
end loop;
close c1;
end;
/

49) What is a Mutating Trigger?

 Into a row-level trigger based on a table, the trigger body cannot read data from the same
table and also we cannot perform DML operation on the same table.
 If we are trying this oracle server returns mutating error oracle-4091: table is mutating.
 This error is called a mutating error, and this trigger is called a mutating trigger, and the
table is called a mutating table.
 Mutating errors are not occurred in statement-level trigger because through this statement-
level trigger when we are performing DML operations automatically data committed into the
database, whereas in the row-level trigger when we are performing transaction data is not
committed and also again we are reading this data from the same table then only mutating
errors is occurred.

50) What are Triggering Events (or) Trigger Predicate Clauses?

If we want to perform multiple operations in different tables then we must use triggering events
within the trigger body. These are inserting, updating, deleting clauses. These clauses are used in
the statement, row-level triggers. These triggers are also called trigger predicate clauses. 

→ Explore Oracle PL SQL Sample Resumes Download & Edit, Get Noticed by Top Employers! 

51) What is the Discard File?

 This file extension is .dsc

 Discard file we must specify within the control file by using the discard file clause.

 The discard file also stores reflected records based on when clause condition within the
control file. This condition must be satisfied in the table clause.

52) What is REF CURSOR (or) CURSOR VARIABLE (or) DYNAMIC CURSOR?

Oracle 7.2 introduced ref cursor, This is a user-defined type that is used to process multiple records
and also this is a record by record process.

In static cursor database servers execute only one select statement at a time for a single active set
area wherein ref cursor database servers execute a number of select statements dynamically for a
single active set area that's why those cursors are also called a dynamical cursor.

Generally, we are not allowed to pass static cursor as parameters to use subprograms whereas we
can also pass ref cursor as a parameter to the subprograms because basically precursor is a user-
defined type in oracle we can also pass all user-defined type as a parameter to the subprograms.

Generally, the static cursor does not return multiple records into the client application whereas the
ref cursor is allowed to return multiple records into the client application (Java, .Net, PHP, VB, C++).

This is a user-defined type so we are creating it in 2 steps process i.e first we are creating a type
then only we are creating a variable from that type that’s why this is also called a cursor variable.

53) What are The Types of Ref Cursors?

In all databases having 2 ref cursors.


1. Strong ref cursor
2. Weak ref cursor

A strong ref cursor is a ref cursor that has a return type, whereas a weak ref cursor has no return
type.

Syntax:

Type typename is ref cursor return record type data type;


Variable Name typename

Syntax

Type typename is ref cursor


Variable Name typename;

In the Weak ref cursor, we must specify a select statement by using open for clause this clause is
used in the executable section of the PL/SQL block.

Syntax:

Open ref cursor varname for SELECT * FROM table_name condition;

54) What is the Difference Between the trim, delete collection methods?

SQL> declare
type t1 is table of number(10);
v_t t1;=t1(10,20,30,40,50,60);

beign

v_t.trim(2);
dbms_output.put_line(‘after deleting last two elements’);

for i in v_t.first.. V_t.last


loop
dbms_output.put_line(v_t(i));
End loop;

vt.delete(2);
dbms_output.put_line(‘after deleting second element;);

for i in v_t.first..v_t.last
loop

If v_t.exists(i) then
dbms_output.put_line(v_t(i));

end if;
end loop;
end;
/

55) What are Overloading Procedures?

Overload refers to the same name that can be used for a different purpose, in oracle we can also
implement an overloading procedure through the package. Overloading procedure having the
same name with different types or different numbers of parameters.

56) What are the Global Variables?

In oracle, we are declaring global variables in Package Specification only.

57) What is Forward Declaration?

In oracle declaring procedures within the package body are called forward declaring generally
before we are calling private procedures into public procedure first we must implement private
procedure within body otherwise use a forward declaration within the package body.

58) What is Invalid_number, Value_Error?

In oracle when we try to convert “string type to number type” or” data string into data type” then
the oracle server returns two types of errors.

1. Invalid_number
2. Value_error (or) numeric_error

Invalid_number:

When PL/SQL block has a SQL statement and also those SQL statements try to convert string type
to number type or data string into data type then oracle server returns an error: ora-1722-Invalid
Number

For handling this error oracle provides number exception Invalid_number exception name.

Example:

begin
Insert
intoemp(empno, ename, sal)
values(1,’gokul’, ‘abc’)
exception when invalid_number then dbms_output.put_line(‘insert proper data only’);
end;/

value_error:

Whenever PL/SQL block having procedural statements and also those statements find to convert
string type to number type then oracle servers return an error: ora-6502: numeric or value error:
character to a number conversion error
For handling, this error oracle provided exception value_error exception name

Example:

begin
declare z number(10);
begin
z:= ‘&x’ + ‘&y’;
dbms_output.put_line(z);
exception when value_error then dbms_output.put_line(‘enter numeric data value for x
& y only’);
end;/

 Output:

Enter value for x:3


Enter value for y:2
z:=5

Enter value for x:a


Enter value for y:b

Error:enter numeric data value for x & y only.

59) What is Flashback Query?

 Flashback query is handled by the Database Administrator the only flashback queries along
to allow the content of the table to be retrieved with reference to the specific point of time
by using as of clause that is flashback queries retrieves accidental data after committing the
transaction also.
 Flashback queries generally use undo file that is flashback queries retrieve old data before
committing the transaction oracle to provide two methods for flashback queries

         Method1: using the timestamp


         Method2: using scn number

60) Explain what PL/SQL package consists of?

PL/SQL consists of two major parts, they are package specification and package body.

1. Package specification: it acts as a public interface for your application which includes
procedures, types, etc.
2. Package Body: It contains the code required to implement the Package Specification  

61) Explain what the benefits of PL/SQL Packages are?  

These are the benefits of PL/SQL Packages

 We can store functions and procedures in a single unit called a package.


 Packages provide security to grant privileges.
 Functions and procedures, within the package, shares a common variable among them.  
 Packages support even if the functions are overloaded.
 Packages enhance the performance even when multiple objects loaded into memory.

62) Explain different methods to trace the PL/SQL code?

Tracing code is a necessary technique to test the performance of the code during runtime.  We
have different methods in PL/SQL to trace the code, which are,

 DBMS_ TRACE
 DBMS_ APPLICATION_INFO
 Tkproof utilities and trcsess  
 DBMS_SESSION and DBMS_MONITOR

63) What does it mean by PL/SQL Cursors?

In  PL/SQL to retrieve and process more, it requires a special resource, and that resource is known
as Cursor. A cursor is defined as a pointer to the context area. The context area is an area of
memory that contains information and SQL statements for processing the statements.    

64) What is the difference between Implicit and Explicit Cursors?

An implicit cursor used in PL/SQL to declare, all SQL data manipulation statements. An implicit
cursor is used to declare SQL statements such as open, close, fetch, etc.
An explicit cursor is a cursor and which is explicitly designed to select the statement with the help
of a cursor. This explicit cursor is used to execute the multirow select function. An explicit function
is used PL/SQL to execute tasks such as update, insert, delete, etc.

65) What is a trigger?

It is a program in PL/SQL, stored in the database, and executed instantly before or after the
UPDATE, INSERT and DELETE commands.          

66) What are the uses of database triggers?

Triggers are programs that are automatically fired or executed when some events happen and are
used for:

 To implement complex security authorizations.


 To drive column values.
 To maintain duplicate tables.
 To implement complex business rules.
 To bring transparency in log events. 

67) Name the two exceptions in PL/SQL?

Error handling part of PL/SQL is called an exception.  We have two types of exceptions, and they are
User-defined and predefined.

Related article: Error Handling in SQL Server

68) Which command is used to delete the package?

To delete the ‘Package’ in PL/SQL we use the DROP PACKAGE command.

69) what is the process for PL/SQL compilation?

The compilation process consists of syntax check, bind, and p-code generation. It checks the errors
in PL/SQL code while compiling. Once all errors are corrected, a storage address allocated to a
variable that stores this data. This process is called binding. P-Code consists of a list of rules for the
PL/SQL engine. It is stored in the database and triggered when the next time it is used.

References: Stackoverflow | Scribd | Slideshare

If inserting then stmts;


else if updating then stmts;
else if deleting then stmts;
end if;
PL/SQL stands for Procedural Language extensions to SQL (Structured Query Language ). It was
created by Oracle in order to overcome the disadvantages of SQL for easier building and handling
of critical applications in a comprehensive manner.
Following are the disadvantages of SQL:

 There is no provision of decision-making, looping, and branching in SQL.


 Since the SQL statements get passed to the Oracle engine all at the same time, the speed of execution
decreases due to the nature of increased traffic.
 There is no feature of error checking while manipulating the data.

PL/SQL was introduced to overcome the above disadvantages by retaining the power of SQL and
combining it with the procedural statements. It is developed as a block-structured language and the
statements of the block are passed to the oracle engine which helps to increase the speed of
processing due to the decrease in traffic.

Crack your next tech interview with confidence!

Take a free mock interview, get instant⚡️feedback and recommendation💡

Attempt Now

Events|Powered By

 Software Dev

 Data Science
 All Events

Roadmap to an SDE career at PayTm

Starts on: 07:00 PM, 13 July 2023 (IST)

2000+ Registered

Know More

Register Now

Java Concurrency and Multi-threading

Starts on: 05:00 PM, 16 July 2023 (IST)

2000+ Registered

Know More
Register Now

Roadmap to Become a Full-Stack Developer

Starts on: 08:00 PM, 18 July 2023 (IST)

2000+ Registered

Know More

Register Now

Roadmap to an SDE career at Fintech Companies

Starts on: 07:00 PM, 19 July 2023 (IST)

2000+ Registered

Know More

Register Now

Monolithic and Microservices architecture

Starts on: 08:00 PM, 20 July 2023 (IST)

2000+ Registered

Know More

Register Now

View All
PL/SQL Basic Interview Questions
1. What are the features of PL/SQL?

Following are the features of PL/SQL:

 PL/SQL provides the feature of decision making, looping, and branching by making use of its
procedural nature.
 Multiple queries can be processed in one block by making use of a single command using PL/SQL.
 The PL/SQL code can be reused by applications as they can be grouped and stored in databases as
PL/SQL units like functions, procedures, packages, triggers, and types.
 PL/SQL supports exception handling by making use of an exception handling block.
 Along with exception handling, PL/SQL also supports error checking and validation of data before
data manipulation.
 Applications developed using PL/SQL are portable across computer hardware or operating system
where there is an Oracle engine.

2. What do you understand by PL/SQL table?

 PL/SQL tables are nothing but objects of type tables that are modeled as database tables. They are a
way to provide arrays that are nothing but temporary tables in memory for faster processing.
 These tables are useful for moving bulk data thereby simplifying the process.

3. Explain the basic structure followed in PL/SQL?

 The basic structure of PL/SQL follows the BLOCK structure. Each PL/SQL code comprises SQL and
PL/SQL statement that constitutes a PL/SQL block.
 Each PL/SQL block consists of 3 sections:
o The optional Declaration Section
o The mandatory Execution Section
o The optional Exception handling Section

[DECLARE]
--declaration statements (optional)
BEGIN
--execution statements
[EXCEPTION]
--exception handling statements (optional)
END;
You can download a PDF version of Pl Sql Interview Questions.

Download PDF
4. What is a PL/SQL cursor?

 A PL/SQL cursor is nothing but a pointer to an area of memory having SQL statements and the
information of statement processing. This memory area is called a context area. This special area
makes use of a special feature called cursor for the purpose of retrieving and processing more than
one row.
 In short, the cursor selects multiple rows from the database and these selected rows are individually
processed within a program.
 There are two types of cursors:
o Implicit Cursor:
 Oracle automatically creates a cursor while running any of the commands - SELECT
INTO, INSERT, DELETE or UPDATE implicitly.
 The execution cycle of these cursors is internally handled by Oracle and returns the
information and status of the cursor by making use of the cursor attributes-
ROWCOUNT, ISOPEN, FOUND, NOTFOUND.
o Explicit Cursor:
 This cursor is a SELECT statement that was declared explicitly in the declaration block.
 The programmer has to control the execution cycle of these cursors starting from
OPEN to FETCH and close.
 The execution cycle while executing the SQL statement is defined by Oracle along
with associating a cursor with it.
 Explicit Cursor Execution Cycle:
o Due to the flexibility of defining our own execution cycle, explicit cursors are used in many
instances. The following diagram represents the execution flow of an explicit cursor:

 Cursor Declaration:
o The first step to use an explicit cursor is its declaration.
o Declaration can be done in a package or a block.
o Syntax: CURSOR cursor_name IS query; where cursor_name is the name of the cursor, the query
is the query to fetch data from any table.
 Open Cursor:
o Before the process of fetching rows from cursor, the cursor has to be opened.
o Syntax to open a cursor: OPEN cursor_name;
o When the cursor is opened, the query and the bind variables are parsed by Oracle and the
SQL statements are executed.
o The execution plan is determined by Oracle and the result set is determined after associating
the cursor parameters and host variables and post these, the cursor is set to point at the first
row of the result set.
 Fetch from cursor:
o FETCH statement is used to place the content of the current row into variables.
o Syntax: FETCH cursor_name INTO variable_list;
o In order to get all the rows of a result set, each row needs to be fetched.
 Close Cursor:
o Once all the rows are fetched, the cursor needs to be closed using the CLOSE statement.
o Syntax: CLOSE cursor_name;
o The instructions tell Oracle to release the memory allocated to the cursor.
 Cursors declared in procedures or anonymous blocks are by default closed post their
execution.
 Cursors declared in packages need to be closed explicitly as the scope is global.
 Closing a cursor that is not opened will result in INVALID_CURSOR exception.

5. What is the use of WHERE CURRENT OF in cursors?

 We use this clause while referencing the current row from an explicit cursor. This clause allows
applying updates and deletion of the row currently under consideration without explicitly referencing
the row ID.
 Syntax:
UPDATE table_name SET field=new_value WHERE CURRENT OF cursor_name

6. How can a name be assigned to an unnamed PL/SQL Exception Block?

 This can be done by using Pragma called EXCEPTION_INIT.


 This gives the flexibility to the programmer to instruct the compiler to provide custom error
messages based on the business logic by overriding the pre-defined messages during the compilation
time.
 Syntax:

DECLARE
exception_name EXCEPTION;
PRAGMA EXCEPTION_INIT (exception_name, error_code);
BEGIN
// PL/SQL Logic
EXCEPTION
WHEN exception_name THEN
// Steps to handle exception
END;

7. What is a Trigger? Name some instances when “Triggers” can be used.

 As the name indicates, ‘Trigger’ means to ‘activate’ something. In the case of PL/SQL, a trigger is a
stored procedure that specifies what action has to be taken by the database when an event related to
the database is performed.
 Syntax:

TRIGGER trigger_name
trigger_event
[ restrictions ]
BEGIN
actions_of_trigger;
END;

In the above syntax, if the trigger_name the trigger is in the enabled state, the trigger_event causes the
database to fire actions_of_trigger if the restrictions are TRUE or unavailable.

 They are mainly used in the following scenarios:


o In order to maintain complex integrity constraints.
o For the purpose of auditing any table information.
o Whenever changes are done to a table, if we need to signal other actions upon completion of
the change, then we use triggers.
o In order to enforce complex rules of business.
o It can also be used to prevent invalid transactions.
 You can refer https://docs.oracle.com/database/121/TDDDG/tdddg_triggers.htm  for more
information regarding triggers.

8. When does a DECLARE block become mandatory?

 This statement is used by anonymous blocks of PL/SQL such as non-stored and stand-alone
procedures. When they are being used, the statement should come first in the stand-alone file.

9. How do you write comments in a PL/SQL code?

 Comments are those sentences that have no effect on the functionality and are used for the purpose
of enhancing the readability of the code. They are of two types:
o Single Line Comment: This can be created by using the symbol -- and writing what we want to
mention as a comment next to it.
o Multi-Line comment: These are the comments that can be specified over multiple lines and
the syntax goes like /* comment information */
 Example:

SET SERVEROUTPUT ON;


DECLARE

-- Hi There! I am a single line comment.


var_name varchar2(40) := 'I love PL/SQL' ;
BEGIN
/*
Hi! I am a multi line
comment. I span across
multiple lines
*/
dbms_output.put_line(var_name);
END;
/
Output:
I love PL/SQL

10. What is the purpose of WHEN clause in the trigger?

 WHEN clause specifies for what condition the trigger has to be triggered.

PL/SQL Intermediate Interview Questions


11. Can you explain the PL/SQL execution architecture?

The PL/SQL engine does the process of compilation and execution of the PL/SQL blocks and
programs and can only work if it is installed on an Oracle server or any application tool that
supports Oracle such as Oracle Forms.

 PL/SQL is one of the parts of Oracle RDBMS, and it is important to know that most of the Oracle
applications are developed using the client-server architecture. The Oracle database forms the
server-side and requests to the database form a part of the client-side.
 So based on the above fact and the fact that PL/SQL is not a standalone programming language, we
must realize that the PL/SQL engine can reside in either the client environment or the server
environment. This makes it easy to move PL/SQL modules and sub-programs between server-side
and client-side applications.
 Based on the architecture shown below, we can understand that PL/SQL engine plays an important
role in the process and execute the PL/SQL statements and whenever it encounters the SQL
statements, they are sent to the SQL Statement Processor.
 Case 1: PL/SQL engine is on the server: In this case, the whole PL/SQL block gets passed to the
PL/SQL engine present on the Oracle server which is then processed and the response is sent.
 Case 2: PL/SQL engine is on the client: Here the engine lies within the Oracle Developer tools and
the processing of the PL/SQL statements is done on the client-side.
o In case, there are any SQL statements in the PL/SQL block, then they are sent to the Oracle
server for SQL processing.
o When there are no SQL statements, then the whole block processing occurs at the client-side.

12. Why is SYSDATE and USER keywords used?

 SYSDATE:
o This keyword returns the current time and date on the local database server.
o The syntax is SYSDATE.
o In order to extract part of the date, we use the TO_CHAR function on SYSDATE and specify
the format we need.
o Usage:
 SELECT SYSDATE FROM dual;
 SELECT id, TO_CHAR(SYSDATE, 'yyyy/mm/dd') from InterviewBitEmployeeTable where
customer_id < 200;
 USER:
o This keyword returns the user id of the current session.
o Usage:
 SELECT USER FROM dual;

13. Differentiate between implicit cursor and explicit cursor.


Implicit Cursor  Explicit Cursor

An implicit cursor is used when a query When a subquery returns more than one row, an explicit cursor
returns a single row value. is used. These rows are called Active Set.
Implicit Cursor  Explicit Cursor

This is used for all DML operations like


This is used to process Multirow SELECT Statements.
DECLARE, OPEN, FETCH, CLOSE.

NO_DATA_FOUND Exception is handled


NO_DATA_FOUND cannot be handled here.
here.

14. Differentiate between SQL and PL/SQL.


SQL PL/SQL

SQL is a natural language meant for the interactive


PL/SQL is a procedural extension of SQL.
processing of data in the database.

PL/SQL supports all features of procedural


Decision-making and looping are not allowed in SQL. language such as conditional and looping
statements.

All SQL statements are executed at a time by the database PL/SQL statements are executed one block at a
server which is why it becomes a time-consuming process. time thereby reducing the network traffic.

There is no error handling mechanism in SQL. This supports an error handling mechanism.

15. What is the importance of %TYPE and %ROWTYPE data types in PL/SQL?

 %TYPE: This declaration is used for the purpose of anchoring by providing the data type of any
variable, column, or constant. It is useful during the declaration of a variable that has the same data
type as that of its table column.
o Consider the example of declaring a variable named ib_employeeid which has the data type
and its size same as that of the column employeeid in table ib_employee. 
The syntax would be : ib_employeeid ib_employee.employeeid%TYPE;
 %ROWTYPE: This is used for declaring a variable that has the same data type and size as that of a
row in the table. The row of a table is called a record and its fields would have the same data types
and names as the columns defined in the table.
o For example: In order to declare a record named ib_emprecord for storing an entire row in a
table called ib_employee, the syntax is:
ib_emprecord ib_employee%ROWTYPE;

16. What are the various functions available for manipulating the character data?

 The functions that are used for manipulating the character data are called String Functions.
o LEFT: This function returns the specified number of characters from the left part of a string.
 Syntax: LEFT(string_value, numberOfCharacters).
 For example, LEFT(‘InterviewBit’, 9) will return ‘Interview’.
o RIGHT: This function returns the defined number of characters from the right part of a string.
 Syntax: RIGHT(string_value, numberOfCharacters)
 For example, RIGHT(‘InterviewBit’,3) would return ‘Bit’.
o SUBSTRING: This function would select the data from a specified start position through the
number of characters defined from any part of the string.
 Syntax: SUBSTRING(string_value, start_position, numberOfCharacters)
 For example, SUBSTRING(‘InterviewBit’,2,4) would return ‘terv’.
o LTRIM: This function would trim all the white spaces on the left part of the string.
 Syntax: LTRIM(string_value)
 For example, LTRIM(’ InterviewBit’) will return ‘InterviewBit’.
o RTRIM: This function would trim all the white spaces on the right part of the string.
 Syntax: RTRIM(string_value)
 For example, RTRIM('InterviewBit ') will return ‘InterviewBit’.
o UPPER: This function is used for converting all the characters to the upper case in a string.
 Syntax: UPPER(string_variable)
 For example, UPPER(‘interviewBit’) would return ‘INTERVIEWBIT’.
o LOWER: This function is used for converting all the characters of a string to lowercase.
 Syntax: LOWER(string_variable)
 For example, LOWER(‘INterviewBit’) would return ‘interviewbit’.

17. What is the difference between ROLLBACK and ROLLBACK TO statements in PL/SQL?

 ROLLBACK command is used for rolling back all the changes from the beginning of the transaction.
 ROLLBACK TO command is used for undoing the transaction only till a SAVEPOINT. The
transactions cannot be rolled back before the SAVEPOINT and hence the transaction remains active
even before the command is specified.

18. What is the use of SYS.ALL_DEPENDENCIES?

 SYS.ALL_DEPENDENCIES is used for describing all the dependencies between procedures, packages,
triggers, functions that are accessible to the current user. It returns the columns like name,
dependency_type, type, referenced_owner etc.

19. What are the virtual tables available during the execution of the database trigger?

 The THEN and NOW tables are the virtual tables that are available during the database trigger
execution. The table columns are referred to as THEN.column and NOW.column respectively.
 Only the NOW.column is available for insert-related triggers.
 Only the THEN.column values are available for the DELETE-related triggers.
 Both the virtual table columns are available for UPDATE triggers.

20. Differentiate between the cursors declared in procedures and the cursors declared in the package
specifications.

 The cursors that are declared in the procedures will have the local scope and hence they cannot be
used by other procedures.
 The cursors that are declared in package specifications are treated with global scope and hence they
can be used and accessed by other procedures.

PL/SQL Advanced Interview Questions


21. What are COMMIT, ROLLBACK and SAVEPOINT statements in PL/SQL?

 These are the three transaction specifications that are available in PL/SQL.
 COMMIT: Whenever any DML operations are performed, the data gets manipulated only in the
database buffer and not the actual database. In order to save these DML transactions to the
database, there is a need to COMMIT these transactions.
o COMMIT transaction action does saving of all the outstanding changes since the last commit
and the below steps take place:
 The release of affected rows.
 The transaction is marked as complete.
 The details of the transaction would be stored in the data dictionary.
o Syntax: COMMIT;
 ROLLBACK: In order to undo or erase the changes that were done in the current transaction, the
changes need to be rolled back. ROLLBACK statement erases all the changes since the last COMMIT.
o Syntax: ROLLBACK;
 SAVEPOINT: This statement gives the name and defines a point in the current transaction process
where any changes occurring before that SAVEPOINT would be preserved whereas all the changes
after that point would be released.
o Syntax: SAVEPOINT <savepoint_name>;

22. How can you debug your PL/SQL code?

 We can use DBMS_OUTPUT and DBMS_DEBUG statements for debugging our code:
o DBMS_OUTPUT prints the output to the standard console.
o DBMS_DEBUG prints the output to the log file.

23. What is the difference between a mutating table and a constraining table?

 A table that is being modified by the usage of the DML statement currently is known as a mutating
table. It can also be a table that has triggers defined on it.
 A table used for reading for the purpose of referential integrity constraint is called a constraining
table.

24. In what cursor attributes the outcomes of DML statement execution are saved?

 The outcomes of the execution of the DML statement is saved in the following 4 cursor attributes:
o SQL%FOUND: This returns TRUE if at least one row has been processed.
o SQL%NOTFOUND: This returns TRUE if no rows were processed.
o SQL%ISOPEN: This checks whether the cursor is open or not and returns TRUE if open.
o SQL%ROWCOUNT: This returns the number of rows processed by the DML statement.

25. Is it possible to declare column which has the number data type and its scale larger than the precision?
For example defining columns like: column name NUMBER (10,100), column name NUMBER (10,-84)

 Yes, these type of declarations are possible.


 Number (9, 12) indicates that there are 12 digits after decimal point. But since the maximum
precision is 9, the rest are 0 padded like 0.000999999999.
 Number (9, -12) indicates there are 21 digits before the decimal point and out of that there are 9
possible digits and the rest are 0 padded like 999999999000000000000.0

PL/SQL Programs
26. Write a PL/SQL program using WHILE loop for calculating the average of the numbers entered by user.
Stop the entry of numbers whenever the user enters the number 0.
DECLARE
n NUMBER;
average NUMBER :=0 ;
sum NUMBER :=0 ;
count NUMBER :=0 ;
BEGIN
-- Take input from user
n := &input_number;
WHILE(n<>0)
LOOP
-- Increment count to find total elements
count := count+1;
-- Sum of elements entered
sum := sum+n;
-- Take input from user
n := &input_number;
END LOOP;
-- Average calculation
average := sum/count;
DBMS_OUTPUT.PUT_LINE(‘Average of entered numbers is ’||average);
END;

27. Write a PL/SQL procedure for selecting some records from the database using some parameters as
filters.

 Consider that we are fetching details of employees from ib_employee table where salary is a
parameter for filter.

CREATE PROCEDURE get_employee_details @salary nvarchar(30)


AS
BEGIN
SELECT * FROM ib_employee WHERE salary = @salary;
END;

28. Write a PL/SQL code to count the number of Sundays between the two inputted dates.
--declare 2 dates of type Date
DECLARE
start_date Date;
end_date Date;
sundays_count Number:=0;
BEGIN
-- input 2 dates
start_date:='&input_start_date';
end_date:='&input_end_date';
/*
Returns the date of the first day after the mentioned date
and matching the day specified in second parameter.
*/
start_date:=NEXT_DAY(start_date-1, 'SUNDAY');
--check the condition of dates by using while loop.
while(start_date<=end_date)
LOOP
sundays_count:=sundays_count+1;
start_date:=start_date+7;
END LOOP;

-- print the count of sundays


dbms_output.put_line('Total number of Sundays between the two dates:'||sundays_count);
END;
/

Input:
start_date = ‘01-SEP-19’
end_date = ‘29-SEP-19’
Output:
Total number of Sundays between the two dates: 5

29. Write PL/SQL code block to increment the employee’s salary by 1000 whose employee_id is 102 from
the given table below.
EMPLOYEE_I PHONE_NUMBE
FIRST_NAME LAST_NAME EMAIL_ID JOIN_DATE JOB_ID  SALARY
D R

AD_PRE 24000.0
100 ABC DEF abef 9876543210 2020-06-06 
S 0

17000.0
101 GHI JKL ghkl 9876543211  2021-02-08 AD_VP
0

17000.0
102 MNO  PQR mnqr 9876543212 2016-05-14 AD_VP
0

103 STU VWX stwx 9876543213 2019-06-24 IT_PROG 9000.00

DECLARE
employee_salary NUMBER(8,2);

PROCEDURE update_salary (
emp NUMBER,
salary IN OUT NUMBER
) IS
BEGIN
salary := salary + 1000;
END;

BEGIN
SELECT salary INTO employee_salary
FROM ib_employee
WHERE employee_id = 102;

DBMS_OUTPUT.PUT_LINE
('Before update_salary procedure, salary is: ' || employee_salary);

update_salary (100, employee_salary);

DBMS_OUTPUT.PUT_LINE
('After update_salary procedure, salary is: ' || employee_salary);
END;
/

Result:

Before update_salary procedure, salary is: 17000


After update_salary procedure, salary is: 18000

30. Write a PL/SQL code to find whether a given string is palindrome or not.
DECLARE
-- Declared variables string, letter, reverse_string where string is the original string.
string VARCHAR2(10) := 'abccba';
letter VARCHAR2(20);
reverse_string VARCHAR2(10);
BEGIN
FOR i IN REVERSE 1..LENGTH(string) LOOP
letter := SUBSTR(string, i, 1);
-- concatenate letter to reverse_string variable
reverse_string := reverse_string ||''||letter;
END LOOP;
IF reverse_string = string THEN
dbms_output.Put_line(reverse_string||''||' is palindrome');
ELSE
dbms_output.Put_line(reverse_string ||'' ||' is not palindrome');
END IF;
END;

31. Write PL/SQL program to convert each digit of a given number into its corresponding word format.
DECLARE
-- declare necessary variables
-- num represents the given number
-- number_to_word represents the word format of the number
-- str, len and digit are the intermediate variables used for program execution
num INTEGER;
number_to_word VARCHAR2(100);
digit_str VARCHAR2(100);
len INTEGER;
digit INTEGER;
BEGIN
num := 123456;
len := LENGTH(num);
dbms_output.PUT_LINE('Input: ' ||num);
-- Iterate through the number one by one
FOR i IN 1..len LOOP
digit := SUBSTR(num, i, 1);
-- Using DECODE, get the str representation of the digit
SELECT Decode(digit, 0, 'Zero ',
1, 'One ',
2, 'Two ',
3, 'Three ',
4, 'Four ',
5, 'Five ',
6, 'Six ',
7, 'Seven ',
8, 'Eight ',
9, 'Nine ')
INTO digit_str
FROM dual;
-- Append the str representation of digit to final result.
number_to_word := number_to_word || digit_str;
END LOOP;
dbms_output.PUT_LINE('Output: ' ||number_to_word);
END;

Input: 12345
Output: One Two Three Four Five

32. Write PL/SQL program to find the sum of digits of a number.


DECLARE
--Declare variables num, sum_of_digits and remainder of datatype Integer
num INTEGER;
sum_of_digits INTEGER;
remainder INTEGER;
BEGIN
num := 123456;
sum_of_digits := 0;
-- Find the sum of digits until original number doesnt become null
WHILE num <> 0 LOOP
remainder := MOD(num, 10);
sum_of_digits := sum_of_digits + remainder;
num := TRUNC(num / 10);
END LOOP;
dbms_output.PUT_LINE('Sum of digits is '|| sum_of_digits);
END;

Input: 9874
Output: 28

PL/SQL Conclusion
33. PL SQL Interview

Best PL/SQL Interview Questions & Answers

Below are some common basic and advanced pl/sql interview questions and answers
which are asked in the interview by the interviewer

Question: What is PL/SQL?

Answer: A procedural language where code can be executed in blocks. It is an extension


of SQL.

Question: What are the differences between PL/SQL and SQL?

Answer:

SQL PL/SQL

SQL is a query language to interact with the It is an extension of SQL which supports procedures,
database. functions and many more features.

Can perform complex tasks like a high-level programming


Supports only simple queries that can
language, for example, while loop, if-else statements,
perform insert, update, delete on tables.
etc…

SQL statements can be executed only one at a The entire block of statements is sent to the database
time, thereby making it a time-consuming server at once to be executed, saving time and increasing
process. efficiency.
No provision for error handling. Customized error handling is possible.

Question: What is the basic structure of PL/SQL?

Answer:

[DECLARE]

--declaration statements (optional)

BEGIN

--execution statements

[EXCEPTION]

--exception handling statements

END;

Question: Define cursor and its use.

Answer: A cursor is a pointer to a memory area assigned by Oracle to process SQL


statements. The cursor is used to hold records returned by the SQL query. There are 2
types of cursors – implicit and explicit.

Question: Why do we use database triggers? Give the syntax of a trigger.

Answer: The trigger is a stored procedure that is automatically invoked when an event


happens. The event could be: insert, update, delete, etc… Syntax –

create trigger [trigger_name]

[before | after]

on [table_name]

[for each row]

[trigger_body]
Question: How do you compile PL/SQL code?

Answer: Firstly, the syntax check is performed. When the developer corrects any syntax
errors, Oracle binds all the variables holding data with a storage address. Finally, the p-
code generation process takes place.

Question: Explain exception handling in PL/SQL.

Answer: PL/SQL offers customized exception handling. When an error occurs, an error


handling code is included in the program itself. There are 3 types of exceptions –

 Pre-defined exceptions – common errors that are already defined. Example –


NO_DATA_FOUND
 Undefined exceptions – the errors that do not have predefined names.
 User-defined exceptions – handled by the code written by the user.

Question: Tell about a few data types in PL/SQL.

Answer: There are many data types –

 Scalar types – primitive data types like CHAR, DATE, LONG, VARCHAR2 etc…
 Composite – these are made up of other data types and can be easily updated.
Example, RECORD, TABLE etc…
 Reference data types like CURSOR
 Large object types – BLOB, CLOB etc…

Question: What is the difference between %TYPE and %ROWTYPE? Give an


example.

Answer:

%TYPE %ROWTYPE

The attribute that declares a variable of type RECORD


The attribute that declares a variable of the having the same structure as a table row. The row is the
same data type as of a table column. RECORD that contains fields having the same data
types and names as the columns of a table or view.

Example – Example –

DECLARE DECLARE
studentId stud_rec

students.student_id%TYPE; students.%ROWTYPE;

Question: What constitutes a PL/SQL package?

Answer: Packages are schema objects that place functions, procedures, variables, etc… in
one place. Packages should have –

 Package specifications
 Package body

Question: List some schema objects that are created using PL/SQL.

Answer: Database links, triggers, stored procedures, functions and packages, views,


synonyms, external procedure libraries, sequences, etc…

Check here: Basic SQL Commands Checklist

Question: What are the various predefined exceptions?

Answer: Predefined exceptions are internally defined exceptions that occur during the
execution of a program. For example, PL/SQL raises NO_DATA_FOUND when there are no
rows returned upon a select operation, and if more than one row is returned using a
select statement, TOO_MANY_ROWS error is generated. Some more examples:

 COLLECTION_IS_NULL: when a collection is null


 CURSOR_ALREADY_OPEN: When a cursor is already open
 LOGIN_DENIED: When login is incorrect or permission is not there

For the complete list of predefined exceptions, check out Oracle docs.

Question: What is the difference between syntax and runtime errors?

Answer:

Syntax error Runtime error

These are compile-time errors found These are not detected by the compiler and cause the program
by the compiler. to give an incorrect result.

The code doesn't build and run until The code is compiled and run, and if an error occurs, the
these issues are resolved. program stops halfway.

Some examples are missing


Examples are null pointer exceptions, dividing a number by zero,
semicolons or brackets (;, {}), incorrect
array index out of bounds, etc.
spelling of classes, keywords etc.

int x = 9 String name = null; In the first String name = null; if(name.equals(“hackr.io”)){….} Since name is
line, a semicolon is missing which the null, the exception will be caught during runtime when the code
compiler will catch is executed

Question: What are the various packages available for PL-SQL Developers?

Answer: The several packages available for PL/SQL developers are:

alert an application using triggers when particular database values change. The alerts
DBMS_ALERT
are transaction-based and asynchronous.

DBMS_OUTPU display output from PL/SQL blocks, packages, subprograms and triggers. Mostly used for
T displaying PL/SQL debugging information.

different sessions communicate over named pipes using this package. The procedures
DBMS_PIPE PACK_MESSAGE and SEND_MESSAGE pack a message into a pipe, then send it to
another session.

HTF and HTP allow PL/SQL programs to generate HTML tags.

UTL_FILE lets PL/SQL programs read and write OS text files.

allows your PL/SQL programs to make hypertext transfer protocol (HTTP) callouts. The
package has two entry points, each of which accepts a URL (uniform resource locator)
UTL_HTTP
string, contacts the specified site, and returns the requested data, which is usually in
HTML format.

UTL_SMTP allows PL/SQL programs to send emails over SMTP.

Source: Oracle docs

Question: Explain Character Functions?

Answer: Character functions are functions that manipulate character data. These are
more popularly called as string functions. Example:

Returns the mentioned number of characters from left of a


LEFT LEFT('Hackr', 4) will give Hack.
string. LEFT(value, NoOfChars). Example

return specified number of characters from right.


RIGHT RIGHT('banker', 2) will return er.
RIGHT(value, NoOfChars). Example

SUBSTRIN selects data from any part of the string. SUBSTRING(value, SUBSTRING('hackr.io',0,4ll
G StartPosition, NoOfChars). Example return hackr.

LTRIM(' hackr.io') will
LTRIM trims white spaces from the left. Example
return hackr.io.

RTRIM('hackr.io ') will
RTRIM trims white spaces from the right. Example
return hackr.io.

UPPER('hackr.io')
UPPER converts all the characters to uppercase. Example
returns HACKR.IO.

LOWER('HACKR.IO')
LOWER converts all the characters to lowercase. Example
returns hackr.io.

Question: What is the use of SYSDATE and USER keywords? Explain with
examples.

Answer: SYSDATE: returns the current date and time on the local database server. The
syntax is SYSDATE. If we have to extract part of the date, then we use the TO_CHAR
function. Examples:

SELECT SYSDATE FROM dual;


select customer_id, TO_CHAR(SYSDATE, 'yyyy/mm/dd') from customer where
customer_id < 200;

USER: USER returns the user_id of the current session.

Example:

select USER from dual;

Question: What is the difference between SGA and PGA?

Answer:

SGA PGA

System Global Area Program Global Area

Contains data and control information for one Contains data and control information exclusively for
Oracle database instance a single Oracle process

Shared memory region for components Non-shared memory region

example: cached data blocks and SQL areas Example: session memory, SQL work area
Question: Explain the uses of Merge with Syntax in PL-SQL.

Answer: Merge reduces the number of table scans and performs parallel operations if
required. MERGE inserts or updates data conditionally from one table to another. For
example,

MERGE INTO orders o


USING customer c
ON (o.cust_id = c.cust_id)
WHEN MATCHED THEN
UPDATE SET o.del_address = c.address
WHEN NOT MATCHED THEN
INSERT (cust_id, address)
VALUES (c.emp_id, c.address);

In this example, if a record with the matching condition is found, then the address of the
same record is updated, else a new row is inserted.

Question: Explain the benefits of PL-SQL packages.

Answer: There are many benefits of using PL/SQL packages:

 Better code management as packages provide a container for sub-programs


 Top-down application design approach with separation of implementation and
specifications (interface)
 If there are any changes to the subprograms, there is no need to change the
dependent objects or recompile the entire package
 Accessibility can be specified (private/public) thus maintaining the security of the
code
 Data can be maintained across transactions for the entire session without storing in
database
 At the first call of the subprogram, the entire package is loaded, so there is no need
for disk I/O for subsequent calls, thus giving better performance.

Question: Explain the difference between ROLLBACK and ROLLBACK TO


statements?

Answer: ROLLBACK command rolls back all the changes from the beginning of the
transaction. In ROLLBACK TO, the transaction is rolled back (or undone) only till a point
known as the SAVEPOINT. The transactions before the SAVEPOINT cannot be undone,
and the transaction remains active even when the command is given.
Question: Explain the difference between procedure and function.

Answer:

Function Procedure

The function is compiled every time


Procedures are pre-compiled and saved. They execute
it is called for execution. the pre-compiled code whenever called.

Can be called from SQL statements. Can not be called from SQL statements.

The function has to return a value. Need not return any value.

Generally used for computation purpose. Used for executing complex business logic.

Can return multiple values using other methods,


Can return multiple values
otherwise, return only a single value.

Returns scalar data types. Returns an int by default.

A stored procedure can not be called from a


The procedure can call any function
function

Inside a select statement, a procedure cannot be


Functions can be embedded in a select statement
called.

Exception handling is not possible Try/catch block can be defined inside a procedure

Question: Explain the difference between procedure and trigger.

Answer:

PROCEDURE TRIGGER

Called explicitly by a user, trigger or an Executed by the DBMS whenever an event occurs in the
application database.

Can have parameters Doesn’t have parameters

Cannot be inactive Can be enabled or disabled on need basis

Creation – CREATE PROCEDURE Creation – CREATE TRIGGER

Question: What are the different types of cursors in PL/SQL?

Answer: There are two types of cursors –

 Implicit cursor – PL/SQL applies implicit cursors for INSERT, UPDATE, DELETE and
SELECT statements returning a single row.
 Explicit cursor – created by a programmer for queries returning more than one row.
Syntax–

CURSOR is

SELECT statement;

OPEN ;

FETCH INTO ;

CLOSE ;

Question: What are the different types of constraints?

Answer:

 Not NULL
 Unique
 Primary key
 Foreign key
 Check

Question: What are the differences between triggers and constraints?

Answer:

TRIGGERS CONSTRAINTS

A constraint on a table is stored along with the


Stored as separate objects
table definition

Constraints are fired as soon as the


Triggers are fired upon an event; hence they are fired
after constraints the table is used.

Perform table to table Performs memory location to table the


comparison which is slow leading to low
comparison, hence faster performance.

Trigger is for the entire table The constraint is for a column of the table

They are just stored procedures that get Prevent duplicate and invalid data entries
automatically executed, hence don’t check for data
integrity.

Question: Explain the PL/SQL block with an example.

Answer: PL/SQL block consists of three sections: declaration, executable and exception-


handling sections. The executable section is mandatory. There are two types of blocks:
named and anonymous.

Named blocks are functions and procedures which are stored in the database server and
can be reused. Anonymous blocks are for one time use and are not stored in the server.
Example:

DECLARE

message VARCHAR2(255):= 'Welcome to PL/SQL';


byzero NUMBER;

BEGIN

DBMS_OUTPUT.put_line (message);
byzero := 1/0;

EXCEPTION

WHEN ZERO_DIVIDE THEN


DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;

Question: Explain PL/SQL Records.

Answer: Records contain a set of data of various data types that can be related to each
other as fields. Three types of records that are supported in PL/SQL are table-based
records, programmer-based records, and cursor-based records.

Question: Explain the difference between commit and savepoint.

Answer:

COMMIT – is used to make the database changes permanent. All the save points are
erased and the transaction ends. Once committed, a transaction cannot be rolled back.

SAVEPOINT – is used to set points during a transaction to which a programmer can roll-
back later. it is helpful when there is a series of transactions that can be divided into
groups having a savepoint.
Question: What is the difference between actual and formal parameters?

Answer: The parameters that are used to call a procedure are called as actual
parameters. Example –

get_student_details(stud_name, dob); -- here stud_name and dob are


actual parameters.

The variables declared in a procedure header used in the body are called formal
parameters. Example –

PROCEDURE get_student_details (dob DATE) IS – here stud_name is a


formal parameter.

Question: How is a DECLARE statement used?

Answer: DECLARE is used as the first statement for stand-alone files that consist of
anonymous block of code which are not stored procedures, functions or triggers.
Example –

DECLARE

num1 NUMBER(2);

num2 NUMBER(3);

BEGIN

-- logic goes here

END;

Question: Tell us about SQLCODE and SQLERRM.

Answer: SQLCODE and SQLERRM are used to trace exceptions that are not explicitly
handled in the program. These are globally defined variables. SQLCODE returns the error
code while SQLERRM returns the corresponding error message.

Question: What is rollback? How is it different from rollback to statement?

Answer: Rollback erases all the database changes including all the savepoints. It ends a
transaction.
‘Rollback to’ rollbacks the changes up to the savepoint mentioned in the code. The
transaction will still be active.

Question: What is IN OUT parameter?

Answer: IN OUT parameter mode passes a value to a subprogram and returns an


updated value.

Question: Is it possible to accept user inputs during runtime? How?

Answer: Yes, it is possible. Use ACCEPT keyword to take inputs from the user. Example –

ACCEPT age number prompt ‘Enter your age:’

Question: Give a simple way to run a query faster.

Answer: By using ROWID. It is not a physical column but the logical address of a row. It
contains the block number, file number and row number thereby reducing I/O time
hence making query execution faster.

Question: What are some of the pre-defined exceptions in PL/SQL?

Answer: ZERO_DIVIDE, NO_DATA_FOUND, TOO_MANY_ROWS, INVALID_CURSOR,


DUP_VAL_ON_INDEX etc…

Question: How do you trace the PL/SQL code?

Answer: You can trace through DBMS_* methods like

 DBMS_APPLICATION_INFO
 DBMS_TRACE
 DBMS_SESSION and DBMS_MONITOR

Question: How to restrict string length in PL/SQL?

Answer: Use CHAR (NUMBER) to get fixed length for a variable. Example – CHAR (10). If
the length of the string is less than the specified number, it will be padded with white
spaces.

Question: What is the purpose of the UTL_FILE package in PL/SQL?

Answer: By using this package, developers can get the code read and write files to and
from the computer. For doing this, the developer will need access grant from DBA user.
Question: What are DBMS_OUTPUT and DBMS_DEBUG?

Answer: Both can be used for debugging the code. DBMS_OUTPUT prints the output to
console whereas DBMS_DEBUG prints it to a log file.

Question: List some cursor attributes in PL/SQL.

Answer:

 %ISOPEN: Check if the cursor is open


 %ROWCOUNT: Get the number of rows that are updated, deleted or fetched.
 %FOUND: Checks if the cursor has fetched any row, returns Boolean.
 %NOT FOUND: Checks if the cursor has fetched any row. Returns Boolean.

Question: What is the purpose of NVL?

Answer: NVL lets the programmer substitute a value for a NULL value. Example –

NVL (occupation, ‘default’)

Question: On a table, how many triggers can be applied?

Answer: 12 is the maximum number.

Question: How can we achieve consistency using PL/SQL?

Answer: We can achieve consistency by setting the appropriate isolation level. For
example, to give read consistency, the isolation level can be set to READ COMMITTED.

Question:  Write a simple procedure to select some records from the database
using some parameters.

Answer: Example code –

CREATE PROCEDURE get_customer_details @age nvarchar(30), @city


nvarchar(10)

AS

BEGIN
SELECT * FROM customers WHERE age = @age AND city = @city;

END;

Question: Explain the error ORA-03113.

Answer: The error end-of-file on communication channel ORA-03113 means that there is


a broken connection between the client and server channels. It could be a timeout
because of which the connection was lost. You can troubleshoot by pinging the server
and checking the connectivity.

Question: Can you use IF statement inside a SELECT statement? How?

Answer: Yes, we can do so using the DECODE keyword in versions 9 and above. Example

SELECT day_of_week,

DECODE (number, 0, 'Sunday',

1, 'Monday',

2, 'Tuesday',

3, 'Wednesday',

4, 'Thursday',

5, 'Friday',

6, 'Saturday',

'No match') result FROM weekdays;

Question: What is SYS.ALL_DEPENDENCIES?

Answer: SYS.ALL_DEPENDENCIES describes all the dependencies between packages,


procedures, functions, triggers accessible to the current user. It shows columns like name,
type, dependency_type, referenced_owner, etc…
Conclusion

PL SQL Collections
A collection is a group of elements of homogenous data types. It generally comprises arrays, lists,
sets, and so on. Each of the elements has a particular subscript which reflects its position.

PL SQL Collections are of the following types:


1. Associative Array/Index-by tables
2. Nested tables.
3. Varrays.
PL SQL collections are generally used for storage and manipulation of big chunks of data, using the
keyword BULK COLLECT in Oracle.

Collection Methods
Pl/SQL has some built-in methods under collection which are listed below.
Sl. No. Name Descriptions

1 count Gives the number of elements present in the collection.

2 limit Verifies the collection size.

3 exists(m) Returns true if mth element present in the collection else returns false.

4 first Gives start index number in collection with integer subscript.

5 last Gives end index number in collection with integer subscript.

6 extend Adds NULL element to the collection end.

7 next(m) Gives the index that succeeds mth index.

8 prior(m) Gives the index that precedes m index.

9 trim Deletes an element from the end of collection.

10 trim(m) Deletes m elements from the end of collection.

11 delete Deletes all elements from collection, setting count to 0.

12 delete(m) Deletes mth element from collection, if mth element is NULL, then no action is performed.

13 delete(m,k) Deletes element from mth to kth position.

14 extend(m) Adds m element to the collection end.

15 extend(m,k) Adds m copies of the kth element to the end of collection.

Collection Exceptions
Some of the common collection exceptions are as follows:
1. VALUE_ERROR: This exception is thrown if a subscript cannot be converted to the key type
or is NULL. This exception is normally raised if a key is of type PLS_INTEGER range and the
subscript resides beyond this range.
2. NO_DATA_FOUND: This exception is thrown by PL/SQL if either a SELECT statement
fetches no rows or a program points to an element that is deleted in a nested table. This
exception can also be raised by an element which is uninitialized in an index-by table.
3. COLLECTION_IS_NULL: This exception is thrown by PL/SQL if the collection is NULL by
default.
4. SUBSCRIPT_BEYOND_COUNT: This exception is thrown when a subscript is more than the
total count of the number of elements in the collection.
5. SUBSCRIPT_OUTSIDE_LIMIT: This exception is thrown when a subscript is beyond the
threshold range.
Nested Tables In PL/SQL
The nested tables are like a single column database table or a 1-dimensional array where the array
size is dynamic. Its subscript is of numeric type. We can get the nested table into a variable by
giving the rows a subscript that begins with 1. This feature makes it similar in nature like an array.

A nested table can be held in a column of a database. It can also be used for manipulating SQL
operations by joining tables. Since it is like a dynamic array so the upper limit can be of any size.

A nested table can have both dense and sparse collection characteristics which means any element
can be deleted randomly (making it sparse) with the help of the DELETE procedure. The deletion of
data causes a discontinuity in the index but the NEXT function helps to iterate to the next subscripts.
Since the data is stored in the form of a table, it can be retrieved with the help of SELECT
statements.

A nested table can be built at the schema level or in PL/SQL block. It is like a database object which
is accessible within the database or subprogram.

Differences between Array and Nested table are listed below:


 The size of the nested tables can be increased dynamically. The upper limit of an array is fixed
but it is not fixed for nested tables.
 The array has consecutive subscripts which makes it dense. However, a nested table is dense
at the time of creation but becomes sparse once the elements are deleted in between.
Syntax of the nested table:
TYPE <<type>> IS TABLE OF <<element>> [NOT NULL];
Here, ‘type’ is the type specifier. ‘element’ is the data type.
Code implementation with the nested table:
DECLARE
    TYPE subject IS TABLE OF VARCHAR(15);
    TYPE teacher IS TABLE OF VARCHAR2(20);
    subjectnames subject;
    subjectteacher teacher;
    summ integer;
BEGIN
 
    -- adding subject and its teachers to the table
    subjectnames := subject('PLSQL', 'SELENIUM', 'JMETER');
    subjectteacher:= teacher('Sashi', 'Mala', 'Mukund');
 
    -- returns count of number of elements in nested table
    summ:= subjectteacher.count;
 
    -- printing the content to the console
    dbms_output.put_line('Total Number of Teachers: '|| summ);
    FOR i IN 1 .. summ LOOP
         dbms_output.put_line('Subject:'||subjectnames(i)||', Teacher:' || subjectteach
    end loop;
END;
The output of the above code should be:

Associative Array Or Index-By Tables


The index-by table is commonly called the associative array. In terms of structure, both the index-by
table and nested tables are similar and have subscript to access the elements.

An associative array is represented by a key-value pair. Each of the unique keys is used to identify
the value in the array. The data type of the key can be a string or an integer defined while creating
it. A key is added to the index-by table by simply assigning a value for the first time. To modify the
same entry, we have to use the same key.

The key should be a unique one either as a primary key in a table or by combining strings together
to develop unique value. This type of collection has an array size that is dynamic and has either
sparse or dense characteristics. One difference between the index-by table and the nested table is
that the former cannot be stored in the column of the database but the nested table can be stored.

The associative arrays provide easy maintenance of subscript and are created within a PL/SQL
block. It is like a SQL table where values are obtained with the help of the primary key. This is
generally used for temporary data storage and can be used instead of SQL tables for avoiding
network traffic and disk storage required by SQL tables.

As the associative arrays do not store persistent data, they cannot be used with SQL statements like
SELECT and INSERT. However, they can be made unending for a session of the database by
declaring their data type as a package and defining them inside the body of the package.

Syntax of the index-by table:


TYPE type IS TABLE OF element [NOT NULL]
 INDEX BY [BINARY_INTEGER | PLS_INTEGER | VARCHAR2(size)];
 INDEX BY key;
Here, the ‘key’ is numeric. It can be either BINARY_INTEGER or PLS_INTEGER. Its datatype can
be varchar, varchar2, long, or string. The varchar based key should be mentioned with length.
‘element’ is the data type.
‘size’ is the maximum number of elements. It is a positive integer.
‘type’ is the type specifier.
Code implementation with the index-by table:
DECLARE
    TYPE age IS TABLE OF NUMBER INDEX BY VARCHAR(25);
    age_empl age;
    employee VARCHAR(25);
BEGIN
 
    -- adding employee details to the table
    age_empl('Sam') := 30;
    age_empl('Venu') := 35;
 
    -- printing the table contents in the console
    employee := age_empl.FIRST;
    WHILE employee IS NOT null LOOP
         dbms_output.put_line
         ('Employee name is ' || employee || ' and age is ' || TO_CHAR(age_empl(employe
         employee := age_empl.NEXT(employee);
     END LOOP;
END;
/
The output of the above code should be:
Varrays In PL/SQL
The Varrays store a fixed number of elements and are like a one-dimensional array. However, the
number of elements can be modified at runtime. It is a consecutive set of elements of similar data
types. It can be stored in a database table that can be handled with the help of SQL statements. But
the process is not as easy and flexible as in a nested table.

The maximum size of Varray is defined in its type definition. It has a one after another memory
arrangement beginning with 1 subscript and the lowest location address points to the starting
element and the highest location address points to the end element. All the elements of a Varray are
identified with an index.

This type of collection has numeric subscript and has dense characteristics. Thus the array
elements cannot be deleted in between. Either the entire Varray should be deleted or its end can be
trimmed. Due to its dense characteristics, it has less flexibility of use.

The Varray can be created either within a PL/SQL block or at the level of schema. It is treated as a
database object which can be accessed within the database or within a subprogram. Varray is used
more frequently when the size of the array is known to us. It should be initialized prior to using them
and it can be initialized with the help of a constructor. Its value is NULL when declared and should
be initialized before referencing its elements.

Syntax of Varray:
TYPE <<type>> IS {VARRAY | VARYING ARRAY} (<<size>>)

OF <<element>> [NOT NULL];

Here,
‘type’ is the type specifier.
‘element’ is the data type.
‘size’ is the maximum number of elements in an array. It is a positive integer.
Varray Variables Declaration And Initialization
After creating a Varray, we can declare it in the way described below:
Syntax:
name type_n [:= type_n(...)];
Here,
‘name’ is the Varray name.
‘type_n’ is the type of Varray.
‘type_n(…)’ is the constructor of type Varray. The argument lists are mentioned by a comma
separator and of type Varray.
We have to initialize a Varray variable before using it else it gives uninitialized collection error. The
initialization is done in the way described below.

Syntax:
name type_n := type_n();

This will initialize the variable with zero elements. In order to populate elements in the varray
variables, the syntax is:
name type_n := type_n(e1, e2, ...);

Accessing Elements Of Varray


The elements can be accessed by the process described below:
name(m); where m is the element index which starts with 1 and terminates with the maximum count
of the number of elements of type Varray. In case m is beyond the range of 1 and the maximum
count of the number of elements, SUBSCRIPT_BEYOND_COUNT exception is thrown by PL/SQL.
The size of Varray can be modified with the help of the ALTER statement.

Code implementation with ALTER.


CREATE Or REPLACE TYPE names AS VARRAY(2) OF VARCHAR(5);

The output of the above code should be.

Code implementation with Varray:


DECLARE
   type countrynames IS VARRAY(3) OF VARCHAR2(50);
   type currency IS VARRAY(3) OF VARCHAR2(15);
   country countrynames;
   cur currency;
   addition integer;
BEGIN
 
   -- adding country and its currency to the table
   country := countrynames('INDIA', 'USA', 'UK');
   cur:= currency('INR', 'DOLLAR', 'POUND');
 
   -- returns count of number of countries in varray
   addition := country.count;
 
   -- printing the content to the console
   dbms_output.put_line('Total Number of countries : '|| addition);
   FOR i in 1 .. addition LOOP
       dbms_output.put_line('Country: ' || country(i) ||
' ,Currency : ' || cur(i));
   END LOOP;
END;
/
The output of the above code should be:

Differences Between Varrays And Nested Tables


The differences are as follows:
 While we are working on a scenario where the number of elements is known and they are
arranged sequentially, Varrays are generally used. A Varray which is stored in a database
maintains its subscripts and sequence. It is always maintained as a single object. Whereas,
the nested tables are used when the count of the number of elements is not restricted.
 The nested table has sparse characteristics which enable it to remove any element from any
position and not always from the end. The order and subscript of a nested table are not
maintained when it is stored in the database. The elements of Varray are always maintained in
sequence.
Differences Between Associative Arrays And Nested Tables
The differences are as follows:
 Nested tables can be stored in a column of the database but associative arrays are not. The
nested tables maintain proper data relationships that are stored persistently.
 The associative arrays are good for small lookup tables where the collection can be built in
memory every time a package is initialized or a procedure is called. They are a good choice
when the size is not known beforehand. The index values of an associative array are flexible
since the subscripts of this array are not in sequence or can be negative or a string instead of
a number.
Frequently Asked Questions And Answers
Q #1) Why do we use collections in PL/SQL?
Answer: There are three types of collections – Nested tables, Associative arrays, and Varrays. The
nested tables can store a random number of elements. Varrays can store a fixed number of
elements and the associative arrays allow us to search elements with random numbers and strings.
Q #2) What is the nested table in PL/SQL?
Answer: A nested table is 1 dimensional in nature and its size does not remain fixed. They can be
accessed in SQL and PL/SQL blocks. It can be utilized in records, object definitions, and tables.
Q #3) What is a bulk collection in Oracle PL/SQL?
Answer: A bulk collection is a method of getting data when the PL/SQL engine informs the SQL
engine to accumulate multiple rows at once and store them in a collection.
Q #4) Why bulk collect is faster in Oracle?
Answer: The bulk collect enhances the performance by cutting down the interaction between the
PL/SQL engine and database.
Q #5) What is Varray in PL/SQL?
Answer: It is a PL/SQL data structure that is used to store a homogenous collection of elements of
the same data type in a sequence. It is mostly used to hold an ordered set of data.
Q #6) Can we delete element from Varray in Oracle?
Answer: We cannot modify or delete elements in a Varray with the help of SQL statements directly.
We have to select the Varray from the table, modify it in PL/SQL, and then modify the table and
store it in a new Varray.
Q #7) What is the difference between the Nested Table and Varray?
Answer: The differences are listed below:
 We take the Varray when the number of elements is known from before. This restriction is not
possible in nested tables.
 In Varray, the elements are used in sequence. The nested table has both dense and sparse
characteristics.
 The elements are always ordered in Varray. The elements may or may not be in order in the
nested table.
Conclusion
In this tutorial, we have discussed in detail some of the basic concepts of PL/SQL that are essential
in developing knowledge on it.

We have covered the following topics listed below:


 Basic understanding of PL SQL collections (methods and exceptions).
 Varrays.
 Nested tables
 Index-by tables

What is BULK COLLECT?


BULK COLLECT reduces context switches between SQL and PL/SQL engine and
allows SQL engine to fetch the records at once.

Oracle PL/SQL provides the functionality of fetching the records in bulk rather than
fetching one-by-one. This BULK COLLECT can be used in ‘SELECT’ statement to
populate the records in bulk or in fetching the cursor in bulk. Since the BULK
COLLECT fetches the record in BULK, the INTO clause should always contain a
collection type variable. The main advantage of using BULK COLLECT is it increases
the performance by reducing the interaction between database and PL/SQL engine.

Syntax:
SELECT <columnl> BULK COLLECT INTO bulk_varaible FROM <table name>;
FETCH <cursor_name> BULK COLLECT INTO <bulk_varaible >;
In the above syntax, BULK COLLECT is used in collect the data from ‘SELECT’ and
‘FETCH’ statement.

In this tutorial you will learn-

 FORALL Clause
 LIMIT Clause
 BULK COLLECT Attributes

FORALL Clause
The FORALL allows to perform the DML operations on data in bulk. It is similar to that
of FOR loop statement except in FOR loop things happen at the record-level whereas
in FORALL there is no LOOP concept. Instead the entire data present in the given
range is processed at the same time.
EXPLORE MORE Learn Java Programming with Beginners Tutorial08:32

Linux Tutorial for Beginners: Introduction to Linux Operating...01:35

What is Integration Testing Software Testing Tutorial03:04 What is

JVM (Java Virtual Machine) with Architecture JAVA...02:24 How to write a

TEST CASE Software Testing Tutorial01:08 Seven Testing Principles Software

Testing05:01 Linux File Permissions Commands with Examples13:29

How to use Text tool in Photoshop CC Tutorial08:32 What is NoSQL


Database Tutorial02:00 Important Linux Commands for Beginners Linux
Tutorial15:03

Syntax:
FORALL <loop_variable>in<lower range> .. <higher range>

<DML operations>;
In the above syntax, the given DML operation will be executed for the entire data that
is present between lower and higher range.

LIMIT Clause
The bulk collect concept loads the entire data into the target collection variable as a
bulk i.e. the whole data will be populated into the collection variable in a single-go.
But this is not advisable when the total record that needs to be loaded is very large,
because when PL/SQL tries to load the entire data it consumes more session
memory. Hence, it is always good to limit the size of this bulk collect operation.

However, this size limit can be easily achieved by introducing the ROWNUM condition
in the ‘SELECT’ statement, whereas in the case of cursor this is not possible.

To overcome this Oracle has provided ‘LIMIT’ clause that defines the number of
records that needs to be included in the bulk.

Syntax:
FETCH <cursor_name> BULK COLLECT INTO <bulk_variable> LIMIT <size>;
In the above syntax, the cursor fetch statement uses BULK COLLECT statement along
with the LIMIT clause.

BULK COLLECT Attributes


Similar to cursor attributes BULK COLLECT has %BULK_ROWCOUNT(n) that returns
the number of rows affected in the nth DML statement of the FORALL statement, i.e. it
will give the count of records affected in the FORALL statement for every single value
from the collection variable. The term ‘n’ indicates the sequence of value in the
collection, for which the row count is needed.
Example 1: In this example, we will project all the employee name from emp table
using BULK COLLECT and we are also going to increase the salary of all the
employees by 5000 using FORALL.

DECLARE
CURSOR guru99_det IS SELECT emp_name FROM emp;
TYPE lv_emp_name_tbl IS TABLE OF VARCHAR2(50);
lv_emp_name lv_emp_name_tbl;
BEGIN
OPEN guru99_det;
FETCH guru99_det BULK COLLECT INTO lv_emp_name LIMIT 5000;
FOR c_emp_name IN lv_emp_name.FIRST .. lv_emp_name.LAST
LOOP
Dbms_output.put_line(‘Employee Fetched:‘||c_emp_name);
END LOOP:
FORALL i IN lv_emp_name.FIRST .. lv emp_name.LAST
UPDATE emp SET salaiy=salary+5000 WHERE emp_name=lv_emp_name(i);
COMMIT;
Dbms_output.put_line(‘Salary Updated‘);
CLOSE guru99_det;
END;
/

Output
Employee Fetched:BBB
Employee Fetched:XXX
Employee Fetched:YYY
Salary Updated
Code Explanation:

 Code line 2: Declaring the cursor guru99_det for statement ‘SELECT emp_name
FROM emp’.
 Code line 3: Declaring lv_emp_name_tbl as table type of VARCHAR2(50)
 Code line 4: Declaring lv_emp_name as lv_emp_name_tbl type.
 Code line 6: Opening the cursor.
 Code line 7: Fetching the cursor using BULK COLLECT with the LIMIT size as
5000 intl lv_emp_name variable.
 Code line 8-11: Setting up FOR loop to print all the record in the collection
lv_emp_name.
 Code line 12: Using FORALL updating the salary of all the employee by 5000.
 Code line 14: Committing the transaction.

AWS DynamoDB Interview Questions

A managed NoSQL database service, Amazon DynamoDB offers quick and predictable performance
along with seamless scalability. By using DynamoDB, you can delegate the administrative tasks
associated with running and scaling a distributed database, freeing you from having to worry about
hardware provisioning, setup, software patching, replication or cluster scalability. Additionally,
DynamoDB provides encryption at rest, which removes the operational complexity and burden of
protecting sensitive data.

Now that you know what AWS DynamoDB is, let us learn some less-known facts about it:

 You can create a new DynamoDB table and import data directly from Amazon S3 without

creating any code or trying to manage any additional infrastructure.

 DynamoDB is supported by AWS Glue Elastic Views as a source to continuously integrate and

replicate data across various databases in almost real-time.

 To query, update, insert and delete table data in DynamoDB, use the PartiQL query

language.

 You can record item-level changes in the DynamoDB tables using Amazon Kinesis Data

Streams.
 Even more quickly restore DynamoDB tables.

 DynamoDB is now supported by the AWS Pricing Calculator.

 Export your DynamoDB data to Amazon Simple Storage Service (Amazon S3), then use

Amazon Athena and other AWS services to analyze the data and derive useful insights.

The demand for AWS DynamoDB is quite high and therefore, the opportunities are quite vast. We
assure you that the interview questions will assist you in acing the interview and getting the dream
job and role.

AWS DynamoDB Interview Questions For Freshers


1. What are the data types supported by DynamoDB?

The four scalar data types that DynamoDB supports are as follows:

 Numbers
 Strings
 Binary
 Boolean.

Data types for collections that DynamoDB supports include:

 String Set Number Set


 Heterogeneous Binary Set
 Differentiated Map
 DynamoDB accepts Null values.

2. Can DynamoDB perform atomic updates in place?

Quick in-place atomic notifications are supported by Amazon DynamoDB, allowing you to add or
eliminate lists, sets or maps while also incrementing and decrementing a numeric attribute with
just one API call.

3. What sort of query functionality is supported by DynamoDB?

 GET/PUT operations that use the user-defined unique identifier are supported.

 By enabling querying of a non-primary key characteristic using both local and global

secondary indexes, it offers flexible querying.


 Data for an item linked to a single attribute separation primary key can be read and written

quickly.

 It enables you to access all the items for a single aggregate partition-sort key along a number

of sort keys using the Query API.

4. Is conditional operation support available for Amazon DynamoDB?

You must specify a condition in order for a procedure to be finished on an item. A condition
expression which can be created from the following is one you can define:

 Comparison operators: =, >,, >, =, >, BETWEEN, and IN Logic operations: NOT, AND, and OR

Boolean functions: ATTRIBUTE EXIST, CONTAINS, and BEGINS WITH

 You can also create a conditional expression that is free-form and combines several

conditional clauses, including nested clauses.

5. What are some of the differences between Amazon SimpleDB and Amazon DynamoDB?

Crafted for internet application domains, Amazon DynamoDB is a quick and scalable NoSQL
database provider that is also highly recommended. It maintains predictable high performance and
is extremely cost-effective for caseloads of any scale. 

Although it has scaling restrictions, Amazon SimpleDB is a good choice for smaller caseloads that
demand query flexibility.

At the expense of performance and scale, it supports query flexibility and instantaneously indexes
all item attributes.

6. What are the APIs provided by Amazon DynamoDB?

 UpdateTable
 CreateTable
 DescribeTable
 DeleteTable
 PutItem
 ListTables
 UpdateItem
 BatchWriteItem
 GetItem
 Query
 DeleteItem
 Scan
 BatchGetItem.

7. Could you please define global secondary indexes?

The term "global Secondary index" refers to an index with a partition and divided key that differs
from those on the table.

In the context that questions on the index could indeed cover every item in a table throughout all
partitions, it is regarded as being "global."

8. What kinds of secondary indexes does Amazon's DynamoDB support?

Amazon DynamoDB supports a pair of index types. As follows:

 A global secondary index is one that has a partition or partition sort key that really is distinct

from the table's primary key. Because questions on the index can cover all the items in a

table across all the partitions, it is referred to as a global secondary index.

 Local secondary index: An index with a different sort key than the table's partition key. Since

every index partition is bound to a table partition with the same partition key, it is regarded

as being "local."

9. How can a Global Secondary Index be removed from Amazon DynamoDB?

The console or an API call can be used to remove a Global Secondary Index.
 The Global Secondary index can be removed from a table by selecting it on the console,

going to the "Table items" section and choosing the "indexes" tab, and then clicking the

"Delete" button next to the delete index.

 The Notification Table API call can also be used to delete a Global Secondary Index.

[ Learn Complete about  Amazon Web Services Console  ]

10. How many global secondary indexes are created for each table?

Per table, a maximum of 5 global associated increases are permitted.

11. What types of API calls does a global secondary index support?

The API calls that Global Secondary Index supports are "Query" and "Scan."

12. On how many different tables can local secondary indexes be created?

There can be a maximum of five local associated increases per table.

13. Are local secondary indexes erasable?

Currently, once local secondary indexes are created, Amazon DynamoDB is unable to remove them
from the table; however, the entire table can be deleted.

14. A table that already exists can I add local supplementary indexes to?

Once you establish a table to local secondary indexes, you can define a sort key element that isn't
currently used to set up a local secondary index for use in the future.

This means that adding local secondary indexes to the existing table is likely impossible right now.

15. How can local secondary indexes be made?

 When making a table that can't be added right now, a Local secondary index must be

created.

 You should therefore try specifying the two following parameters later on when you

establish a local secondary index.


 The characteristic about which indexing and query processing are done is known as an

indexed sort key.

 Attributes that can be directly copied into the school serves index are known as projected

attributes. The local secondary index only contains primary keys and secondary keys when

projected attributes are absent.

16. What exactly are projects?

The group of characteristics which are copied or predicted from a table to an index are called
projections. They are in addition to the automatically projected index key attributes and primary
key attributes. The characteristics that are estimated into the index must always be specified when
defining a local secondary index. Each index has a minimum of three of the following attributes:

 Table segment key value


 To be used as the index sort key attribute
 Table key-value sort.

17. What sort of query functionality is supported by DynamoDB?

It supports GET/PUT operations using the user-specified primary key. The ability to query a non-
primary key attribute using both local and global secondary indexes promotes flexible querying.

18. Redis: Is it quicker than DynamoDB?

 Inserts/Delete are extremely quick because DynamoDB is a NoSQL database.

 DynamoDB permanently stores data (although it is less fast than Redis because we don't

need that much speed).

19. Are writes to DynamoDB Atomic?

Atomic counters are a feature of DynamoDB that let you change the value of an established
attribute without affecting other write requests by using the update method. It increases the value
of this characteristic by one each time the programme is executed.
AWS DynamoDB Interview Questions For Experienced
20. What are the main advantages of using DynamoDB over an established MySQL-style SQL-
based database?

Several advantages of DynamoDB over conventional SQL databases. You don't have to worry about
providing or managing servers because it is a completely managed service. Second, you could
indeed quickly increase or decrease ability as needed because it is highly scalable. Finally, you can
be sure that your data is secure because it has built-in compliance and security features.

21. How is Amazon's NoSQL implementation different from other well-known ones like
Cassandra or MongoDB?

A managed NoSQL database service, DynamoDB provides quick, predictable performance with easy
scalability. Several significant aspects set DynamoDB apart from other well-liked NoSQL
implementations:

 DynamoDB provides a managed service rather than needing setup and management by the
user.
 DynamoDB utilizes a proprietary query language rather than SQL, DynamoDB employs a
proprietary storage format rather than JSON.

22. What are a few applications of DynamoDB?

Any application that requires latency access to data and is willing to give up some data modeling
flexibility in exchange for performance should consider DynamoDB. Additionally, it is a wise choice
for programs that need to be highly available and are willing to give up some performance to
achieve it.

23. Do you have any restrictions when using DynamoDB? If so, what exactly are they?

Although DynamoDB is a strong tool, it does have some drawbacks. Its inability to handle large
amounts of information is one of its limitations. You might want to think about a different solution
if you need to store a lot of data. You should compare the costs of using DynamoDB with the
advantages it offers because it can be costly to use.

24. What different methods are there for accessing data in DynamoDB?

The Query or Scan APIs can be used to access data stored in DynamoDB. The Scan API enables you
to browse through data for items that meet specific criteria, whereas the Query API enables you to
ask a series using a primary key.
25. How well do you comprehend DynamoDB Streams?

You can record data changes in one's DynamoDB table in almost real-time using DynamoDB
Streams, a feature of DynamoDB. This can be helpful for a variety of purposes, such as auditing or
maintaining a backup copy of your data in a different location for disaster recovery.

26. What functions are carried out by DynamoDB Streams?

You can record data adjustments done to goods in a DynamoDB table using a feature called
DynamoDB Streams. Then, you can take action with that data by retrieving, filtering, or trying to
export it to a different DynamoDB table, among other actions.

27. Can DynamoDB be used to access data kept in AWS S3?

It is possible to use DynamoDB to connect data stored in AWS S3. But to do that, you'll need to use
an API designed specifically for DynamoDB.

28. Could you provide me with a few instances of real-world applications that employ the use of
DynamoDB as their main database?

The Amazon.com website, the Kindle Fire tablet line, and the Amazon Web Services cloud
computing service are a few real examples of applications that use DynamoDB as their main
database.

29. What do you think about Firebase vs DynamoDB?

If you're going to look for a controlled NoSQL database that scales well, DynamoDB is a fantastic
choice. Additionally, if you require precise control over your data, it is a wise choice. If you want a
controlled NoSQL database that is feature-rich and simple to use, Firebase is a good choice.

[ Related Article:  MongoDB vs DynamoDB  ]

30. What do you know about DynamoDB's partition keys and sort keys?

Sort credentials are used to choose the order wherein items are kept within a partition, while
separation keys are used to ascertain what partition an item would be stored in. A DynamoDB table
can be searched for items using separation keys and sort keys combined.

31. What do you know about DynamoDB's local secondary indexes?

The capacity to query the DynamoDB data using a different sort key from the one used to shop the
data in the table is provided by local secondary indexes. If you want to query the data in a variety of
ways or if you need to, this can be helpful.
32. What do you think provisioned throughput means?

DynamoDB's provisioned throughput feature enables users to specify the read and write capacity
needs for their table. The user can then make sure that one‘s table can accommodate the volume
of traffic they anticipate.

33. What do you think the eventual consistency model means?

A technique for making sure that all duplicates of a data item are updated is the eventual
consistency model. In distributed applications, where it may take some time for adjustments to
reach every node, it is frequently used. According to the eventual consistency model, if enough
time passes, all copies of a data item will have been updated.

34. What transpires if an application tries to read or write so many requests per second than is
permitted?

Applications will encounter errors if they try to read or write greater than the maximum number of
request units per second allowed. The application will need to increase its allotted request units or
decrease the number of queries it is currently making.

35. Can you describe how conditional writes are used by DynamoDB to enhance performance?

By enabling you to clearly state conditions on write operations that need to be met in order for the
write to be successful, DynamoDB uses conditional writes to help enhance efficiency. By doing this,
you can prevent overwriting data which has already been revised by another process or writing
duplicate data.

Advantages of AWS DynamoDB


 Scalable: Virtually unlimited storage allows users to store as much data as necessary

 Cost-effectiveness: It appears to be reducing costs while allowing a sizable portion of data to

move from SQL to NoSQL. Basically, it charges for data reading, writing, and stashing as well

as any optional DynamoDB features you decide to enable.

 Replication of data: Replication of data is either internally managed throughout multiple

availability zones in a region or it may be made available across multiple regions. All data

items are stored on SSDs.


 DynamoDB scales horizontally without a server by expanding a single table across several

servers.

 Simple Administration: Because Amazon DynamoDB is a fully managed service, you don't

have to worry about setting up and configuring hardware or software, applying software

patches, managing distributed database clusters.

Visit here to learn  AWS Training in Hyderabad

Most Common AWS DynamoDB FAQs


1. What is AWS DynamoDB?

It is a NoSQL database provider that is well-designed, offers quick and unavoidable performance,
and scales easily. Additionally, it allows users to delegate running and scaling online digital content
distribution to AWS for the users' convenience, so they won't need to worry about
setup, configuration, hardware requirements, replication, bandwidth capacity planning, cluster
scaling, or software patching.

2. What are the benefits of AWS DynamoDB?

The following is a list of some advantages of using Amazon DynamoDB:

 It is a managed service, so there is no need to employ professionals or stress about setup,


installation, clustering, etc.
 It can be scaled.
 Users can access it with high throughput and very little latency.
 It is strong and widely accessible.
 It is adaptable and enables the creation of dynamic tables with multi-valued attributes.
 It is economical.

3. What are non-relational databases?

The NoSQL databases used in non-relational databases. These databases are divided into four
groups, which are as follows:

 Key-value registries
 Stores in graphs
 Stores in columns
 document archives.
4. Is AWS DynamoDB free?

You just pay for the assets you provision in Amazon DynamoDB. Start out with DynamoDB's free
tier limitations, which power many applications. Depending on the kind of resources you need,
there are different monthly prices when you're in need of them.

5. What is DynamoDB Mapper class?

It serves as DynamoDB's entry point. The DynamoDB Mapper class allows users to connect to a
DynamoDB endpoint, allowing them to execute questions and scan against tables as well as carry
out CRUD operations on items and access their data in various tables.

Explore  AWS Sample Resumes! Download & Edit, Get Noticed by Top Employers!

6. What are key-value stores?

It is a database service that offers and facilitates the storing, updating, and querying of objects that
are recognised using key and value pairs and make up the actual material that is being stored.

7. Explain DynamoDB auto-scaling.

Using DynamoDB, a global value with respect or a DynamoDB can automatically scale up and down
in terms of read and write capacity.

8. Explain what DynamoDB BatchWriteItem does.

Multiple items all over multiple tables can be added, removed, or replaced using the Amazon
DynamoDB BatchWriteItem in just one request, but not in a single transaction. It supports inserting
or deleting combinations of up to 25 items with a total combined request size of 16 MB.

9. How are DynamoDB Access Key and Secret Key generated?

You can generate an access key and a secret key by creating a user in AWS IAM which is Identity
Access Management.

[ Learn More About  AWS IAM  ]

10. Is DynamoDBMapper safe for threads?

It is possible to share this class across threads and it is thread-safe. DynamoDBMapper will start
chucking DynamoDBMappingException while using the load, save and delete methods to
demonstrate that domain courses are incorrectly marked or otherwise incompatible with this class.
Key Upshots

Key-value and document data models are supported by the NoSQL database Amazon DynamoDB.
The use of DynamoDB by developers allows them to create serverless, modern applications that
can scale globally and support petabytes of data as well as tens of millions of read and write
requests per second. High-performance, internet-scale applications that would tax conventional
relational databases can run on DynamoDB.

Q1. What is DynamoDB?

DynamoDB is a fully managed NoSQL database service. It is backed by AWS and provides exciting features
like seamless scalability, fast performance, and high reliability over data. DynamoDB supports both key-value
and document data structures. In addition, this service comes with different pricing tiers to suit varying user
requirements.

DynamoDB is quite effective at data storing and retrieval in all traffic levels and allows the users to create
tables for the database.

02. What are NoSQL databases?

NoSQL or non-relational databases focus on different data storing models rather than a tabular structure.
There are four types of NoSQL databases:

1. Key-value stores
2. Document stores
3. Graph store
4. Colum stores
DynamoDB supports both document and key-value structures.

03. What are the key features of DynamoDB?

 Highly Scalable without any intervention from the user.


 It has a latency of microseconds.
 Serverless and enterprise-ready.
 Encryption at Rest.
 On-demand backup and restore.
 Point-in-time recovery.
Read more: DynamoDB Features.
04. What are the advantages of using DynamoDB?

 We can store any amount of data with the unlimited storage provided by the
DynamoDB service.
 The data we store replicates over many availability regions. It allows to easily
cope with global-scale applications and make sensitive information highly
available.
 It is highly cost-effective, and the users have to pay only for what they use.
 Easy administration and the user doesn't have to worry about software patching,
setup, and configuration because AWS fully manages DynamoDB.
 DynamoDB has an advanced system for reporting and highly secured user
authentication mechanisms to provide maximum security over sensitive data.
Read more: DynamoDB Pros & Cons.
05. What are the disadvantages of DynamoDB?

 No triggers and lack server-side scripts.


 No table joins possible.
 Data querying is highly limited.
 The cost can be unpredictable with spikes in usage.

06. What are the use cases of DynamoDB?

 Daily web analytics


 Real-time analytics
 Data warehousing
 Business Intelligence

07. What is DynamoDBMapper?

The DynamoDBMapper class is an entry point that allows access to DynamoDB endpoints, enabling users
to handle the database. Users can perform CRUD operations, run queries, and scan against tables. And this
class is only available for Java.

Read more: DynamoDBMapper.
08. What is meant by Partition Key in DynamoDB?

A partition key is a primary key composed of only a single attribute. In DynamoDB, the value of the partition
key work as the input for internal hash functions. The resulting output from that function helps determine the
partition to store the item in question.

09. How does DynamoDB Query functionality work?

DynamoDB provides 2 options to fetch data from collections as Query and Scan. When using Scan,
DynamoDB will look through the complete table for records with matching criteria, while Query uses key
constraints to perform a direct lookup for a particular data set.

In addition to the primary key, DynamoDB uses global secondary key, local secondary key, and partition
primary key to help improve flexibility and improve the read/ write operation speed.

As a result, it is fast and time effective compared to the DynamoDB Scan operation and is recommended for
most data fetching scenarios.
10. What are the key differences between Amazon DynamoDB and Amazon Aurora?

 DynamoDB is NoSQL, whereas Aurora is a relational database service.


 Aurora uses SQL for data manipulation and fetching, whereas DynamoDB uses
a specialized syntax.
 Aurora uses horizontal partitioning, whereas DynamoDB uses sharding as the
partitioning method.
 DynamoDB implements key-value and document models where Aurora
operates with relational DBMS.
 Aurora supports server-side scripting, but DynamoDB does not.
Read more: DynamoDB vs. Aurora.
11. List 5 ways to fetch data from DynamoDB

 GetItem
 Query
 Scan
 BatchGet
 TransactRead

12. What are DynamoDB Projections?

As its name suggests, the attributes in a table projected to the index are the projections. (Similar to the
GROUP BY operation in SQL). Projections can exclude all the unnecessary items and reduce the overall size
of the payload returned by the API.

We have to define the projected attributes each time we create a local secondary index. Each index must
have minimally three attributes: table partition key, index sort key, and table sort key.

Read more: DynamoDB Projections.


13. How does DynamoDB prevent data loss?

There is long-term storage and a two-tier backup system in DynamoDB to keep data loss at a minimal level.
There are three nodes for each participant, and each node contains the same data from the partition. In
addition, there is a B tree for data location and a replication log to track the changes in each node.
DynamoDB stores snapshots of these and stores them in another AWS database for a month for data
restoration when necessary.

14. Does DynamoDB support in-place atomic updates?

DynamoDB supports quick in-place atomic updates enabling users to add or remove values to sets or lists at
the atomic level using Transactions.

15. What are DynamoDB Streams?

DynamoDB Streams allow capturing the time-ordered sequence of item-level modifications made to a
DynamoDB table. This information saves in a log for 24 hours, and each modification made to the database
records sequentially to the modification order.
Read more: DynamoDB Streams.
16. What are the DynamoDB pricing tiers?

1. On-demand capacity mode: This pricing tier focuses on the incoming traffic to


the application and scales the database instance based on that. This pricing tier
is ideal when the traffic is not predictable.
2. Provisioned capacity mode: This pricing tier lets users specify the reads and
writes per second or choose auto-scaling. This option works best when the
traffic is consistent and predictable.
Read more: DynamoDB Pricing.
17. What is the maximum item size in Amazon DynamoDB?

400KB

Including both the attribute name length and the value lengths in binary format, 400KB is the maximum item
size.

18. What is DynamoDB Auto Scaling?

Most of the time, it is hard to tell beforehand about the database's workload. So, DynamoDB introduced
DynamoDB Auto Scaling to scale the read and writes capacity in response to the traffic.

Scaling up allows the tables or the global secondary indexes to gain more read and write. Whereas scaling
down automatically based on the traffic makes DynamoDB cost-effective to use.

19. What is the DynamoDB Local?

DynamoDB Local is a downloadable version of DynamoDB. It allows developing and testing applications in
the local environment without using the DynamoDB web service. Once the application is ready for
deployment, the local endpoint can be changed and redirected to the DynamoDB web service.
20. How many Global Secondary Indexes can you create on a single table?

Up to 20 Global Secondary Indexes.

21. What is Encryption at Rest?

Encryption at Rest is a security mechanism DynamoDB uses to protect sensitive data. This database service
uses the AWS KMS (AWS Key Management Service) keys to encrypt all the data at rest. There are three
types of AWS KMS keys to select from:

1. AWS owned key


2. AWS managed key
3. Customer managed key
Read more: Encryption at Rest.
22. What is DynamoDB Accelerator?

DAX (Amazon DynamoDB Accelerator) is a type of in-memory cache. Even when it is millions of requests per
second, DynamoDB Accelerator provides a performance up to 10 times the original rate. In addition, it is fully
managed and is highly available.

Read more: DAX.
23. What are DynamoDB Global Tables?

DynamoDB Global Tables allow users to replicate their data over different regions of choice. That makes the
data highly available and quickly delivered across global applications of enormous size. Every data write
made to a global table is replicated over all the regions having replicas of the same table.
24. What does BatchGetItem do in DynamoDB?

BatchGetItem allows retrieving attributes of one or more items from one or more tables using the primary
key. There is a limitation of 16MB up to which this operation can return items.

Read more: BatchGetItem.
25. What are Indexes and Secondary Indexes in DynamoDB?

An index is a data structure that enhances the data retrieval speed from the database. However, it costs
some storage space and additional writes to the database to maintain the index data structure.

DynamoDB has two types of indexes:

1. Global secondary index


2. Local secondary index
Secondary indexes allow to storage of a sub-group of attributes from a table. With that, it supports query
functionality with alternate keys.

Closing Thoughts

This article discussed 25 of the top questions you may face about DynamoDB in an interview. DynamoDB is
a developing technology becoming a huge asset amongst the NoSQL database services. Because of that,
positions are opening up for individuals passionate about working with this fantastic service.

So, I hope you find this article helpful to crush your next interview with confidence!

DynamoDB Interview Questions: DynamoDB is a powerful NoSQL database service


provided by AWS, and its popularity is on the rise. As more companies move to the
cloud, the demand for professionals with DynamoDB skills is also increasing. To ace a
DynamoDB technical interview, candidates need to have a strong understanding of the
database’s features and capabilities which can be obtained with the help of the
provided DynamoDB Technical Interview Questions. In this article, we’ll cover some of
the Latest DynamoDB Interview Questions and answers, including those designed
for freshers, to help you prepare for your next interview and stand out from the
competition. Make use of the DynamoDB Interview Questions for Freshers presented
and prepare well.

Top 50 DynamoDBInterview Questions and Answers 2023


1. What is DynamoDB?

Ans: DynamoDB is a fully-managed NoSQL database service provided by Amazon Web


Services (AWS).

2. What are the benefits of using DynamoDB?

Join Telegram

Join Whatsapp Groups

Ans: The benefits of using DynamoDB include automatic scaling, high performance,


high availability, and low latency.

3. What is NoSQL?

Ans: NoSQL refers to non-relational databases that do not use SQL for querying and
are designed to handle large amounts of unstructured data.

4. What are the primary components of DynamoDB?

Ans: The primary components of DynamoDB are tables, items, and attributes.


5. How does DynamoDB handle data partitioning?

Ans: DynamoDB partitions data automatically across multiple servers using partition


keys and sort keys.

6. What is a partition key in DynamoDB?

Ans: A partition key is the primary key for a DynamoDB table and is used to partition
data across multiple servers.

7. What is a sort key in DynamoDB?

Ans: A sort key is a secondary key for a DynamoDB table and is used to sort items
within a partition.

8. What is the maximum size of a DynamoDB item?

Ans: The maximum size of a DynamoDB item is 400 KB.

9. What is the maximum size of a DynamoDB table?

Ans: The maximum size of a DynamoDB table is 10 GB.

10. How does DynamoDB handle query optimization?

Ans: DynamoDB optimizes queries by selecting the most efficient index and partition to
retrieve data.

11. What is a secondary index in DynamoDB?

Ans: A secondary index is an index that allows you to query a DynamoDB table using
an alternate key.

12. What is a global secondary index in DynamoDB?


Ans: A global secondary index is an index that allows you to query a DynamoDB table
using an alternate key and can be created after the table is created.

13. What is a local secondary index in DynamoDB?

Ans: A local secondary index is an index that allows you to query a DynamoDB table
using an alternate key and must be created when the table is created.

14. What is eventual consistency in DynamoDB?

Ans: Eventual consistency in DynamoDB means that it may take some time for all
replicas of an item to be updated after a write operation.

15. What is strong consistency in DynamoDB?

Ans: Strong consistency in DynamoDB means that all replicas of an item are updated
before a read operation is performed.

16. What is DynamoDB Streams?

Ans: DynamoDB Streams is a feature that allows you to capture changes to a


DynamoDB table in real-time.

17. What is the difference between a DynamoDB table and a DynamoDB


stream?

Ans: A DynamoDB table stores data while a DynamoDB stream captures changes to
the data.

18. What is the maximum number of DynamoDB streams allowed per table?

Ans: The maximum number of DynamoDB streams allowed per table is two.

19. What is the maximum retention period for a DynamoDB stream?


Ans: The maximum retention period for a DynamoDB stream is 24 hours.

20. What is DynamoDB Accelerator (DAX)?

Ans: DynamoDB Accelerator (DAX) is a fully-managed in-memory cache for DynamoDB


that provides faster performance and lower latency.

21. What is the purpose of DynamoDB Auto Scaling?

Ans: DynamoDB Auto Scaling automatically adjusts the read and write capacity of a
DynamoDB table based on traffic patterns.

22. How does DynamoDB handle data encryption?

Ans: DynamoDB provides encryption at rest and in transit using AWS Key Management
Service (KMS).

23. What is the difference between a strongly-typed and a weakly-typed


attribute in DynamoDB?

Ans: A strongly-typed attribute in DynamoDB has a defined data type while a weakly-
typed attribute does not.

24. What is the difference between a PUT and a UPDATE operation in


DynamoDB?

Ans: A PUT operation in DynamoDB adds a new item or replaces an existing item while
an UPDATE operation modifies an existing item.

25. What is the maximum number of items that can be retrieved in a single
DynamoDB query?

Ans: The maximum number of items that can be retrieved in a single DynamoDB query
is 1 MB.
26. What is the difference between a query and a scan operation in
DynamoDB?

Ans: A query operation retrieves items based on a specified partition key and sort key
while a scan operation retrieves all items in a table.

27. How does DynamoDB handle pagination of query results?

Ans: DynamoDB provides pagination of query results using a LastEvaluatedKey value.

28. What is the maximum size of a BatchGetItem operation in DynamoDB?

Ans: The maximum size of a BatchGetItem operation in DynamoDB is 16 MB.

29. What is the maximum number of items that can be retrieved in a single
BatchGetItem operation?

Ans: The maximum number of items that can be retrieved in a single BatchGetItem


operation is 100.

30. What is the difference between a BatchGetItem and a Query operation in


DynamoDB?

Ans: A BatchGetItem operation retrieves multiple items from one or more tables while
a Query operation retrieves items from a single table based on a partition key and sort
key.

31. What is the maximum number of tables that can be created in a single
AWS account?

Ans: The maximum number of tables that can be created in a single AWS account is
256.
32. What is the maximum number of secondary indexes that can be created on
a single DynamoDB table?

Ans: The maximum number of secondary indexes that can be created on a single


DynamoDB table is five.

33. What is the difference between a local and a global secondary index in
DynamoDB?

Ans: A local secondary index is a secondary index that has the same partition key as
the table while a global secondary index has a different partition key than the table.

34. What is the difference between a strong and eventual consistent read in
DynamoDB?

Ans: A strong read in DynamoDB returns the most up-to-date data while an eventual
consistent read may return stale data.

35. What are the different data types supported by DynamoDB?

Ans: The four scalar data types that DynamoDB supports are:

 Binary
 Boolean
 Numbers
 Strings

Data types for collections that DynamoDB supports include:

 String Set Number Set


 Differentiated Map
 Heterogeneous Binary Set
 DynamoDB accepts Null values.
36. What is the purpose of DynamoDB point-in-time recovery (PITR)?

Ans: DynamoDB point-in-time recovery (PITR) allows you to restore a table to a


specific point in time.

37. What is the maximum number of tags that can be applied to a single
DynamoDB table?

Ans: The maximum number of tags that can be applied to a single DynamoDB table is
50.

38. What is the difference between a conditional write and a non-conditional


write in DynamoDB?

Ans: A conditional write in DynamoDB only performs the write operation if a specified
condition is met while a non-conditional write always performs the write operation.

39. What is the purpose of the DynamoDB CLI?

Ans: The DynamoDB CLI is a command-line interface for DynamoDB that allows you to
manage DynamoDB tables and data.

40. What is the purpose of the DynamoDB data mapper for JavaScript?

Ans: The DynamoDB data mapper for JavaScript is a library that allows you to map
JavaScript objects to DynamoDB tables.

41. What is the difference between a hash key and a range key in DynamoDB?

Ans: Ahash key is a required attribute for every item in a DynamoDB table and is used
to determine the partition where the item is stored. A range key is an optional attribute
that is used to sort items with the same partition key.

42. What is the purpose of DynamoDB Streams?


Ans: DynamoDB Streams is a feature of DynamoDB that captures a time-ordered
sequence of item-level modifications made to a table.

43. What is the difference between a stream view type of NEW_IMAGE and
OLD_IMAGE?

Ans: A stream view type of NEW_IMAGE includes the new values of the modified item
while a stream view type of OLD_IMAGE includes the old values of the modified item.

44. What is the difference between a stream view type of


NEW_AND_OLD_IMAGES and NEW_IMAGE?

Ans: A stream view type of NEW_AND_OLD_IMAGES includes both the new and old
values of the modified item while a stream view type of NEW_IMAGE only includes the
new values.

45. What is the maximum size of a DynamoDB transaction operation?

Ans: The maximum size of a DynamoDB transaction operation is 4 MB.

46. What is the purpose of the DynamoDB Accelerator (DAX)?

Ans: The DynamoDB Accelerator (DAX) is a fully managed, highly available, in-


memory cache for DynamoDB.

47. What is the difference between a reserved capacity and a provisioned


capacity in DynamoDB?

Ans: A reserved capacity in DynamoDB is a commitment to pay for a certain amount of


capacity for a specified period of time while a provisioned capacity is a specified
amount of capacity that is allocated to a table.

48. What is the purpose of the DynamoDB backup feature?


Ans: The DynamoDB backup feature allows you to create on-demand backups and
restore tables to a specified point in time.

49. What is the difference between a table backup and a table export in
DynamoDB?

Ans: A table backup in DynamoDB includes all of the table’s attributes and indexes
while a table export only includes the attributes that you specify.

50. What is the difference between the PutItem and BatchWriteItem


operations in DynamoDB?

Ans: The PutItem operation in DynamoDB writes a single item to a table while the
BatchWriteItem operation writes multiple items to one or more tables in a single
request.

These Top 50 DynamoDB Interview Questions and Answers provide a


comprehensive understanding of the database service. By studying and mastering
these questions, candidates can confidently navigate their DynamoDB interviews and
land their dream jobs. Follow our site @ freshersnow.com for more learnings.

Spark Interview Questions for Freshers


1. Can you tell me what is Apache Spark about?

Apache Spark is an open-source framework engine that is known for its speed, easy-to-use nature
in the field of big data processing and analysis. It also has built-in modules for graph processing,
machine learning, streaming, SQL, etc. The spark execution engine supports in-memory
computation and cyclic data flow and it can run either on cluster mode or standalone mode and can
access diverse data sources like HBase, HDFS, Cassandra, etc.
2. What are the features of Apache Spark?

 High Processing Speed: Apache Spark helps in the achievement of a very high processing speed of
data by reducing read-write operations to disk. The speed is almost 100x faster while performing in-
memory computation and 10x faster while performing disk computation.
 Dynamic Nature: Spark provides 80 high-level operators which help in the easy development of
parallel applications.
 In-Memory Computation: The in-memory computation feature of Spark due to its DAG execution
engine increases the speed of data processing. This also supports data caching and reduces the time
required to fetch data from the disk.
 Reusability: Spark codes can be reused for batch-processing, data streaming, running ad-hoc queries,
etc.
 Fault Tolerance: Spark supports fault tolerance using RDD. Spark RDDs are the abstractions designed
to handle failures of worker nodes which ensures zero data loss.
 Stream Processing: Spark supports stream processing in real-time. The problem in the earlier
MapReduce framework was that it could process only already existing data.
 Lazy Evaluation: Spark transformations done using Spark RDDs are lazy. Meaning, they do not
generate results right away, but they create new RDDs from existing RDD. This lazy evaluation
increases the system efficiency.
 Support Multiple Languages: Spark supports multiple languages like R, Scala, Python, Java which
provides dynamicity and helps in overcoming the Hadoop limitation of application development only
using Java.
 Hadoop Integration: Spark also supports the Hadoop YARN cluster manager thereby making it
flexible.
 Supports Spark GraphX for graph parallel execution, Spark SQL, libraries for Machine learning, etc.
 Cost Efficiency: Apache Spark is considered a better cost-efficient solution when compared to
Hadoop as Hadoop required large storage and data centers while data processing and replication.
 Active Developer’s Community: Apache Spark has a large developers base involved in continuous
development. It is considered to be the most important project undertaken by the Apache
community.
3. What is RDD?

RDD stands for Resilient Distribution Datasets. It is a fault-tolerant collection of parallel running
operational elements. The partitioned data of RDD is distributed and immutable. There are two
types of datasets:

 Parallelized collections: Meant for running parallelly.


 Hadoop datasets: These perform operations on file record systems on HDFS or other storage
systems.

You can download a PDF version of Spark Interview Questions.


Download PDF

4. What does DAG refer to in Apache Spark?

DAG stands for Directed Acyclic Graph with no directed cycles. There would be finite vertices and
edges. Each edge from one vertex is directed to another vertex in a sequential manner. The vertices
refer to the RDDs of Spark and the edges represent the operations to be performed on those
RDDs.

5. List the types of Deploy Modes in Spark.

There are 2 deploy modes in Spark. They are:

 Client Mode: The deploy mode is said to be in client mode when the spark driver component runs on
the machine node from where the spark job is submitted.
o The main disadvantage of this mode is if the machine node fails, then the entire job fails.
o This mode supports both interactive shells or the job submission commands.
o The performance of this mode is worst and is not preferred in production environments.
 Cluster Mode: If the spark job driver component does not run on the machine from which the spark
job has been submitted, then the deploy mode is said to be in cluster mode.
o The spark job launches the driver component within the cluster as a part of the sub-process
of ApplicationMaster.
o This mode supports deployment only using the spark-submit command (interactive shell
mode is not supported).
o Here, since the driver programs are run in ApplicationMaster, in case the program fails, the
driver program is re-instantiated.
o In this mode, there is a dedicated cluster manager (such as stand-alone, YARN, Apache Mesos,
Kubernetes, etc) for allocating the resources required for the job to run as shown in the below
architecture.
Apart from the above two modes, if we have to run the application on our local machines for unit
testing and development, the deployment mode is called “Local Mode”. Here, the jobs run on a
single JVM in a single machine which makes it highly inefficient as at some point or the other there
would be a shortage of resources which results in the failure of jobs. It is also not possible to scale
up resources in this mode due to the restricted memory and space.

6. What are receivers in Apache Spark Streaming?

Receivers are those entities that consume data from different data sources and then move them to
Spark for processing. They are created by using streaming contexts in the form of long-running
tasks that are scheduled for operating in a round-robin fashion. Each receiver is configured to use
up only a single core. The receivers are made to run on various executors to accomplish the task of
data streaming. There are two types of receivers depending on how the data is sent to Spark:

 Reliable receivers: Here, the receiver sends an acknowledegment to the data sources post successful
reception of data and its replication on the Spark storage space.
 Unreliable receiver: Here, there is no acknowledgement sent to the data sources.

7. What is the difference between repartition and coalesce?


Repartition  Coalesce

Usage repartition can increase/decrease the Spark coalesce can only reduce the number of data
number of data partitions. partitions.

Repartition creates new data partitions and Coalesce makes use of already existing partitions to
performs a full shuffle of evenly distributed data. reduce the amount of shuffled data unevenly.

Repartition internally calls coalesce with shuffle Coalesce is faster than repartition. However, if there are
parameter thereby making it slower than coalesce. unequal-sized data partitions, the speed might be slightly
Repartition  Coalesce

slower.

8. What are the data formats supported by Spark?

Spark supports both the raw files and the structured file formats for efficient reading and
processing. File formats like paraquet, JSON, XML, CSV, RC, Avro, TSV, etc are supported by Spark.

9. What do you understand by Shuffling in Spark?

The process of redistribution of data across different partitions which might or might not cause
data movement across the JVM processes or the executors on the separate machines is known as
shuffling/repartitioning. Partition is nothing but a smaller logical division of data.

It is to be noted that Spark has no control over what partition the data gets distributed across.

10. What is YARN in Spark?

 YARN is one of the key features provided by Spark that provides a central resource management
platform for delivering scalable operations throughout the cluster.
 YARN is a cluster management technology and a Spark is a tool for data processing.
Spark Interview Questions for Experienced
11. How is Apache Spark different from MapReduce?
MapReduce Apache Spark

Apache Spark can process the data both in real-time


MapReduce does only batch-wise processing of data.
and in batches.

Apache Spark runs approximately 100 times faster


MapReduce does slow processing of large data.
than MapReduce for big data processing.

MapReduce stores data in HDFS (Hadoop Distributed


Spark stores data in memory (RAM) which makes it
File System) which makes it take a long time to get the
easier and faster to retrieve data when needed.
data.

MapReduce highly depends on disk which makes it to be Spark supports in-memory data storage and caching
a high latency framework. and makes it a low latency computation framework.

Spark has its own job scheduler due to the in-


MapReduce requires an external scheduler for jobs.
memory data computation.

12. Explain the working of Spark with the help of its architecture.

Spark applications are run in the form of independent processes that are well coordinated by the
Driver program by means of a SparkSession object. The cluster manager or the resource manager
entity of Spark assigns the tasks of running the Spark jobs to the worker nodes as per one task per
partition principle. There are various iterations algorithms that are repeatedly applied to the data to
cache the datasets across various iterations. Every task applies its unit of operations to the dataset
within its partition and results in the new partitioned dataset. These results are sent back to the
main driver application for further processing or to store the data on the disk. The following
diagram illustrates this working as described above:
13. What is the working of DAG in Spark?

DAG stands for Direct Acyclic Graph which has a set of finite vertices and edges. The vertices
represent RDDs and the edges represent the operations to be performed on RDDs sequentially.
The DAG created is submitted to the DAG Scheduler which splits the graphs into stages of tasks
based on the transformations applied to the data. The stage view has the details of the RDDs of
that stage.

The working of DAG in spark is defined as per the workflow diagram below:

 The first task is to interpret the code with the help of an interpreter. If you use the Scala code, then
the Scala interpreter interprets the code.
 Spark then creates an operator graph when the code is entered in the Spark console.
 When the action is called on Spark RDD, the operator graph is submitted to the DAG Scheduler.
 The operators are divided into stages of task by the DAG Scheduler. The stage consists of detailed
step-by-step operation on the input data. The operators are then pipelined together.
 The stages are then passed to the Task Scheduler which launches the task via the cluster manager to
work on independently without the dependencies between the stages.
 The worker nodes then execute the task.
Each RDD keeps track of the pointer to one/more parent RDD along with its relationship with the
parent. For example, consider the operation val childB=parentA.map() on RDD, then we have the RDD
childB that keeps track of its parentA which is called RDD lineage.

14. Under what scenarios do you use Client and Cluster modes for deployment?

 In case the client machines are not close to the cluster, then the Cluster mode should be used for
deployment. This is done to avoid the network latency caused while communication between the
executors which would occur in the Client mode. Also, in Client mode, the entire process is lost if the
machine goes offline.
 If we have the client machine inside the cluster, then the Client mode can be used for deployment.
Since the machine is inside the cluster, there won’t be issues of network latency and since the
maintenance of the cluster is already handled, there is no cause of worry in cases of failure.

15. What is Spark Streaming and how is it implemented in Spark?

Spark Streaming is one of the most important features provided by Spark. It is nothing but a Spark
API extension for supporting stream processing of data from different sources.

 Data from sources like Kafka, Kinesis, Flume, etc are processed and pushed to various destinations
like databases, dashboards, machine learning APIs, or as simple as file systems. The data is divided
into various streams (similar to batches) and is processed accordingly.
 Spark streaming supports highly scalable, fault-tolerant continuous stream processing which is mostly
used in cases like fraud detection, website monitoring, website click baits, IoT (Internet of Things)
sensors, etc.
 Spark Streaming first divides the data from the data stream into batches of X seconds which are
called Dstreams or Discretized Streams. They are internally nothing but a sequence of multiple RDDs.
The Spark application does the task of processing these RDDs using various Spark APIs and the
results of this processing are again returned as batches. The following diagram explains the workflow
of the spark streaming process.

16. Write a spark program to check if a given keyword exists in a huge text file or not?
def keywordExists(line):
if (line.find(“my_keyword”) > -1):
return 1
return 0
lines = sparkContext.textFile(“test_file.txt”);
isExist = lines.map(keywordExists);
sum = isExist.reduce(sum);
print(“Found” if sum>0 else “Not Found”)

17. What can you say about Spark Datasets?

Spark Datasets are those data structures of SparkSQL that provide JVM objects with all the
benefits (such as data manipulation using lambda functions) of RDDs alongside Spark SQL-
optimised execution engine. This was introduced as part of Spark since version 1.6.

 Spark datasets are strongly typed structures that represent the structured queries along with their
encoders.
 They provide type safety to the data and also give an object-oriented programming interface.
 The datasets are more structured and have the lazy query expression which helps in triggering the
action. Datasets have the combined powers of both RDD and Dataframes. Internally, each dataset
symbolizes a logical plan which informs the computational query about the need for data production.
Once the logical plan is analyzed and resolved, then the physical query plan is formed that does the
actual query execution.

Datasets have the following features:

 Optimized Query feature: Spark datasets provide optimized queries using Tungsten and Catalyst
Query Optimizer frameworks. The Catalyst Query Optimizer represents and manipulates a data flow
graph (graph of expressions and relational operators). The Tungsten improves and optimizes the
speed of execution of Spark job by emphasizing the hardware architecture of the Spark execution
platform.
 Compile-Time Analysis: Datasets have the flexibility of analyzing and checking the syntaxes at the
compile-time which is not technically possible in RDDs or Dataframes or the regular SQL queries.
 Interconvertible: The type-safe feature of datasets can be converted to “untyped” Dataframes by
making use of the following methods provided by the Datasetholder:
o toDS():Dataset[T]
o toDF():DataFrame
o toDF(columName:String*):DataFrame
 Faster Computation: Datasets implementation are much faster than those of the RDDs which helps
in increasing the system performance.
 Persistent storage qualified: Since the datasets are both queryable and serializable, they can be easily
stored in any persistent storages.
 Less Memory Consumed: Spark uses the feature of caching to create a more optimal data layout.
Hence, less memory is consumed.
 Single Interface Multiple Languages: Single API is provided for both Java and Scala languages. These
are widely used languages for using Apache Spark. This results in a lesser burden of using libraries for
different types of inputs.

18. Define Spark DataFrames.

Spark Dataframes are the distributed collection of datasets organized into columns similar to SQL.
It is equivalent to a table in the relational database and is mainly optimized for big data operations.
Dataframes can be created from an array of data from different data sources such as external
databases, existing RDDs, Hive Tables, etc. Following are the features of Spark Dataframes:

 Spark Dataframes have the ability of processing data in sizes ranging from Kilobytes to Petabytes on
a single node to large clusters.
 They support different data formats like CSV, Avro, elastic search, etc, and various storage systems
like HDFS, Cassandra, MySQL, etc.
 By making use of SparkSQL catalyst optimizer, state of art optimization is achieved.
 It is possible to easily integrate Spark Dataframes with major Big Data tools using SparkCore.

19. Define Executor Memory in Spark

The applications developed in Spark have the same fixed cores count and fixed heap size defined
for spark executors. The heap size refers to the memory of the Spark executor that is controlled by
making use of the property spark.executor.memory that belongs to the -executor-memory flag. Every
Spark applications have one allocated executor on each worker node it runs. The executor memory
is a measure of the memory consumed by the worker node that the application utilizes.

20. What are the functions of SparkCore?

SparkCore is the main engine that is meant for large-scale distributed and parallel data processing.
The Spark core consists of the distributed execution engine that offers various APIs in Java,
Python, and Scala for developing distributed ETL applications.
Spark Core does important functions such as memory management, job monitoring, fault-tolerance,
storage system interactions, job scheduling, and providing support for all the basic I/O
functionalities. There are various additional libraries built on top of Spark Core which allows
diverse workloads for SQL, streaming, and machine learning. They are responsible for:

 Fault recovery
 Memory management and Storage system interactions
 Job monitoring, scheduling, and distribution
 Basic I/O functions

21. What do you understand by worker node?

Worker nodes are those nodes that run the Spark application in a cluster. The Spark driver program
listens for the incoming connections and accepts them from the executors addresses them to the
worker nodes for execution. A worker node is like a slave node where it gets the work from its
master node and actually executes them. The worker nodes do data processing and report the
resources used to the master. The master decides what amount of resources needs to be allocated
and then based on their availability, the tasks are scheduled for the worker nodes by the master.
22. What are some of the demerits of using Spark in applications?

Despite Spark being the powerful data processing engine, there are certain demerits to using
Apache Spark in applications. Some of them are:

 Spark makes use of more storage space when compared to MapReduce or Hadoop which may lead to
certain memory-based problems.
 Care must be taken by the developers while running the applications. The work should be distributed
across multiple clusters instead of running everything on a single node.
 Since Spark makes use of “in-memory” computations, they can be a bottleneck to cost-efficient big
data processing.
 While using files present on the path of the local filesystem, the files must be accessible at the same
location on all the worker nodes when working on cluster mode as the task execution shuffles
between various worker nodes based on the resource availabilities. The files need to be copied on all
worker nodes or a separate network-mounted file-sharing system needs to be in place.
 One of the biggest problems while using Spark is when using a large number of small files. When
Spark is used with Hadoop, we know that HDFS gives a limited number of large files instead of a
large number of small files. When there is a large number of small gzipped files, Spark needs to
uncompress these files by keeping them on its memory and network. So large amount of time is
spent in burning core capacities for unzipping the files in sequence and performing partitions of the
resulting RDDs to get data in a manageable format which would require extensive shuffling overall.
This impacts the performance of Spark as much time is spent preparing the data instead of
processing them.
 Spark doesn’t work well in multi-user environments as it is not capable of handling many users
concurrently.
23. How can the data transfers be minimized while working with Spark?

Data transfers correspond to the process of shuffling. Minimizing these transfers results in faster
and reliable running Spark applications. There are various ways in which these can be minimized.
They are:

 Usage of Broadcast Variables: Broadcast variables increases the efficiency of the join between large
and small RDDs.
 Usage of Accumulators: These help to update the variable values parallelly during execution.
 Another common way is to avoid the operations which trigger these reshuffles.

24. What is SchemaRDD in Spark RDD?

SchemaRDD is an RDD consisting of row objects that are wrappers around integer arrays or strings
that has schema information regarding the data type of each column. They were designed to ease
the lives of developers while debugging the code and while running unit test cases on the
SparkSQL modules. They represent the description of the RDD which is similar to the schema of
relational databases. SchemaRDD also provides the basic functionalities of the common RDDs
along with some relational query interfaces of SparkSQL.

Consider an example. If you have an RDD named Person that represents a person’s data. Then
SchemaRDD represents what data each row of Person RDD represents. If the Person has attributes
like name and age, then they are represented in SchemaRDD.

25. What module is used for implementing SQL in Apache Spark?

Spark provides a powerful module called SparkSQL which performs relational data processing
combined with the power of the functional programming feature of Spark. This module also
supports either by means of SQL or Hive Query Language. It also provides support for different
data sources and helps developers write powerful SQL queries using code transformations.
The four major libraries of SparkSQL are:

 Data Source API


 DataFrame API
 Interpreter & Catalyst Optimizer
 SQL Services

Spark SQL supports the usage of structured and semi-structured data in the following ways:
 Spark supports DataFrame abstraction in various languages like Python, Scala, and Java along with
providing good optimization techniques.
 SparkSQL supports data read and writes operations in various structured formats like JSON, Hive,
Parquet, etc.
 SparkSQL allows data querying inside the Spark program and via external tools that do the
JDBC/ODBC connections.
 It is recommended to use SparkSQL inside the Spark applications as it empowers the developers to
load the data, query the data from databases and write the results to the destination.

26. What are the different persistence levels in Apache Spark?

Spark persists intermediary data from different shuffle operations automatically. But it is
recommended to call the persist() method on the RDD. There are different persistence levels for
storing the RDDs on memory or disk or both with different levels of replication. The persistence
levels available in Spark are:

 MEMORY_ONLY: This is the default persistence level and is used for storing the RDDs as the
deserialized version of Java objects on the JVM. In case the RDDs are huge and do not fit in the
memory, then the partitions are not cached and they will be recomputed as and when needed.
 MEMORY_AND_DISK: The RDDs are stored again as deserialized Java objects on JVM. In case the
memory is insufficient, then partitions not fitting on the memory will be stored on disk and the data
will be read from the disk as and when needed.
 MEMORY_ONLY_SER: The RDD is stored as serialized Java Objects as One Byte per partition.
 MEMORY_AND_DISK_SER: This level is similar to MEMORY_ONLY_SER but the difference is that the
partitions not fitting in the memory are saved on the disk to avoid recomputations on the fly.
 DISK_ONLY: The RDD partitions are stored only on the disk.
 OFF_HEAP: This level is the same as the MEMORY_ONLY_SER but here the data is stored in the off-
heap memory.

The syntax for using persistence levels in the persist() method is: 
df.persist(StorageLevel.<level_value>)

The following table summarizes the details of persistence levels:

Persistence Level  Space Consumed  CPU time In-memory? On-disk?

MEMORY_ONLY High Low Yes No

MEMORY_ONLY_SER Low High Yes No

MEMORY_AND_DISK High Medium Some Some

MEMORY_AND_DISK_SE
Low High Some Some
R

DISK_ONLY Low High No Yes

OFF_HEAP Low High Yes (but off-heap) No

27. What are the steps to calculate the executor memory?

Consider you have the below details regarding the cluster:

Number of nodes = 10
Number of cores in each node = 15 cores
RAM of each node = 61GB

To identify the number of cores, we follow the approach:

Number of Cores = number of concurrent tasks that can be run parallelly by the executor. The optimal value as part of
a general rule of thumb is 5.

Hence to calculate the number of executors, we follow the below approach:

Number of executors = Number of cores/Concurrent Task


= 15/5
=3
Number of executors = Number of nodes * Number of executor in each node
= 10 * 3
= 30 executors per Spark job

28. Why do we need broadcast variables in Spark?

Broadcast variables let the developers maintain read-only variables cached on each machine
instead of shipping a copy of it with tasks. They are used to give every node copy of a large input
dataset efficiently. These variables are broadcasted to the nodes using different algorithms to
reduce the cost of communication.
29. Differentiate between Spark Datasets, Dataframes and RDDs.
Criteria Spark Datasets Spark Dataframes Spark RDDs

Representation Spark Datasets is a combination Spark Dataframe is a


Spark RDDs are a distributed
of Data of Dataframes and RDDs with distributed collection of data
collection of data without
features like static type safety that is organized into named
schema.
and object-oriented interfaces. columns.

Optimization Dataframes also makes use of


Datasets make use of catalyst There is no built-in
catalyst optimizer for
optimizers for optimization. optimization engine.
optimization.

Schema Datasets find out schema Dataframes also find the Schema needs to be defined
Projection automatically using SQL Engine. schema automatically. manually in RDDs.

Aggregation RDDs are slower than both


Aggregations are faster in
Speed Dataset aggregation is faster the Dataframes and the
Dataframes due to the
than RDD but slower than Datasets while performing
provision of easy and
Dataframes. even simple operations like
powerful APIs.
data grouping.

30. Can Apache Spark be used along with Hadoop? If yes, then how?

Yes! The main feature of Spark is its compatibility with Hadoop. This makes it a powerful
framework as using the combination of these two helps to leverage the processing capacity of
Spark by making use of the best of Hadoop’s YARN and HDFS features.
Hadoop can be integrated with Spark in the following ways:

 HDFS: Spark can be configured to run atop HDFS to leverage the feature of distributed replicated
storage.
 MapReduce: Spark can also be configured to run alongside the MapReduce in the same or different
processing framework or Hadoop cluster. Spark and MapReduce can be used together to perform
real-time and batch processing respectively.
 YARN: Spark applications can be configured to run on YARN which acts as the cluster management
framework.

31. What are Sparse Vectors? How are they different from dense vectors?

Sparse vectors consist of two parallel arrays where one array is for storing indices and the other for
storing values. These vectors are used to store non-zero values for saving space.

val sparseVec: Vector = Vectors.sparse(5, Array(0, 4), Array(1.0, 2.0))

 In the above example, we have the vector of size 5, but the non-zero values are there only at indices
0 and 4.
 Sparse vectors are particularly useful when there are very few non-zero values. If there are cases that
have only a few zero values, then it is recommended to use dense vectors as usage of sparse vectors
would introduce the overhead of indices which could impact the performance.
 Dense vectors can be defines as follows:

val denseVec = Vectors.dense(4405d,260100d,400d,5.0,4.0,198.0,9070d,1.0,1.0,2.0,0.0)

 Usage of sparse or dense vectors does not impact the results of calculations but when used
inappropriately, they impact the memory consumed and the speed of calculation.

32. How are automatic clean-ups triggered in Spark for handling the accumulated metadata?

The clean-up tasks can be triggered automatically either by setting spark.cleaner.ttl parameter or by


doing the batch-wise division of the long-running jobs and then writing the intermediary results on
the disk.
33. How is Caching relevant in Spark Streaming?

Spark Streaming involves the division of data stream’s data into batches of X seconds called
DStreams. These DStreams let the developers cache the data into the memory which can be very
useful in case the data of DStream is used for multiple computations. The caching of data can be
done using the cache() method or using persist() method by using appropriate persistence levels.
The default persistence level value for input streams receiving data over the networks such as
Kafka, Flume, etc is set to achieve data replication on 2 nodes to accomplish fault tolerance.

 Caching using cache method:

val cacheDf = dframe.cache()

 Caching using persist method:

val persistDf = dframe.persist(StorageLevel.MEMORY_ONLY)

The main advantages of caching are:

 Cost efficiency: Since Spark computations are expensive, caching helps to achieve reusing of data
and this leads to reuse computations which can save the cost of operations.
 Time-efficient: The computation reusage leads to saving a lot of time.
 More Jobs Achieved: By saving time of computation execution, the worker nodes can
perform/execute more jobs.

34. Define Piping in Spark.

Apache Spark provides the pipe() method on RDDs which gives the opportunity to compose
different parts of occupations that can utilize any language as needed as per the UNIX Standard
Streams. Using the pipe() method, the RDD transformation can be written which can be used for
reading each element of the RDD as String. These can be manipulated as required and the results
can be displayed as String.
35. What API is used for Graph Implementation in Spark?

Spark provides a powerful API called GraphX that extends Spark RDD for supporting graphs and
graph-based computations. The extended property of Spark RDD is called as Resilient Distributed
Property Graph which is a directed multi-graph that has multiple parallel edges. Each edge and the
vertex has associated user-defined properties. The presence of parallel edges indicates multiple
relationships between the same set of vertices. GraphX has a set of operators such as subgraph,
mapReduceTriplets, joinVertices, etc that can support graph computation. It also includes a large
collection of graph builders and algorithms for simplifying tasks related to graph analytics.

36. How can you achieve machine learning in Spark?

Spark provides a very robust, scalable machine learning-based library called MLlib. This library aims
at implementing easy and scalable common ML-based algorithms and has the features like
classification, clustering, dimensional reduction, regression filtering, etc. More information about
this library can be obtained in detail from Spark’s official documentation site
here: https://spark.apache.org/docs/latest/ml-guide.html

Conclusion
37. Conclusion

In this article, we have seen the most commonly asked Spark interview questions. Apache Spark is
the fastest-growing cluster computational platform that was designed to process big data in a
faster manner along with the compatibility to previously existing big data tools and support to
various libraries. These integrations help to build seamlessly fast and powerful applications with the
power of different computational models. Due to these reasons, Spark has become a hot and
lucrative technology, and knowing Spark will open doors to new, better, and challenging career
opportunities for Software Developers and Data Engineers.
Top 50 Azure Interview Questions You Must Prepare In 2023

Section 1: General Cloud Questions


1. What are the different types of services offered in the cloud?

IAAS VS PAAS VS SAAS

IAAS PAAS SAAS

In infrastructure as a service, you get Platform as a Service, gives You get software as a service in
the raw hardware from your cloud you a platform to publish Azure, i.e no infrastructure, no
provider as a service i.e you get a without giving the access to platform, simple software that
server which you can configure with the underlying software or you can use without purchasing
your own will. OS.  it.

For example: when you launch a


VM on Azure, you are not buying
For example: Web Apps,
For Example: Azure VM, Amazon EC2. the OS, you are basically renting
Mobile Apps in Azure.
it for the time you will be running
that instance.

2. What is cloud computing?


Explanation: It is the use of servers on the internet to “store”, “manage” and “process” data.
The difference is, instead of using your own servers, you are using someone else’s servers to do
your task, paying them for the amount of time you use it for.

3. What are the different cloud deployment models?


Explanation: Following are the three cloud deployment models:

Public Cloud: The infrastructure is owned by your cloud provider and the server that you are
using could be a multi-tenant system.

Microsoft Azure Architect Certification Training Course (AZ-305)


Explore Curriculum
Private Cloud: The infrastructure is owned by you or your cloud provider gives you that service
exclusively. For eg: Hosting your website on your servers, or hosting your website with the
cloud provider on a dedicated server.

Hybrid Cloud: When you use both Public Cloud, Private Cloud together, it is called Hybrid
Cloud. For Example: Using your in-house servers for confidential data, and the public cloud for
hosting your company’s public facing website. This type of setup would be a hybrid cloud.

4. I have some private servers on my premises, also I have distributed some of my workload on
the public cloud, what is this architecture called?

A. Virtual Private Network


B. Private Cloud
C. Virtual Private Cloud
D. Hybrid Cloud

Answer: D. Hybrid Cloud

Explanation: This type of architecture would be a hybrid cloud. Why? Because we are using
both, the public cloud, and on premises servers i.e the private cloud. To make this hybrid
architecture easy to use, wouldn’t it be better if your private and public cloud were all on the
same network (virtually). This is established by including your public cloud servers in a virtual
private cloud, and connecting virtual cloud with your on premise servers using a VPN (Virtual
Private Network).

Apart from this Azure Interview Questions Blog, if you want to get trained from professionals
on this technology, you can opt for a structured training from edureka! Click below to know
more.

Section 2: Basic Azure Questions and Answers for Freshers


5. What is Microsoft Azure and why is it used?
Explanation: As discussed above, the companies which provide the cloud service are called the
Cloud Providers. There are a lot of cloud providers out there, out of them one is Microsoft
Azure. It is used for accessing Microsoft’s infrastructure for cloud.

6. Which service in Azure is used to manage resources in Azure?

A. Application Insights
B. Azure Resource Manager
C. Azure Portal
D. Log Analytics

Answer: B Azure Resource Manager


Explanation: Azure Resource Manager is used to “manage” infrastructures which involve a no.
of azure services. It can be used to deploy, manage and delete all the resources together using
a simple JSON script.

7. Which of the following web applications can be deployed with Azure?

A. ASP.NET
B. PHP
C. WCF
D. All of the mentioned

Answer: D All of the mentioned

Explanation: Microsoft also has released SDKs for both Java and Ruby to allow applications
written in those languages to place calls to the Azure Service Platform API to the AppFabric
Service.

You can learn more from the Azure cloud engineer certification

Section 3: Azure Interview Questions and Answers for Experienced Professional


8. What are Roles and why do we use them?
Explanation: Roles are nothing servers in layman terms. These servers are managed, load
balanced, Platform as a Service virtual machines that work together to achieve a common goal.

There are 3 types of roles in Microsoft Azure:

Cloud Computing Training

AWS SOLUTIONS ARCHITECT CERTIFICATION TRAINING COURSE


AWS Solutions Architect Certification Training Course
Reviews

 5(149898)

SALESFORCE TRAINING COURSE: ADMINISTRATOR AND APP BUILDER CERTIFICATION


Salesforce Training Course: Administrator and App Builder Certification
Reviews

 5(36278)
MICROSOFT AZURE ARCHITECT CERTIFICATION TRAINING COURSE (AZ-305)
Microsoft Azure Architect Certification Training Course (AZ-305)
Reviews

 5(17028)

GOOGLE CLOUD PLATFORM (GCP) CERTIFICATION TRAINING


Google Cloud Platform (GCP) Certification Training
Reviews

 5(16115)

MICROSERVICES CERTIFICATION TRAINING COURSE


Microservices Certification Training Course
Reviews

 5(12684)

MICROSOFT AZURE DEVOPS CERTIFICATION TRAINING COURSE (AZ-400)


Microsoft Azure DevOps Certification Training Course (AZ-400)
Reviews

 5(3818)

MICROSOFT AZURE ADMINISTRATOR CERTIFICATION TRAINING: AZ-104


Microsoft Azure Administrator Certification Training: AZ-104
Reviews

 5(7812)

AWS DEVELOPER CERTIFICATION TRAINING


AWS Developer Certification Training
Reviews

 4(11850)
MIGRATING APPLICATIONS TO AWS TRAINING
Migrating Applications to AWS Training
Reviews

 5(5970)

Next

 Web Role
 Worker Role
 VM  Role

Let’s discuss each of these roles in detail:

 Web Role – A web role is basically used to deploy a website, using languages supported by the
IIS platform like, PHP, .NET etc. It is configured and customized to run web applications.
 Worker Role – A worker role is more like an help to the Web role, it used to execute background
processes unlike the Web Role which is used to deploy the website.
 VM Role – The VM role is used by a user to schedule tasks and other windows services. This role
can be used to customize the machines on which the web and worker role is running.

9. A _________ role is a virtual machine instance running Microsoft IIS Web server that can accept
and respond to HTTP or HTTPS requests.

A. Web
B. Server
C. Worker
D. Client

Answer: A. Web 

Explanation: The answer should be Web Roles, there are no roles such as Server or Client
roles. Also, Worker roles can only communicate with Azure Storage or through direct
connections to clients.

Apart from this Azure Interview Questions Blog, if you want to get trained from professionals
on this technology, you can opt for a structured training from edureka! Click below to know
more.
10. Is it possible to create a Virtual Machine using Azure Resource Manager in a Virtual Network
that was created using classic deployment?
Explanation: This is not supported. You cannot use Azure Resource Manager to deploy a
virtual machine into a virtual network that was created using classic deployment.

11. What are virtual machine scale sets in Azure?


Explanation: Virtual machine scale sets are Azure compute resource that you can use to
deploy and manage a set of identical VMs. With all the VMs configured the same, scale sets are
designed to support true autoscale, and no pre-provisioning of VMs is required. So it’s easier to
build large-scale services that target big compute, big data, and containerized workloads.

12. Are data disks supported within scale sets?


Explanation: Yes. A scale set can define an attached data disk configuration that applies to all
VMs in the set. Other options for storing data include:

 Azure files (SMB shared drives)


 OS drive
 Temp drive (local, not backed by Azure Storage)
 Azure data service (for example, Azure tables, Azure blobs)
 External data service (for example, remote database)

13. What is an Availability Set?


Explanation: An availability set is a logical grouping of VMs that allows Azure to understand
how your application is built to provide redundancy and availability. It is recommended that
two or more VMs are created within an availability set to provide for a highly available
application and to meet the 99.95% Azure SLA. When a single VM is used with Azure Premium
Storage, the Azure SLA applies for unplanned maintenance events.

Apart from this Azure Interview Questions Blog, if you want to get trained from professionals
on this technology, you can opt for a structured training from edureka! Click below to know
more.

14. What are Fault Domains?


Explanation: A fault domain is a logical group of underlying hardware that share a common
power source and network switch, similar to a rack within an on-premise data-centers. As you
create VMs within an availability set, the Azure platform automatically distributes your VMs
across these fault domains. This approach limits the impact of potential physical hardware
failures, network outages, or power interruptions.

15. What are Update Domains?


Explanation: An update domain is a logical group of underlying hardware that can undergo
maintenance or can be rebooted at the same time. As you create VMs within an availability set,
the Azure platform automatically distributes your VMs across these update domains. This
approach ensures that at least one instance of your application always remains running as the
Azure platform undergoes periodic maintenance. The order of update domains being rebooted
may not proceed sequentially during planned maintenance, but only one update domain is
rebooted at a time.

Microsoft Azure Architect Certification Training Course (AZ-305)


Weekday / Weekend BatchesSee Batch Details

16. What are Network Security Groups?


Explanation: A network security group (NSG) contains a list of Access Control List (ACL) rules
that allow or deny network traffic to subnets, NICs, or both. NSGs can be associated with either
subnets or individual NICs connected to a subnet. When an NSG is associated with a subnet,
the ACL rules apply to all the VMs in that subnet. In addition, traffic to an individual NIC can be
restricted by associating an NSG directly to a NIC.

17. Do scale sets work with Azure availability sets?


Explanation: Yes. A scale set is an implicit availability set with 5 fault domains and 5 update
domains. Scale sets of more than 100 VMs span multiple placement groups, which are
equivalent to multiple availability sets. An availability set of VMs can exist in the same virtual
network as a scale set of VMs. A common configuration is to put control node VMs (which often
require unique configuration) in an availability set and put data nodes in the scale set.

Want to upskill yourself to get ahead in your career? Check out this video

 
Here are the Top 10 Technologies to Learn in 2023 | Edureka

18. What is a break-fix issue?


Explanation: Technical problems are called break-fix issue, it is an industry term which refers
to “work involved in supporting a technology when it fails in the normal course of its function,
which requires intervention by a support organization to be restored to working order”.

19. Why is Azure Active Directory used?


Explanation: Azure Active Directory is an Identity and Access Management system. It is used to
grant access to your employees to specific products and services in your network. For example:
Salesforce.com, twitter etc. Azure AD has some in-built support for applications in its gallery
which can be added directly.

20. What happens when you exhaust the maximum failed attempts for authenticating yourself via Azure AD?
Explanation: We use a more sophisticated strategy to lock accounts. This is based on the IP
address of the request and the passwords entered. The duration of the lockout also increases
based on the likelihood that it is an attack.

21. Where can I find a list of applications that are pre-integrated with Azure AD and their capabilities?
Explanation: Azure AD has around 2600 pre-integrated applications. All pre-integrated
applications support single sign-on (SSO). SSO let you use your organizational credentials to
access your apps. Some of the applications also support automated provisioning and de-
provisioning.

Apart from this Azure Interview Questions Blog, if you want to get trained from professionals
on this technology, you can opt for a structured training from edureka! Click below to know
more.
22. How can I use applications with Azure AD that I’m using on-premises?
Explanation: Azure AD gives you an easy and secure way to connect to the web applications
you choose. You can access these applications in the same way you access your SaaS apps in
Azure AD, no need for a VPN to change your network infrastructure.

23. What is Azure Service Fabric?


Explanation: Azure Service Fabric is a distributed systems platform that makes it easy to
package, deploy, and manage scalable and reliable micro-services. Service Fabric also
addresses the significant challenges in developing and managing cloud applications.
Developers and administrators can avoid complex infrastructure problems and focus on
implementing mission-critical, demanding workloads that are scalable, reliable, and
manageable. Service Fabric represents the next-generation middleware platform for building
and managing these enterprise-class, tier-1, cloud-scale applications.

24. What is a VNet?


Explanation: VNet is a representation of your own network in the cloud. It logically isolates
your instances launched in the cloud, from the rest of your resources.

25. What are the differences between Subscription Administrator and Directory Administrator?
Explanation: By default, one is assigned the Subscription Administrator role when he/she
signs up for Azure. A subscription admin can use either a Microsoft account or a work or school
account from the directory that the Azure subscription is associated with. This role is
authorized to manage services in the Azure portal. If others need to sign in and access services
by using the same subscription, you can add them as co-admins.

Azure AD has a different set of admin roles to manage the directory and identity-related
features. These admins will have access to various features in the Azure portal or the Azure
classic portal. The admin’s role determines what they can do, like create or edit users, assign
administrative roles to others, reset user passwords, manage user licenses, or manage
domains.

You can check out the Azure online training to learn more.

26. Are there any scale limitations for customers using managed disks?
Explanation: Managed Disks eliminates the limits associated with storage accounts. However,
the number of managed disks per subscription is limited to 2000 by default.

27. What is the difference between Service Bus Queues and Storage Queues?
Explanation: The Azure Storage Queue is simple and the developer experience is quite good. It
uses the local Azure Storage Emulator and debugging is made quite easy. The tooling for Azure
Storage Queues allows you to easily peek at the top 32 messages and if the messages are in
XML or Json, you’re able to visualize their contents directly from Visual Studio Furthermore,
these queues can be purged of their contents, which is especially useful during development
and QA efforts.
The Azure Service Bus Queues are evolved and surrounded by many useful mechanisms that
make it enterprise-worthy! They are built into the Service Bus and are able to forward
messages to other Queues and Topics. They have a built-in dead-letter queue and messages
have a time to live that you control, hence messages don’t automatically disappear after 7 days.

Furthermore, Azure Service Bus Queues have the ability of deleting themselves after a
configurable amount of idle time. This feature is very practical when you create Queues for
each user, because if a user hasn’t interacted with a Queue for the past month, it automatically
gets clean it up. Its also a great way to drive costs down. You shouldn’t have to pay for storage
that you don’t need. These Queues are limited to a maximum of 80gb. Once you’ve reached
this limit your application will start receiving exceptions.

Find out our Microsoft Azure Training in Top Cities/Countries

India USA Other Cities/Countries

Bangalore New York UK

Hyderabad Chicago London

Delhi Atlanta Canada

Chennai Houston Toronto

Mumbai Los Angeles Australia

Pune Boston UAE

Kolkata Miami Dubai

Ahmedabad San Francisco Philippines

28. What is Azure Redis Cache?


Redis is an open source (BSD licensed), in-memory data structure store, used as a
database, cache and message broker. Azure Redis Cache is based on the popular open-source
Redis cache. It gives you access to a secure, dedicated Redis cache, managed by Microsoft, and
accessible from any application within Azure.  It supports data structures such as strings,
hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes
with radius queries.

Apart from this Azure Interview Questions Blog, if you want to get trained from professionals
on this technology, you can opt for a structured training from edureka! Click below to know
more.

29. Why doesn’t Azure Redis Cache have an MSDN class library reference like some of the other Azure services?
Explanation: Microsoft Azure Redis Cache is based on the popular open source Redis Cache
and can be accessed by a wide variety of Redis clients for many programming languages. Each
client has its own API that makes calls to the Redis cache instance using Redis commands.
Because each client is different, there is not one centralized class reference on MSDN, and each
client maintains its own reference documentation. In addition to the reference documentation,
there are several tutorials showing how to get started with Azure Redis Cache using different
languages and cache clients. To access these tutorials, see How to use Azure Redis Cache and
click the desired language from the language switcher at the top of the article.

30. What are Redis databases?


Explanation: Redis Databases are just a logical separation of data within the same Redis
instance. The cache memory is shared between all the databases and actual memory
consumption of a given database depends on the keys/values stored in that database. For
example, a C6 cache has 53 GB of memory. You can choose to put all 53 GB into one database
or you can split it up between multiple databases.

31. Is it possible to add an existing VM to an availability set?


Explanation: No. If you want your VM to be part of an availability set, you need to create the
VM within the set. There currently no way to add a VM to an availability set after it has been
created.

32. What are the username requirements when creating a VM?


Explanation: Usernames can be a maximum of 20 characters in length and cannot end in a
period (“.”).

The following usernames are not allowed:

33. What are the password requirements when creating a VM?


Explanation: Passwords must be 12 – 123 characters in length and meet 3 out of the following
4 complexity requirements:
 Have lower characters
 Have upper characters
 Have a digit
 Have a special character (Regex match [W_])

The following passwords are not allowed:

Apart from this Azure Interview Questions Blog, if you want to get trained from professionals
on this technology, you can opt for a structured training from edureka! Click below to know
more.

34. How much storage can I use with a virtual machine?


Explanation: Each data disk can be up to 1 TB. The number of data disks which you can use
depends on the size of the virtual machine. 

Azure Managed Disks are the new and recommended disk storage offerings for use with Azure
Virtual Machines for persistent storage of data. You can use multiple Managed Disks with each
Virtual Machine. Managed Disks offer two types of durable storage options: Premium and
Standard Managed Disks.

Azure storage accounts can also provide storage for the operating system disk and any data
disks. Each disk is a .vhd file stored as a page blob. You can learn more about this from the MS
Azure certification.

35. How can one create a Virtual Machine in Powershell?


# Define a credential object
$cred = Get-Credential
# Create a virtual machine configuration
$vmConfig = New-AzureRmVMConfig -VMName myVM -VMSize Standard_DS2 |
` Set-AzureRmVMOperatingSystem -Windows -ComputerName myVM -Credential $cred |
` Set-AzureRmVMSourceImage -PublisherName MicrosoftWindowsServer -Offer
WindowsServer `
-Skus 2016-Datacenter -Version latest | Add-AzureRmVMNetworkInterface -Id
$nic.Id
36. How to create a Network Security Group and a Network Security Group Rule?
# Create an inbound network security group rule for port 3389
$nsgRuleRDP = New-AzureRmNetworkSecurityRuleConfig -Name
myNetworkSecurityGroupRuleRDP -Protocol Tcp `
-Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -
DestinationAddressPrefix * `
-DestinationPortRange 3389 -Access Allow
# Create an inbound network security group rule for port 80
$nsgRuleWeb = New-AzureRmNetworkSecurityRuleConfig -Name
myNetworkSecurityGroupRuleWWW -Protocol Tcp `
-Direction Inbound -Priority 1001 -SourceAddressPrefix * -SourcePortRange * -
DestinationAddressPrefix * `
-DestinationPortRange 80 -Access Allow

# Create a network security group


$nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName myResourceGroup -
Location EastUS `
-Name myNetworkSecurityGroup -SecurityRules $nsgRuleRDP,$nsgRuleWeb

37. How to create a new storage account and container using Power Shell?
$storageName = "st" + (Get-Random)
New-AzureRmStorageAccount -ResourceGroupName "myResourceGroup" -AccountName
$storageName -Location "West US" -SkuName "Standard_LRS" -Kind Storage
$accountKey = (Get-AzureRmStorageAccountKey -ResourceGroupName myResourceGroup -
Name $storageName).Value[0]
$context = New-AzureStorageContext -StorageAccountName $storageName -
StorageAccountKey $accountKey
New-AzureStorageContainer -Name "templates" -Context $context -Permission
Container
38. How can one create a VM in Azure CLI?
az vm create ` --resource-group myResourceGroup ` --name myVM --image
win2016datacenter ` --admin-username azureuser ` --admin-password myPassword12
Apart from this Azure Interview Questions Blog, if you want to get trained from professionals
on this technology, you can opt for a structured training from edureka! Click below to know
more.

39. What are the various power states of a VM?


Power State Description
Starting Indicates the virtual machine is being started
Running Indicates that the virtual machine is running
Stopping Indicates that the virtual machine is being stopped
Stopped Indicates that the virtual machine is stopped
Deallocating Indicates that the virtual machine is being deallocated
Indicates that the virtual machine is completely
removed from the hypervisor but still available in the
Deallocated
control plane. Virtual Machines in the deallocated
state do not incur compute charges.
40. How can you retrieve the state of a particular VM?
Get-AzureRmVM `
-ResourceGroupName myResourceGroup `
-Name myVM `
-Status | Select @{n="Status"; e={$_.Statuses[1].Code}}
41. How can you stop a VM using Power Shell?
Stop-AzureRmVM -ResourceGroupName myResourceGroupVM -Name "myVM" -Force
42. Why was my client disconnected from the cache?
Explanation: The following are some common reason for a cache disconnect.

 Client-side causes
o The client application was redeployed.
o The client application performed a scaling operation.
o In the case of Cloud Services or Web Apps, this may be due to auto-scaling.
o The networking layer on the client side changed.
o Transient errors occurred in the client or in the network nodes between the client and the
server.
o The bandwidth threshold limits were reached.
o CPU bound operations took too long to complete.
 Server-side causes
o On the standard cache offering, the Azure Redis Cache service initiated a fail-over from
the primary node to the secondary node.
o Azure was patching the instance where the cache was deployed
o This can be for Redis server updates or general VM maintenance.

43. What is Azure Search?


Explanation: Azure Search is a cloud search-as-a-service solution that delegates server and
infrastructure management to Microsoft, leaving you with a ready-to-use service that you can
populate with your data and then use to add search to your web or mobile application. Azure
Search allows you to easily add a robust search experience to your applications using a simple
REST API or .NET SDK without managing search infrastructure or becoming an expert in search.

44. My web app still uses an old Docker container image after I’ve updated the image on Docker Hub. Does
Azure support continuous integration/deployment of custom containers?
Explanation: Yes, it does. For private registries, you can update the container by stopping and
then re-starting your web app. Alternatively, you can also change or add a dummy application
setting to force an update of your container.

Apart from this Azure Interview Questions Blog, if you want to get trained from professionals
on this technology, you can opt for a structured training from edureka! Click below to know
more.

45. What are the expected values for the Startup File section when I configure the runtime stack?
Explanation: For Node.Js, you specify the PM2 configuration file or your script file. For .NET
Core, specify your compiled DLL name. For Ruby, you can specify the Ruby script that you want
to initialize your app with.

46. How are Azure Marketplace subscriptions priced?


Explanation:

Pricing will vary based on product types. ISV software charges and Azure infrastructure costs
are charged separately through your Azure subscription. Pricing models include:
BYOL Model: Bring-your-own-license. You obtain outside of the Azure Marketplace, the right to
access or use the offering and are not charged Azure Marketplace fees for use of the offering in
the Azure Marketplace.

Free: Free SKU. Customers are not charged Azure Marketplace fees for use of the offering.

Free Software Trial: Full-featured version of the offer that is promotionally free for a limited
period of time. You will not be charged Azure Marketplace fees for use of the offering during a
trial period. Upon expiration of the trial period, customers will automatically be charged based
on standard rates for use of the offering.

Usage-Based: You are charged or billed based on the extent of your use of the offering. For
Virtual Machines Images, you are charged an hourly Azure Marketplace fee. For Data Services,
Developer services, and APIs, you are charged per unit of measurement as defined by the
offering.

Monthly Fee: You are charged or billed a fixed monthly fee for a subscription to the offering
(from the date of subscription start for that particular plan). The monthly fee is not prorated for
mid-month cancellations or unused services.

47. What is the difference between “price,” “software price,” and “total price” in the cost structure for Virtual
Machine offers in the Azure Marketplace?
Explanation: “Price” refers to the cost of the Azure Virtual Machine to run the software.
“Software price” refers to the cost of the publisher software running on an Azure Virtual
Machine. “Total price” refers to the combined total cost of the Azure Virtual Machine and the
publisher software running on an Azure Virtual Machine.

48. What are stateful and stateless microservices for Service Fabric?


Explanation: Service Fabric enables you to build applications that consist of microservices.
Stateless microservices (such as protocol gateways and web proxies) do not maintain a
mutable state outside a request and its response from the service. Azure Cloud Services worker
roles are an example of a stateless service. Stateful microservices (such as user accounts,
databases, devices, shopping carts, and queues) maintain a mutable, authoritative state
beyond the request and its response. Today’s Internet-scale applications consist of a
combination of stateless and stateful microservices.

49. What is the meaning of application partitions?


Explanation: The application partitions are a part of the Active Directory system and having
said so, they are directory partitions which are replicated to domain controllers. Usually,
domain controllers that are included in the process of directory partitions hold a replica of that
directory partition. The attributes and values of application partitions is that you can replicated
them to any specific domain controller in a forest, meaning that it could lessen replication
traffic. While the domain directory partitions transfer all their data to all of the domains, the
application partitions can focus on only one in the domain area. This makes application
partitions redundant and more available.
50. What are special Azure Regions?
Explanation: Azure has some special regions that you may wish to use when buildingyour
applications for compliance or legal purposes. These special regions include:

 US Gov Virginia and US Gov Iowa


o A physical and logical network-isolated instance of Azure for US government agencies and
partners, operated by screened US persons. Includes additional compliance certifications
such as FedRAMP and DISA. 
 China East and China North
o These regions are available through a unique partnership between Microsoft and
21Vianet, whereby Microsoft does not directly maintain the datacenters. 
 Germany Central and Germany Northeast
o These regions are available via a data trustee model whereby customer data remains in
Germany under control of T-Systems, a Deutsche Telekom company, acting as the
German data trustee.

Apart from this Azure Interview Questions Blog, if you want to get trained from professionals
on this technology, you can opt for a structured training from edureka! Click below to know
more.

Basic Cloud Computing Interview Questions for Freshers

1. Compare Cloud and On-premise Computing.


Criteria Cloud On-premise
Initial cost Low High
Maintenance and Hassle- Needs
support free attention
Upgrade Automatic Manual
Scalability Excellent Good
Pay as you go Yes No

2. What is a Cloud?

A cloud is a collaboration of networks, hardware, services, storage, and interfaces that help in
delivering computing as a service. It has three users:

1. End users
2. Business management users
3.  Cloud service providers

Learn more about AWS from this insightful  AWS blog!

3. What is Cloud Computing?

It is an advanced-stage technology implemented so that the cloud provides its services globally
as per the user requirements. It provides a method to access several servers worldwide.

4. What are some of the key features of Cloud Computing?

 Reliable
 Scalable
 Agile
 Location Independent
 Multi-tenant

5. What are the benefits of Cloud Computing?

The main benefits of Cloud Computing are:

  Data backup and storage of data


  Powerful server capabilities
  Incremented productivity
  Cost-effective and time-saving

Get started on your  cloud computing project  today and take advantage of the scalability,
flexibility, and cost-effectiveness of the cloud!

6. Mention the layers of PaaS architecture.

Cloud Controller

 Automatically creates virtual machines and controllers


  Deploys applications
 Connects to services
 Automatically scales up and down

Storage Services

 Object
 NoSQL
 Relational
 Block storage

Applications Stored in Storage Services

 Simple-to-scale applications
 Easier recovery from failure

7. What are the cloud service models?

Infrastructure as a Service (IaaS)


Platform as a Service (PaaS)
Software as a Service (SaaS)
8. Write about Function as a Service.

FaaS provides users with a fully functional platform where they can create, manage and run
their applications without having to worry about maintaining the infrastructure.

Go through the  AWS Course in London  to get a clear understanding of AWS!


Get 100% Hike!
Master Most in Demand Skills Now !

Submit

9. What are the components of Windows Azure?

Windows Azure Platform Services

 Cloud
 SQL Azure
 App Fabric: Allows fabric cloud

Want to master the core concepts of Azure? Check out our  Microsoft Azure Training
Course  and become a Certified Administrator!

10. What are the main constituents of the cloud ecosystem?

 Cloud service providers


 Cloud consumers
 Direct consumers

11. Who are the cloud consumers in a cloud ecosystem?

People and teams who use different types of cloud services, within your organization.
12. Who are the direct consumers in a cloud ecosystem?

The individuals who utilize the service provided by your company, build within a cloud
environment.

13. Who are the Cloud service providers in a cloud ecosystem?

Cloud service providers are the companies that sell their cloud services to others. Sometimes
these companies also provide cloud services internally to their partners, employees, etc.

14. What are the differences occurred in distributed operations?

FC: Master–Slave operations


Nova: Parallel process and its shared database

15. Which agent is equivalent of Nova Compute?

Azure Agent

Learn more about AWS in this  AWS Training in New York  to get ahead in your career!

16. Mention the reliability and availability of Cloud Computing.

Use of Fault Domains:

 Two virtual machines are in a single fault domain if a single piece of hardware can bring
down both virtual machines.
 Azure automatically distributes instances of a role across fault domains.
Use of Upgrade Domains:

 When a new version of the software is rolled out, then only one up-gradation of the domain
is done at a time.
 It ensures that any instance of the service is always available.
 There is an availability of the applications in multiple instances.

Look into the  Cloud Computing Tutorial  by Intellipaat.

Storage and Network Availability:

 Copies of data are stored in different domains.


 it is a mechanism to guard against DoS and DDoS attacks.
Career Transition



17. Explain the Cloud Computing Architecture.

Cloud Computing Architecture brings together two components of cloud computing – the front-
end and the back-end. It is important to bring the correct services together for the benefit of
both internal and external people. If need be, cloud management should be able to quickly
make the required changes.

18. What are the Cloud Storage Levels?

 Files
 Blocks
 Datasets
 Objects

Intermediate Cloud Computing Interview Questions

19. What are the serverless components in cloud computing


with their advantages and disadvantages?

These components allow you to create apps without the stress of managing the infrastructure.
Advantages Disadvantages
Cost-effective Can cause late responses
Increases productivity Not ideal for high-computing
operations
Scalable More vulnerable when it comes to
security
No server Debugging is challenging
management

To know more pros and cons of cloud computing check our blog on the  Advantages and
disadvantages of cloud computing  now!
20. Give the best example of the open-source Cloud Computing.

OpenStack

21. What are microservices and their importance in the cloud?

Microservices help create apps that consist of codes that are independent of one another and
the platform they were developed on.  Microservices are important in the cloud because of the
following reasons:

 Each of them is built for a particular purpose. This makes app development simpler.
 They make changes easier and quicker. 
 Their scalability makes it easier to adapt the service as needed.

22. What is an AMI? How do we implement it?

AMI is Amazon Machine Image, which basically is a copy of your root file system. It feeds the
information required to launch an instance.

We implement AMI by specifying an AMI whenever we want to launch an instance. Multiple


instances can be launched from a single AMI with the same configuration.
In the case of launching instances with different configurations, we would need to launch
different AMIs.
AMI includes one or more snapshots of your EBS volumes, in the case of instance-store-backed
AMIs, along with a template for the root volume of your instance (like an operating system, an
application server, and applications).

It launches the permissions that decide which AWS accounts can use the AMI for launching
instances. It also needs a block device mapping for specifying the volumes in order to attach
them to the instances whenever they are launched.

Interested in learning AWS? Check out our  AWS Training in Sydney!

23. Why Hybrid Clouds are so important?

Cloud Bursting:
Access capacity and specialized software are available in the public cloud and not in the private
cloud.
Examples: Virtual Amazon and Dynamo

Leveraging the best of both public and private is the Hybrid Clouds.

vCloud:

 It is a VMware cloud.
 It is expensive.
 It gives enterprise quality.

OpenStack:

 It has commodity servers and storage.


 It is less reliable.
 We can run web servers on OpenStack.
 the database is built on vCloud.

24. List the platforms that are used for large-scale Cloud
Computing.

The platforms that are used for large-scale Cloud Computing are:
 Apache Hadoop
 MapReduce

25. Mention the different types of models used for the


deployment in Cloud Computing.

The different deployment models in Cloud Computing are:

 Private Cloud
 Public Cloud
 Community Cloud
 Hybrid Cloud

Become a master of AWS by enrolling in this online  AWS Course in Toronto!

26. Explain security management in terms of Cloud Computing.

 Identity management access provides the authorization of application services.


 Access control permission is given to users to have complete controlling access of another
user who is entering into the cloud environment.
 Authentication and Authorization permits only authorized and authenticated users to
have access to the data and applications.

27. Which are the layers of Cloud Computing?

The different layers used by cloud architecture are as follows:

 CLC or Cloud Controller


 Walrus
 Cluster Controller
 Storage Controller (SC)
 Node Controller (NC)
28. Explain the full form and the usage of 'Eucalyptus' in Cloud
Computing.

The full form of ‘Eucalyptus’ is ‘Elastic Utility Computing Architecture for Linking Your Programs
to Useful Systems.

Eucalyptus is an open-source software infrastructure in Cloud Computing, which enables us to


implement clusters on the Cloud Computing platform. It is mainly used to build public, hybrid,
and private Clouds. It has the capability to convert one’s own data center into a private Cloud; it
provides its functionalities to various other organizations.

Get certified from the top  AWS Course in Singapore  now!

29. Mention the names of some large cloud providers and


databases.

 Google Bigtable
 Amazon Simple Database
 Cloud-based SQL (Sequential Query Language)

Courses you may like


30. Explain the difference between cloud and traditional data
centers.

 The traditional data center is expensive due to heating and hardware/software


issues. Mostly, the expenditure is on the maintenance of the data centers.
 Cloud is scaled up when there is an increase in demand, hence such expenditure issues are
not faced in Cloud Computing.

Learn more about AWS in  AWS Tutorial!


31. What is meant by Edge Computing?

Edge computing is a part of the distributed computing structure. It brings companies closer to
the sources of data. This benefits businesses by giving them better insights, good response
time and better bandwidth.

Advanced Cloud Computing Interview Questions for


Experienced Professionals

32. What are the uses of APIs in cloud services?

 APIs (Application Programming Interfaces) are used to eliminate the necessity to write
complete programs.
 Here, instructions are provided to make communication between one or more applications.
 Creation of applications is made easy and accessible for the link of cloud services with other
systems.

33. Mention different data center deployments of Cloud


Computing.

Cloud Computing consists of different data centers as follows:

 Containerized data centers: Containerized data centers are packages that contain a


consistent set of servers, network components, and storage delivered to large warehouse
kind of facilities. Here, each deployment is relatively unique.
 Low-density data centers: Containerized data centers promote heavy density which in turn
causes much heat and significant engineering troubles. Low-density data centers are the
solution to this problem. Here, the equipment is established far apart so that it cools down
the generated heat.

Learn the complete concepts of  AWS at Hyderabad  in 26 hours!

34. List down the three basic functioning clouds in Cloud


Computing.

 Professional cloud
 Personal cloud
 Performance cloud

35. What are the characteristics of cloud architecture that differ


from traditional cloud architecture?

The characteristics are:

  In cloud, the hardware requirement is fulfilled as per the demand created for cloud
architecture.
 Cloud architecture is capable of scaling up resources when there is a demand.
 Cloud architecture is capable of managing and handling dynamic workloads without any
point of failure.

36. What are the building blocks of cloud architecture?

 Reference architecture
 Technical architecture
 Deployment operation architecture
37. Explain AWS.

AWS stands for Amazon Web Services which is a collection of remote computing services also
known as Cloud Computing. This technology is also known as IaaS or Infrastructure as a
Service.

Be a master of AWS by enrolling in Intellipaat’s  AWS Training in Bangalore!

38. Mention the key components of AWS.

The key components of AWS are as follows:

 AWS Route 53: AWS Route 53 is a DNS (Domain Name Server) web-based service platform.
 Simple E-mail Service: Sending of e-mail is done by using a RESTFUL API call or via regular
SMTP (Simple Mail Transfer Protocol).
 Identity and Access Management: Improvised security and identity management are
provided for an AWS account.
 Simple Storage Device (S3): It is a huge storage medium, widely used for AWS services.
 Elastic Compute Cloud (EC2): It allows on-demand computing resources for hosting
applications and is essentially useful for unpredictable workloads.
 Elastic Block Stores (EBS): They are storage volumes attached to EC2 and allow the data
lifespan of a single EC2.
 CloudWatch: Amazon CloudWatch is used to monitor AWS resources, and it allows
administrators to view and collect the keys required. Access is provided so that one can set a
notification alarm in the case of trouble.

Go through the  Challenges of Cloud Computing  Blog to know about the challenges you can
face while working.
39. Explain how you can vertically scale an Amazon instance.

This is one of the essential features of AWS and cloud virtualization. We spinup a newly
developed large instance where we pause that instance and detach the root EBS volume from
the server and discard. Later, we stop our live instance and detach its root volume connected.
here, we note down the unique device ID and attach the same root volume to the new server,
and we restart it again. This results in a vertically scaled Amazon instance.

40. Explain the security usage in the Amazon Web Services


model.

 AWS supports security groups.


 Access is provided to create a security group for a jump box with SSH access only for port 22
open. Later, a webserver group and a database group are created. The webserver group
provides 80 and 443 from around the world, but only port 22 will be vital among the jump
box group. The database group allows port 3306 from the webserver group and port 22
from the jump box group. The addition of any machines to the webserver group can store in
the database. No one can directly SSH to any of our boxes.

Watch this Cloud Computing Full Course video:

41. What are reasons that make Amazon so big?

 In Amazon, the backup storage of EBS volumes is maintained by inserting the snapshot
facility via an API call or via a GUI interface like Elasticfox.
 Performance is improved by using Linux software RAID and striping across four volumes.

Hope that you will find these interviews questions for the role of Cloud Engineer useful. Do let
us know in the comments section below!
Do you want to become an AWS expert? Learn about the technology from Intellipaat’s  AWS
Certified Solutions Architect Course.

42. How does Resource Replication take place in Cloud


Computing?

Resource Replication creates duplicates of the same resource. Replication is employed when a
resource is needed more and more. The resource is virtualized to replicate cloud-based
resources. 

43. What is Containers as a Service (CaaS)?

CaaS is a system that allows developers to run, scale, manage, upload, and organize containers
by using virtualization.

A container is a software pack. It allows teams to scale their apps to highly available cloud
infrastructures. 
AWS Basic Interview Questions

1. What is AWS?

AWS (Amazon Web Services) is a platform to provide secure cloud services, database
storage, offerings for computing power, content delivery, and other services to help the
business scale and develop.

Want to learn the basics of AWS Cloud Solutions? Check out our  AWS Certification Course!

2. Give the comparison between AWS and OpenStack.


Criteria AWS OpenStack
License Amazon proprietary Open-source
Operating system Provided as per the cloud AMIs provided by AWS
administrator
Performing Through templates Through text files
repeatable
operations

3. What is the importance of buffer in Amazon Web Services?

An Elastic Load Balancer ensures that the incoming traffic is distributed optimally across
various AWS instances. A buffer will synchronize different components and make the
arrangement additionally elastic to a burst of load or traffic. The components are prone to
working in an unstable way of receiving and processing requests. The buffer creates an
equilibrium linking various apparatus and crafts them to work at an identical rate to supply
more rapid services.

4. How are Spot Instance, On-demand Instance, and Reserved


Instance different from one another?

Both Spot Instance and On-demand Instance are models for pricing.
Spot Instance On-demand Instance
With Spot Instance, customers can purchase With On-demand Instance, users can launch
compute capacity with no upfront instances at any time based on the demand.
commitment at all.
Spot Instances are spare Amazon instances On-demand Instances are suitable for the
that you can bid for. high-availability needs of applications.
When the bidding price exceeds the spot On-demand Instances are launched by users
price, the instance is automatically launched, only with the pay-as-you-go model.
and the spot price fluctuates based on
supply and demand for instances.
When the bidding price is less than the spot On-demand Instances will remain persistent
price, the instance is immediately taken away without any automatic termination from
by Amazon. Amazon.
Spot Instances are charged on an hourly On-demand Instances are charged on a per-
basis. second basis.

5. Why do we make subnets?

Creating subnets means dividing a large network into smaller ones. These subnets can be
created for several reasons. For example, creating and using subnets can help reduce
congestion by making sure that the traffic destined for a subnet stays in that subnet. This helps
in efficiently routing the to the network, which reduces the network’s load.

Learn more about AWS from this  AWS Training in New York  to get ahead in your career!

6. Is there a way to upload a file that is greater than 100


megabytes on Amazon S3?

Yes, it is possible by using the multipart upload utility from AWS. With the multipart upload
utility, larger files can be uploaded in multiple parts that are uploaded independently. You can
also decrease upload time by uploading these parts in parallel. After the upload is done, the
parts will be merged into a single object or file to create the original file from which the parts
were created.
To learn more about the  Amazon S3 bucket, read the blog.
7. What is the maximum number of S3 buckets you can create?

The maximum number of S3 buckets that can be created is 100.


Get 100% Hike!
Master Most in Demand Skills Now !

Submit

8. How can you save the data on root volume on an EBS-backed


machine?

We can save the data by overriding the terminate option

9. When should you use the classic load balancer and the
application load balancer?

The classic load balancer is used for simple load balancing of traffic across multiple EC2
instances.

While the application load balancing is used for more intelligent load balancing, based on the
multi-tier architecture or container-based architecture of the application. Application load
balancing is mostly used when there is a need to route traffic to multiple services.
Want to learn about AWS DevOps! Check out our blog on  What is AWS DevOps.

10. How many total VPCs per account/region and subnets per
VPC can you have?

We can have a total of 5 VPCs for every account/region and 200 subnets for every VPC that you
have.

11. Your organization has decided to have all its workloads on


the public cloud. But, due to certain security concerns, your
organization decides to distribute some of the workload on
private servers. You are asked to suggest a cloud architecture
for your organization. What will your suggestion be?

A hybrid cloud. The hybrid cloud architecture is where an organization can use the public cloud
for shared resources and the private cloud for confidential workloads.
Career Transition



12. Which one of the storage solutions offered by AWS would you
use if you need extremely low pricing and data archiving?

AWS Glacier is an extremely low-cost storage service offered by Amazon that is used for data
archiving and backup purposes. The longer you store data in Glacier, the lesser it will cost you.
Go through the  AWS Course in London  to get a clear understanding of AWS!

13. You have connected four instances to ELB. To automatically


terminate your unhealthy instances and replace them with new
ones, which functionality would you use?

Auto-scaling groups

14. The data on the root volumes of store-backed and EBS-


backed instances get deleted by default when they are
terminated. If you want to prevent that from happening, which
instance would you use?

EBS-backed instances. EBS-backed instances use EBS volume as their root volume. EBS volume
consists of virtual drives that can be easily backed up and duplicated by snapshots.

The biggest advantage of EBS-backed volumes is that the data can be configured to be stored
for later retrieval even if the virtual machine or the instances are shut down.
15. How will you configure an Amazon S3 bucket to serve static
assets for your public web application?

By configuring the bucket policy to provide public read access to all objects

That is all we have in our section on basic Amazon Web Services interview questions section.
Let’s move on to the next section on AWS interview questions for experienced professionals.

Intermediate AWS Interview Questions

16. Your organization wants to send and receive compliance


emails to its clients using its own email address and domain.
What service would you suggest for achieving the same in an
easy and cost-effective way?

Amazon Simple Email Service (Amazon SES), which is a cloud-based email-sending service, can


be used for this purpose.

17. Can you launch Amazon Elastic Compute Cloud (EC2)


instances with predetermined private IP addresses? If yes, then
with which Amazon service is it possible?

Yes. It is possible by using VPC (Virtual Private Cloud).

Looking for the Perfect Job Interview Attire? Worry Not. Read our perfect guide on  Interview
Outfits  to land your dream job.

18. If you launch a standby RDS, will it be launched in the same


availability zone as your primary?

No, standby instances are automatically launched in different availability zones than the
primary, making them physically independent infrastructures. This is because the whole
purpose of standby instances is to prevent infrastructure failure. So, in case the primary goes
down, the standby instance will help recover all of the data.
19. What is the name of Amazon's Content Delivery Network?

Amazon CloudFront

20. Which Amazon solution will you use if you want to accelerate
moving petabytes of data in and out of AWS, using storage
devices that are designed to be secure for data transfer?

Amazon Snowball. AWS Snowball is the data transport solution for large amounts of data that
need to be moved into and out of AWS using physical storage devices.
Courses you may like
21. If you are running your DB instance as Multi-AZ deployment,
can you use standby DB instances along with your primary DB
instance?

No, the standby DB instance cannot be used along with the primary DB instances since the
standby DB instances are supposed to be used only if the primary instance goes down.

Interested in learning AWS? Enroll in our  AWS Training in Sydney!

22. Your organization is developing a new multi-tier web


application in AWS. Being a fairly new and small organization,
there’s limited staff. But, the organization requires high
availability. This new application comprises complex queries
and table joins. Which Amazon service will be the best solution
for your organization’s requirements?

DynamoDB will be the right choice here since it is designed to be highly scalable, more than
RDS or any other relational database service.
23. You accidently stopped an EC2 instance in a VPC with an
associated Elastic IP. If you start the instance again, what will be
the result?

Elastic IP will only be disassociated from the instance if it’s terminated. If it’s stopped and
started, there won’t be any change to the instance, and no data will be lost.

24. Your organization has around 50 IAM users. Now, it wants to


introduce a new policy that will affect the access permissions of
an IAM user. How can it implement this without having to apply
the policy at the individual user level?

It is possible using AWS IAM groups, by adding users in the groups as per their roles and by
simply applying the policy to the groups.

Get certified from the top  AWS Course in Singapore  now!

Advanced AWS Interview Questions

25. Your organization is using DynamoDB for its application. This


application collects data from its users every 10 minutes and
stores it in DynamoDB. Then every day, after a particular time
interval, the data (respective of each user) is extracted from
DynamoDB and sent to S3. Then, the application visualizes this
data for the users. You are asked to propose a solution to help
optimize the backend of the application for latency at a lower
cost. What would you recommend?

ElastiCache. Amazon ElastiCache is a caching solution offered by Amazon.


It can be used to store a cached version of the application in a region closer to users so that
when requests are made by the users the cached version of the application can respond, and
hence latency will be reduced.

Become a master of AWS by going through this online  AWS Course in Toronto!

26. I created a web application with autoscaling. I observed that


the traffic on my application is the highest on Wednesdays and
Fridays between 9 AM and 7 PM. What would be the best
solution for me to handle the scaling?

Configure a policy in autoscaling to scale as per the predictable traffic patterns.


27. How would you handle a situation where the relational
database engine crashes often whenever the traffic to your RDS
instances increases, given that the replica of RDS instance is not
promoted as the master instance?

A bigger RDS instance type needs to be opted for handling large amounts of traffic, creating
manual or automated snapshots to recover data in case the RDS instance goes down.

Learn more about Amazon Web Services from our  AWS Tutorial!

28. You have an application running on your Amazon EC2


instance. You want to reduce the load on your instance as soon
as the CPU utilization reaches 100 percent. How will you do that?

It can be done by creating an autoscaling group to deploy more instances when the CPU
utilization exceeds 100 percent and distributing traffic among instances by creating a load
balancer and registering the Amazon EC2 instances with it.

Watch this video on Free AWS Full Course:

29. What would I have to do if I want to access Amazon Simple


Storage buckets and use the information for access audits?

AWS CloudTrail can be used in this case as it is designed for logging and tracking API calls, and
it has also been made available for storage solutions.

Learn the complete concepts of  AWS at Hyderabad  in 26 hours!


30. I created a key in North Virginia region to encrypt my data in
Oregon region. I also added three users to the key and an
external AWS account. Then, to encrypt an object in S3, when I
tried to use the same key, it was not listed. Where did I go
wrong?

The data and the key should be in the same region. That is, the data that has to be encrypted
should be in the same region as the one in which the key was created. In this case, the data is
in the Oregon region, whereas the key was created in the North Virginia region.

31. Suppose, you hosted an application on AWS that lets the


users render images and do some general computing. Which of
the below-listed services can you use to route the incoming user
traffic?

 Classic Load Balancer


 Application Load Balancer

 Network Load balancer

Application Load Balancer: It supports path-based routing of the traffic and hence helps in
enhancing the performance of the application structured as smaller services.
Using an application load balancer, the traffic can be routed based on the requests made. In
this case scenario, the traffic where requests are made for rendering images can be directed to
the servers only deployed for rendering images, and the traffic where requests are made for
computing can be directed to the servers deployed only for general computing purposes.

32. Suppose, I created a subnet and launched an EC2 instance in


the subnet with default settings. Which of the following options
will be ready to use on the EC2 instance as soon as it is
launched?

 Elastic IP
 Private IP

 Public IP

 Internet Gateway

Private IP. Private IP is automatically assigned to the instance as soon as it is launched. While
elastic IP has to be set manually, Public IP needs an Internet Gateway which again has to be
created since it’s a new VPC.
33. Your organization has four instances for production and
another four for testing. You are asked to set up a group of IAM
users that can only access the four production instances and not
the other four testing instances. How will you achieve this?

We can achieve this by defining tags on the test and production instances and then adding a
condition to the IAM policy that allows access to specific tags.

34. Your organization wants to monitor the read and write IOPS
for its AWS MySQL RDS instance and then send real-time alerts
to its internal operations team. Which service offered by
Amazon can help your organization achieve this scenario?

Amazon CloudWatch would help us achieve this. Since Amazon CloudWatch is a monitoring
tool offered by Amazon, it’s the right service to use in the above-mentioned scenario.

35. Which of the following services can be used if you want to


capture client connection information from your load balancer
at a particular time interval?

 Enabling access logs on your load balancer


 Enabling CloudTrail for your load balancer

 Enabling CloudWatch metrics for your load balancer

Enabling CloudTrail for your load balancer. AWS CloudTrail is an inexpensive log monitoring
solution provided by Amazon. It can provide logging information for load balancers or any
other AWS resources. The provided information can be further used for analysis.

Learn more about  AWS CloudWatch  in the blog by Intellipaat.


36. You have created a VPC with private and public subnets. In
what kind of subnet would you launch the database servers?

Database servers should be ideally launched on private subnets. Private subnets are ideal for
the backend services and databases of all applications since they are not meant to be accessed
by the users of the applications, and private subnets are not routable from the Internet.

37. Is it possible to switch from an Instance-backed root volume


to an EBS-backed root volume at any time?

No, it is not possible.

38. Can you change the instance type of the instances that are
running in your application tier and are also using autoscaling?
If yes, then how? (Choose one of the following)

 Yes, by modifying autoscaling launch configuration


 Yes, by modifying autoscaling tags configuration

 Yes, by modifying autoscaling policy configuration

 No, it cannot be changed

Yes, the instance type of such instances can be changed by modifying the autoscaling launch
configuration. The tags configuration is used to add metadata to the instances.

Do you know about the different  types of AWS Certifications? Read the Blog to find out.

39. Can you name the additional network interface that can be
created and attached to your Amazon EC2 instance launched in
your VPC?

AWS Elastic Network Interface


40. Out of the following options, where does the user specify the
maximum number of instances with the autoscaling
commands?

 Autoscaling policy configuration


 Autoscaling group

 Autoscaling tags configuration

 Autoscaling launch configuration

Autoscaling launch configuration

41. Which service provided by AWS can you use to transfer


objects from your data center, when you are using Amazon
CloudFront?

Amazon Direct Connect. It is an AWS networking service that acts as an alternative to using the
Internet to connect customers in on-premise sites with AWS.

42. You have deployed multiple EC2 instances across multiple


availability zones to run your website. You have also deployed a
Multi-AZ RDS MySQL Extra Large DB Instance. The site performs
a high number of small read and write operations per second.
After some time, you observed that there is read contention on
RDS MySQL. What would be your approach to resolve the
contention and optimize your website?

We can deploy ElastiCache in-memory cache running in every availability zone. This will help in
creating a cached version of the website for faster access in each availability zone. We can also
add an RDS MySQL read replica in each availability zone that can help with efficient and better
performance for read operations. So, there will not be any increased workload on the RDS
MySQL instance, hence resolving the contention issue.
43. Your company wants you to propose a solution so that the
company’s data center can be connected to the Amazon cloud
network. What would your proposal be?

The data center can be connected to the Amazon cloud network by establishing a virtual
private network (VPN) between the VPC and the data center. A virtual private network lets you
establish a secure pathway or tunnel from your premise or device to the AWS global network.

Are you interested in learning AWS from experts? Enroll in our  AWS Course in Bangalore  and
be a master of it!

44. Which of the following Amazon Services would you choose if


you want complex querying capabilities but not a whole data
warehouse?

 RDS
 Redshift

 ElastiCache

 DynamoDB

Amazon RDS

45. You want to modify the security group rules while it is being
used by multiple EC2 instances. Will you be able to do that? If
yes, will the new rules be implemented on all previously running
EC2 instances that were using that security group?

Yes, the security group that is being used by multiple EC2 instances can be modified. The
changes will be implemented immediately and applied to all the previously running EC2
instances without restarting the instances
46. Which one of the following is a structured data store that
supports indexing and data queries to both EC2 and S3?

 DynamoDB
 MySQL

 Aurora

 SimpleDB

SimpleDB

47. Which service offered by Amazon will you choose if you want
to collect and process e-commerce data for near real-time
analysis? (Choose any two)

 DynamoDB
 Redshift

 Aurora

 SimpleDB

DynamoDB. DynamoDB is a fully managed NoSQL database service that can be fed any type of
unstructured data. Hence, DynamoDB is the best choice for collecting data from e-commerce
websites. For near-real-time analysis, we can use Amazon Redshift.
48. If in CloudFront the content is not present at an edge
location, what will happen when a request is made for that
content?

CloudFront will deliver the content directly from the origin server. It will also store the content
in the cache of the edge location where the content was missing.

49. Can you change the private IP address of an EC2 instance


while it is in running or in a stopped state?

No, it cannot be changed. When an EC2 instance is launched, a private IP address is assigned to


that instance at boot time. This private IP address is attached to the instance for its entire
lifetime and can never be changed.

50. Which of the following options will you use if you have to
move data over long distances using the Internet, from
instances that are spread across countries to your Amazon S3
bucket?

 Amazon CloudFront
 Amazon Transfer Acceleration

 Amazon Snowball

 Amazon Glacier

Amazon Transfer Acceleration. It throttles the data transfer up to 300 percent using optimized
network paths and Amazon Content Delivery Network. Snowball cannot be used here as this
service does not support cross-region data transfer.
51. Which of the following services is a data storage system that
also has a REST API interface and uses secure HMAC-SHA1
authentication keys?

 Amazon Elastic Block Store


 Amazon Snapshot

 Amazon S3

Amazon S3. It gets various requests from applications, and it has to identify which requests are
to be allowed and which are to be denied. Amazon S3 REST API uses a custom HTTP scheme
based on a keyed HMAC for the authentication of requests.

52. What is EC2?

Launched in 2006, EC2 is a virtual machine that you can use to deploy your own servers in the
cloud, giving you OS-level control. It helps you have control over the hardware and updates,
similar to the case of on-premise servers. EC2 can run on either of these operating systems-
Microsoft and Linux. It can also support applications like Python, PHP, Apache, and more.

Learn more about  AWS Secrets Manager  in the blog by Intellipaat.

53. What is Snowball?

Snowball is an application designed for transferring terabytes of data into and outside of the
AWS cloud. It uses secured physical storage to transfer the data. Snowball is considered a
petabyte-scale data transport solution that helps with cost and time savings.

54. What is CloudWatch?

The Amazon CloudWatch is used for monitoring and managing data and getting actionable
insights for AWS, on-premise applications, etc. It helps you to monitor your entire task stack
that includes the applications, infrastructure, and services. Apart from this, CloudWatch also
assists you in optimizing your resource utilization and cost by providing analytics-driven
insights.

55. What is Elastic Transcoder?

In the AWS cloud, the Elastic Transcoder is used for converting media files into versions that
can be run/played on devices such as Tablets, PCs, Smartphones, etc. It consists of advanced
transcoding features with conversion rates starting from $ 0.0075 per minute.

Learn more about  Amazon systems manager  in the blog by intellipaat.

56. What do you understand by VPC?

VPC is the abbreviated form of Virtual Private Cloud. It allows you to launch AWS resources that
can be defined by you and fully customize the network configurations. Through VPC, you can
define and take full control of your virtual network environment. For example- you can have a
private address range, internet gateways, subnets, etc.

57. What does an AMI include?

AMI stands for Amazon Machine Images. It includes the following:

 Single or multiple Amazon Elastic Block Store (Amazon EBS) snapshots. Basically, templates
for the root volume of the instance.
 Launch permissions that let AWS accounts use AMI to launch instances.
 A block device mapping to specify what volumes to be attached to the instance during its
launch.

58. What are the Storage Classes available in Amazon S3?

The following storage classes are available in Amazon S3:

 S3 Standard- It is by and large the default storage class. In cases where no specification
about the storage class is provided while uploading the object, Amazon S3 assigns the S3
Standard storage class by default.
 Reduced Redundancy- It is assigned when non-critical, reproducible data needs to be stored.
The Reduced Redundancy Storage class is designed in a way that the above data categories
can be stored with less redundancy. 

However, it is always advisable to go ahead with the S3 Standard storage class.

59. What are the native AWS security logging capabilities?

The native AWS security logging capabilities include AWS CloudTrail, AWS Config, AWS detailed
billing reports, Amazon S3 access logs, Elastic load balancing Access logs, Amazon CloudFront
access logs, Amazon VPC Flow logs, etc. To know about native AWS security logging capabilities
in detail, click here.

Take up the  AWS Masters Certification Course  by Intellipaat and upgrade your skill set.

60. What are key pairs?

When connecting to an Amazon EC2 instance, you need to prove your identity. Key pairs are
used to execute this. Basically, a  key pair is a set of security credentials that are used during
identity proofing. It consists of a public key and a private key.

61. What are policies and what are the different types of
policies?

Policies define the permissions required to execute an operation, irrespective of the method
used to perform it. AWS supports six types of policies:

 Identity-based policies
 Resource-based policies
 Permissions boundaries
 Organizations SCPs
 ACLs
 Session policies
1- Identity-based policies- These are JSON permissions policy documents that control what
actions an identity can perform, under what conditions, and on which resources. These policies
are further classified into 2 categories:

o Managed Policies– These policies are standalone identity-based policies that


can be attached to different users, groups in your AWS environment.
o Inline policies- These policies are directly attached to a single user, group, or
role. In situations where inline policies are used, a strict one-to-one relationship
between a policy and an identity is maintained. 

2- Resource-based policies- These policies are the ones attached to a resource such as an


Amazon S3 bucket. They define which actions can be performed on the particular resource and
under what circumstances.

3- IAM permissions boundaries- They actually refer to the maximum level of permissions that
identity-based policies can grant to the specific entity.

4- Service Control Policies (SCPs)- SCPs are the maximum level of permissions for an
organization or organizational unit. 

5- Access Control lists- They define and control which principals in another AWS account can
access the particular resource.

6- Session policies- They are advanced policies that are passed as a parameter when a
temporary session is programmatically created for a role or federated user.

62. What kind of IP address can you use for your customer
gateway (CGW) address?

We can use the Internet routable IP address, which is a public IP address of your NAT device.

If you have any doubts or queries related to AWS, get them clarified by AWS experts in
our  AWS Community!
63. Which of the following is not an option in security groups?

 List of users
 Ports

 IP addresses

 List of protocols

 List of users

List of Users

Hope these top AWS Interview questions and answers for freshers and the experienced, helps
you in preparing for top AWS jobs in the Cloud market.

AWS Scenario Based Questions

64. A Company has a running Web Application Server in the N.


Virginia region and the server has a large size EBS volume of
approximately 500 GB, and to see the demand of business, the
company needs to migrate the server from the current region to
another AWS account’s Mumbai location. Which is the best way
to migrate the server from the current location to the Mumbai
region? And what information AWS administrator does require
about AWS A/C?

Create an AMI of the server running in the North Virginia region. Once the AMI is created,
The administrator will need the 12-digit account number of the #2 AWS account. This is
required for copying the AMI which we have created.

Once the AMI is successfully copied into the Mumbai region, you can launch the instance using
copied AMI in the Mumbai region. Once the instance is running and if it’s completely
operational, the server in the North Virginia region could be terminated. This is the best way to
migrate a server to a different account without any hassle.

65. Unable to ping Instance We launched a Windows 2019 IIS


server in the Ohio region and deployed a dynamic website in
this server, in addition, the webserver also connected with a
backend MS-SQL server to store and access data related to the
application. Our users were able to access the website over the
Internet. The next day our client informed us that they were
able to access the website, but weren’t able to ping the server
from the Internet. To ensure ICMP rule in Security Group, we
checked, and the Security Group had allowed rule from 0.0.0.0/0.
Would you try to help troubleshoot the issue?

If the client is able to access the website from his/her end, it means the connection is perfect
and there is no issue with connectivity and the Security Group configuration also seems
correct.

We can check the internal firewall of the Windows 2019 IIS server. If it is blocking ICMP traffic,
we should enable it.

Intellipaat provides industrial-based  SQL Training. Enroll now and learn from the experts.
66. A start-up company has a web application based in the us-
east-1 Region with multiple Amazon EC2 instances running
behind an Application Load Balancer across multiple Availability
Zones. As the company's user base grows in the us-west-1
region, the company needs a solution with low latency and
improved high availability. What should a solutions architect do
to achieve it.?

You need to notice here that, currently, the web application is in the us-ease-1, and the user
base grows in the us-east-1 region. The very first step, provision multiple EC2 instances (web
application servers) and configure an Application Load Balancer in us-west-1. Now, create
Global Accelerator in AWS Global Accelerator which uses an endpoint group that includes the
load balancer endpoints in both regions.

Read this blog about  AWS services  to know more.

67. A company currently operates a web application backed by


an Amazon RDS MySQL database. It has automated backups that
are run daily and are not encrypted. A security audit requires
future backups to be encrypted and unencrypted backups to be
destroyed. The company will make at least one encrypted
backup before destroying the old backups. What should be done
to enable encryption for future backups?

 Create a snapshot of the database.


 Copy it to an encrypted snapshot.
 Restore the database from the encrypted snapshot.
68. A company is going to launch one branch in the UK and
needs to continue with its existing main branch in the USA. The
company has almost 15 GB of data which is stored in an S3
Bucket in the Ohio region and data is stored with the default
storage class. The Company also wants to provide its updated &
stored data in the London S3 bucket using one zone accessibility
storage class to save storage costs. In addition, the company
also wants that the data must be updated automatically in S3’s
London bucket; if any data is modified or written in the S3
bucket in Ohio.

Configure Cross Region Replication Rule in the Ohio region bucket and select the destination
bucket in the London region to replicate the data and store it in the destination using one zone
IA storage class to save cost.

69. You are an AWS Architect in your company, and you are
asked to create a new VPC in the N.Virginia Region with two
Public and two Private subnets using the following CIDR blocks:

VPC CIDR = 10.10.10.0/24

Public Subnet

Subnet01 : 10.10.10.0/26
Subnet02 : 10.10.10.64/26

Private Subnet

Subnet03: 10.10.10.128/26
Subnet04: 10.10.10.192/26

Using the above CIDRs you created a new VPC, and you launched EC2 instances in all
subnets as per the need.
Now, you are facing an issue in private instances that you are unable to update
operating systems from the internet. So, what architectural changes and configurations
will you suggest to resolve the issue?

NAT G/W to be installed in one public subnet and will configure the route-table associated with
private subnets to add NAT G/W entry to provide internet access to private instances.

70. The data on the root volumes of store-backed and EBS-


backed instances get deleted by default when they are
terminated. If you want to prevent that from happening, which
instance would you use? And ensure if the EC2 instance is
restarted, the data or configuration in the EC2 instance should
not be lost.

EBS-backed instances or instances with EBS Volume. EBS-backed instances use EBS volume as
their root volume. These volumes contain Operating Systems, Applications, and Data. We can
create Snapshots from these volumes or AMI from Snapshots.

The main advantage of EBS-backed volumes is that the data can be configured to be stored for
later retrieval even if the virtual machine or instances are shut down.

71. You have an application running on an EC2 instance. You


need to reduce the load on your instance as soon as the CPU
utilization reaches 80 percent. How will you accomplish the job?

It can be done by creating an autoscaling group to deploy more instances when the CPU
utilization of the EC2 instance exceeds 80 percent and distributing traffic among instances by
creating an application load balancer and registering EC2 instances as target instances.
72. In AWS, three different storage services are available, such
as EFS, S3, and EBS. When should I use Amazon EFS vs. Amazon
S3 vs. Amazon Elastic Block Store (EBS)?

Amazon Web Services (AWS) offers cloud storage services to support a wide range of storage
workloads.

Amazon EFS is a file storage service for use with Amazon compute (EC2, containers, and
serverless) and on-premises servers. Amazon EFS provides a file system interface, file system
access semantics (such as strong consistency and file locking), and concurrently accessible
storage for up to thousands of Amazon EC2 instances.

Amazon EBS is a block-level storage service for use with Amazon EC2. Amazon EBS can deliver
performance for workloads that require the lowest latency for access to data from a single EC2
instance.

Amazon S3 is an object storage service. Amazon S3 makes data available through an Internet
API that can be accessed anywhere

73. A company's web application is using multiple Linux Amazon


EC2 instances and stores data on Amazon EBS volumes. The
company is looking for a solution to increase the resiliency of
the application in case of a failure and to provide storage that
complies with atomicity, consistency, isolation, and durability
(ACID). What should a solution architect do to meet these
requirements?

Create an Application Load Balancer with AWS Auto Scaling groups across multiple Availability
Zones. Store data on Amazon EFS and mount a target on each instance.
74. An application running on AWS uses an Amazon Aurora
Multi-AZ deployment for its database. When evaluating
performance metrics, a solutions architect discovered that the
database reads were causing high I/O and adding latency to the
write requests against the database. What should the solution
architect do to separate the read requests from the write
requests?

Create a read replica and modify the application to use the appropriate endpoint.

75. A client reports that they wanted to see an audit log of any
changes made to AWS resources in their account. What can the
client do to achieve this?

Enable AWS CloudTrail logs to be delivered to an Amazon S3 bucket

76. Usually, you have noticed that one EBS volume can be
connected with one EC2 instance, our company wants to run a
business-critical application on multiple instances in a single
region and needs to store all instances output in single storage
within the VPC. Instead of using EFS, our company is
recommending the use of multi-attach volume with instances.
As an architect, you need to suggest to them what instance type
and EBS volumes they should use.

The instance type should be EC2 Nitro-based instances and Provisioned IOPs io1 multi-attach
EBS volumes.
77. A company is using a VPC peering connection option to
connect its multiple VPCs in a single region to allow for cross
VPC communication. A recent increase in account creation and
VPCs has made it difficult to maintain the VPC peering strategy,
and the company expects to grow to hundreds of VPCs. There
are also new requests to create site-to-site VPNs with some of
the VPCs. A solutions architect has been tasked with creating a
central networking setup for multiple accounts and VPNs. Which
networking solution would you recommend to resolve it?

Configure a transit gateway with AWS Transit Gateway and connect all VPCs and VPNs.

78. An organization has multiple facilities in various continents


such as North America, Europe, and the Asia Pacific. The
organization is designing a new distributed application to
manage and optimize its global supply chain and its
manufacturing process. It needs to design the process in such a
way that the booked order in one continent should be able to
support data failover with a short Recovery Time Objective
(RTO). The uptime of the application should not impact
manufacturing, what kind of solution would you recommend as
a solution architect?

Use Amazon DynamoDB global tables feature for the database


Top 65 SQL Interview Questions You
Must Prepare For 2021

SQL Interview Questions

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

Q2. What are the different subsets of SQL?

 DDL (Data Definition Language) — It allows you to perform


various operations on the database such as CREATE, ALTER and
DELETE objects.
 DML ( Data Manipulation Language) — It allows you to access
and manipulate data. It helps you to insert, update, delete and
retrieve data from the database.
 DCL ( Data Control Language) — It allows you to control access to
the database. Example — Grant, Revoke access permissions.
Q3. What do you mean by DBMS? What are its different types?

A database is a structured collection of data.

A Database Management System ( DBMS) is a software application that


interacts with the user, applications and the database itself to capture and
analyze data.

A DBMS allows a user to interact with the database. The data stored in the
database can be modified, retrieved and deleted and can be of any type like
strings, numbers, images, etc.

There are two types of DBMS:

 Relational Database Management System: The data is


stored in relations (tables). Example — MySQL.
 Non-Relational Database Management System: There is
no concept of relations, tuples, and attributes. Example —
MongoDB

Q4. What do you mean by table and field in SQL?

A table refers to a collection of data in an organized manner in the form of rows


and columns. A field refers to the number of columns in a table. For example:
Table: StudentInformation
Field: Stu Id, Stu Name, Stu Marks

Q5. 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:

 Inner Join
 Right Join
 Left Join
 Full Join

Q6. What is the difference between CHAR and VARCHAR2 datatype in SQL?

Both Char and Varchar2 are used for characters datatype but varchar2 is used for
character strings of variable length whereas 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 length whereas varchar2(10) can store any length i.e
6,8,2 in this variable.

Q7. What is the Primary key?

 A Primary key is a column (or collection of columns) or a set of


columns that uniquely identifies each row in the table.
 Uniquely identifies a single row in the table
 Null values not allowed

Example- In the Student table, Stu_ID is the primary key.

Q8. 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. The sample of
constraints are:

 NOT NULL
 CHECK
 DEFAULT
 UNIQUE
 PRIMARY KEY
 FOREIGN KEY

Q9. What is the difference between SQL and MySQL?

SQL is a standard language which stands for Structured Query Language based
on the English language whereas MySQL is a database management system. SQL
is the core of the relational database which is used for accessing and managing
database, MySQL is an RDMS (Relational Database Management System) such
as SQL Server, Informix, etc.

Q10. What is a Unique key?

 Uniquely identifies a single row in the table.


 Multiple values allowed per table.
 Null values allowed.
Q11. What is a Foreign key?

 Foreign key maintains referential integrity by enforcing a link


between the data in two tables.
 The foreign key in the child table references the primary key in the
parent table.
 The foreign key constraint prevents actions that would destroy
links between the child and parent tables.

Q12. What do you mean by data integrity?

Data Integrity defines accuracy as well as the consistency of the data stored in a
database. It also defines integrity constraints to enforce business rules on the
data when it is entered into an application or a database.

Q13. What is the difference between clustered and non clustered index in SQL?

The differences between the clustered and non clustered index in SQL are :

1. The clustered index is used for easy retrieval of data from the
database and its faster whereas reading from a non clustered index
is relatively slower.
2. Clustered index alters the way records are stored in a database as it
sorts out rows by the column which is set to be clustered index
whereas in a non clustered index, it does not alter the way it was
stored but it creates a separate object within a table which points
back to the original table rows after searching.
3. One table can only have one clustered index whereas it can have
many non clustered index.
Q14. Write a SQL query to display the current date?

In SQL, there is a built-in function called GetDate() which helps to return the
current timestamp/date.

Q15. List the different type of joins

There are various types of joins which are used to retrieve data between the
tables. There are four types of joins, namely:

 Inner join: Inner Join in MySQL is the most common type of join.


It is used to return all the rows from multiple tables where the join
condition is satisfied.
 Left Join: Left Join in MySQL is used to return all the rows from
the left table but only the matching rows from the right table where
the join condition is fulfilled.
 Right Join: Right Join in MySQL is used to return all the rows
from the right table but only the matching rows from the left table
where the join condition is fulfilled.
 Full Join: Full join returns all the records when there is a match
in any of the tables. Therefore, it returns all the rows from the left-
hand side table and all the rows from the right-hand side table.

Q16. What do you mean by Denormalization?

Denormalization refers to a technique which is used to access data from higher


to lower forms of a database. It helps the database managers to increase the
performance of the entire infrastructure as it introduces redundancy into a table.
It adds the redundant data into a table by incorporating database queries that
combine data from various tables into a single table.

Q17. What are Entities and Relationships?

Entities: A person, place, or thing in the real world about which data can be
stored in a database. Tables store data that represents one type of entity. For
example — A bank database has a customer table to store customer information.
Customer table stores this information as a set of attributes (columns within the
table) for each customer.

Relationships: Relation or links between entities that have something to do


with each other. For example — The customer name is related to the customer
account number and contact information, which might be in the same table.
There can also be relationships between separate tables (for example, customer
to accounts).

Q18. What is an Index?

An index refers to a performance tuning method of allowing faster retrieval of


records from the table. An index creates an entry for each value and hence it will
be faster to retrieve data.
Q19. Explain different types of index.

There are three types of index namely:

Unique Index:

This index does not allow the field to have duplicate values if the column is
unique indexed. If a primary key is defined, a unique index can be applied
automatically.

Clustered Index:

This index reorders the physical order of the table and searches based on the
basis of key values. Each table can only have one clustered index.

Non-Clustered Index:

Non-Clustered Index does not alter the physical order of the table and maintains
a logical order of the data. Each table can have many nonclustered indexes.

Q20. What is Normalization and what are the advantages of it?

Normalization is the process of organizing data to avoid duplication and


redundancy. Some of the advantages are:

 Better Database organization


 More Tables with smaller rows
 Efficient data access
 Greater Flexibility for Queries
 Quickly find the information
 Easier to implement Security
 Allows easy modification
 Reduction of redundant and duplicate data
 More Compact Database
 Ensure Consistent data after modification

Q21. What is the difference between DROP and TRUNCATE commands?

DROP command removes a table and it cannot be rolled back from the database
whereas TRUNCATE command removes all the rows from the table.

Q22. Explain different types of Normalization.

There are many successive levels of normalization. These are called normal


forms. Each consecutive normal form depends on the previous one. The first
three normal forms are usually adequate.

 First Normal Form (1NF) — No repeating groups within rows


 Second Normal Form (2NF) — Every non-key (supporting)
column value is dependent on the whole primary key.
 Third Normal Form (3NF) — Dependent solely on the primary
key and no other non-key (supporting) column value.

Q23. What is ACID property in a database?

ACID stands for Atomicity, Consistency, Isolation, Durability. It is used to ensure


that the data transactions are processed reliably in a database system.

 Atomicity: Atomicity refers to the transactions that are


completely done or failed where transaction refers to a single
logical operation of a data. It means if one part of any transaction
fails, the entire transaction fails and the database state is left
unchanged.
 Consistency: Consistency ensures that the data must meet all the
validation rules. In simple words, you can say that your transaction
never leaves the database without completing its state.
 Isolation: The main goal of isolation is concurrency control.
 Durability: Durability means that if a transaction has been
committed, it will occur whatever may come in between such as
power loss, crash or any sort of error.

Q24. What do you mean by “Trigger” in SQL?

Trigger in SQL is are a special type of stored procedures that are defined to
execute automatically in place or after data modifications. It allows you to
execute a batch of code when an insert, update or any other query is executed
against a specific table.

Q25. What are the different operators available in SQL?

There are three operators available in SQL, namely:

1. Arithmetic Operators
2. Logical Operators
3. Comparison Operators

Q26. Are NULL values the 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. The NULL value
represents a value which is unavailable, unknown, assigned or not applicable
whereas zero is a number and blank space is a character.
Q27. 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.

Q28. What is subquery in SQL?

A subquery is a query inside another query where a query is defined to retrieve


data or information back from the database. In a subquery, the outer query is
called as the main query whereas the inner query is called subquery. Subqueries
are always executed first and the result of the subquery is passed on to the main
query. It can be nested inside a SELECT, UPDATE or any other query. A
subquery can also use any comparison operators such as >,< or =.

Q29. What are the different types of subquery?

There are two types of subquery namely, Correlated and Non-Correlated.

 Correlated subquery: These are queries which select the data


from a table referenced in the outer query. It is not considered as
an independent query as it refers to another table and refers the
column in a table.
 Non-Correlated subquery: This query is an independent query
where the output of subquery is substituted in the main query.

Q30. List the ways to get the count of records in a table?

To count the number of records in a table, you can use the below commands:
SELECT * FROM table1
SELECT COUNT(*) FROM table1
SELECT rows FROM sysindexes WHERE id = OBJECT_ID(table1) AND indid < 2
Q31. Write a SQL query to find the names of employees that begin with ‘A’?

To display the name of the employees that begin with ‘A’, type in the below
command:
SELECT * FROM Table_name WHERE EmpName like 'A%'

Q32. Write a SQL query to get the third-highest salary of an employee from
employee_table?
SELECT TOP 1 salary
FROM(
SELECT TOP 3 salary
FROM employee_table
ORDER BY salary DESC) AS emp
ORDER BY salary ASC;

Q33. What is the need for group functions in SQL?

Group functions work on the set of rows and return one result per group. Some
of the commonly used group functions are AVG, COUNT, MAX, MIN, SUM,
VARIANCE.

Q34. What is a Relationship and what are they?

Relation or links are between entities that have something to do with each other.
Relationships are defined as the connection between the tables in a database.
There are various relationships, namely:

 One to One Relationship.


 One to Many Relationship.
 Many to One Relationship.
 Self-Referencing Relationship.

Q35. How can you insert NULL values in a column while inserting the data?

NULL values can be inserted in the following ways:

 Implicitly by omitting column from column list.


 Explicitly by specifying NULL keyword in the VALUES clause

Q36. 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);

Q37. Why are SQL functions used?

SQL functions are used for the following purposes:

 To perform some calculations on the data


 To modify individual data items
 To manipulate the output
 To format dates and numbers
 To convert the data types

Q38. What is the need of the MERGE statement?

This statement allows conditional update or insertion of data into a table. It


performs an UPDATE if a row exists, or an INSERT if the row does not exist.
Q39. What do you mean by recursive stored procedure?

Recursive stored procedure refers to a stored procedure which calls by itself until
it reaches some boundary condition. This recursive function or procedure helps
the programmers to use the same set of code n number of times.

Q40. What is CLAUSE in SQL?

SQL clause helps to limit the result set by providing a condition to the query. A
clause helps to filter the rows from the entire set of records.

For example — WHERE, HAVING clause.

Q41. What is the difference between ‘HAVING’ CLAUSE and a ‘WHERE’


CLAUSE?

HAVING clause can be used only with SELECT statement. It is usually used in a
GROUP BY clause and whenever GROUP BY is not used, HAVING behaves like a
WHERE clause. Having Clause is only used with the GROUP BY function in a
query whereas WHERE Clause is applied to each row before they are a part of
the GROUP BY function in a query.

Q42. List the ways in which Dynamic SQL can be executed?

Following are the ways in which dynamic SQL can be executed:

 Write a query with parameters.


 Using EXEC.
 Using sp_executesql.

Q43. What are the various levels of constraints?

Constraints are the representation of a column to enforce data entity and


consistency. There are two levels of a constraint, namely:
 column-level constraint
 table-level constraint

Q44. How can you fetch common records from two tables?

You can fetch common records from two tables using INTERSECT. For example:
Select studentID from student INTERSECT Select StudentID from ExaM

Q45. List some case manipulation functions in SQL?

There are three case manipulation functions in SQL, namely:

 LOWER: This function returns the string in lowercase. It takes a


string as an argument and returns it by converting it into lower
case. Syntax:
LOWER('string')

 UPPER: This function returns the string in uppercase. It takes a


string as an argument and returns it by converting it into
uppercase. Syntax:
UPPER('string'

 INITCAP: This function returns the string with the first letter in


uppercase and the rest of the letters in lowercase. Syntax:
INITCAP(‘string’)

Q46. What are the different set operators available in SQL?

Some of the available set operators are — Union, Intersect or Minus operators.

Q47. What is an ALIAS command?

ALIAS name can be given to any table or a column. This alias name can be
referred in WHERE clause to identify a particular table or a column.
For example-
Select emp.empID, dept.Result from employee emp, department as dept where
emp.empID=dept.empID

In the above example, emp refers to alias name for employee table and dept
refers to alias name for department table.

Q48. What are the aggregate and scalar functions?

Aggregate functions are used to evaluate mathematical calculation and return a


single value. These calculations are done from the columns in a table. For
example- max(),count() are calculated with respect to numeric.

Scalar functions return a single value based on the input value. For example —
UCASE(), NOW() are calculated with respect to the string.

Q49. How can you fetch alternate records from a table?

You can fetch alternate records i.e both odd and even row numbers. For
example- To display even numbers, use the following command:

Select studentId from (Select rowno, studentId from student) where


mod(rowno,2)=0

Now, to display odd numbers:


Select studentId from (Select rowno, studentId from student) where
mod(rowno,2)=1

Q50. Name the operator which is used in the query for pattern matching?

LIKE operator is used for pattern matching, and it can be used as -.

1. % — It matches zero or more characters.


For example- select * from students where studentname like ‘a%’

_ (Underscore) — it matches exactly one character. For example- select * from


student where studentname like ‘abc_’

Q51. 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.

Q52. 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

Q53. 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.

Q54. What is a View?

A view is a virtual table which consists of a subset of data contained in a table.


Since views are not present, it takes less space to store. View can have data of one
or more tables combined and it depends on the relationship.

Q55. What are Views used for?

A view refers to a logical snapshot based on a table or another view. It is used for
the following reasons:
 Restricting access to data.
 Making complex queries simple.
 Ensuring data independence.
 Providing different views of the same data.

Q56. What is a Stored Procedure?

A Stored Procedure is a function which consists of many SQL statements to


access the database system. Several SQL statements are consolidated into a
stored procedure and execute them whenever and wherever required which
saves time and avoid writing code again and again.

Q57. List some advantages and disadvantages of Stored Procedure

Advantages:

A Stored Procedure can be used as modular programming which means create


once, store and call for several times whenever it is required. This supports faster
execution. It also reduces network traffic and provides better security to the data.

Disadvantages:

The only disadvantage of Stored Procedure is that it can be executed only in the
database and utilizes more memory in the database server.

Q58. List all the types of user-defined functions?

There are three types of user-defined functions, namely:

 Scalar Functions
 Inline Table-valued functions
 Multi-statement valued functions
Scalar returns the unit, variant defined the return clause. Other two types of
defined functions return table.

Q59. What do you mean by Collation?

Collation is defined as a set of rules that determine how data can be sorted as
well as compared. Character data is sorted using the rules that define the correct
character sequence along with options for specifying case-sensitivity, character
width, etc.

Q60. What are the different types of Collation Sensitivity?

Following are the different types of collation sensitivity:

 Case Sensitivity
 Kana Sensitivity
 Width Sensitivity
 Accent Sensitivity

Q61. What are Local and Global variables?

Local variables:

These variables can be used or exist only inside the function. These variables are
not used or referred by any other function.

Global variables:

These variables are the variables which can be accessed throughout the program.
Global variables cannot be created whenever that function is called.
Q62. 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 the PRIMARY KEY is used.

AUTOINCREMENT keyword can be used in Oracle and IDENTITY keyword can


be used in SQL SERVER.

Q63. What is a Datawarehouse?

Datawarehouse refers to a central repository of data where the data is assembled


from multiple sources of information. Those data are consolidated, transformed
and made available for the mining as well as online processing. Warehouse data
also have a subset of data called Data Marts.

Q64. What are the different authentication modes in SQL Server? How can it be
changed?

Windows mode and Mixed Mode — SQL and Windows. You can go to the below
steps to change authentication mode in SQL Server:

 Click Start> Programs> Microsoft SQL Server and click SQL


Enterprise Manager to run SQL Enterprise Manager from the
Microsoft SQL Server program group.
 Then select the server from the Tools menu.
 Select SQL Server Configuration Properties, and choose the
Security page.

Q65. What are STUFF and REPLACE function?

STUFF Function: This function is used to overwrite an existing character or


inserts a string into another string. Syntax:
STUFF(string_expression,start, length, replacement_characters)
where,

 string_expression: it is the string that will have characters


substitute
 start: This refers to the starting position
 length: It refers to the number of characters in the string which are
substituted.
 replacement_string: They are the new characters which are
injected in the string.

REPLACE function: This function is used to replace the existing characters of


all the occurrences. Syntax:
REPLACE (string_expression, search_string, replacement_string)

Here every search_string in the string_expression will be replaced with the


replacement_string.

Differences Between SQL & NoSQL


Databases — MySQL & MongoDB
Comparison

With the amount of data present in the world, it is almost next to impossible, to
manage data without proper databases. In today’s market, there are different
kinds of databases present, and deciding on the best database which suits your
business can be an overwhelming task. So, in this article on SQL vs NoSQL, I will
compare these two type of databases to help you choose which type of database
can help you and your organization.

The following topics will be covered in this article:

 What is SQL?
 What is NoSQL?
 SQL vs NoSQL
 Examples of SQL and NoSQL
 What is MySQL?
 What is MongoDB?
 MySQL vs MongoDB
 Demo: Insert values into tables and collections

So, let us get started, folks!!

What is SQL?
SQL aka Structured Query Language is the core of the relational database which
is used for accessing and managing the databases. This language is used to
manipulate and retrieve data from a structured data format in the form of tables
and holds relationships between those tables. The relations could be as follows:
 A One-to-One Relationship is when a single row in Table A is
related to a single row in Table B.
 A One-to-Many Relationship is when a single row in Table A is
related to many rows in table B.
 A Many-to-Many Relationship is when many rows in table A can be
related to many rows in table B.
 A Self -Referencing Relationship is when a record in table A is
related to the same table itself.

Now, next in this article let us understand what is NoSQL?

What is NoSQL?
NoSQL, or most commonly known as Not only SQL database, provides a
mechanism for storage and retrieval of unstructured data. This type of database
can handle a humongous amount of data and has a dynamic schema. So, a
NoSQL database has no specific query language, no or a very few relationships,
but has data stored in the format of collections and documents.

So, a database can have a ’n’ number of collections and each collection can have
‘m ‘ number of documents. Consider the example below.
As you can see from the above image, there is an Employee Database which has 2
collections i.e. the Employee and Projects Collection. Now, each of these
collections has Documents, which are basically the data values. So, you can
assume the collections to be your tables and the Documents to be your fields in
the tables.

Alright, So, now that you know what is SQL & NoSQL, let us now see, how these
databases stand against each other.

SQL vs NoSQL
So, in this face off, I will be comparing both these databases based on the
following grounds:

1. Type of Database
2. Schema
3. Database Categories
4. Complex Queries
5. Hierarchical Data Storage
6. Scalability
7. Language
8. Online Processing
9. Base Properties
10. External Support

Type of database

SQL is called a relational database as it organizes structured data into


defined rows and columns, with each table being related to the other tables in the
database.

NoSQL, on the other hand, is known as a non-relational database. This is


because data is stored in the form of collections with no or few relations between
them.

Schema

SQL needs a predefined schema for structured data. So, before you start using
SQL to extract and manipulate data, you need to make sure that your data
structure is pre-defined in the form of tables.

However, NoSQL, have a dynamic schema for unstructured data. So, if you


are using a NoSQL database, then there is no pre-defined schema present, and
the complete schema of your data completely depends upon how you wish to
store data. i.e. which fields do you want to store in documents and collections.
Database Categories

The SQL databases are . So, you can have ’n’ number of tables related to each
other and each table can have rows and columns which store data in each cell of
the table.

Now, if we talk about NoSQL Databases, then NoSQL databases have the
following categories of databases:

 Document Database– It pairs each key with a complex data


structure known as the document. It can contain many different
key-value pairs, or key array pairs or even nested documents
 Key value stores– They are the simplest NoSQL databases.
Every single item in the database is stored as an attribute name or
key together with its value.
 Graph store– They are used to store information about networks,
such as social connections. Graph stores include Neo4J and
HyperGraphDB.
 Wide column stores– Wide column stores such as Cassandra
and HBase are optimized for queries over large datasets, and store
columns of data together, instead of rows.

So, SQL databases store data in the form of tables and NoSQL databases store
data in the form of key-value pair, documents, graph databases or wide-column
stores.

Complex Queries

SQL is a better fit for complex query environment when compared to


NoSQL as the schema in SQL databases is structured and has data stored in a
tabular format. So, even if you wish to apply nested queries with many
subqueries inside the outer query, you can easily do by using the proper table
and column names.

Now, the reason why NoSQL databases isn’t a good fit for complex
queries is because the NoSQL databases aren’t queried in a standard language
like SQL.

Hierarchical Data Storage

Well, when we compare the databases on this factor, NoSQL fits better for
hierarchical storage when compared to SQL databases.

This is because as the number of tables increases, the complexity of maintaining


relations between them also keeps increasing. So, in such a scenario, you cannot
relate the humongous amount of tables with many columns in them to each
other. But, when you consider a NoSQL database, this kind of database fits better
for the hierarchical data storage as it follows the key-value pair way of storing
data which is similar to JSON data.

Scalability

The SQL databases are vertically scalable. You can load balance the data
servers by optimizing hardware such as increasing CPU, RAM, SSD, etc.

On the other hand, NoSQL databases are horizontally scalable. You can


perform load balancing by adding more servers to your cluster to handle a large
amount of traffic.
Language

The SQL databases have a specific language, and it does not vary from


databases to databases. This kind of databases uses the SQL ( Structured Query
Language ) for retrieving and manipulating the data.

The NoSQL databases have no specific language used for queries, and it


varies from database to database. In the NoSQL database, the queries are mainly
focused on the collection of documents and the language is known as UnQL
(Unstructured Query Language).

Online Processing

On comparing SQL and NoSQL, based on this factor, SQL databases are used
for heavy-duty transactional type applications. Well, this is because
SQL provides atomicity, integrity, and stability of the data. Also, you can use
NoSQL for transactions purpose, but, it is still not stable enough in high load and
for complex transactional applications. So, you can understand that SQL is
mainly used for OLTP(Online Transactional Processing) and NoSQL is mainly
used for OLAP(Online Analytical Processing).

Base Properties

SQL databases are based on ACID properties ( Atomicity, Consistency,


Isolation, and Durability) whereas the NoSQL databases are based on
the Brewers CAP theorem ( Consistency, Availability, and Partition
tolerance ).

Let me explain you the ACID properties first:

 Atomicity: Atomicity refers to the transactions that are completely


done or failed where transaction refers to a single logical operation
of a data. It means if one part of any transaction fails, the entire
transaction fails and the database state is left unchanged.
 Consistency: Consistency ensures that the data must meet all the
validation rules. In simple words, you can say that your transaction
never leaves the database without completing its state.
 Isolation: The main goal of isolation is concurrency control.
 Durability: Durability means that if a transaction has been
committed, it will occur whatever may come in between such as
power loss, crash or any sort of error.

Coming to CAP Theorem,

Brewers CAP Theorem states that a database can only achieve at most two out of
three guarantees: Consistency, Availability and Partition Tolerance. Here

 Consistency: All the nodes see the same data at the same time.
 Availability: Guarantees whether every request is successful in
failed.
 Partition Tolerance: Guarantees whether a system continues to
operate despite message loss or failure of part of the system.

NoSQL can not provide consistency and high availability together.

External Support

All the SQL vendors offer excellent support since SQL has been into existence for
more than the past 40 years. However, for some NoSQL database, only limited
experts are available and you still have to rely on community support to deploy
your large scale NoSQL deployments. This is because NoSQL has come into
existence in the late 2000s and people haven’t explored it yet much.
So, if I have to summarize the differences for SQL and NoSQL in this article on
SQL vs NoSQL, you can refer to the below table.

So, folks, with this we come to an end of this face-off between SQL and NoSQL.
Now, that we have discussed so much about SQL and NoSQL, let me show you
some examples of the same.

Examples of SQL and NoSQL


Examples of SQL and NoSQL are as follows:

Now, the most popular databases from SQL and NoSQL


are MySQL and MongoDB.
So, next in this article on SQL vs NoSQL, we will be comparing MySQL and
MongoDB.

What is MySQL?

MySQL is an open-source relational database management system that works on


many platforms. It provides multi-user access to support many storage engines
and is backed by Oracle. So, you can buy a commercial license version from
Oracle to get premium support services.

The following are the features of MySQL:


 Ease of Management — The software very easily gets
downloaded and also uses an event scheduler to schedule the tasks
automatically.
 Robust Transactional Support — Holds the ACID (Atomicity,
Consistency, Isolation, Durability) property, and also allows
distributed multi-version support.
 Comprehensive Application Development — MySQL has
plugin libraries to embed the database into any application. It also
supports stored procedures, triggers, functions, views and many
more for application development.
 High Performance — Provides fast load utilities with distinct
memory caches and table index partitioning.
 Low Total Cost Of Ownership — This reduces licensing costs
and hardware expenditures.
 Open Source & 24 * 7 Support — This RDBMS can be used on
any platform and offers 24*7 support for open source and
enterprise edition.
 Secure Data Protection — MySQL supports powerful
mechanisms to ensure that only authorized users have access to the
databases.
 High Availability — MySQL can run high-speed master/slave
replication configurations and it offers cluster servers.
 Scalability & Flexibility — With MySQL you can run deeply
embedded applications and create data warehouses holding a
humongous amount of data.

Next, in this article let us understand what is MongoDB?


What is MongoDB?

MongoDB is a non-relational database which stores the data in documents. This


type of database stores the related information together for quick query
processing.

The features of MongoDB are as follows:

 Indexing: It indexes are created in order to improve the search


performance.
 Replication: MongoDB distributes the data across different
machines.
 Ad-hoc Queries: It supports ad-hoc queries by indexing the
BSON documents & using a unique query language.
 Schemaless: It is very flexible because of its schema-less database
that is written in C++.
 Sharding: MongoDB uses sharding to enable deployments with
very large data sets and high throughput operations.

Alright, So, now that you know what is MySQL & MongoDB, let us now see, how
these databases stand against each other.

MySQL vs MongoDB
So, in this face off, I will be comparing both these databases based on the
following grounds:
1. Query Language
2. Flexibility of Schema
3. Relationships
4. Security
5. Performance
6. Support
7. Key Features
8. Replication
9. Usage
10. Active Community

Query Language
MongoDB, on the other hand,MySQL uses the Structured Query
language(SQL). This language is simple and consists of mainly DDL, DML
DCL & TCL commands to retrieve and manipulate data. MongoDB on the other
hand uses an Unstructured Query Language. So, the query language is
basically the MongoDB query language. Refer to the image below.

Flexibility of Schema

MySQL has good flexibility of schema for structured data as you just
need to clearly define tables and columns. Now, has no restrictions on
schema design. You can directly mention, a couple of documents inside a
collection without having any relations between those documents. But, the only
problem with MongoDB is that you need to optimize your schema based on how
you want to access the data .

Relationships

On comparing MySQL and MongoDB based on this factor, MySQL support


relationships with the help of JOIN statements but MongoDB does
not support the JOIN statements. But, it supports placing one document
inside another document (also known as the embedding of documents) and
multi-dimensional data types such as arrays.

Security

MySQL basically uses a privilege-based security model. This kind of


security model authenticates a user and facilitates the user privileges on a
particular database.

MongoDB, on the other hand, uses a role-based access control with a


flexible set of privileges providing security features such as authorization, and
authentication.

Performance

On comparing MySQL and MongoDB on this parameter, let me tell you


that MySQL is quite slow in comparison to MongoDB when large
databases are considered. This is mainly due to because MySQL cannot be used
for large and unstructured amounts of data.

However, MongoDB has the ability to handle large unstructured data. So, it is
faster than MySQL where large databases are considered as it allows users to
query in such a way that the load on servers are reduced.
NOTE: There is as such no hard and fast rule that MongoDB will be
faster for your data all the time, It completely depends on your data and
infrastructure.

Support

Well, both of them offer excellent support 24*7 for security fixes,


maintenance releases, bug fixes, patches, and updates. So, there is as such no
difference between both of them based on this parameter.

Key Features

You can refer to the following image for the key features of MySQL and
MongoDB:

Replication

MySQL supports master-slave replication and master-master replication.


MongoDB, on the other hand, supports built-in replication, sharding,
and auto-elections. So, with the help of auto-elections in MongoDB, you can
set up another or secondary database to automatically take over if the primary
database fails.
Usage

You can refer to the following image for understanding where to use MySQL and
MongoDB:

Active Community

On comparing MySQL with MongoDB based on this factor, MySQL


databases offer a better community than MongoDB as it is owned, and
maintained by the Oracle Corporation.

So, if I have to summarize the differences between MySQL and MongoDB, you
can refer to the below table.
So, folks, with this we come to an end of this face-off between MySQL and
MongoDB. Now, knowing so much more about MySQL and MongoDB might
have raised a question on your mind i.e.Wether businesses should go for
MySQL or MongoDB?

Well, there is no clear winner between both of them. The choice of database
completely depends upon the schema of your database and how you wish to
access it. Nevertheless, you can use MySQL when you have a fixed schema, high
transaction, low maintenance, data security with a limited budget and MongoDB
while you have an unstable schema, high availability, cloud computing, with in-
built sharding.
So, there won’t be any final verdict as to which among them is the best as each
one of these excel based on your requirement.

Now, that you know the differences between MySQL and MongoDB, next in this
article on SQL vs NoSQL let me show you how to insert data into tables and
collections in MySQL Workbench and MongoDB Compass respectively.

Demo: Insert Data Into Tables And Collections


Let us start with inserting data into a table using MySQL Workbench.

Insert data into a table using MySQL Workbench

To insert data into tables using MySQL Workbench, you can follow the below
steps:

Step 1: Open MySQL Workbench and create a connection.

Step 2: Now, once your connection has been created, open your connection and
then you will be redirected to the following dashboard.
Step 3: Now to create a database and a table, follow the below queries:
//Create Database
CREATE DATABASE Employee_Info;
//Use Database
USE Employee_Info;
//Create Table
CREATE TABLE Employee
(EmpID int,
EmpFname varchar(255),
EmpLname varchar(255),
Age int,
EmailID varchar(255),
PhoneNo int8,
Address varchar(255));

Step 4: Now, once your table is created, to insert values into the table, use the
INSERT INTO syntax as below:
//Insert Data into a Table
INSERT INTO Employee(EmpID, EmpFname, EmpLname,Age, EmailID, PhoneNo,
Address)
VALUES ('1', 'Vardhan','Kumar', '22', '[email protected]', '9876543210',
'Delhi');

Step 5: When you view your table, you will the output as below.
Now, next in this article on SQL vs NoSQL, let us see how to create database and
collections in MongoDB Compass.

Insert data into a collection using MongoDB Compass

To insert data into tables using MongoDB Compass, you can follow the below
steps:

Step 1: Open MongoDB Compass and create a host. Once your host is


created click on Connect. Refer below.
Step 2: Now, once your host is connected, to create a database, click on
the Create Database option and mention the Database and the Collection
Name.

Step 3: Now, open your database, and choose the collection. Here I have chosen
samplecollection. To add documents into the collection, choose the Insert
Document option and mention the parameters. Here I have mentioned the
EmpID and EmpName.

Now with this, we come to an end of this comparison on SQL vs NoSQL. I


Hope you guys enjoyed this article and understood all the differences. If you wish
to check out more articles on the market’s most trending technologies like
Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s
official site.
DBMS Interview Questions

Generic DBMS Interview Questions

Q1. What are the differences between a DBMS and RDBMS?

Q2. Explain the terms database and DBMS. Also, mention the different types of
DBMS.

A software application that interacts with databases, applications, and users to


capture and analyze the required data. The data stored in the database can be
retrieved, deleted, and modified based on the client’s requirement.

The different types of DBMS are as follows:

 Relational DBMS (RDBMS): This type of DBMS, uses a


structure that allows the users to access data in relation to another
piece of data in a database. In this type of DBMS, data is stored in
the form of tables.
 Hierarchical DBMS: As the name suggests, this type of DBMS
has a structure similar to that of a tree, wherein the nodes
represent records and the branches of the tree represent fields.
 Network DBMS: This type of DBMS supports many-to-many
relations wherein multiple member records can be linked.
 Object-oriented DBMS: Uses small individual software called to
object to store pieces of data and the instructions for the actions to
be done with the data.
Q3. What are the advantages of DBMS?

The advantages of DBMS are as follows:

 Data Independence: Allows to change the structure of the data


without affecting the structure of any of the running application
programs.
 Sharing of Data: Multiple users can use data from the same
database simultaneously.
 Integrity constraints: These constraints allow the data to be
stored in a database in a refined manner.
 Redundancy control: Supports a mechanism to control the
redundancy of data by integrating all the data into a single
database.
 Provide backup and recovery facility: Provides a feature of
‘backup and recovery’ to automatically create the data backup and
restore the data as and when required.

Q4. Mention the different languages present in DBMS

The different languages present in DBMS are as follows:

 Data Definition Language(DDL) — Consists of commands


which are used to define the database.
 Data Manipulation Language(DML) — Consists of commands
which are used to manipulate the data present in the database.
 Data Control Language(DCL) — Consists of commands which
deal with the user permissions and controls of the database system.
 Transaction Control Language(TCL) — Consist of commands
which deal with the transaction of the database.
Q5. What do you understand by query optimization?

Query optimization is the phase which identifies a plan for evaluation query that
has the least estimated cost. This phase comes into the picture when there are a
lot of algorithms and methods to execute the same task.

The advantages of query optimization are as follows:

 The output is provided faster


 A larger number of queries can be executed in less time
 Reduces time and space complexity

Q6. Do we consider NULL values the same as that of blank space or zero?

A NULL value is not at all same as that of zero or a blank space. The NULL value
represents a value that is unavailable, unknown, assigned, or not applicable
whereas zero is a number and blank space is a character.

Q7. What do you understand by aggregation and atomicity?

This is a feature of the E-R model which allows a relationship set to participate in
another relationship set.

This property states that a database modification must either follow all the rules
or nothing at all. So, if one part of the transaction fails, then the entire
transaction fails.

Q8. What are the different levels of abstraction in the DBMS?

There are three levels of data abstraction in DBMS. They are:

 Physical Level: It is the lowest level of abstraction and describes


how the data is stored.
 Logical Level: This is the next level of abstraction after the
Physical level. This layer determines what data is stored in the
database, and what is the relationship between the data points.
 View Level: The View Level is the highest level of abstraction and
it describes only a part of the entire database.

Q9. What is an entity-relationship model?

It is a diagrammatic approach to database design, where you represent real-


world objects as entities and mention relationships between them. This approach
helps the team of DBAs’ to understand the schema easily.

Q10. What do you understand by the terms Entity, Entity Type, and Entity Set in
DBMS?

 Entity: An entity is a real-world object having attributes, which are


nothing but characteristics of that particular object. For example,
an employee can be an entity. This particular entity can have
attributes such as empid, empname, etc.
 Entity Type: Entity type is nothing but a collection of entities,
having the same attributes. Generally, an entity type refers to one
or more related tables in a particular database. So, you can
understand, entity type as a characteristic which uniquely identifies
the entity. For example, An employee can have attributes such as
empid, empname, department, etc.
 Entity Set: An entity set is the collection of all the entities of a
particular entity type in a database. For example, a set of
employees, a set of companies, and a set of people can come under
an entity set.
Q11. What are relationships and mention different types of relationships in the
DBMS

A relationship in DBMS is the scenario where two entities are related to each
other. In such a scenario, the table consisting of foreign key references to that of
a primary key of the other table.

The different types of relationships in DBMS are as follows:

 One-to-One Relationship — Used when a single row in Table A


is related to a single row in Table B.
 One-to-Many Relationship — Used when a single row in Table
A is related to many rows in table B.
 Many-to-Many Relationship — Used when many rows in table
A can be related to many rows in table B.
 Self -Referencing Relationship — Used when a record in table
A is related to the same table itself.

Q12. What is concurrency control?

This is a process of managing simultaneous operations in a database so that


database integrity is not compromised. The following are the two approaches
involved in concurrency control:

 Optimistic approach — Involves versioning


 Pessimistic approach — Involves locking

Q13. What are the ACID properties in DBMS?

ACID stands for Atomicity, Consistency, Isolation, Durability. It is used to ensure


that the data transactions are processed reliably in a database system.
 Atomicity: Atomicity refers to those transactions which are
completely successful or failed. Here each transaction refers to a
single logical operation of data. So, even if one part of any
transaction fails, the entire transaction fails and the database state
is left unchanged.
 Consistency: Consistency ensures that the data must meet all the
validation rules. In simple words, you can say that your transaction
never leaves the database without completing its state.
 Isolation: The main goal of isolation is concurrency control.
 Durability: Durability means that if a transaction has been
committed, it will occur whatever may be the scenario.

Q14. What is normalization and what are the different types of normalization?

The process of organizing data to avoid any duplication of data and redundancy
is known as Normalization. There are many successive levels of normalization
which are known as normal forms. Each consecutive normal form depends on
the previous one. The following are the first three normal forms. Apart from
these, you have higher normal forms such as BCNF.

 First Normal Form (1NF) — No repeating groups within rows


 Second Normal Form (2NF) — Every non-key (supporting)
column value is dependent on the whole primary key.
 Third Normal Form (3NF) — Dependent solely on the primary
key and no other non-key (supporting) column value.

Q15. What are the different types of keys in the database?

There are mainly 7 types of Keys, that can be considered in a database. I am


going to consider the below tables to explain to you the various keys.
Fig 1: Different Types of Keys in Database — DBMS Interview Questions

 Candidate Key — This is a set of attributes that can uniquely


identify a table. Each table can have more than a candidate key.
Apart from this, out of all the candidate keys, one key can be
chosen as the Primary key. In the above example, since
CustomerID and PanNumber can uniquely identify every tuple,
they would be considered as a Candidate Key.
 Super Key — This is a set of attributes that can uniquely identify a
tuple. So, a candidate key, primary key, and a unique key is a
superkey, but vice-versa isn’t true.
 Primary Key — This is a set of attributes that are used to uniquely
identify every tuple. In the above example, since CustomerID and
PanNumber are candidate keys, any one of them can be chosen as a
Primary Key. Here CustomerID is chosen as the primary key.
 Unique Key — The unique key is similar to the primary key, but
allows NULL values in the column. Here the PanNumber can be
considered as a unique key.
 Alternate Key — Alternate Keys are the candidate keys, which are
not chosen as a Primary key. From the above example, the alternate
key is PanNumber
 Foreign Key — An attribute that can only take the values present
as the values of some other attribute, is the foreign key to the
attribute to which it refers. in the above example, the CustomerID
from the Customers Table is referred to as the CustomerID from
the Customer_Payment Table.
 Composite Key — A composite key is a combination of two or
more columns that identify each tuple uniquely. Here, the
CustomerID and Date_of_Payment can be grouped together to
uniquely identify every tuple in the table.

Q16. What do you understand by correlated subqueries in DBMS?

A correlated subquery is also a sort of subquery reliant on another query. So,


when subqueries are executed for each of the rows of outer queries, then they are
termed as correlated subqueries. Each subquery is executed a single time for
every row of the outer query.

You can also understand correlated subqueries as those queries, which are used
for row-by-row processing by the parent statement. Here, the parent statement
can be SELECT, UPDATE or DELETE statement.

Q17. Explain Database partitioning and its importance.

Data partitioning is the process of dividing a logical database into independent


units for the betterment of availability, performance, and manageability.
The importance of database partitioning is as follows:

 Enables you to access large parts of a specific partition


 Cheap and slower storage can be used to store data
 Improves query performance

Q18. What do you understand by functional dependency and transitive


dependency in DBMS?

Functional Dependency: A functional dependency is a constraint that is used


in describing the relationship among different attributes in a relation.

Consider a relation “A1” having attributes X and Y. The functional dependency


among these two attributes will be Example: X -> Y, this implies that Y is
functionally dependent on X.

Transitive Dependency: A transitive dependency is a constraint that can only


occur in a relation of three or more attributes.

Example: Consider a relation “A1” having attributes X, Y, and Z. Now, X->Z is


said to hold transitive dependency, only if the following functional dependencies
hold true:

Q19. What is the difference between two and three-tier architectures?


Q20. Mention the differences between Unique Key and Primary Key

Q21. What is a checkpoint in DBMS and when does it occur?

A checkpoint is a mechanism where all the previous logs are removed from the
system and are permanently stored on the storage disk. So, basically,
checkpoints are those points from where the transaction log record can be used
to recover all the committed data up to the point of crash.

Next, le us discuss one of the most commonly asked DBMS interview questions,
that is:
Q22. Mention the differences between Trigger and Stored Procedures

Q23. What are the differences between Hash join, Merge join and Nested
loops?

Q24. What do you understand by Proactive, Retroactive and Simultaneous


Update?

 Proactive Update: These updates are applied to the database


before it becomes effective in the real-world environment.
 Retroactive Update: These retroactive updates are applied to a
database after it becomes effective in the real-world environment.
 Simultaneous Update: These updates are applied to the
database at the same instance of time as it becomes effective in a
real-world environment.
Q25. What are indexes? Mention the differences between the clustered and
non-clustered index

Indexes are data structures responsible for improving the speed of data retrieval
operations on a table. This data structure uses more storage space to maintain
extra copies of data by using additional writes. So, indexes are mainly used for
searching algorithms, where you wish to retrieve data in a quick manner.

The differences between clustered and non-clustered index are as follows:

Q26. What do you understand by intension and extension?

Intension: Intension or most commonly known as Database schema defines


the description of the database. This is specified during the database design and
mostly remains unchanged.

Extension: Extension is the number of tuples available in the database at any


instance of time. This value keeps changing as and when the tuples are created,
updated and destroyed. So, the data present in the database at a specific instance
of time is known as the extension of the database or most commonly known as
the snapshot of the database.

Q27. What do you understand by cursor? Mention the different types of cursor

A cursor is a database object which helps in manipulating data, row by row, and
represents a result set.
The types of the cursor are as follows:

 Implicit cursor: This type of cursor is declared automatically as


soon as the execution of SQL takes place. Here, the user is not
indicated about the declaration of the cursor.
 Explicit cursor: This type of cursor is defined by the PL/ SQL, as
it handles a query in more than a single row.

Q28. Explain the terms specialization and generalization

 Specialization: Specialization is a process of defining a set of


subclasses of the entity type. Here, each subclass will contain all the
attributes and relationships of the parent entity. Apart from this,
the subclasses may contain additional attributes and relationships
specific to themselves.
 Generalization: Generalization is a process of finding relations,
common attributes for a particular set of entities; and finally
defining a common superclass for them.

Q29. What do you understand by Data Independence?

When you say an application has data independence, it implies that the
application is independent of the storage structure and data access strategies of
data.

Q30. What are the different integrity rules present in the DBMS?

The different integrity rules present in DBMS are as follows:

 Entity Integrity: This rule states that the value of the primary key
can never be NULL. So, all the tuples in the column identified as
the primary key should have a value.
 Referential Integrity:This rule states that either the value of the
foreign key is NULL or it should be the primary key of any other
relation.

Q31. What does Fill Factor concept mean with respect to indexes?

Fill Factor is used to mention the percentage of space left on every leaf-level
page, which is packed with data. Usually, the default value is 100.

Q32. What is Index hunting and how does it help in improving query
performance?

The process of boosting a collection of indexes is known as Index hunting. This is


done as indexes improve the query performance and the speed at which they are
processed.

It helps in improving query performance in the following way:

 The best queries are suggested using the query optimizer.


 Index, query distribution, and their performance are used as
metrics to check the effect
 Databases are tuned into a small collection of problem queries.

Q33. What are the differences between the network and hierarchical database
model?
Q34. Explain what is a deadlock and mention how it can be resolved?

Deadlock is a situation that occurs when two transactions wait on a resource that
is locked or other transaction holds. Deadlocks can be prevented by making all
the transactions acquire all the locks at the same instance of time. So, once a
deadlock occurs, the only way to cure is to abort one of the transactions and
remove the partially completed work.

Q35. What are the differences between an exclusive lock and a shared lock?

Next, in this article on DBMS interview questions, let us discuss the top
questions asked about SQL.

SQL Interview Questions

Q1. What are the differences between DROP, TRUNCATE and DELETE
commands?

Q2. What is SQL and where is it used?

SQL aka Structured Query Language is the core of the relational database which
is used for accessing and managing the databases. This language is used to
manipulate and retrieve data from a structured data format in the form of tables
and holds relationships between those tables. So, in layman's terms, you can use
SQL to communicate with the database.

Q3. What do you understand by sub-queries in SQL?

A subquery is a query inside another query where a query is defined to retrieve


data or information back from the database. In a subquery, the outer query is
called as the main query whereas the inner query is called a subquery.
Subqueries are always executed first and the result of the subquery is passed on
to the main query. It can be nested inside a SELECT, UPDATE, or any other
query. A subquery can also use any comparison operators such as >,< or =.

Q4. Mention the differences between UNION and UNION ALL

Q5. What do you understand by CLAUSE in SQL?

CLAUSE in SQL is used to limit the result set by mentioning a condition to the
query. So, you can use a CLAUSE to filter rows from the entire set of records.

Example: WHERE HAVING clause.

Q6. Mention the differences between HAVING and WHERE clause?


Note: Whenever GROUP BY is not used, HAVING behaves like a WHERE
clause.

Q7. How can you perform pattern matching in SQL?


You can perform pattern matching in SQL by using the LIKE operator. With the
LIKE operator, you can use the following symbols:

1. %(Percentage sign) — To match zero or more characters.


2. _ (Underscore) –To match exactly one character.

Example:

SELECT * FROM Customers WHERE CustomerName LIKE ‘s%’

SELECT * FROM Customers WHERE CustomerName like ‘xyz_’

Q8. Mention few case manipulation functions in SQL

There are three case manipulation functions in SQL, namely:

LOWER: This function returns the string in lowercase. It takes a string as an


argument and returns it by converting it into a lower case.

Syntax: LOWER(‘string’)

UPPER: This function returns the string in uppercase. It takes a string as an


argument and returns it by converting it into uppercase.
Syntax: UPPER('string')
INITCAP: This function returns the string with the first letter in uppercase and
the rest of the letters in lowercase.

Syntax: INITCAP(‘string’)

Q9. What are joins in SQL and what are the different types of joins?

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:

 Inner Join
 Right Join
 Left Join
 Full Join

Q10. What do you understand by the view and mention the steps to create,
update and drop a view?

A view in SQL is a single table, which is derived from other tables. So, a view
contains rows and columns similar to a real table and has fields from one or
more table.

To create a view, use the following syntax:


CREATE VIEW ViewName AS
SELECT Column1, Column2, ..., ColumnN
FROM TableName
WHERE Condition;

To update a view, use the following syntax:


CREATE VIEW OR REPLACE ViewName AS
SELECT Column1, Column2, ..., ColumnN
FROM TableName
WHERE Condition;
To drop a view, use the following syntax:
DROP VIEW ViewName;

Next, in this article on DBMS interview questions, let us discuss the most
frequently asked queries about SQL.

SQL Query Based Interview Questions

Q1. Write a query to create a duplicate table with and without data present?

Consider you have a table named Customers, having details such as CustomerID,
CustomerName and so on. Now, if you want to create a duplicate table named
‘DuplicateCustomer’ with the data present in it, you can mention the following
query:
CREATE TABLE DuplicateCustomer AS SELECT * FROM Customers;

Similarly, if you want to create a duplicate table without the data present,
mention the following query:
CREATE TABLE DuplicateCustomer AS SELECT * FROM Customers WHERE 1=2;

Q2. Mention a query to calculate the even and odd records from a table

To write a query to calculate the even and odd records from a table, you can write
two different queries by using the MOD function.

So, if you want to retrieve the even records from a table, you can write a query as
follows:
SELECT CustomerID FROM (SELECT rowno, CustomerID from Customers) where
mod(rowno,2)=0;

Similarly, if you want to retrieve the odd records from a table, you can write a
query as follows:
SELECT CustomerID FROM (SELECT rowno, CustomerID from Customers) where
mod(rowno,2)=1;
Q3. Write a query to remove duplicate rows from a table?

To remove duplicate rows from a table, you have to initially select the duplicate
rows from the table without using the DISTINCT keyword. So, to select the
duplicate rows from the table, you can write a query as follows:
SELECT CustomerNumber FROM Customers WHERE ROWID (SELECT MAX (rowid) FROM
Customers C WHERE CustomerNumber = C.CustomerNumber);

Now, to delete the duplicate records from the Customers table, mention the
following query:
DELETE FROM Customers WHERE ROWID(SELECT MAX (rowid) FROM Customers C WHERE
CustomerNumber = C.CustomerNumber);

Q4. Mention a query to add email validation to your database

Well, there are multiple ways to add email validation to your database, but one
out the lot is as follows:
SELECT Email FROM Customers WHERE NOT REGEXP_LIKE(Email, '[A-Z0-9._%+-]+@[A-
Z0-9.-]+.[A-Z]{2,4}', 'i');

Q5. Write a query to retrieve the last day of next month in Oracle.

To write a query to retrieve the last day of the next month in Oracle, you can
write a query as follows:
SELECT LAST_DAY (ADD_MONTHS (SYSDATE,1)) from dual;

So this brings us to the end of the DBMS Interview Questions article. I hope this
set of DBMS Interview Questions will help you ace your job interview. All the
best for your interview! If you wish to check out more articles on the
market’s most trending technologies like Artificial Intelligence, DevOps, Ethical
Hacking, then you can refer to Edureka’s official site.

Do look out for other articles in this series that will explain the various other
aspects of SQL.
SQL Interview Questions

mayuri budake
·

Follow
5 min read

Dec 30, 2019

65

Tables.
Q.1. Write a SQL query to fetch the count of employees working in
project ‘P1’.
Ans. Here, we use aggregate function count() with the SQL where clause.

Query:select count(*) Count_of_Project

FROM [sampleDB].[dbo].[EmployeeSalary]

where Project=’P1' ;

Fig 1

Q.2. Write a SQL query to fetch employee names having salary


greater than or equal to 5000 and less than or equal 10000.
Ans. Here, we will use BETWEEN in the ‘where’ clause to return the empId of
the employees with salary satifying the required criteria and then use it as
subquery to find the fullName of the employee form EmployeeDetails table.

Query:

SELECT EmpFN

FROM [sampleDB].[dbo].[EmployeeDetails]
WHERE Empid IN

(SELECT Empsid FROM [sampleDB].[dbo].[EmployeeSalary]

WHERE Salary BETWEEN 5000 AND 10000);

Fig 2

Q.3. Write a SQL query to fetch project-wise count of employees


sorted by project’s count in descending order.
Ans. The query has two requirements — first to fetch the project-wise count and
then to sort the result by that count. For project wise count, we will be using
GROUPBY clause and for sorting, we will use ORDER BY clause on the alias of
the project-count.

Query:

select Project, Count(Empsid) EmpProjectCount

FROM [sampleDB].[dbo].[EmployeeSalary]

Group By Project
Order By EmpProjectCount DESC;

fig 3

Q.4. Write a query to fetch only the first name(string before space)
from the FullName column of EmployeeDetails table.
Ans. In this question, we are required to first fetch the location of the space
character in the FullName field and then extract the first name out of the
FullName field. For finding the location we will use LOCATE method in mySQL
and CHARINDEX in SQL SERVER and for fetching the string before space, we
will use SUBSTRING OR MID method.

Query:

 SELECT SUBSTRING(EmpFN,0,CHARINDEX(‘
‘,EmpFN))FirstName

FROM [sampleDB].[dbo].[EmployeeDetails];

 SELECT LEFT(EmpFN, CHARINDEX(‘ ‘,EmpFN) — 1) FirstName

FROM [sampleDB].[dbo].[EmployeeDetails];
// LEFT returns the left part of a string

fig 4

Q.5. Write a query to fetch employee names and salary records.


Return employee details even if the salary record is not present for
the employee.
Ans. Here, we can use left join with EmployeeDetail table on the left side.

Query:

SELECT E.EmpFN, S.Salary

FROM [sampleDB].[dbo].[EmployeeDetails]

E LEFT JOIN [sampleDB].[dbo].[EmployeeSalary] S

ON E.Empid = S.Empsid;
Fig 5

Q.6. Write a SQL query to fetch all the Employees who are also
managers from EmployeeDetails table.
Ans. Here, we have to use Self-Join as the requirement wants us to analyze the
EmployeeDetails table as two different tables, each for Employee and manager
records.

Query:

SELECT E.EmpFN

FROM [sampleDB].[dbo].[EmployeeDetails] E

INNER JOIN [sampleDB].[dbo].[EmployeeDetails] M

ON E.EmpiD = M.ManagrID;
Fig 6

Q.7. Write a SQL query to fetch all employee records from


EmployeeDetails table who have a salary record in EmployeeSalary
table.
Ans. Using ‘Exists’-

Query:

SELECT * FROM [sampleDB].[dbo].[EmployeeDetails] E

WHERE EXISTS

(SELECT * FROM [sampleDB].[dbo].[EmployeeSalary] S

WHERE E.Empid = S.Empsid);


Fig 7

//Refer Fig 5 for Understanding.

Q.8. Write a SQL query to fetch duplicate records from a table.


Ans. In order to find duplicate records from table we can use GROUP BY on all
the fields and then use HAVING clause to return only those fields whose count is
greater than 1 i.e. the rows having duplicate records.

Query:

SELECT Project, COUNT(*) CountDup

FROM [sampleDB].[dbo].[EmployeeSalary]

GROUP BY Project

HAVING COUNT(*) > 1;


Fig 8

Q.9. Write a SQL query to remove duplicates from a table without


using temporary table.
Ans. Using Group By and Having clause-

Query:

DELETE FROM [sampleDB].[dbo].[EmployeeSalary] HERE Project IN

(SELECT Project FROM [sampleDB].[dbo].[EmployeeSalary]

GROUP BY Project HAVING COUNT(*) > 1)

AND NOT Empid IN (SELECT min(Empid)

FROM [sampleDB].[dbo].[EmployeeSalary]

GROUP BY Project)
Original Table.

It Delete Two Duplicate Records 3rd and 4th.

It Delete All Duplicate Records 1st, 3rd and 4th.

Q.10. Write a SQL query to fetch only odd and even rows from the
table.
Ans. This can be achieved by using Row_number in SQL server.
Query:

/* fetch ODD rows*/

SELECT E.EmpsId, E.Project, E.Salary

FROM (SELECT *, Row_Number() OVER(ORDER BY EmpsId) AS RowNumber


FROM EmployeeSalary ) E

WHERE E.RowNumber % 2 = 1

/* fetch EVEN rows*/

SELECT E.EmpsId, E.Project, E.Salary

FROM (SELECT *, Row_Number() OVER(ORDER BY EmpsId) AS RowNumber


FROM EmployeeSalary ) E

WHERE E.RowNumber % 2 = 0
Q.11. Write a SQL query to create a new table with data and structure
copied from another table.
Ans. Using SELECT INTO command.

Query:

SELECT * INTO EmployeeData FROM EmployeeDetails


Q.12. Write a SQL query to create an empty table with the same
structure as some other table.
Ans. Using SELECT INTO command with False ‘WHERE’ condition.

Query:

SELECT * INTO EMP FROM EmployeeDetails WHERE 1 = 0;

Q.13. Write a SQL query to find the current date-time.


Ans: SQL Server-

Q.14. Write a SQL query to fetch top n records?


Ans:In SQL server using TOP command.

Query: SELECT TOP(3) * FROM EmployeeSalary ORDER BY Salary DESC

If you are new to SQL refer Below video for better Understanding.

LIST OF IMPORTANT SQL QUERIES


FOR INTERVIEW
1. Query for Retrieving Tables
This query can be run to retrieve the list of tables present in a database where the
database is “My_Schema”.

SELECT * FROM My_Schema.Tables;

2. Query for Selecting Columns from a Table


This is perhaps the most widely used of SQL queries examples. In the example
below, we are extracting the “Student_ID” column or attribute from the table
“STUDENT”.

SELECT Student_ID FROM STUDENT;

If you want to display all the attributes from a particular table, this is the right
query to use:

SELECT * FROM STUDENT;

3. Query for Outputting Data Using a Constraint


This SQL query retrieves the specified attributes from the table on the
constraint Employee ID =0000

SELECT EMP_ID, NAME FROM EMPLOYEE_TBL WHERE EMP_ID = '0000';

4. Query for Outputting Sorted Data Using ‘Order By’


This query orders the results with respect to the attribute which is referenced to
using “Order By” — so for example, if that attribute is an integer data type, then
the result would either be sorted in ascending or descending order; likewise, if
the data type is a String then the result would be ordered in alphabetical order.

SELECT EMP_ID, LAST_NAME FROM EMPLOYEE

WHERE CITY = 'Seattle' ORDER BY EMP_ID;

The ordering of the result can also be set manually, using “asc ” for ascending
and “desc” for descending.

SELECT EMP_ID, LAST_NAME FROM EMPLOYEE_TBL

WHERE CITY = 'INDIANAPOLIS' ORDER BY EMP_ID asc;

5. Query for Outputting Sorted Data Using ‘Group By’


The ‘Group By’ property groups the resulting data according to the specified
attribute.

SELECT Name, Age FROM Patients WHERE Age > 40

GROUP BY Age ORDER BY Name;

SQL Queries for Data Manipulation Using Math Functions


There are a lot of built-in math functions like COUNT and AVG which provide
basic functionalities of counting the number of results and averaging them
respectively.
6. Data Manipulation Using COUNT
This query displays the total number of customers by counting each customer ID.
In addition, it groups the results according to the country of each customer.

SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country;

7. Data Manipulation Using SUM


SUM calculates the total of the attribute that is given to it as an argument.

SELECT SUM(Salary)FROM Employee WHERE Emp_Age < 30;

8. Data Manipulation Using AVG


Simple — an average of a given attribute.

SELECT AVG(Price)FROM Products;

9. Query for Listing all Views


This SQL query lists all the views available in the schema.

SELECT * FROM My_Schema.views;

10. Query for Creating a View


A view is a tailored table that is formed as a result of a query. It has tables and
rows just like any other table. It’s usually a good idea to run queries in SQL as
independent views because this allows them to be retrieved later to view the
query results, rather than computing the same command every time for a
particular set of results.
CREATE VIEW Failing_Students AS

SELECT S_NAME, Student_ID

FROM STUDENT

WHERE GPA > 40;

11. Query for Retrieving a View


The standard syntax of selecting attributes from a table is applicable to views as
well.

SELECT * FROM Failing_Students;

12. Query for Updating a View


This query updates the view named ‘Product List’ — and if this view doesn’t exist,
then the Product List view gets created as specified in this query.

CREATE OR REPLACE VIEW [ Product List] AS

SELECT ProductID, ProductName, Category

FROM Products

WHERE Discontinued = No;

13. Query for Dropping a View


This query will drop or delete a view named ‘V1’.
DROP VIEW V1;

14. Query to Display User Tables


A user-defined table is a representation of defined information in a table, and
they can be used as arguments for procedures or user-defined functions.
Because they’re so useful, it’s useful to keep track of them using the following
query.

SELECT * FROM Sys.objects WHERE Type='u'

15. Query to Display Primary Keys


A primary key uniquely identifies all values within a table. The following SQL
query lists all the fields in a table’s primary key.

SELECT * from Sys.Objects WHERE Type='PK'

16. Query for Displaying Unique Keys


A Unique Key allows a column to ensure that all of its values are different.

SELECT * FROM Sys.Objects WHERE Type='uq'

17. Displaying Foreign Keys


Foreign keys link one table to another — they are attributes in one table which
refer to the primary key of another table.

SELECT * FROM Sys.Objects WHERE Type='f'
18. Displaying Triggers
A Trigger is sort of an ‘event listener’ — i.e, it’s a pre-specified set of
instructions that execute when a certain event occurs. The list of defined
triggers can be viewed using the following query.

SELECT * FROM Sys.Objects WHERE Type='tr'

19. Displaying Internal Tables


Internal tables are formed as a by-product of a user-action and are usually
not accessible. The data in internal tables cannot be manipulated; however, the
metadata of the internal tables can be viewed using the following query.

SELECT * FROM Sys.Objects WHERE Type='it'

20. Displaying a List of Procedures


A stored procedure is a group of SQL queries that logically form a single unit
and perform a particular task. Thus, using the following query you can keep track
of them:

SELECT * FROM Sys.Objects WHERE Type='p'

.. and TWENTY More Advanced SQL Queries for our Users!

21. Swapping the Values of Two Columns in a table


In this and subsequent examples, we will use a common company database
including several tables which are easily visualized. Our practice DB will include
a Customers table and an Order table. The Customers table will contain some
obvious columns including ID, Name, Address, zip, and email, for example,
where we assume for now that the primary key field for indexing is
the Customer_ID field.

With this in mind, we can easily imagine an Orders table which likewise contains
the indexed customer ID field, along with details of each order placed by the
customer. This table will include the order Number, Quantity, Date, Item, and
Price. In our first one of SQL examples, imagine a situation where the zip and
phone fields were transposed and all the phone numbers were erroneously
entered into the zip code field. We can easily fix this problem with the following
SQL statement:

UPDATE Customers SET Zip=Phone, Phone=Zip

22. Returning a Column of Unique Values


Now, suppose that our data entry operator added the same Customers to the
Customers table more than once by mistake. As you know, proper indexing
requires that the key field contain only unique values. To fix the problem, we will
use SELECT DISTINCT to create an indexable list of unique customers:

SELECT DISTINCT ID FROM Customers

23. Making a Top 25 with the SELECT TOP Clause


Next, imagine that our Customers table has grown to include thousands of
records, but we just want to show a sample of 25 of these records to demonstrate
the column headings and The SELECT TOP clause allows us to specify the
number of records to return, like a Top-25 list. In this example we will return the
top 25 from our Customers table:

SELECT TOP 25 FROM Customers WHERE Customer_ID<>NULL;
24. Searching for SQL Tables with Wildcards
Wildcard characters or operators like “%” make it easy to find particular strings
in a large table of thousands of records. Suppose we want to find all of our
customers who have names beginning with “Herb” including Herberts, and
Herbertson. The % wildcard symbol can be used to achieve such a result. The
following SQL query will return all rows from the Customer table where
the Customer_name field begins with “Herb”:

SELECT * From Customers WHERE Name LIKE 'Herb%'

25. Between Monday and Tuesday


Today is Wednesday, and we arrive at work and discover that our new data entry
clerk in training has entered all new orders incorrectly on Monday and Tuesday.
We wish to teach our new trainee to find and correct all erroneous records.
What’s the easiest way to get all the records from the Orders table entered on
Monday and Tuesday? The Between clause makes the task a breeze:

SELECT ID FROM Orders WHERE

Date BETWEEN ‘01/12/2018’ AND ‘01/13/2018’

26. Finding the Intersection of Two Tables


Undoubtedly the whole reason that a relational database exists in the first place
is to find matching records in two tables! The JOIN statement accomplishes this
core objective of SQL and makes the task easy. Here we are going to fetch a list of
all records which have matches in the Customers and Orders tables:

SELECT ID FROM Customers INNER
JOIN Orders ON Customers.ID = Orders.ID

The point of INNER JOIN, in this case, is to select records in the Customers table
which have a matching customer ID values in the Orders table and return only
those records. Of course there are many types of JOIN, such as FULL, SELF, and
LEFT, but for now, let’s keep things interesting and move on to more diverse
types of queries.

27. Doubling the Power with UNION


We can combine the results of two SQL queries examples into one naturally with
the UNION keyword. Suppose we want to create a new table by combining the
Customer_name and phone from Customers with a list of that customer’s recent
orders so that we can look for patterns and perhaps suggest future purchases.
Here is a quick way to accomplish the task:

SELECT phone FROM Customers

UNION SELECT item FROM Orders

The UNION keyword makes it possible to combine JOINS and other criteria to
achieve very powerful new table generation potential.

28. Making Column Labels More Friendly


Aliasing column labels give us the convenience of renaming a column label to
something more readable. There is a tradeoff when naming columns to make
them succinct results in reduced readability in subsequent daily use. In our
Orders table, the item column contains the description of purchased products.
Let’s see how to alias the item column to temporarily rename it for greater user-
friendliness:
SELECT Item AS item_description FROM Orders

29. Always and Everywhere!


Wouldn’t it be great if there were a set of conditions you could depend on every
time? The SQL queries using ANY and ALL can make this ideal a reality! Let’s
look at how the ALL keyword is used to include records only when a set of
conditions is true for ALL records. In the following example, we will return
records from the Orders table where the idea is to get a list of high volume orders
for a given item, in this case for customers who ordered more than 50 of the
product:

SELECT Item FROM Orders

WHERE id = ALL

(SELECT ID FROM Orders

WHERE quantity > 50)

30. Writing Developer Friendly SQL


An often overlooked but very important element of SQL scripting is adding
comments to a script of queries to explain what it’s doing for the benefit of future
developers who may need to revise and update your queries.
The — single line and the /* .. */ multi-line delimiters empower us to add useful
comments to scripts, but this is also used in another valuable way. Sometimes a
section of code may not be in use, but we don’t want to delete it, because we
anticipate using it again. Here we can simply add the comment delimiter to
deactivate it momentarily:
/* This query below is commented so it won't execute*/

/*

SELECT item FROM Orders

WHERE date ALL = (SELECT Order_ID FROM Orders

WHERE quantity > 50)

*/

/* the sql query below the will be executed

ignoring the text after "--"

*/

SELECT item -- single comment

FROM Orders -- another single comment

WHERE id

ALL = (SELECT ID FROM Orders

WHERE quantity > 25)


31. SQL queries for Database Management
So far we have explored SQL query commands for querying tables and
combining records from multiple queries. Now it’s time to take a step upward
and look at the database on a structural level. Let’s start with the easiest SQL
statement of all which creates a new database. Here, we are going to create the
DB as a container for our Customers and Orders tables used in the previous ten
examples above:

CREATE DATABASE AllSales

32. Adding Tables to Our New DB


Next, we will actually add the Customers table which we’ve been using in
previous examples, and then add some of the column labels which we are already
familiar with:

CREATE TABLE Customers (

ID varchar(80),

Name varchar(80),

Phone varchar(20),

....

);
Although most databases are created using a UI such as Access or OpenOffice, it
is important to know how to create and delete databases and tables
programmatically via code with SQL statements. This is especially so when
installing a new web app and the UI asks new users to enter names for DBs to be
added during installation.

33. Modifying and Deleting Tables with SQL


Imagine that you decide to send a birthday card to your customers to show your
appreciation for their business, and so you want to add a birthday field to the
Customers table. In these SQL examples, you see how easy it is to modify
existing tables with the ALTER statement:

ALTER TABLE Customers ADD Birthday varchar(80)

If a table becomes corrupted with bad data you can quickly delete it like this:

DROP TABLE table_name

34. The Key to Successful Indexing


Accurate indexing requires that the Primary Key column contain only unique
values for this purpose. This guarantees that JOIN statements will maintain
integrity and produce valid matches. Let’s create our Customers table again and
establish the ID column as the Primary Key:

CREATE TABLE Customers (

ID int NOT NULL,

Name varchar(80) NOT NULL,
PRIMARY KEY (ID)

);

We can extend the functionality of the Primary Key so that it automatically


increments from a base. Change the ID entry above to add
the AUTO_INCREMENT keyword as in the following statement:

ID int NOT NULL AUTO_INCREMENT

35. Advanced Concepts For Improving Performance


Whenever practical, is always better to write the column name list into a SELECT
statement rather than using the *delimiter as a wildcard to select all columns.
SQL Server has to do a search and replace operation to find all the columns in
your table and write them into the statement for you (every time the SELECT is
executed). For example:

SELECT * FROM Customers

Would actually execute much faster on our database as:

SELECT Name, Birthday, Phone,

Address, Zip FROM Customers

Performance pitfalls can be avoided in many ways. For example, avoid the time
sinkhole of forcing SQL Server to check the system/master database every time
by using only a stored procedure name, and never prefix it with SP_. Also setting
NOCOUNT ON reduces the time required for SQL Server to count rows affected
by INSERT, DELETE, and other commands. Using INNER JOIN with a
condition is much faster than using WHERE clauses with conditions. We advise
developers to learn SQL server queries to an advanced level for this purpose. For
production purposes, these tips may be crucial to adequate performance. Notice
that our tutorial examples tend to favor the INNER JOIN.

36. Conditional Subquery Results


The SQL operator EXISTS tests for the existence of records in a subquery and
returns a value TRUE if a subquery returns one or more records. Have a look at
this query with a subquery condition:

SELECT Name FROM Customers WHERE EXISTS

(SELECT Item FROM Orders

WHERE Customers.ID = Orders.ID AND Price < 50)

In this example above, the SELECT returns a value of TRUE when a customer
has orders valued at less than $50.

37. Copying Selections from Table to Table


There are a hundred and one uses for this SQL tool. Suppose you want to archive
your yearly Orders table into a larger archive table. This next example shows how
to do it.

INSERT INTO Yearly_Orders

SELECT * FROM Orders
WHERE Date<=1/1/2018

This example will add any records from the year 2018 to the archive.

38. Catching NULL Results


In cases where NULL values are allowed in a field, calculations on those values
will produce NULL results as well. This can be avoided by use of the IFNULL
operator. In this next example, a value of zero is returned rather than a value of
NULL when the calculation encounters a field with NULL value:

SELECT Item, Price *

(QtyInStock + IFNULL(QtyOnOrder, 0))

FROM Orders

39. HAVING can be Relieving!


The problem was that the SQL WHERE clause could not operate on aggregate
functions. The problem was solved by using the HAVING clause. As an example,
this next query fetches a list of customers by the region where there is at least
one customer per region:

SELECT COUNT(ID), Region

FROM Customers

GROUP BY Region

HAVING COUNT(ID) > 0;
40. Tie things up with Strings!
Let’s have a look at processing the contents of field data using functions.
Substring is probably the most valuable of all built-in functions. It gives you
some of the power of Regex, but it’s not so complicated as Regex. Suppose you
want to find the substring left of the dots in a web address. Here’s how to do it
with an SQL Select query:

SELECT SUBSTRING_INDEX("medium.com/gumare64", ".", 2);

This line will return everything to the left of the second occurrence of “. ” and so,
in this case, it will return

Dataset
Let’s assume we have a table named “employees” with the following columns:

 id: the unique identifier for each employee


 name: the name of the employee
 gender: the gender of the employee
 salary: the salary of the employee
 department: the department of the employee

Here’s the MySQL script to create the employees table and insert the sample
data:

CREATE TABLE employees (


id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
gender VARCHAR(10) NOT NULL,
salary INT NOT NULL,
department VARCHAR(50) NOT NULL
);

INSERT INTO employees (name, gender, salary, department) VALUES


('Ramesh Gupta', 'Male', 55000, 'Sales'),
('Priya Sharma', 'Female', 65000, 'Marketing'),
('Sanjay Singh', 'Male', 75000, 'Sales'),
('Anjali Verma', 'Female', 45000, 'Finance'),
('Rajesh Sharma', 'Male', 80000, 'Marketing'),
('Smita Patel', 'Female', 60000, 'HR'),
('Vikram Yadav', 'Male', 90000, 'Sales'),
('Neha Sharma', 'Female', 55000, 'Marketing'),
('Rahul Singh', 'Male', 70000, 'Finance'),
('Sonali Gupta', 'Female', 50000, 'Sales');

Employee Table

Image by Author

Queries:

1. Write a query to find the average salary of male and female employees in each
department.

Solution Query:

SELECT department, gender, AVG(salary) AS avg_salary


FROM employees
GROUP BY department, gender;
Output:

Image by Author

2. Write a query to find the name and salary of the employee with the highest
salary in each department.

Solution Query:

SELECT department, name, salary


FROM employees
WHERE (department, salary) IN (
SELECT department, MAX(salary)
FROM employees
GROUP BY department
);

Output:

Image by Author

3. Write a query to find the names of employees who earn more than the average
salary in their department.

Solution Query:
SELECT name, salary, department
FROM employees
WHERE salary > (
SELECT AVG(salary)
FROM employees AS e2
WHERE e2.department = employees. Department
);

Output:

Image by Author

4. Find the top 3 highest paid employees in each department.

Solution Query:

SELECT e.department, e.name, e.salary


FROM employees e
WHERE (
SELECT COUNT(*)
FROM employees
WHERE department = e.department AND salary > e.salary
) < 3;

Output:
Image by Author

5. Find the names of employees who have a salary greater than the average
salary of their department.

Solution Query:

SELECT e.name
FROM employees e
JOIN (
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
) AS dept_avg ON e.department = dept_avg.department
WHERE e.salary > dept_avg.avg_salary;

Output:

Image by Author

6. Find the department(s) with the highest paid employee(s).

Query Solution:

WITH max_salary AS (
SELECT department, MAX(salary) AS highest_salary
FROM employees
GROUP BY department
)
SELECT m.department, e.name, e.salary
FROM employees e
JOIN max_salary m ON e.department = m.department AND e.salary = m.highest_salary;

Output:
Image by Author

25 Advanced SQL Query Examples


with Explanations

            Example #1 - Ranking Rows Based


on a Specific Ordering Criteria
Sometimes we need to create a SQL query to show a ranking of rows based on a
specific order criteria. In this example query, we will show a list of all employees ordered
by salary (highest salary first). The report will include the position of each employee in
the ranking.

Here’s the code:

SELECT 

  employee_id,

  last_name,

  first_name,

  salary,

  RANK() OVER (ORDER BY salary DESC) as ranking

FROM employee
ORDER BY ranking

In the above query, we use the function RANK(). It is a window function that returns each
row’s position in the result set, based on the order defined in the OVER clause  (1 for the
highest salary, 2 for the second-highest, and so on). We need to use an ORDER BY ranking
clause at the end of the query to indicate the order on which the result set will be shown.

If you want to know more about ranking functions in SQL, I recommend our article What
Is the RANK() Function in SQL, and How Do You Use It?

Example #2 - List The First 5 Rows of a


Result Set
The next SQL query creates a report with the employee data for the top 5 salaries in the
company. This kind of report must be ordered based on a given criteria; in our example,
the order criteria will again be salary DESC:

WITH employee_ranking AS (

  SELECT

    employee_id,

    last_name,

    first_name,

    salary,

    RANK() OVER (ORDER BY salary DESC) as ranking

  FROM employee

SELECT

  employee_id,

  last_name,
  first_name,

  salary

FROM employee_ranking

WHERE ranking <= 5

ORDER BY ranking

The WITH clause in the previous query creates a CTE called employee_ranking, which is a


kind of virtual table that’s consumed in the main query. The subquery in the CTE uses
the function RANK() to obtain the position of each row in the ranking. The clause OVER
(ORDER BY salary DESC) indicates how the RANK() value must be calculated.
The RANK() function for the row with the highest salary will return 1, and so on.

Finally, in the WHERE of the main query we ask for those rows with a ranking value smaller
or equal than 5. This lets us obtain only the top 5 rows by ranking value. Again, we use
an ORDER BY clause to show the result set, which is ordered by rank ascending.

Example #3 - List the Last 5 Rows of a


Result Set
This query is similar to the top 5 query, but we want the last 5 rows. We only need to
introduce a change in the type of order, i.e. using ASC instead of DESC. In the CTE, we
will create a ranking column based on an ascendent order of salary (lowest salary first):

WITH employee_ranking AS (

  SELECT

    employee_id,

    last_name,

    first_name,

    salary,
    RANK() OVER (ORDER BY salary ASC) as ranking

  FROM employee

SELECT

  employee_id,

  last_name,

  first_name,

  salary

FROM employee_ranking

WHERE ranking <= 5

ORDER BY ranking

In the main query, we use WHERE ranking <= 5 to filter the rows with the 5 lowest salaries.
After that, we use ORDER BY ranking to order the rows of the report by ranking value.

Example #4 - List The Second Highest


Row of a Result Set
Let’s suppose we’d like to obtain the data of the employee with the second highest salary
in the company. We can apply a similar approach to our previous query:

WITH employee_ranking AS (

  SELECT

    employee_id,

    last_name,

    first_name,
    salary,

    RANK() OVER (ORDER BY salary DESC) as ranking

  FROM employee

SELECT

  employee_id,

  last_name,

  first_name,

  salary

FROM employee_ranking

WHERE ranking = 2

The WHERE condition ranking = 2 is used to filter the rows with the salary in position 2.
Note that we can have more than one employee in position 2 if they have the same
salary.

At this point, it is important to understand the behavior of the RANK() function as well as


other available functions like ROW_NUMBER() and DENSE_RANK(). This topic is covered in
detail in our Overview of Ranking Functions in SQL. I strongly recommend reading this
article if you need to work with different kinds of rankings.

Example #5 - List the Second Highest


Salary By Department
Let’s add a variation to the previous SQL query. As each of our employees belongs to a
department, we now want a report showing the department ID and the employee name
with the second highest salary in this department. We want one record for each
department in the company. Here’s the query:

WITH employee_ranking AS (
  SELECT

    employee_id,

    last_name,

    first_name,

    salary,

    dept_id

    RANK() OVER (PARTITION BY dept_id ORDER BY salary DESC) as ranking

  FROM employee

SELECT

  dept_id,

  employee_id,

  last_name,

  first_name,

  salary

FROM employee_ranking

WHERE ranking = 2

ORDER BY dept_id, last_name

The main change introduced in this query is the PARTITION BY dept_id clause in OVER. This
clause groups rows with the same dept_id, ordering the rows in each group by
salary DESC. Then the RANK() function is calculated for each department.

In the main query, we return the dept_id and the employee data for those employees in
position 2 of their departmental ranking.
For those readers who want to find out more about finding the Nth highest row in a
group, I recommend the article How to Find the Nth-Highest Salary by Department with
SQL.

Tired of doing simple SQL exercises? Let's move to a more advanced level! Check
out our Advanced SQL track!

Example #6 - List the First 50% Rows in a


Result Set
In some cases, we could be interested in obtaining the first 50% of the result set, (or any
other percentage). For this kind of report, there is a SQL function called NTILE() which
receives an integer parameter indicating the number of subsets into which we want to
divide the entire result set. For example NTILE(2) divides the result set into 2 subsets with
the same quantity of elements; for each row, it returns a 1 or a 2 depending on the
subset where the row is located.

Here’s the query:

WITH employee_ranking AS (

  SELECT

    employee_id,

    last_name,

    first_name,

    salary,

    NTILE(2) OVER (ORDER BY salary ) as ntile

  FROM employee

SELECT

  employee_id,
  last_name,

  first_name,

  salary

FROM employee_ranking

WHERE ntile = 1

ORDER BY salary

The above query returns only the rows in the first half of a report of employees ordered
by salary in ascending order. We use the condition ntile = 1 to filter only those rows in
the first half of the report. If you are interested in the NTILE() window function, see the
article Common SQL Window Functions: Using Partitions With Ranking Functions.

Example #7 - List the Last 25% Rows in a


Result Set
As with the previous query, in this example we will use NTILE(4) to divide the result set
into 4 subsets; each subset will have 25% of the total result set. Using
the NTILE() function, we will generate a column called ntile with the values 1, 2, 3, and
4:

WITH employee_ranking AS (

  SELECT

    employee_id,

    last_name,

    first_name,

    salary,

    NTILE(4) OVER (ORDER BY salary) as ntile

  FROM employee
)

SELECT

  employee_id,

  last_name,

  first_name,

  salary

FROM employee_ranking

WHERE ntile = 4

ORDER BY salary

The WHERE ntile = 4 condition filters only the rows in the last quarter of the report. The
last clause ORDER BY salary orders the result set to be returned by the query, while OVER
(ORDER BY salary) orders the rows before dividing them into 4 subsets using NTILE(4).

Example #8 - Number the Rows in a


Result Set
Sometimes we want to create a ranking that assigns each row a number indicating the
position of that row in the ranking: 1 to the first row, 2 to the second one, and so on. SQL
provides a few ways to do this. If we want a simple sequence of numbers from 1 to N, we
can use the ROW_NUMBER() function. However if we want a ranking that allows two rows in
the same position (i.e. because they share the same value) we can use
the RANK() or DENSE_RANK() function. The following query creates a report where each row
has a position value:

SELECT

  employee_id,

  last_name,
  first_name,

  salary,

  ROW_NUMBER() OVER (ORDER BY employee_id) as ranking_position

FROM employee

If you want to learn about different advanced ranking functions, I recommend the
article Overview of Ranking Functions in SQL.

Example #9 - List All Combinations of


Rows from Two Tables
In some cases, we could need a join that includes all the possible combinations of rows
from two tables. Suppose we have a food company where we sell 3 kinds of cereal: corn
flakes, sugared corn flakes, and rice flakes. All these cereals are sold in 3 different
packaging sizes: 1 pound, 3 pounds, and 5 pounds. As we offer 3 products in 3 different
package sizes, then we offer nine different combinations.

We have a product table with 3 records (corn flakes, sugared corn flakes and rice flakes)
and another table called box_size with 3 records one for 1 pound and two records for 3
and 5 pounds, respectively. If we want to create a report with the price list for our nine
combinations, we can use the following query:

SELECT

  grain.product_name,

  box_size.description,

  grain.price_per_pound * box_size.box_weight

FROM product

CROSS JOIN  box_sizes

The query result will be:


product package_size price

Corn flake 1 pound box 2.43

Corn flake 3 pound box 7.29

Corn flake 5 pound box 12.15

Sugared corn flake 1 pound box 2.85

Sugared corn flake 3 pound box 8.55

Sugared corn flake 5 pound box 14.25

Rice flake 1 pound box 1.98

Rice flake 3 pound box 5.94

Rice flake 5 pound box 9.90

The CROSS JOIN clause without any condition produces a table with all row combinations
from both tables. Note we calculate the price based on the per-pound price stored in
the product table and the weight from box_sizes with the expression:

grain.price_per_pound * box_size.box_weight

A deep dive into the CROSS JOIN can be found in An Illustrated Guide to the SQL CROSS
JOIN.
Example #10 – Join a Table to Itself
In some cases, we need to join a table to itself. Think about the employee table. Every row
has a column called manager_id with the ID of the manager supervising this employee.
Using a self-join we can obtain a report with the columns employee_name and manager_name;
this will show us who manages each employee. Here is the query:

SELECT 

  e1.first_name ||’ ‘|| e1.last_name AS manager_name,

  e2.first_name ||’ ‘|| e2.last_name AS employee_name

FROM employee e1

JOIN employee e2

ON e1.employee_id = e2.manager_id

In the above query, we can see the table employee is referenced twice as e1 and e2, and
the join condition is e1.employee_id = e2.manager_id. This condition links each employee
row with the manager row. The article What Is a Self Join in SQL? An Explanation With
Seven Examples  will give you more ideas about when you can apply self joins in your
SQL queries.

LearnSQL.com is a great place to learn SQL. LearnSQL.com offers over 60


interactive courses that range in difficulty from beginner to advanced. You can
choose from a full learning track, mini-tracks to sharpen targeted skills, and individual
courses.

Example #11 – Show All Rows with an


Above-Average Value
We need a report showing all employees with a salary that’s higher than the company
average. We can first create a subquery to obtain the average salary of the company,
and then compare the salary of every employee with the subquery result. This is shown
in the following example:
SELECT

  first_name,

  last_name,

  salary

FROM employee 

WHERE salary > ( SELECT AVG(salary) FROM employee )

You can see the subquery that obtains the average salary in the WHERE clause. In the
main query, we select the employee name and salary. You can read more about
subqueries in the article How to practice SQL subqueries.

Example #12 – Employees with Salaries


Higher Than Their Departmental Average
Let’s suppose we want to obtain records for employees with salaries higher than the
average salary in their departments. This query is different than the previous one
because now we need a subquery to obtain the average salary for the current
employee’s department rather than the entire company This is called a correlated
subquery because there’s a reference to a column in the current row of the main table of
the query within the subquery.

Here’s the code:

SELECT

  first_name,

  last_name,

  salary

FROM employee e1

WHERE salary >
    (SELECT AVG(salary)

     FROM employee e2

     WHERE e1.departmet_id = e2.department_id)

In the subquery, we can see a reference to the column e1.department_id, which is a


column referenced in the main query. The condition e1.departmet_id =
e2.department_id is the key in the subquery because it allows us to obtain the average of
all employees in the department of the current row. Once we obtain the departmental
average salary, we compare it with the employee salary and filter accordingly.

Example #13 – Obtain All Rows Where a


Value Is in a Subquery Result
Suppose that John Smith manages several departments and we want to obtain a list of
all the employees in those departments. We’ll use a subquery to obtain IDs of the
departments managed by John Smith. Then we’ll use the IN operator to find the
employees working in those departments:

SELECT 

  first_name,

  last_name

FROM employee e1

WHERE department_id IN (

   SELECT department_id

   FROM department

   WHERE manager_name=‘John Smith’)

The previous subquery is a multi-row subquery: it returns more than one row. In fact, it
will return several rows because John Smith manages many departments. When working
with multi-row subqueries, you need to use specific operators (like IN) in the WHERE
condition involving the subquery.

Example #14 – Find Duplicate Rows in


SQL
If a table has duplicate rows, you can find them with SQL. Use a query with a GROUP
BY clause including all the columns in the table and a HAVING clause to filter rows that
appear more than one time. Here’s an example:

SELECT 

  employee_id,

  last_name,

  first_name,

  dept_id,

  manager_id,

  salary

FROM employee

GROUP BY   

  employee_id,

  last_name,

  first_name,

  dept_id,

  manager_id,

  salary

HAVING COUNT(*) > 1
The rows that are not duplicated will have a COUNT(*) equal to 1, but those rows that exist
many times will have a COUNT(*) returning the number of times that the row exists. I
suggest the article How to Find Duplicate Values in SQL if you want to find more details
about this technique.

Example #15 – Count Duplicate Rows


If you want to count duplicate rows, you can use the following query. It’s similar to the
previous one, but we add a COUNT(*) in the SELECT list to show how many times each
duplicate row appears in the table:

SELECT 

  employee_id,

  last_name,

  first_name,

  dept_id,

  manager_id,

  salary,

  COUNT(*) AS number_of_rows

FROM employee

GROUP BY

  employee_id,

  last_name,

  first_name,

  dept_id,

  manager_id,

  salary
HAVING COUNT(*) > 1

Again, you can find valuable information about how to manage duplicate records in the
article How To Find Duplicate Records in SQL.

Example #16 – Find Common Records


Between Tables
If you have two tables with the same schema or if two tables have a subset of columns in
common, you can obtain the rows that appear in both tables with the set
operator INTERSECT. Let’s suppose we have a snapshot of the table employee taken in Jan
2020 called employee_2020_jan and we want to obtain the list of employees that exist in
both tables. We can do that with this query:

SELECT

  last_name,

  first_name

FROM employee

INTERSECT

SELECT

  last_name,

  first_name

FROM employee_2020_jan

As a result, we will obtain a list of employees that appear in both tables. Perhaps they’ll
have different values on the columns like salary or dept_id. In other words, we are
obtaining those employees who worked for the company in Jan 2020 and who are still
working for the company.

If you are interested in finding more about set operators, I suggest the article Introducing
SQL Set Operators: Union, Union All, Minus, and Intersect.
Example #17 – Grouping Data with
ROLLUP
The GROUP BY clause in SQL is used to aggregate rows in groups and apply functions to
all the rows in the group, returning a single result value. For example, if we want to obtain
a report with the total salary amount per department and expertise level, we can do the
following query:

SELECT 

  dept_id,

  expertise,

  SUM(salary) total_salary

FROM    employee

GROUP BY dept_id, expertise

The GROUP BY has the optional clause ROLLUP, which allows it to include additional


groupings in one query. Adding the ROLLUP clause to our example could give us the total
sum of salaries for each department (no matter what expertise level the employee has)
and the total sum of salaries for the whole table (no matter the employee’s department
and expertise level). The modified query is:

SELECT

  dept_id,

  expertise,

  SUM(salary) total_salary

FROM employee

GROUP BY ROLLUP (dept_id, expertise)

And the result will be:


dept_id expertise total_salary

Account Senior 90000

Account Junior 80000

Account NULL 170000

CEO Senior 250000

CEO NULL 250000

IT Senior 250000

IT NULL 250000

Sales Junior 110000

Sales Semisenior 140000

Sales Senior 180000

Sales NULL 430000

NULL NULL 1100000

The rows in the result set with a NULL are the extra rows added by the ROLLUP clause.
A NULL value in the column expertise means a group of rows for a specific value
of dept_id but without a specific expertise value. In other words, it is the total amount of
salaries for each dept_id. In the same way, the last row of the result having a NULL for
columns dept_id and expertise means the grand total for all departments in the
company.
If you want to learn more about the ROLLUP clause and other similar clauses like CUBE, the
article Grouping, Rolling, and Cubing Data has lots of examples.

Example #18 – Conditional Summation


In some cases, we need to summarize or count values based on some condition(s). For
example, if we want to obtain the total salaries in the Sales and Human Resources
departments combined and in the IT and Support departments combined, we can
execute the following query:

SELECT

  SUM (CASE

    WHEN dept_id IN (‘SALES’,’HUMAN RESOURCES’)

    THEN salary

    ELSE 0 END) AS total_salary_sales_and_hr,

  SUM (CASE

    WHEN dept_id IN (‘IT’,’SUPPORT’)

    THEN salary

    ELSE 0 END) AS total_salary_it_and_support

FROM employee

The query returns a single row with two columns. The first column shows the total salary
for the Sales and Human Resources departments. This value is calculated using
the SUM() function on the salary column – but only when the employee belongs to the
Sales or Human Resources department. A zero is added to the sum when the employee
belongs to any other department. The same idea is applied for
the total_salary_it_and_support column.

The articles Useful SQL Patterns: Conditional Summarization with CASE and  How to


Use CASE WHEN with SUM() in SQL provide more details about this technique.
Example #19 – Group Rows by a Range
In the next example query, we will create the salary ranges low, medium, and high. Then
we will count how many employees are in each salary range:

SELECT

  CASE

    WHEN salary <= 750000 THEN ‘low’

    WHEN salary > 750000 AND salary <= 100000 THEN ‘medium’

    WHEN salary > 100000 THEN ‘high’

  END AS salary_category,

  COUNT(*) AS number_of_employees

FROM    employee

GROUP BY

  CASE

    WHEN salary <= 750000 THEN ‘low’

    WHEN salary > 750000 AND salary <= 100000 THEN ‘medium’

    WHEN salary > 100000 THEN ‘high’

END

In this query, we use CASE to define the salary range for each employee. You can see
the same CASE statement twice. The first one defines the ranges, as we just said; the
second one in the GROUP BY aggregates records and applies the COUNT(*) function to
each group of records. You can use the CASE statement in the same way to compute
counts or sums for other custom-defined levels.

How to Use CASE in SQL explains other examples of CASE statements like the one
used in this query.

Do you want to take your SQL skills to the next level? Check out our Advanced
SQL track.
Example #20 – Compute a Running Total
in SQL
A running total is a very common SQL pattern, one that’s used frequently in finance and
in trend analysis.

When you have a table that stores any daily metric, such as a sales table with the
columns day and daily_amount, you can calculate the running total as the cumulative sum
of all previous daily_amount values. SQL provides a window function called SUM() to do
just that.

In the following query, we’ll calculate the cumulative sales for each day:

SELECT

  day,

  daily_amount,

  SUM (daily_amount) OVER (ORDER BY day) AS running_total

FROM sales

The SUM() function uses the OVER() clause to define the order of the rows; all rows
previous to the current day are included in the SUM(). Here’s a partial result:

day daily_amount running_total

Jan 30, 2023 1000.00 1000.00

Jan 31, 2023 800.00 1800.00

Feb 1, 2023 700.00 2500.00

The first two columns day and daily_amount are values taken directly from the
table sales. The column running_total is calculated by the expression:

SUM (daily_amount) OVER (order by day)


You can clearly see how the running_total is the accumulated sum of the
previous daily_amounts.

If you wish to go deeper on this topic, I suggest the article What Is a SQL Running Total
and How Do You Compute It?, which includes many clarifying examples.

Example #21 – Compute a Moving


Average in SQL
A moving average is a time series technique for analyzing trends in data. It is calculated
as the average of the current value and a specified number of immediately preceding
values for each point in time. The main idea is to examine how these averages behave
over time instead of examining the behavior of the original or raw data points.

Let’s calculate the moving average for the last 7 days using the sales table from the
previous example:

SELECT

  day,

  daily_amount,

  AVG (daily_amount) OVER (ORDER BY day ROWS 6 PRECEDING)

    AS moving_average

FROM sales

In the above query, we use the AVG() window function to calculate the average using the
current row (today) and the previous 6 rows. As the rows are ordered by day, the current
row and the 6 previous rows defines a period of 1 week.

The article What a Moving Average Is and How to Compute it in SQL goes into detail
about this subject; check it out if you want to learn more.
Example #22 – Compute a Difference
(Delta) Between Two Columns on
Different Rows
There’s more than one way to calculate the difference between two rows in SQL. One
way to do it is by using the window functions LEAD() and LAG(), as we will do in this
example.

Let’s suppose we want to obtain a report with the total amount sold on each day, but we
also want to obtain the difference (or delta) related to the previous day. We can use a
query like this one:

SELECT

  day,

  daily_amount,

  daily_amount - LAG(daily_amount) OVER (ORDER BY day)

    AS delta_yesterday_today

FROM sales

The key expression in this query is:

daily_amount - LAG(daily_amount) OVER (ORDER BY day)

Both elements of the arithmetic difference come from different rows. The first element
comes from the current row and LAG(daily_amount) comes from the previous day
row. LAG() returns the value of any column from the previous row (based on the ORDER
BY specified in the OVER clause).

If you want to read more about LAG() and LEAD(), I suggest the article How to Calculate


the Difference Between Two Rows in SQL.
Example #23 – Compute a Year-Over-Year
Difference
Year-over-year (YOY) or month-to-month comparisons are a popular and effective way to
evaluate the performance of several kinds of organizations. You can calculate the
comparison as a value or as a percentage.

In this example, we will use the sales table, which has data in a daily granularity. We first
need to aggregate the data to the year or month, which we will do by creating a CTE with
amounts aggregated by year. Here’s the query:

WITH year_metrics AS (

  SELECT

    extract(year from day) as year,

    SUM(daily_amount) as year_amount

  FROM sales

  GROUP BY year)

SELECT

  year,

  year_amount,

  LAG(year_amount) OVER (ORDER BY year) AS revenue_previous_year,

  year_amount - LAG(year_amount) OVER (ORDER BY year) as yoy_diff_value,

  ((year_amount - LAG(year_amount) OVER (ORDER BY year) ) /

     LAG(year_amount) OVER (ORDER BY year)) as yoy_diff_perc

FROM year_metrics

ORDER BY 1

The first expression to analyze is the one used to calculate yoy_diff_value:


year_amount - LAG(year_amount ) OVER (ORDER BY year)

It is used to calculate the difference (as a value) between the amount of the current year
and the previous year using the LAG() window function and ordering the data by year.

In the next expression, we calculate the same difference as a percentage. This


calculation is a little more complex because we need to divide by the previous year’s
amount. (Note: We use the previous year as the base for percentage calculation, so the
previous year is 100 percent.)

((year_amount-LAG(year_amount ) OVER(ORDER BY year))/LAG(year_amount )
OVER(ORDER BY year))

In the article How to Compute Year-Over-Year Differences in SQL, you can find several
examples of calculating year-to-year and month-to-month differences.

Example #24 – Use Recursive Queries to


Manage Data Hierarchies
Some tables in SQL can have an implicit kind of data hierarchy. As an example,
our employee table has a manager_id for each employee. We have a manager who is in
charge of other managers, who in turn have other employees under their charge, and so
on.

When we have this sort of organization, we can have a hierarchy of various levels. In
each row, the column manager_id refers to the row on the immediate upper level in the
hierarchy. In these cases, a frequent request is to obtain a list of all employees reporting
(directly or indirectly) to the CEO of the company (who, in this case, has
the employee_id of 110). The query to use is:

WITH RECURSIVE subordinate AS (

 SELECT 

   employee_id,

   first_name,
   last_name,

   manager_id

  FROM employee

  WHERE employee_id = 110 -- id of the top hierarchy employee (CEO)

  

  UNION ALL

  

  SELECT 

    e.employee_id,

    e.first_name,

    e.last_name,

    e.manager_id

  FROM employee e

  JOIN subordinate s

  ON e.manager_id = s.employee_id

SELECT 

  employee_id,

  first_name,

  last_name,

  manager_id

FROM subordinate ;
In this query, we created a recursive CTE called subordinate. It’s the key part of this
query because it traverses the data hierarchy going from one row to the rows in the
hierarchy immediately below it.

There are two subqueries connected by a UNION ALL; the first subquery returns the top
row of the hierarchy and the second query returns the next level, adding those rows to
the intermediate result of the query. Then the second subquery is executed again to
return the next level, which again will be added to the intermediate result set. This
process is repeated until no new rows are added to the intermediate result.

Finally, the main query consumes the data in the subordinate CTE and returns data in
the way we expect. If you want to learn more about recursive queries in SQL, I suggest
the article How to Find All Employees Under Each Manager in SQL.

Example #25 – Find the Length of a Series


Using Window Functions
Suppose we have a table with user registration data. We store information about how
many users registered on each date. We define a data series as the sequence of
consecutive days when users registered. A day when no user registers breaks the data
series. For each data series, we want to find its length.

The table below shows data series:

id day Registered users

1 Jan 25 2023 51

2 Jan 26 2023 46

3 Jan 27 2023 41

4 Jan 30 2023 59

5 Jan 31 2023 73
id day Registered users

6 Feb 1 2023 34

7 Feb 2 2023 56

8 Feb 4 2023 34

There are 3 different data series shown in different colors. We are looking for a query to
obtain the length of each data series. The first data series starts on Jan 25 and has a
length of 3 elements, the second one starts on Jan 30 and its length is 4, and so on.

The query is as follows:

WITH data_series AS (

  SELECT   

    RANK() OVER (ORDER BY day) AS row_number,

    day,

    day - RANK() OVER (ORDER BY day) AS series_id

 FROM   user_registration )

SELECT 

  MIN(day) AS series_start_day,

  MAX(day) AS series_end_day,

  MAX(day) - MIN (day) + 1 AS series_length

FROM    data_series

GROUP BY series_id
ORDER BY series_start_date

In the previous query, the CTE has the column series_id, which is a value intended to be
used as an ID for the rows in the same data series. In the main query, the GROUP BY
series_id clause is used to aggregate rows of the same data series. Then we can obtain
the start of the series with MIN(day) and its end with MAX(day). The length of the series is
calculated with the expression:

MAX(day) - MIN (day) + 1

If you want to go deeper with this topic, the article How to Calculate the Length of a
Series with SQL  provides a detailed explanation of this technique.

Practice Advanced SQL with


LearnSQL.com Courses
SQL is an easy-to-learn and powerful language. In this article, we showed 25 examples
of advanced SQL queries. All of them can be explained in about 5 minutes, showing that
SQL is an accessible language even when you need to do complex reports or queries.

LearnSQL.com provides a one-stop-shop for all things SQL, covering basic to


advanced concepts in one single platform. LearnSQL.com is specifically geared
towards SQL. It offers over 60 interactive SQL courses that range in difficulty from
beginner to advanced and monthly SQL challenges to practice your SQL skills.

If you want to continue learning SQL, I suggest our advanced SQL courses: Window
Functions, Recursive Queries, and GROUP BY Extensions in SQL. All of them cover
complex areas of the SQL language in simple words and with plenty of examples.
Increase your skill and invest in yourself with SQL!

1.   SQL Query for Retrieving Tables


This query can be run to retrieve the list of tables present in a database where the database is
“My_Schema”.
With the SELECT command, users can define the columns that they want to get in the query
output. This command is also useful to get which column users want to see as the output table.
The SELECT statement is applied to pick data from a table. The data retrieved is put in a result
table, named the result set. The output data is saved in a result table. This output table is also
termed the result set.
1SELECT * FROM My_Schema.Tables;

2.   Query for Selecting Columns from a Table


These are perhaps the most useful SQL query examples. In the example below, we are
extracting the “Student_ID” column or attribute from the table “STUDENT”. The select statement
is used to select data from the database.
1SELECT Student_ID FROM STUDENT;

If you want to display all the attributes from a particular table, this is the right query to use:
1SELECT * FROM STUDENT;

3.   Query for Outputting Data Using a Constraint


This SQL query retrieves the specified attributes from the table on the constraint Employee ID
=0000
1SELECT EMP_ID, NAME FROM EMPLOYEE_TBL WHERE EMP_ID = '0000';

4. Query for Outputting Sorted Data Using ‘Order By’


This query orders the results with respect to the attribute which is referenced using “Order By”
– so for example, if that attribute is an integer data type, then the result would either be sorted
in ascending or descending order; likewise, if the data type is a String then the result would be
ordered in alphabetical order. The order by clause is used to sort the data from the table. The
order by clause should always be used in the last of the SQL query.
1SELECT EMP_ID, LAST_NAME FROM EMPLOYEE
2WHERE CITY = 'Seattle' ORDER BY EMP_ID;

The ordering of the result can also be set manually, using “asc ” for ascending and “desc” for
descending.
Ascending (ASC) is the default condition for the ORDER BY clause. In other words, if users don’t
specify ASC or DESC after the column name, then the result will be ordered in ascending order
only.
1SELECT EMP_ID, LAST_NAME FROM EMPLOYEE_TBL
2WHERE CITY = 'INDIANAPOLIS' ORDER BY EMP_ID asc;
5.   SQL Query for Outputting Sorted Data Using ‘Group By’
The ‘Group By’ property groups the resulting data according to the specified attribute.
The SQL query below will select Name, Age columns from the Patients table, then will filter
them by Age value to include records where Age is more than 40 and then will group records
with similar Age value and then finally will output them sorted by Name. The basic rule is that
the group by clause should always follow a where clause in a Select statement and must
precede the Order by clause.
1SELECT Name, Age FROM Patients WHERE Age > 40
2GROUP BY Name, Age ORDER BY Name;

Another sample of use of Group By: this expression will select records with a price lesser than
70 from the Orders table, will group records with a similar price, will sort the output by price,
and will also add the column COUNT(price) that will display how many records with similar
price were found:
1SELECT COUNT(price), price FROM orders
2WHERE price < 70 GROUP BY price ORDER BY price

Note: you should use the very same set of columns for both SELECT and GROUP BY commands,
otherwise you will get an error. Many thanks to Sachidannad for pointing it out!

SQL Queries for Data Manipulation Using Math Functions


There are a lot of built-in math functions like COUNT and AVG which provide basic
functionalities of counting the number of results and averaging them respectively.

6. Data Manipulation Using COUNT


This query displays the total number of customers by counting each customer ID. In addition, it
groups the results according to the country of each customer. In count, if users define
DISTINCT, then they cal also define the query_partition_clause. This clause is a part of the
analytic clause, and other clauses such as order_by_clause and windowing_clause are not
permitted.
Syntax: SELECT COUNT(colname) FROM table name;
1SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country;
7. Data Manipulation Using SUM
SUM calculates the total of the attribute that is given to it as an argument. SUM is an aggregate
function and it calculates the sum of all the distinct values. and the sum of all the duplicate
values.
1SELECT SUM(Salary)FROM Employee WHERE Emp_Age < 30;

8. Data Manipulation Using AVG


Simple – an average of a given attribute. Average is also an aggregate function in SQL. The
AVG() function computes the average of non-NULL values in a column. It ignores the null
values.
1SELECT AVG(Price)FROM Products;

9.   SQL Query for Listing all Views


This SQL query lists all the views available in the schema.
1SELECT * FROM My_Schema.views;

10. Query for Creating a View


A view is a tailored table that is formed as a result of a query. It has tables and rows just like
any other table. It’s usually a good idea to run queries in SQL as independent views because
this allows them to be retrieved later to view the query results, rather than computing the
same command every time for a particular set of results.

1CREATE VIEW Failing_Students AS


2SELECT S_NAME, Student_ID
3FROM STUDENT
4WHERE GPA > 40;

11. Query for Retrieving a View


The standard syntax of selecting attributes from a table is applicable to views as well.
1SELECT * FROM Failing_Students;
12. Query for Updating a View
This query updates the view named ‘Product List’ – and if this view doesn’t exist, then the
Product List view gets created as specified in this query. The view is also called a virtual table. In
other words, a view is just a mirrored copy of a table whose data is the result of a stored query.
A view is a legitimate copy of a different table or sequence of tables. A view obtains its
information or data from the tables from previously created tables known as base tables. Base
tables are real tables. All procedures implemented on a view really modify the base table.
Users can use views just like the real or base tables. In view, users can apply various DDL, DML
commands such as update, insert into, and delete.

1CREATE OR REPLACE VIEW [ Product List] AS


2SELECT ProductID, ProductName, Category
3FROM Products
4WHERE Discontinued = No;

13. Query for Dropping a View


This query will drop or delete a view named ‘V1’. The important thing to remember here is that
the DROP VIEW is disallowed if there are any views dependent on the view you are about to
drop.
1DROP VIEW V1;

14. Query to Display User Tables


A user-defined table is a representation of defined information in a table, and it can be used
as arguments for procedures or user-defined functions. Because they’re so useful, it’s useful
to keep track of them using the following query. User tables explain the relational tables of the
current user.
1SELECT * FROM Sys.objects WHERE Type='u'

15. Query to Display Primary Keys


A primary key uniquely identifies all values within a table. A primary key imposes a NOT NULL
restriction and a unique constraint in one declaration. In other words, it prevents various rows
from having similar values or sequences of columns. It doesn’t allow null values. The primary
key can be defined as a single column or the combination of two columns in a table. It is
responsible for all the relationships between the tables.
The following SQL query lists all the fields in a table’s primary key.
1SELECT * from Sys.Objects WHERE Type='PK'

16. Query for Displaying Unique Keys


A Unique Key allows a column to ensure that all of its values are different. A unique key also
recognizes a different tuple uniquely in relation to or table. A table can have more than one
unique key. Unique key constraints can take only one NULL value for the column.
1SELECT * FROM Sys.Objects WHERE Type='uq'

17. Displaying Foreign Keys


Foreign keys link one table to another – they are attributes in one table which refer to the
primary key of another table.
1SELECT * FROM Sys.Objects WHERE Type='f'

Primary, Unique, and Foreign are part of the constraints in SQL. Constraints are essential to the
scalability, compliance, and sincerity of the data. Constraints implement particular rules,
assuring the data adheres to the conditions outlined. For example, these are the laws imposed
on the columns of the database tables. These are applied to restrict the kind of data in the
table. This assures the efficiency and authenticity of the database.

18. Displaying Triggers
A Trigger is sort of an ‘event listener’ – i.e, it’s a pre-specified set of instructions that execute
when a certain event occurs. The list of defined triggers can be viewed using the following
query.
1SELECT * FROM Sys.Objects WHERE Type='tr'

19. Displaying Internal Tables


Internal tables are formed as a by-product of a user action and are usually not accessible.
The data in internal tables cannot be manipulated; however, the metadata of the internal
tables can be viewed using the following query.
1SELECT * FROM Sys.Objects WHERE Type='it'
20. Displaying a List of Procedures
A stored procedure is a group of advanced SQL queries that logically form a single unit and
perform a particular task. Thus, using the following query you can keep track of them:
1SELECT * FROM Sys.Objects WHERE Type='p'

21. Swapping the Values of Two Columns in a table


In this and subsequent examples, we will use a common company database including several
tables that are easily visualized. Our practice DB will include a Customer table and an Order
table. The Customers table will contain some obvious columns including ID, Name, Address,
zip, and email, for example, where we assume for now that the primary key field for indexing is
the Customer_ID field.
With this in mind, we can easily imagine an Orders table that likewise contains the indexed
customer ID field, along with details of each order placed by the customer. This table will
include the order Number, Quantity, Date, Item, and Price. In our first one of SQL examples,
imagine a situation where the zip and phone fields were transposed and all the phone
numbers were erroneously entered into the zip code field. We can easily fix this problem with
the following SQL statement:
1UPDATE Customers SET Zip=Phone, Phone=Zip

22. Returning a Column of Unique Values


Now, suppose that our data entry operator added the same Customers to the Customers table
more than once by mistake. As you know, proper indexing requires that the key field contains
only unique values. To fix the problem, we will use SELECT DISTINCT to create an indexable list of
unique customers:
1SELECT DISTINCT ID FROM Customers

23. Making a Top 25 with the SELECT TOP Clause


Next, imagine that our Customers table has grown to include thousands of records, but we just
want to show a sample of 25 of these records to demonstrate the column headings and The
SELECT TOP clause allows us to specify the number of records to return, like a Top-25 list. In this
example we will return the top 25 from our Customers table:
1SELECT TOP 25 FROM Customers WHERE Customer_ID<>NULL;
24. Searching for SQL Tables with Wildcards
Wildcard characters or operators like “%” make it easy to find particular strings in a large table
of thousands of records. Suppose we want to find all of our customers who have names
beginning with “Herb” including Herberts, and Herbertson. The % wildcard symbol can be used
to achieve such a result. The following SQL query will return all rows from the Customer table
where the Customer_name field begins with “Herb”:
1SELECT * From Customers WHERE Name LIKE 'Herb%'

25. Between Monday and Tuesday


Today is Wednesday, and we arrive at work and discover that our new data entry clerk in
training has entered all new orders incorrectly on Monday and Tuesday. We wish to teach our
new trainee to find and correct all erroneous records. What’s the easiest way to get all the
records from the Orders table entered on Monday and Tuesday? The Between clause makes
the task a breeze:
1SELECT ID FROM Orders WHERE
2Date BETWEEN ‘01/12/2018’ AND ‘01/13/2018’

26. Finding the Intersection of Two Tables


Undoubtedly the whole reason that a relational database exists in the first place is to find
matching records in two tables! The JOIN statement accomplishes this core objective of SQL
and makes the task easy. Here we are going to fetch a list of all records which have matches in
the Customers and Orders tables:
1SELECT ID FROM Customers INNER
2JOIN Orders ON Customers.ID = Orders.ID

The point of INNER JOIN, in this case, is to select records in the Customers table which have
matching customer ID values in the Orders table and return only those records. Of course,
there are many types of JOIN, such as FULL, SELF, and LEFT, but for now, let’s keep things
interesting and move on to more diverse types of advanced SQL commands.

27. Doubling the Power with UNION


We can combine the results of two SQL query examples into one naturally with the UNION
keyword. Suppose we want to create a new table by combining the Customer_name and phone
from Customers with a list of that customer’s recent orders so that we can look for patterns
and perhaps suggest future purchases. Here is a quick way to accomplish the task:
1SELECT phone FROM Customers
2UNION SELECT item FROM Orders

The UNION keyword makes it possible to combine JOINS and other criteria to achieve a very
powerful new table generation potential.

28. Making Column Labels More Friendly


Aliasing column labels give us the convenience of renaming a column label to something more
readable. There is a tradeoff when naming columns to make them succinct results in reduced
readability in subsequent daily use. In our Orders table, the item column contains the
description of purchased products. Let’s see how to alias the item column to temporarily
rename it for greater user-friendliness:
1SELECT Item AS item_description FROM Orders

29. Always and Everywhere!


Wouldn’t it be great if there were a set of conditions you could depend on every time? The
complex SQL queries using ANY and ALL can make this ideal a reality! Let’s look at how
the ALL keyword is used to include records only when a set of conditions is true
for ALL records. In the following example, we will return records from the Orders table where
the idea is to get a list of high volume orders for a given item, in this case for customers who
ordered more than 50 of the product:

1SELECT Item FROM Orders


2WHERE id = ALL
3(SELECT ID FROM Orders
4WHERE quantity > 50)

30. Writing Developer Friendly SQL


An often overlooked but very important element of SQL scripting is adding comments to a
script of queries to explain what it’s doing for the benefit of future developers who may need to
revise and update your SQL queries.
A SQL script is a collection of SQL elements and commands accumulated as a file in SQL
Scripts. This script file can include many SQL commands or PL/SQL codes. One can utilize
SQL Scripts to build, edit, design, execute, and delete files.
The — single line and the /* .. */ multi-line delimiters empower us to add useful comments to
scripts, but this is also used in another valuable way. Sometimes a section of code may not be
in use, but we don’t want to delete it, because we anticipate using it again. Here we can simply
add the comment delimiter to deactivate it momentarily:
/* This query below is commented so it won't execute*/
1
/*
2
SELECT item FROM Orders
3
WHERE date ALL = (SELECT Order_ID FROM Orders
4
WHERE quantity > 50)
5
*/
6
 
7
/* the SQL query below the will be executed
8
9ignoring the text after "--"
10*/
11 
12SELECT item -- single comment 
13FROM Orders -- another single comment
14WHERE id
15ALL = (SELECT ID FROM Orders
16WHERE quantity > 25)

31.  SQL queries for Database Management


So far we have explored SQL query examples for querying tables and combining records from
multiple queries. Now it’s time to take a step upward and look at the database on a structural
level. Let’s start with the easiest SQL statement of all which creates a new database. Here, we
are going to create the DB as a container for our Customers and Orders tables used in the
previous ten examples above:
1CREATE DATABASE AllSales

32. Adding Tables to Our New DB


Next, we will actually add the Customers table which we’ve been using in previous examples,
and then add some of the column labels which we are already familiar with:
1CREATE TABLE Customers (
2ID varchar(80),
3Name varchar(80),
4Phone varchar(20),
5....
6);

Although most databases are created using a UI such as Access or OpenOffice, it is important
to know how to create and delete databases and tables programmatically via code with SQL
statements. This is especially so when installing a new web app and the UI asks new users to
enter names for DBs to be added during installation.

33. Modifying and Deleting Tables with SQL


The ALTER statement is used to modify or change the meaning of a table. In the case of the
relational tables with columns, ALTER statement is used to update the table to the new or
modified rules or definition. Alter belongs to the DDL category of Commands. Data definition
language can be described as a pattern for commands through which data structures are
represented.
Imagine that you decide to send a birthday card to your customers to show your appreciation
for their business, and so you want to add a birthday field to the Customers table. In these SQL
examples, you see how easy it is to modify existing tables with the ALTER statement:
1ALTER TABLE Customers ADD Birthday varchar(80)

If a table becomes corrupted with bad data you can quickly delete it like this:
1DROP TABLE table_name

34. The Key to Successful Indexing


An index is a schema element that includes a record for each content that arrives in the indexed
column of the database table or cluster and gives a high-speed path to rows. There are many types
of indexes such as Bitmap indexes, Partitioned indexes, Function-based indexes, and Domain
indexes.
Accurate indexing requires that the Primary Key column contains only unique values for this
purpose. This guarantees that JOIN statements will maintain integrity and produce valid
matches. Let’s create our Customers table again and establish the ID column as the Primary
Key:
1CREATE TABLE Customers (
2ID int NOT NULL,
3Name varchar(80) NOT NULL,
4PRIMARY KEY (ID)
5);

We can extend the functionality of the Primary Key so that it automatically increments from a
base. Change the ID entry above to add the AUTO_INCREMENT keyword as in the following
statement:
1ID int NOT NULL AUTO_INCREMENT

35. Advanced Concepts For Improving Performance


Whenever practical, is always better to write the column name list into a SELECT statement
rather than using the * delimiter as a wildcard to select all columns. SQL Server has to do a
search and replace operation to find all the columns in your table and write them into the
statement for you (every time the SELECT is executed). For example:
1SELECT * FROM Customers

Would actually execute much faster on our database as:


1SELECT Name, Birthday, Phone,
2Address, Zip FROM Customers

Performance pitfalls can be avoided in many ways. For example, avoid the time sinkhole of
forcing SQL Server to check the system/master database every time by using only a stored
procedure name, and never prefix it with SP_. Also setting NOCOUNT ON reduces the time
required for SQL Server to count rows affected by INSERT, DELETE, and other commands. Using
INNER JOIN with a condition is much faster than using WHERE clauses with conditions. We
advise developers to learn SQL server queries to an advanced level for this purpose. For
production purposes, these tips may be crucial to adequate performance. Notice that our
tutorial examples tend to favor the INNER JOIN.

36. Conditional Subquery Results


The SQL operator EXISTS tests for the existence of records in a subquery and returns a value
TRUE if a subquery returns one or more records. Have a look at this query with a subquery
condition:
1SELECT Name FROM Customers WHERE EXISTS
2(SELECT Item FROM Orders
3WHERE Customers.ID = Orders.ID AND Price < 50)

In this example above, the SELECT returns a value of TRUE when a customer has orders valued
at less than $50.
37. Copying Selections from Table to Table
There are a hundred and one uses for this SQL tool. Suppose you want to archive your yearly
Orders table into a larger archive table. This next example shows how to do it.
1INSERT INTO Yearly_Orders
2SELECT * FROM Orders
3WHERE Date<=1/1/2018

This example will add any records from the year 2018 to the archive.

38. Catching NULL Results


The NULL is the terminology applied to describe an absent value. Null does not mean zero. A NULL
value in a column of a table is a condition in a domain that seems to be empty. A column with a
NULL value is a domain with an absent value. It is essential to recognize that a NULL value is distinct
from a zero.
In cases where NULL values are allowed in a field, calculations on those values will produce
NULL results as well. This can be avoided by the use of the IFNULL operator. In this next
example, a value of zero is returned rather than a value of NULL when the calculation
encounters a field with a NULL value:
1SELECT Item, Price *
2(QtyInStock + IFNULL(QtyOnOrder, 0))
3FROM Orders

39. HAVING can be Relieving!


The problem was that the SQL WHERE clause could not operate on aggregate functions. The
problem was solved by using the HAVING clause. As an example, this next query fetches a list
of customers by the region where there is at least one customer per region:

1SELECT COUNT(ID), Region


2FROM Customers
3GROUP BY Region
4HAVING COUNT(ID) > 0;
40. Tie things up with Strings!
Let’s have a look at processing the contents of field data using functions. Substring is probably
the most valuable of all built-in functions. It gives you some of the power of Regex, but it’s not
so complicated as Regex. Suppose you want to find the substring left of the dots in a web
address. Here’s how to do it with an SQL Select query:
1SELECT SUBSTRING_INDEX("www.bytescout.com", ".", 2);

This line will return everything to the left of the second occurrence of “. ” and so, in this case, it
will return
1<a href="https://bytescout.com">www.bytescout.com</a>

Check this video to learn about every SQL query:


 

.. and 20 more useful SQL Queries examples!!

41. Use COALESCE to return the first non-null expression


The SQL Coalesce is used to manage the NULL values of the database. In this method, the
NULL values are substituted with the user-defined value. The SQL Coalesce function assesses
the parameters in series and always delivers the first non-null value from the specified
argument record.

Syntax

1SELECT COALESCE(NULL,NULL,'ByteScout',NULL,'Byte')

Output

ByteScout

42. Use Convert to transform any value into a particular datatype


This is used to convert a value into a defined datatype. For example, if you want to convert a
particular value into int datatype then a convert function can be used to achieve this. For
example,
Syntax

1SELECT CONVERT(int, 27.64)

Output

27

43. DENSE_RANK()Analytical query


It is an analytic query that computes the rank of a row in an arranged collection of rows. An
output rank is a number starting from 1. DENSE_RANK is one of the most important SQL
queries. It returns rank preferences as sequential numbers. It does not jump rank in event of
relations. For example, the following query will give the sequential ranks to the employee.
1SELECT eno,
2dno,
3salary,
4DENSE_RANK() OVER (PARTITION BY dno ORDER BY salary) AS ranking
5FROM employee;

7ENO  DNO SALARY RANKING
8
---------- ---------- ---------- ----------
9
7933  10 1500   1
10
7788  10 2650   2
11
7831  10 6000   3
12
7362  20 900    1
13
7870  20 1200   2
14
7564  20 2575   3
15
7784  20 4000   4
16
7903  20 4000   4
17
7901  30 550    1
18
7655  30 1450   2
19
7522  30 1450   2
20
217844  30 1700   3
7493  30 1500   4
227698  30 2850   5

44. Query_partition_clause
The query_partition_clause breaks the output set into distributions, or collections, of data. The
development of the analytic query is limited to the confines forced by these partitions, related to the
process a GROUP BY clause modifies the performance of an aggregate function. If the
query_partition_clause is eliminated, the entire output collection is interpreted as a separate
partition.
The following query applies an OVER clause, so the average displayed is based on all the
records of the output set.
SELECT eno, dno, salary,
1
AVG(salary) OVER () AS avg_sal
2
FROM employee;
3
 
4
EO  DNO SALARY AVG_SAL
5
---------- ---------- ---------- ----------
6
7364 20 900 2173.21428
7
7494 30 1700 2173.21428
8
7522 30 1350 2173.21428
9
7567 20 3075 2173.21428
10
7652 30 1350 2173.21428
11
7699 30 2950 2173.21428
12
7783 10 2550 2173.21428
13
147789 20 3100 2173.21428
157838 10 5100 2173.21428
167845 30 1600 2173.21428
177877 20 1200 2173.21428
187901 30 1050 2173.21428
197903 20 3100 2173.21428
207935 10 1400 2173.21428
45. Finding the last five records from the table
Now, if you want to fetch the last eight records from the table then it is always difficult to get such
data if your table contains huge information. For example, you want to get the last 8 records from
the employee table then you can use rownum and a union clause. The rownum is temporary in SQL.

For example,
1Select * from Employee A where rownum <=8
2union
3select * from (Select * from Employee A order by rowid desc) where rownum <=8;

The above SQL query will give you the last eight records from the employee table where
rownum is a pseudo column. It indexes the data in an output set.

46. LAG
The LAG is applied to get data from a prior row. This is an analytical function. For example, the
following query gives the salary from the prior row to compute the difference between the salary of
the current row and that of the prior row. In this query, the ORDER BY of the LAG function is
applied. The default is 1 if you do not define offset. The arbitrary default condition is given if
the offset moves past the range of the window. The default is null if you do not define default.

Syntax

1SELECT dtno,
2       eno,
3       emname,
4       job,
5       salary,
6       LAG(sal, 1, 0) OVER (PARTITION BY dtno ORDER BY salary) AS salary_prev
7FROM   employee;

Output

1DTNO ENO ENAME JOB SAL SAL_PREV


2---------- ---------- ---------- --------- ---------- ----------
310 7931 STEVE CLERK 1300 0
4
10 7783 JOHN MANAGER 2450 1300
10 7834 KING PRESIDENT 5000 2450
5
20 7364 ROBIN CLERK 800 0
6
20 7876 BRIAN CLERK 1100 800
7
20 7567 SHANE MANAGER 2975 1100
8
20 7784 SCOTT ANALYST 3000 2975
9
1020 7908 KANE ANALYST 3000 3000
1130 7900 JAMES CLERK 950 0
1230 7651 CONNER SALESMAN 1250 950
1330 7522 MATTHEW SALESMAN 1250 1250
1430 7843 VIVIAN SALESMAN 1500 1250
1530 7494 ALLEN SALESMAN 1600 1500
1630 7695 GLEN MANAGER 2850 1600

47. LEAD
The LEAD is also an analytical query that is applied to get data from rows extra down the
output set. The following query gives the salary from the next row to compute the deviation between
the salary of the prevailing row and the subsequent row. The default is 1 if you do not define
offset. The arbitrary default condition is given if the offset moves past the range of the window.
The default is null if you do not define default.
1SELECT eno,
2       empname,
3       job,
4       salary,
5       LEAD(salary, 1, 0) OVER (ORDER BY salary) AS salary_next,
6       LEAD(salary, 1, 0) OVER (ORDER BY salary) - salary AS salary_diff
7FROM   employee;

9ENO EMPNAME JOB SALARY SALARY_NEXT SALARY_DIFF
10
---------- ---------- --------- ---------- ---------- ----------
11
7369 STEVE CLERK 800 950 150
12
7900 JEFF CLERK 950 1100 150
13
7876 ADAMS CLERK 1100 1250 150
7521 JOHN SALESMAN 1250 1250 0
14
7654 MARK SALESMAN 1250 1300 50
15
7934 TANTO CLERK 1300 1500 200
16
7844 MATT SALESMAN 1500 1600 100
17
187499 ALEX SALESMAN 1600 2450 850
197782 BOON MANAGER 2450 2850 400
207698 BLAKE MANAGER 2850 2975 125
217566 JONES MANAGER 2975 3000 25
227788 SCOTT ANALYST 3000 3000 0
237902 FORD ANALYST 3000 5000 2000
247839 KING PRESIDENT 5000 0 -5000

48. PERCENT_RANK
The PERCENT_RANK analytic query. The ORDER BY clause is necessary for this query. Excluding
a partitioning clause from the OVER clause determines the entire output set is interpreted as a
separate partition. The first row of the standardized set is indicated 0 and the last row of
the set is indicated 1. For example, the SQL query example gives the following output.

Syntax

1SELECT
2     prdid, SUM(amount),
3     PERCENT_RANK() OVER (ORDER BY SUM(amount) DESC) AS percent_rank
4     FROM sales
5     GROUP BY prdid
6     ORDER BY prdid;

Output

1PRDID        SUM(AMOUNT)  PERCENT_RANK


2----------- ----------- ------------
3          1    22623.5            0
4          2   223927.08           1
49. MIN
Utilizing a blank OVER clause converts the MIN into an analytic function. This is also an
analytical query. In this, the entire result set is interpreted as a single partition. It gives you the
minimum salary for all employees and their original data. For example, the following query is
displaying the use of MIN in the Select query.
SELECT eno,
1
       empname,
2
       dtno,
3
       salary,
4
       MIN(salary) OVER (PARTITION BY dtno) AS min_result
5
FROM   employee;
6
 
7
     ENO   EMPNAME          DTNO     SALARY MIN_RESULT
8
9
---------- ---------- ---------- ---------- ---------------

10
      7782 CLARK              10       2450            1300

11      7839 KING               10       5000            1300


12      7934 MILLER             10       1300            1300
13      7566 JONES              20       2975             800
14      7902 FORD               20       3000             800
15      7876 ADAMS              20       1100             800
16      7369 SMITH              20        800             800
17      7788 SCOTT              20       3000             800
18      7521 WARD               30       1250             950
19      7844 TURNER             30       1500             950
20      7499 ALLEN              30       1600             950
21      7900 JAMES              30        950             950
22      7698 BLAKE              30       2850             950
23      7654 MARTIN             30       1250             950

50. MAX
Using a blank row OVER clause converts the MAX into an analytic function. The lack of a
partitioning clause indicates the entire output set is interpreted as a separate
partition. This gives the maximum salary for all employees and their original data. For
example, the following query displays the use of MAX in the select query.
SELECT eno,
1
       empname,
2
       dtno,
3
       salary,
4
       MAX(salary) OVER () AS max_result
5
FROM   employee;
6
 
7
     ENO   EMPNAME          DTNO       SALARY    MAX_RESULT
8
9
---------- ---------- ---------- ---------- ----------
      7369 SMITH              20        800       3000
10
11
      7499 ALLEN              30       1600       3000

12      7521 WARD               30       1250       3000


13      7566 JONES              20       2975       3000
14      7654 MARTIN             30       1250       3000
15      7698 BLAKE              30       2850       3000
16      7782 CLARK              10       2450       3000
17      7788 SCOTT              20       3000       3000
18      7839 KING               10       5000       3000
19      7844 TURNER             30       1500       3000
20      7876 ADAMS              20       1100       3000
21      7900 JAMES              30        950       3000
22      7902 FORD               20       3000       3000
23      7934 MILLER             10       1300       3000

51. Top- N queries


Top-N queries give a process for restricting the number of rows delivered from organized
assemblages of data. They are remarkably beneficial when users want to give the top or
bottom number of rows from a table.
For example, the following query gives the 20 rows with 10 different values:
1SELECT price
FROM   sales_order
2ORDER BY price;

4PRICE
5----------
6100
7100
8200
9200
10
300
11
300
12
400
13
400
14
500
15
500
16
600
17
 
18
PRICE
19
----------
20
600
21
700
22
700
23
24800
25800
26900
27900
281000
291000
30 
3120 rows selected.
52. CORR Analytic Query
The CORR analytic function is utilized to determine the coefficient of correlation. This query is
also used to calculate the Pearson correlation coefficient. The function calculates the following
on rows in the table with no null values. This query always returns the values between +1 and -
1, which describe the following:
Syntax: CORR(exp1, exp2) [ OVER (analytic_clause) ]

Example

1SELECT empid,
2       name,
3       dno,
4       salary,
5       job,
6       CORR(SYSDATE - joiningdate, salary) OVER () AS my_corr_val
7FROM   employee;

53. NTILE Analytic Query


The NTILE enables users to split a sequence set into a detailed number of relatively similar
groups, or containers, rows sanctioning. If the number of rows in the collection is less than the
number of containers defined, the number of containers will be decreased. The basic syntax is
as displayed below:
NTILE(exp) OVER ([ partition_clause ] order_by)

Example

1SELECT empid,
2       name,
3       dno,
4       salary,
5       NTILE(6) OVER (ORDER BY salary) AS container_no
6FROM   employee;
54. VARIANCE, VAR_POP, and VAR_SAMP Query
The VARIANCE, VAR_POP, and VAR_SAMP are aggregate functions. These are utilized to
determine the variance, group variance, and sample variance of a collection of data
individually. As aggregate queries or functions, they decrease the number of rows, therefore
the expression “aggregate”. If the data isn’t arranged we change the total rows in the Employee
table to a separate row with the aggregated values. For example, the following query is
displaying the use of these functions:

1SELECT VARIANCE(salary) AS var_salary,


2       VAR_POP(salary) AS pop_salary,
3       VAR_SAMP(salary) AS samp_salary
4FROM   employee;

6VAR_SALARY   POP_SALARY   SAMP_SALARY
7------------ ----------- ------------
81479414.97  1588574.81   1388717.27

55. STDDEV, STDDEV_POP, and STDDEV_SAMP Queries


The STDDEV, STDDEV_POP, and STDDEV_SAMP aggregate queries or functions are applied to
determine the standard deviation, population standard deviation, and cumulative sample
standard deviation individually. As aggregate queries, they decrease the number of rows,
therefore the expression “aggregate”. If the data isn’t arranged we convert all the rows in the
EMPLOYEE table to a separate row. For example, the following query is displaying the use of all
these functions.

1SELECT STDDEV(salary) AS stddev_salary,


2       STDDEV_POP(salary) AS pop_salary,
3       STDDEV_SAMP(salary) AS samp_salary
4FROM   employee;

6STDDEV_SALARY POP_SALARY SAMP_SALARY
7---------- -------------- ---------------
81193.50     1159.588      1193.603

If there is more than one account after dropping nulls, the STDDEV function gives the result of
the STDDEV_SAMP. Using an empty OVER clause converts the STDDEV query result into an
analytic query. The absence of a partitioning indicates the entire output set is interpreted as a
particular partition, so we accept the standard deviation of the salary and the primary data.

56. Pattern Matching


The pattern matching syntax adds various alternatives. Data must be treated precisely and in a
proper form. The PARTITION BY and ORDER BY conditions of all SQL analytic queries is applied
to split the data into accumulations and within each group. If no partitions are specified, it is
considered the entire sequence set is one huge partition.
For example,
The MEASURES clause specifies the column result that will be provided for each match.

Syntax

1MEASURES  STRT.tstamp AS initial_tstamp,


2          LAST(UP.tstamp) AS first_tstamp,
3          LAST(DOWN.tstamp) AS finished_tstamp

Example

1DEFINE
2  UP AS UP.products_sold > PREV(UP.products_sold),
3  FLAT AS FLAT.products_sold = PREV(FLAT.products_sold),
4  DOWN AS DOWN.products_sold < PREV(DOWN.products_sold)

57. FIRST_VALUE
The simplest way to get analytic functions is to begin by studying aggregate functions. An
aggregate function collects or gathers data from numerous rows into a unique result row. For
instance, users might apply the AVG function to get an average of all the salaries in the
EMPLOYEE table. Let’s take a look at how First_Value can be used. The primary explanation for
the FIRST_VALUE analytic function is displayed below.

Syntax:

1FIRST_VALUE
2  { (expr) [NULLS ]
3  | (expr [NULLS ])
4  }
5  OVER (analytic clause)

Example

1SELECT eno,
2       dno,
3       salary,
4       FIRST_VALUE(salary) IGNORE NULLS
5         OVER (PARTITION BY dno ORDER BY salary) AS lowest_salary_in_dept
6FROM   employee;

The above query will ignore null values.

58. LAST_VALUE
The primary explanation for the LAST_VALUE analytic query or function is displayed below.
1Syntax: LAST_VALUE
2  { (expr) [ { NULLS ]
3  | (expr [ NULLS ])
4  OVER (analytic clause)

The LAST_VALUE analytic query is related to the LAST analytic function. The function enables
users to get the last output from an organized column. Applying the default windowing to the
output can be surprising. For example,

1SELECT eno,
2       dno,
3       salary,
4       LAST_VALUE(salary) IGNORE NULLS
5         OVER (PARTITION BY dno ORDER BY salary) AS highest_salary_in_dept
6FROM   employee;

59. Prediction
The design sample foretells the gender and age of clients who are most expected to adopt an
agreement card (target = 1). The PREDICTION function takes the price matrix correlated with
the design and applies for marital status, and house size as predictors. The syntax of the
PREDICTION function can also apply a piece of arbitrary GROUPING information when getting a
partitioned model.
SELECT client_gender, COUNT(*) AS ct, ROUND(AVG(age)) AS average_age
1
   FROM mining_data_shop
2
   WHERE PREDICTION(sample COST MODEL
3
4
      USING client_marital_status, house_size) = 1

5   GROUP BY client_gender
6   ORDER BY client_gender;
7    
8CUST_GENDER         CNT    AVG_AGE
9------------ ---------- ----------
10F                   270         40
11M                   585         41

60. CLUSTER_SET
CLUSTER_SET can get the data in one of the couple steps: It can use a mining type object to the
information, or it can mine the data by performing an analytic clause that creates and uses one
or more moving mining patterns.
This example enumerates the properties that have the biggest influence on cluster distribution
for client ID 1000. The query requests the CLUSTER_DETAILS and CLUSTER_SET functions, which
use the clustering model my_sample.

Example

1SELECT S.cluster_id, prob,


2       CLUSTER_DETAILS(my_sample, S.cluster_id, 7 USING T.*) kset
3FROM
4  (SELECT v.*, CLUSTER_SET(my_sample, USING *) nset
5    FROM mining_data
6   WHERE client_id = 1000) T,
7  TABLE(T.nset) Q
8ORDER BY 2 DESC; 
A cluster is a group table that distributes the corresponding data blocks i.e. all the tables are
actually put together. For example, EMPLOYEE and DEPARTMENT tables are connected to the
DNO column. If you cluster them, it will actually store all rows in the same data blocks.

.. and TEN More Advanced SQL Queries for our Users!

61. WITH (Common Table Expressions)


A common table expression (CTE) is a defined short result set that endures within the range of
a particular statement and that can be called later within that statement, perhaps on many
occasions. The following query is describing the CTE:

Syntax

WITH all_emp
1
AS
2
(
3
SELECT empId, BossId, FirstName, LastName
4
FROM Emp
5
6WHERE BossId is NULL

8UNION ALL

10SELECT e.empId, e.BossId, e.FirstName, e.LastName
11FROM Emp e INNER JOIN all_emp r
12ON e.BossId = r.Id
13)
14SELECT * FROM all_emp

62. NANVL
This function is utilized to deliver an optional value n1 if the inserted value n2 is NaN (not a
number), and gives n2 if n2 is not a number. This function is used only for type BINARY_FLOAT.
The following query is displaying its use:
Example

1SELECT bin_float, NANVL(bin_float,0)


2FROM my_demo_table;

63. WIDTH_BUCKET
This function is used to obtain the bucket number. In this, it gives the value of the expression
that would come under after being assessed. The following query is displaying its use:

Example

1SELECT emp_id, first_name,last_name,dept_id,mgr_id,


2WIDTH_BUCKET(department_id,20,40,10) "Exists in Dept"
3FROM emp
4WHERE mgr_id < 300
5ORDER BY "Exists in Dept";

64. COSH
This function is used to deliver the hyperbolic cosine of a number. It accepts all numeric or
non-numeric data types as an argument. The following query is displaying its use:

Example

1SELECT COSH(0) "COSH of 0" FROM DUAL;

65. SOUNDEX
The SOUNDEX function delivers a character string comprising the description of char. It allows
users to match words that are spelled antagonistically, but sound similar in English. It does not
support CLOB. The following query is displaying its use:

Example

1SELECT last_name, first_name


2FROM hr.emp
3WHERE SOUNDEX(last_name)
4= SOUNDEX('SCOTTY');

66. TZ_OFFSET
The TZ_OFFSET gives the time zone offset identical to the case based on the date the statement
is given. The following query is displaying its use:

Example

1SELECT TZ_OFFSET('US/Eastern') FROM DUAL;

67. CARDINALITY
CARDINALITY is utilized to obtain the number of components in a nested table. It is supported
in different versions. The following query is displaying its use:

Example

1SELECT product_id, CARDINALITY(ad_mydocs_get)


2FROM my_media_table;

68. DUMP
DUMP is one of the important string/char functions. It is utilized to get a VARCHAR2 value. The
value delivered defines the data type code. The following query is displaying its use:

Example

1SELECT DUMP('pqr', 1033)


2FROM DUAL;

69. PATH
PATH is applied simply with the UNDER_PATH and EQUALS_PATH requirements. It gives the
corresponding path that points to the resource defined in the main state. The following query
is displaying its use:
Example

1SELECT ANY_PATH FROM RESOURCE_VIEW


2WHERE EQUALS_PATH(res, '/sys/schemas/OE/www.pqr.com')=3;

70. UNISTR
UNISTR accepts an expression that determines character data and delivers it in the general
character set. It gives support to the Unicode string literals by allowing users to define the
Unicode value. The following query is displaying its use:

Example

1SELECT UNISTR('pqr\00e4\00f3\00f9') FROM DUAL;

Database Queries in SQL


These are some of the commonly used database queries in SQL.

1. SELECT – database query used to extract data from a table.


2. CREATE DATABASE – database query used to create a new database.
3. DROP DATABASE – database query used to delete a database.
4. CREATE TABLE – database query used to create a table in the specified database.
5. ALTER TABLE – database query used to modify an existing table in the specified database
6. DROP TABLE – database query used to delete an existing table in the specified database
7. CREATE INDEX – Index creation query.
8. CREATE VIEW – View creation query.
9. DROP VIEW – View deletion query.
10. CREATE PROCEDURE – Procedure creation query.
11. CREATE FUNCTION – Function creation query.
12. DROP PROCEDURE – Procedure deletion query.
13. DROP FUNCTION – Function deletion query.

Example of Query in a Database


We will be looking at some SELECT examples of a query in a database, as this is the most
common command.
SELECT * FROM employeeTable WHERE emp_no = ‘12’;
This query filters out results that do not match a specified search.
SELECT * FROM sys.objects WHERE object_id=object_id(‘<table name>’);
This database query will list all the columns of the table whose name matches the argument.

Database Queries Examples


The following are some database queries examples that deal with creating tables, in a bit more
advanced fashion.

1. Create a table with a primary key called “ID”.

CREATE TABLE table_name (


PRIMARY KEY(column_name)
);

2. Create a table with a non-unique index called “IDX” on column_name.

CREATE INDEX idx_name ON table_name (column_name);

3. Create a view with the name “VIEW1” that can be used to query data from table1. The view is
created on columns column1 and column2. It must return the same number of rows as the
underlying table, and it must return the same data type. In this case, we will return the maximum
value for each column in the underlying table when queried against the view. The following
query will be used to populate our view:

CREATE VIEW view1 AS SELECT MAX(column1), MAX(column2) FROM table1;

Common Database Queries


Some more common database queries you will need are:
1)    The Maximum Value for a Column.

SELECT MAX(column_name) FROM table_name

2)    The Minimum Value for a Column.

SELECT MIN(column_name) FROM table_name

3)    The Count of Rows in a Table.

SELECT COUNT(*) FROM table_name;


DBMS Queries with Examples
DBMS stands for DataBase Management System. Following are some DBMS queries with
examples; these are very important for developers and database admins alike.
1. The List of Values for a Column.

SELECT column_name, column_commalist FROM table_name;

2. The Difference between two Column Values.

SELECT column_name(+) FROM table_name WHERE column_name(+) != value;

III.         The List of Unique Values for a Column.

SELECT DISTINCT column_name FROM table_name;

1. Write an SQL query to fetch the EmpId and FullName of all the employees working under
the Manager with id – ‘986’.
We can use the EmployeeDetails table to fetch the employee details with a where clause for the
manager-
SELECT EmpId, FullName

FROM EmployeeDetails

WHERE ManagerId = 986;

2. Write an SQL query to fetch the different projects available from the EmployeeSalary table.
While referring to the EmployeeSalary table, we can see that this table contains project values
corresponding to each employee, or we can say that we will have duplicate project values while
selecting Project values from this table.

So, we will use the distinct clause to get the unique values of the Project.
SELECT DISTINCT(Project)

FROM EmployeeSalary;

3. Write an SQL query to fetch the count of employees working in project ‘P1’.
Here, we would be using aggregate function count() with the SQL where clause-
SELECT COUNT(*)

FROM EmployeeSalary
WHERE Project = 'P1';

4. Write an SQL query to find the maximum, minimum, and average salary of the employees.
We can use the aggregate function of SQL to fetch the max, min, and average values-
SELECT Max(Salary),

Min(Salary),

AVG(Salary)

FROM EmployeeSalary;

5. Write an SQL query to find the employee id whose salary lies in the range of 9000 and
15000.
Here, we can use the ‘Between’ operator with a where clause.
SELECT EmpId, Salary

FROM EmployeeSalary

WHERE Salary BETWEEN 9000 AND 15000;

6. Write an SQL query to fetch those employees who live in Toronto and work under the
manager with ManagerId – 321.
Since we have to satisfy both the conditions – employees living in ‘Toronto’ and working in Project
‘P2’. So, we will use the AND operator here-
SELECT EmpId, City, ManagerId

FROM EmployeeDetails

WHERE City='Toronto' AND ManagerId='321';

7. Write an SQL query to fetch all the employees who either live in California or work under a
manager with ManagerId – 321.
This interview question requires us to satisfy either of the conditions – employees living in
‘California’ and working under Manager with ManagerId – 321. So, we will use the OR operator
here-
SELECT EmpId, City, ManagerId

FROM EmployeeDetails

WHERE City='California' OR ManagerId='321';


8. Write an SQL query to fetch all those employees who work on Projects other than P1.
Here, we can use the NOT operator to fetch the rows which are not satisfying the given condition.
SELECT EmpId

FROM EmployeeSalary

WHERE NOT Project='P1';

Or using the ‘not equal to’ operator-


SELECT EmpId

FROM EmployeeSalary

WHERE Project <> 'P1';

For the difference between NOT and <> SQL operators, check this link – Difference between the
NOT and != operators.

9. Write an SQL query to display the total salary of each employee adding the Salary with
Variable value.
Here, we can simply use the ‘+’ operator in SQL.
SELECT EmpId,

Salary+Variable as TotalSalary

FROM EmployeeSalary;

10. Write an SQL query to fetch the employees whose name begins with any two characters,
followed by a text “hn” and ends with any sequence of characters.
For this question, we can create an SQL query using like operator with ‘_’ and ‘%’ wild card
characters, where ‘_’ matches a single character and ‘%’ matches ‘0 or multiple characters.
SELECT FullName

FROM EmployeeDetails

WHERE FullName LIKE ‘__hn%’;

11. Write an SQL query to fetch all the EmpIds which are present in either of the tables –
‘EmployeeDetails’ and ‘EmployeeSalary’.
In order to get unique employee ids from both tables, we can use the Union clause which can
combine the results of the two SQL queries and return unique rows.
SELECT EmpId FROM EmployeeDetails

UNION

SELECT EmpId FROM EmployeeSalary;

12. Write an SQL query to fetch common records between two tables.


SQL Server – Using INTERSECT operator-
SELECT * FROM EmployeeSalary

INTERSECT

SELECT * FROM ManagerSalary;

MySQL – Since MySQL doesn’t have INTERSECT operator so we can use the subquery-
SELECT *

FROM EmployeeSalary

WHERE EmpId IN

(SELECT EmpId from ManagerSalary);

13. Write an SQL query to fetch records that are present in one table but not in another table.
SQL Server – Using MINUS- operator-
SELECT * FROM EmployeeSalary

MINUS

SELECT * FROM ManagerSalary;

MySQL – Since MySQL doesn’t have a MINUS operator so we can use LEFT join-
SELECT EmployeeSalary.*

FROM EmployeeSalary

LEFT JOIN

ManagerSalary USING (EmpId)

WHERE ManagerSalary.EmpId IS NULL;


14. Write an SQL query to fetch the EmpIds that are present in both the tables – 
‘EmployeeDetails’ and ‘EmployeeSalary.
Using subquery-
SELECT EmpId FROM

EmployeeDetails

where EmpId IN

(SELECT EmpId FROM EmployeeSalary);

15. Write an SQL query to fetch the EmpIds that are present in EmployeeDetails but not in
EmployeeSalary.
Using subquery-
SELECT EmpId FROM

EmployeeDetails

where EmpId Not IN

(SELECT EmpId FROM EmployeeSalary);

16. Write an SQL query to fetch the employee’s full names and replace the space with ‘-’.
Using the ‘Replace’ function-
SELECT REPLACE(FullName, ' ', '-')

FROM EmployeeDetails;

17. Write an SQL query to fetch the position of a given character(s) in a field.
Using the ‘Instr’ function-
SELECT INSTR(FullName, 'Snow')

FROM EmployeeDetails;

18. Write an SQL query to display both the EmpId and ManagerId together.
Here we can use the CONCAT command.
SELECT CONCAT(EmpId, ManagerId) as NewId

FROM EmployeeDetails;
19. Write a query to fetch only the first name(string before space) from the FullName column
of the EmployeeDetails table.
In this question, we are required to first fetch the location of the space character in the FullName
field and then extract the first name out of the FullName field.

For finding the location we will use the LOCATE method in MySQL and CHARINDEX in SQL
SERVER and for fetching the string before space, we will use the SUBSTRING OR MID method.

MySQL – using MID


SELECT MID(FullName, 1, LOCATE(' ',FullName))

FROM EmployeeDetails;

SQL Server – using SUBSTRING


SELECT SUBSTRING(FullName, 1, CHARINDEX(' ',FullName))

FROM EmployeeDetails;

20. Write an SQL query to uppercase the name of the employee and lowercase the city
values.
We can use SQL Upper and Lower functions to achieve the intended results.
SELECT UPPER(FullName), LOWER(City)

FROM EmployeeDetails;

21. Write an SQL query to find the count of the total occurrences of a particular character –
‘n’ in the FullName field.
Here, we can use the ‘Length’ function. We can subtract the total length of the FullName field from
the length of the FullName after replacing the character – ‘n’.
SELECT FullName,

LENGTH(FullName) - LENGTH(REPLACE(FullName, 'n', ''))

FROM EmployeeDetails;

22. Write an SQL query to update the employee names by removing leading and trailing
spaces.
Using the ‘Update’ command with the ‘LTRIM’ and ‘RTRIM’ functions.
UPDATE EmployeeDetails

SET FullName = LTRIM(RTRIM(FullName));


23. Fetch all the employees who are not working on any project.
This is one of the very basic interview questions in which the interviewer wants to see if the person
knows about the commonly used – Is NULL operator.
SELECT EmpId

FROM EmployeeSalary

WHERE Project IS NULL;

24. Write an SQL query to fetch employee names having a salary greater than or equal to
5000 and less than or equal to 10000.
Here, we will use BETWEEN in the ‘where’ clause to return the EmpId of the employees with salary
satisfying the required criteria and then use it as a subquery to find the fullName of the employee
from the EmployeeDetails table.
SELECT FullName

FROM EmployeeDetails

WHERE EmpId IN

(SELECT EmpId FROM EmployeeSalary

WHERE Salary BETWEEN 5000 AND 10000);

25. Write an SQL query to find the current date-time.


MySQL-
SELECT NOW();

SQL Server-
SELECT getdate();

Oracle-
SELECT SYSDATE FROM DUAL;

26. Write an SQL query to fetch all the Employee details from the EmployeeDetails table who
joined in the Year 2020.
Using BETWEEN for the date range ’01-01-2020′ AND ’31-12-2020′-
SELECT * FROM EmployeeDetails

WHERE DateOfJoining BETWEEN '2020/01/01'


AND '2020/12/31';

Also, we can extract the year part from the joining date (using YEAR in MySQL)-
SELECT * FROM EmployeeDetails

WHERE YEAR(DateOfJoining) = '2020';

27. Write an SQL query to fetch all employee records from the EmployeeDetails table who
have a salary record in the EmployeeSalary table.
Using ‘Exists’-
SELECT * FROM EmployeeDetails E

WHERE EXISTS

(SELECT * FROM EmployeeSalary S

WHERE E.EmpId = S.EmpId);

28. Write an SQL query to fetch the project-wise count of employees sorted by project’s
count in descending order.
The query has two requirements – first to fetch the project-wise count and then to sort the result by
that count.

For project-wise count, we will be using the GROUP BY clause and for sorting, we will use the
ORDER BY clause on the alias of the project count.
SELECT Project, count(EmpId) EmpProjectCount

FROM EmployeeSalary

GROUP BY Project

ORDER BY EmpProjectCount DESC;

29. Write a query to fetch employee names and salary records. Display the employee details
even if the salary record is not present for the employee.
This is again one of the very common interview questions in which the interviewer just wants to
check the basic knowledge of SQL JOINS.

Here, we can use the left join with the EmployeeDetail table on the left side of the EmployeeSalary
table.
SELECT E.FullName, S.Salary

FROM EmployeeDetails E
LEFT JOIN

EmployeeSalary S

ON E.EmpId = S.EmpId;

30. Write an SQL query to join 3 tables.


Considering 3 tables TableA, TableB, and TableC, we can use 2 joins clauses like below-

SELECT column1, column2

FROM TableA

JOIN TableB ON TableA.Column3 = TableB.Column3

JOIN TableC ON TableA.Column4 = TableC.Column4;

For more questions on SQL Joins, you can also check our top SQL Joins Interview
Questions.
SQL Query Interview Questions for Experienced

Here is a list of some of the most frequently asked SQL query interview questions for experienced
professionals. These questions cover SQL queries on advanced SQL JOIN concepts, fetching
duplicate rows, odd and even rows, nth highest salary, etc.
31. Write an SQL query to fetch all the Employees who are also managers from the
EmployeeDetails table.
Here, we have to use Self-Join as the requirement wants us to analyze the EmployeeDetails table
as two tables. We will use different aliases ‘E’ and ‘M’ for the same EmployeeDetails table.
SELECT DISTINCT E.FullName
FROM EmployeeDetails E

INNER JOIN EmployeeDetails M

ON E.EmpID = M.ManagerID;

To learn more about Self Join along with some more queries, you can watch the below video that
explains the self-join concept in a very simple way.
Self Join and Its Demonstration

32. Write an SQL query to fetch duplicate records from EmployeeDetails (without considering
the primary key – EmpId).
In order to find duplicate records from the table, we can use GROUP BY on all the fields and then
use the HAVING clause to return only those fields whose count is greater than 1 i.e. the rows having
duplicate records.
SELECT FullName, ManagerId, DateOfJoining, City, COUNT(*)

FROM EmployeeDetails

GROUP BY FullName, ManagerId, DateOfJoining, City

HAVING COUNT(*) > 1;

33. Write an SQL query to remove duplicates from a table without using a temporary table.
Here, we can use delete with alias and inner join. We will check for the equality of all the matching
records and then remove the row with a higher EmpId.
DELETE E1 FROM EmployeeDetails E1

INNER JOIN EmployeeDetails E2

WHERE E1.EmpId > E2.EmpId

AND E1.FullName = E2.FullName

AND E1.ManagerId = E2.ManagerId

AND E1.DateOfJoining = E2.DateOfJoining

AND E1.City = E2.City;

34. Write an SQL query to fetch only odd rows from the table.
In case we have an auto-increment field e.g. EmpId then we can simply use the below query-
SELECT * FROM EmployeeDetails
WHERE MOD (EmpId, 2) <> 0;

In case we don’t have such a field then we can use the below queries.

Using Row_number in SQL server and checking that the remainder when divided by 2 is 1-
SELECT E.EmpId, E.Project, E.Salary

FROM (

SELECT *, Row_Number() OVER(ORDER BY EmpId) AS RowNumber

FROM EmployeeSalary

)E

WHERE E.RowNumber % 2 = 1;

Using a user-defined variable in MySQL-


SELECT *

FROM (

SELECT *, @rowNumber := @rowNumber+ 1 rn

FROM EmployeeSalary

JOIN (SELECT @rowNumber:= 0) r

)t

WHERE rn % 2 = 1;

35. Write an SQL query to fetch only even rows from the table.
In case we have an auto-increment field e.g. EmpId then we can simply use the below query-
SELECT * FROM EmployeeDetails

WHERE MOD (EmpId, 2) = 0;

In case we don’t have such a field then we can use the below queries.

Using Row_number in SQL server and checking that the remainder, when divided by 2, is 1-
SELECT E.EmpId, E.Project, E.Salary
FROM (

SELECT *, Row_Number() OVER(ORDER BY EmpId) AS RowNumber

FROM EmployeeSalary

)E

WHERE E.RowNumber % 2 = 0;

Using a user-defined variable in MySQL-


SELECT *

FROM (

SELECT *, @rowNumber := @rowNumber+ 1 rn

FROM EmployeeSalary

JOIN (SELECT @rowNumber:= 0) r

)t

WHERE rn % 2 = 0;

36. Write an SQL query to create a new table with data and structure copied from another
table.
CREATE TABLE NewTable

SELECT * FROM EmployeeSalary;

37. Write an SQL query to create an empty table with the same structure as some other table.
Here, we can use the same query as above with the False ‘WHERE’ condition-
CREATE TABLE NewTable

SELECT * FROM EmployeeSalary where 1=0;

38. Write an SQL query to fetch top n records.


In MySQL using LIMIT-
SELECT *

FROM EmployeeSalary
ORDER BY Salary DESC LIMIT N;

In SQL server using TOP command-


SELECT TOP N *

FROM EmployeeSalary

ORDER BY Salary DESC;

39. Write an SQL query to find the nth highest salary from a table.
Using Top keyword (SQL Server)-
SELECT TOP 1 Salary

FROM (

SELECT DISTINCT TOP N Salary

FROM Employee

ORDER BY Salary DESC

ORDER BY Salary ASC;

Using limit clause(MySQL)-


SELECT Salary

FROM Employee

ORDER BY Salary DESC LIMIT N-1,1;

40. Write SQL query to find the 3rd highest salary from a table without using the TOP/limit
keyword.
This is one of the most commonly asked interview questions. For this, we will use a correlated
subquery.

In order to find the 3rd highest salary, we will find the salary value until the inner query returns a
count of 2 rows having a salary greater than other distinct salaries.
SELECT Salary

FROM EmployeeSalary Emp1


WHERE 2 = (

SELECT COUNT( DISTINCT ( Emp2.Salary ) )

FROM EmployeeSalary Emp2

WHERE Emp2.Salary > Emp1.Salary

For the nth highest salary-


SELECT Salary

FROM EmployeeSalary Emp1

WHERE N-1 = (

SELECT COUNT( DISTINCT ( Emp2.Salary ) )

FROM EmployeeSalary Emp2

WHERE Emp2.Salary > Emp1.Salary

This concludes our post on frequently asked SQL query interview questions and answers. I hope
you practice these questions and ace your database interviews.

If you feel, we have missed any of the common interview questions on SQL then do let us know in
the comments and we will add those questions to our list.

RDBMS Interview Questions


Let’s start with the basic RDBMS interview questions first and then we will move to more complex
database interview questions. Since these questions focus on the database fundamentals which are
common to all the databases, so the questions hold good for the different databases – SQL server,
Oracle, MySQL, etc.
Ques.1. What is a database?
Ans. A database is a structured collection of data for faster and better access, storage, and
manipulation of data.
A database can also be defined as a collection of tables, schema, views, and other database
objects.
Ques.2. What is database testing?
Ans. Database testing involves checking the integrity of actual data in the front end with the data
present in the database. It involves validating the data, checking that there are no orphan records
(record with a foreign key to a parent record that has been deleted), no junk records are present,
updating records in the database, and verify the value in the front end.

Ques.3. What is RDBMS?


Ans. An RDBMS or Relational Database Management System is a type of DBMS having
relationships between the tables using indexes and different constraints like primary key, foreign
key, etc. The use of indexes and constraints helps in faster retrieval and better management of data
within the databases.

Ques.4. What is the difference between DBMS and RDBMS?


Ans. The primary difference between DBMS and RDBMS is, in RDBMS we have relations between
the tables of the database.
Whereas in DBMS there is no relation between the tables (data may even be stored in files).

RDBMS has primary keys and data is stored in tables. DBMS has no concept of primary keys with
data stored in navigational or hierarchical form.

RDBMS defines integrity constraints in order to follow ACID properties. While DBMS doesn’t follow
ACID properties.

Ques.5. What is a table?


Ans. A table is a database object that is used for storing related records in the form of rows and
columns.

Ques.6. What is a field in a table?


Ans. A field is an entity used for storing a particular type of data within a table like numbers,
characters, dates, etc.

Ques.7. What is a tuple, record, or row in a table?


Ans. A tuple is a single record or single row in a table. For example, in the case of a student table,
the data corresponding to a student will be called a tuple or record of that student.

Ques.8. What is SQL?


Ans. SQL stands for Structured Query Language, It is a language used for creating, storing,
fetching, and updating data and database objects in RDBMS.

Ques.9. What are the different types of SQL commands?


Ans. SQL commands are the set of commands used to communicate and manage the data present
in the database. The different type of SQL commands are-
1. DDL – Data Definition Language
2. DML – Data Manipulation Language
3. DCL – Data Control Language
4. TCL – Transactional Control Language

Ques.10. Explain DDL commands. What are the different DDL commands in SQL?
Ans. DDL refers to Data Definition Language. The DDL commands are used to define or alter the
structure of the database. The different DDL commands are-
 CREATE – Used to create a table in the DB
 DROP – Drops the table from the DB
 ALTER – Alters the structure of the DB
 TRUNCATE – Deletes all the records from the DB but not its database structure
 RENAME – Renames a DB object

Ques.11. Explain DML commands. What are the different DML commands in SQL?
Ans. DML refers to Data Manipulation Language. These commands are used for managing data
present in the database. Some of the DML commands are – select, insert, update, delete, etc.

Ques.12. Explain DCL commands. What are the different DCL commands in SQL?
Ans. DCL refers to Data Control Language. These commands are used to create roles, grant
permission, and control access to the database objects. The three DCL commands are-
 GRANT – Grants permission to a database user.
 REVOKE – Removes access privileges from a user-provided with the GRANT command.
 Deny – Explicitly prevents a user from receiving particular permission(e.g. preventing a
particular user belonging to a group to receive the access controls.

Ques.13. Explain TCL commands. What are the different TCL commands in SQL?
Ans. TCL refers to Transaction Control Language. These commands are used to manage the
changes made by DML statements. These are used to process a group of SQL statements
comprising a logical unit. The three TCL commands are-
 COMMIT – Commit write the changes to the database
 SAVEPOINT – Savepoints are the breakpoints, these divide the transaction into smaller logical
units which could be further roll-backed.
 ROLLBACK – Rollbacks are used to restore the database since the last commit.

Ques.14. What are SQL constraints?


Ans. SQL constraints are the set of rules that impose some restrictions while inserting, deleting, or
updating the data in the databases. In SQL, we have both column level as well as table level
constraints which are applied at columns and tables respectively. Some of the constraints in SQL
are – Primary Key, Foreign Key, Unique Key, Not NULL, DEFAULT, CHECK, and Index constraint.

Ques.15. What is a Unique constraint?


Ans. A unique constraint is used to ensure that a field or column will have only a unique value (no
duplication).

Ques.16. What is a Primary Key?


Ans. A primary key is a column or a combination of columns that uniquely identifies a record in the
database. A primary key can only have unique and not NULL values and there can be only one
primary key in a table.

Ques.17. What is the difference between a unique key and a primary key?
Ans. A unique key allows null value (although only one) but a primary key doesn’t allow null values.
A table can have more than one unique keys columns while there can be only one primary key. A
unique key column creates a non-clustered index whereas the primary key creates a clustered index
on the column.

Ques.18. What is a composite key?


Ans. A composite key is a primary key with multiple columns as in the case of some tables a single
field might not guarantee unique and not null values. So a combination of multiple fields is taken as
the primary key.
Ques.19. What is a NULL value?
Ans. A NULL value in SQL is an unknown or blank value. Since NULL is an unknown value so the
NULL value cannot be compared with another NULL value. Hence we cannot use the ‘=’ operator in
where condition with NULL. For this, we have an IS NULL clause that checks if the value in the field
is NULL or not.

Ques.20. What is a Not NULL constraint?


Ans. A Not NULL constraint is used for ensuring that the value in the field cannot be NULL.

Ques.21. What is a Foreign Key?


Ans. A foreign key is used for enforcing referential integrity in which a field marked as a foreign key
in one table is linked with a primary key of another table. With this referential integrity, we can have
only the data in the foreign key which matches the data in the primary key of the other table.

Ques.22. What is a Check constraint?


Ans. A check constraint is used to limit the value entered in a field. For example, we can ensure that
field ‘Salary’ can only have a value greater than 1000 using check constraint.
CREATE TABLE EMP_SALARY(

EmpID int NOT NULL,

NAME VARCHAR (30) NOT NULL,

Salary INT CHECK (Salary > 1000),

PRIMARY KEY (EmpID)

);

Ques.23. What is a Default constraint?


Ans. A Default constraint is used for providing a default value to a column when no value is supplied
at the time of insertion of record in the database.

Ques.24. What is a clustered index?


Ans. A clustered index defines the order in which data is physically stored in the database tables. A
table can have only one clustered index.

Ques.25. What is a non-clustered index?


Ans. In the case of a non-clustered index, the data and the indices are stored in different locations.
A non-clustered index has a jump table containing key-values with pointers pointing to the location
containing the actual rows. There can be multiple clustered indexes in a table.

Ques.26. What is the difference between delete, truncate and drop command?
Ans. The difference between the Delete, Truncate and Drop command is –
 Delete command is a DML command. It removes rows from a table based on the condition
specified in the where clause, being a DML statement we can rollback changes made by the
delete command.
 Truncate is a DDL command. It removes all the rows from the table and also frees the space
held, unlike the delete command. It takes a lock on the table while the delete command takes
a lock on rows of the table.
 Drop is a DDL command. It removes the complete data along with the table structure (unlike
the truncate command that removes only the rows).

Ques.27. What are the different types of joins in SQL?


Ans. Joins are used to combine records from multiple tables. The different types of joins in SQL are-
1. Inner Join – To fetch rows from two tables having matching data in the specified columns of
both the tables.
SELECT * FROM TABLE1 INNER JOIN TABLE2 ON TABLE1.columnA = TABLE2.columnA;

2. Left Join – To fetch all rows from the left table and matching rows of the right table
SELECT * FROM TABLE1 LEFT JOIN TABLE2 ON TABLE1.columnA = TABLE2.columnA;

3. Right Join – To fetch all rows from right table and matching rows of the left table
SELECT * FROM TABLE1 RIGHT JOIN TABLE2 ON TABLE1.columnA = TABLE2.columnA;

4. Full Outer Join – To fetch all rows of the left table and all rows of right table
SELECT * FROM TABLE1 FULL OUTER JOIN TABLE2 ON TABLE1.columnA =
TABLE2.columnA;

5. Self Join – Joining a table to itself, for referencing its own data
SELECT * FROM TABLE1 T1, TABLE1 T2 WHERE T1.columnA = T2.columnB;

Ques.28. What is the difference between cross join and full outer join?
Ans. A cross join returns the cartesian product of the two tables. So there is no condition or on
clause as each row of TabelA is joined with each row of TableB whereas a full outer join will join the
two tables on the basis of the condition specified in the on clause and for the records not satisfying
the condition null value is placed in the join result.

Ques.29. What is the difference between where and having clause?


Ans. This is one of the commonly asked DBMS interview questions in which the interviewer wants to
check your knowledge of the ‘where’ and ‘having’ clause along with their usage.

A ‘where’ clause is used to fetch data from the database that specifies particular criteria (specified
after the where clause). Whereas a ‘having’ clause is used along with ‘GROUP BY’ to fetch data that
meets particular criteria specified by the aggregate function.
For example – for a table with Employee and Project fields. If we want to fetch Employee working on
a particular project P2, we will use ‘where’ clause-
Select Employee

From Emp_Project

where Project = P2;

Now if we want to fetch Employees who are working on more than one project. We will first have to
group the Employee column along with the count of the project and then the ‘having’ clause can be
used to fetch relevant records-
Select Employee

From Emp_Project

GROUP BY Employee
Having count(Project)>1;

Ques.30. What is the difference between Union and Union All command?
Ans. The fundamental difference between Union and Union All command is – Union is by default
distinct i.e. it combines the distinct result set of two or more select statements. Whereas, Union
All combines all the rows including duplicates in the result set of different select statements.

Ques.31. Define the ‘Select into’ statement.


Ans. Select into statement is used to directly select data from one table and insert it into another.
The new table gets created with the same name and type as of the old table-
SELECT * INTO newtable FROM oldTable;

Ques.32. What is a View in SQL?


Ans. A view is a virtual table. It is a named set of SQL statements that can be later referenced and
used as a table.
CREATE VIEW VIEW_NAME AS

SELECT COLUMN1, COLUMN2

FROM TABLE_NAME WHERE CONDITION;

Ques.33. Can we use ‘where’ clause with ‘GROUP BY‘?


Ans. Yes, we can use ‘where’ clause with ‘GROUP BY’. The rows that don’t meet the where
conditions are removed first and then the grouping is done based on the GROUP BY column.
SELECT Employee, Count(Project )

FROM Emp_Project

WHERE Employee != 'A'

GROUP BY Project;

Ques.34. What is Database Normalisation?


Ans. Database normalization is the process of organization of data in order to reduce the
redundancy and anomalies in the database. We have different Normalisation forms in SQL like –
First Normal Form, Second Normal Form, Third Normal Form, and BCNF.

Ques.35. Explain the First Normal Form(1NF).


Ans. According to First Normal Form, a column cannot have multiple values. Each value in the
columns must be atomic.
Ques.36. Explain the Second Normal Form(2NF).
Ans. For a table to be considered in Second Normal Form. It must follow 1NF and no column should
be dependent on the primary key.

Ques.37. Explain the Third Normal Form(3NF).


Ans. For a table to be Third Normal Form, it must follow 2NF and each non-prime attribute must be
dependent on the primary key of the table.

For each functional dependency X -> Y either-


X should be the super key or Y should be the prime attribute (part of one of the candidate keys) of
the table.

Ques.38. Explain Boyce and Codd Normal Form(BCNF).


Ans. BCNF is the advanced or stricter version of 3NF.
For each functional dependency X -> Y, X should be a super key.

Ques.39. What are transactions in SQL?


Ans. Transactions are a set of operations performed in a logical sequence. It is executed as a
whole. If any statement in the transaction fails, the whole transaction is marked as failed and not
committed to the database.

Ques.40. What are the ACID properties?


Ans. This is one of the frequently asked database interview questions. ACID properties refer to the
four properties of transactions in SQL-
1. Atomicity – All the operations in the transaction are performed as a whole or not performed at
all.
2. Consistency – The state of the database changes only on successfully committed
transactions.
3. Isolation – Even with concurrent execution of multiple transactions. The final state of the DB
would be the same as if transactions got executed sequentially. In other words, each
transaction is isolated from one another.
4. Durability – Even in the state of the crash or power loss the state of the committed transaction
remains persistent.
Ques.41. What are locks in SQL?
Ans. Locks in SQL are used for maintaining database integrity in case of concurrent access to the
same piece of data.

Ques.42. What are the different types of locks in the database?


Ans. The different types of locks in the database are-
1. Shared locks – Allows data to be read-only(Select operations), and prevents the data to be
updated when in the shared lock.
2. Update locks – Applied to resources that can be updated. There can be only one update lock
on data at a time.
3. Exclusive locks – Used to lock data being modified (INSERT, UPDATE, or DELETE) by one
transaction. Thus ensuring that multiple updates cannot be made to the same resource at the
same time.
4. Intent locks – A notification mechanism using which a transaction conveys that intends to
acquire a lock on data.
5. Schema locks – Used for operations when the schema or structure of the DB is required to be
updated.
6. Bulk Update locks – Used in case of bulk operations when the TABLOCK hint is used.

Ques.43. What are the aggregate functions in SQL?


Ans. Aggregate functions are SQL functions that return a single value calculated from multiple
values of columns. Some of the aggregate functions in SQL are-
 Count() – Returns the count of the number of rows returned by the SQL expression
 Max() – Returns the max value out of the total values
 Min() – Returns the min value out of the total values
 Avg() – Returns the average of the total values
 Sum() – Returns the sum of the values returned by the SQL expression

Ques.44. What are scalar functions in SQL?


Ans. Scalar functions are the functions that return a single value by processing a single value in
SQL. Some of the widely used SQL functions are-
 UCASE() – used to convert a string to upper case
 LCASE() – used to convert a string to lowercase
 ROUND() – used to round a number to the decimal places specified
 NOW() – used to fetch the current system date and time
 LEN() – used to find the length of a string
 SUBSTRING() or MID() – MID and SUBSTRING are synonyms in SQL. They are used to
extract a substring from a string by specifying the start and end index. Syntax –
SUBSTRING(ColumnName, startIndex, EndIndex).
 LOCATE() – used to find the index of the character in a string. Syntax –
LOCATE(character,ColumnName)
 LTRIM() – used to trim spaces from the left
 RTRIM() – used to trim spaces from the right

Ques.45. What is a coalesce function?


Ans. Coalesce function is used to return the first, not NULL value out of the different values or
expressions passed to the coalesce function as parameters. Example-
COALESCE(NULL, NULL, 5, ‘ArtOfTesting’) will return the value 5.
COALESCE(NULL, NULL, NULL) will return NULL value as no not NULL value is encountered in
the parameters list.
Ques.46. What are cursors in SQL?
Ans. Cursors are objects in SQL that are used to traverse the result set of a SQL query one by one.

Ques.47. What are the stored procedures? Explain their advantages.


Ans. Stored procedures are SQL procedures (a bunch of SQL statements) that are stored in the
database and can be called by other procedures, triggers, and other applications.
CREATE PROCEDURE

procedureName

AS

Begin

Set of SQL statements

End

The advantages of stored procedure are-


1. Stored procedures improve performance as the procedures are pre-compiled as well as
cached.
2. Make queries easily maintainable and reusable as any change is required to be made at a
single location.
3. Reduce network usage and traffic.
4. Improve security as stored procedures restrict direct access to the database.

Ques.48. What are the triggers in SQL?


Ans. Triggers are special types of stored procedures that get executed when a specified event
occurs. Syntax-
CREATE TRIGGER

triggerName

triggerTime{Before or After}

triggerEvent{Insert, Update or Delete}

ON tableName

FOR EACH ROW

triggerBody

Ques.49. What are orphan records?


Ans. Orphan records are records having a foreign key to a parent record that doesn’t exist or got
deleted.
Ques.50. How can we remove orphan records from a table?
Ans. In order to remove orphan records from the database. We need to create a join on the parent
and child tables and then remove the rows from the child table where id IS NULL.
DELETE PT

FROM ParentTable PT

LEFT JOIN ChildTable CT

ON PT.ID = CT.ID

WHERE PT.ID IS NULL

*Remember: Delete with joins requires name/alias before from clause in order to specify the table of
which data is to be deleted.

Data Warehouse Interview Questions for Freshers


1. What do you mean by data mining? Differentiate between data mining and data warehousing.

Data mining is the process of collecting information in order to find patterns, trends, and usable
data that will help a company to make data-driven decisions from large amounts of data. In other
words, Data Mining is the method of analysing hidden patterns of data from various perspectives
for categorization into useful data, which is gathered and assembled in specific areas such as data
warehouses, efficient analysis, data mining algorithm, assisting decision making, and other data
requirements, ultimately resulting in cost-cutting and revenue generation. Data mining is the
process of automatically examining enormous amounts of data for patterns and trends that go
beyond simple analysis. Data mining estimates the probability of future events by utilising
advanced mathematical algorithms for data segments.

Following are the differences between data warehousing and data mining:-

Data Warehousing Data Mining

A data warehouse is a database system that is intended for The technique of examining data patterns is
analytical rather than transactional purposes.  known as data mining.

In data mining, data is evaluated on a regular


In data warehousing, data is saved on a regular basis.
basis.

With the assistance of technologists,


Engineers are the only ones that do data warehousing.
business users conduct data mining.

Data warehousing is the process of bringing all relevant data Data mining is the process of extracting
together. information from big datasets.

Data Mining can be referred to as a super set


Data warehousing can be referred to as a subset of data mining.
of data warehousing.

2. What do you mean by OLAP in the context of data warehousing? What guidelines should be followed
while selecting an OLAP system?

OLAP is an acronym for On-Line Analytical Processing. OLAP is a software technology


classification that allows analysts, managers, and executives to get insight into information through
quick, reliable, interactive access to data that has been converted from raw data to reflect the true
dimensionality of the company as perceived by the clients. OLAP allows for multidimensional
examination of corporate data while also allowing for complex estimations, trend analysis, and
advanced data modelling. It's rapidly improving the foundation for Intelligent Solutions, which
includes Business Performance Management, Strategy, Budgeting, Predicting, Financial
Documentation, Analysis, Modeling, Knowledge Discovery, and Data Warehouses Reporting. End-
clients can use OLAP to perform ad hoc record analysis in several dimensions, giving them the
information and understanding they need to make better choices.

Following guidelines must be followed while selecting an OLAP system:-

 Multidimensional Conceptual View: This is one of an OLAP system's most important capabilities. It is


feasible to use methods like slice and dice that require a multidimensional view.
 Transparency: Make the technology, the underlying data repository, computing operations, and the
disparate nature of source data completely accessible to consumers. Users' efficiency and
productivity are improved as a result of this transparency.
 Accessibility: OLAP systems must only allow access to the data that is truly needed to do the
analysis, giving clients a single, coherent, and consistent picture. The OLAP system must map its own
logical schema to the disparate physical data storage, as well as to conduct any required
transformations. 
 Consistent Reporting Performance: As the number of dimensions or the size of the database grows,
users should not experience any substantial reduction in documenting performance. That is, as the
number of dimensions grows, OLAP performance should not deteriorate.
 Client/Server Architecture: Make the OLAP tool's server component clever enough that the various
clients can be connected with minimal effort and integration code. The server should be able to map
and consolidate data from disparate databases.
 Generic Dimensionality: Each dimension in an OLAP method should be seen as equal in terms of
structure and operational capabilities. Select dimensions may be granted additional operational
capabilities, although such duties should be available to all dimensions.
 Dynamic Sparse Matrix Handling: To optimise sparse matrix handling by adapting the physical
schema to the unique analytical model being built and loaded. When confronted with a sparse
matrix, the system must be able to dynamically assume the information distribution and change
storage and access in order to achieve and maintain a constant level of performance.
 Multiuser Support: OLAP technologies must allow several users to access data at the same time
while maintaining data integrity and security.
 Unrestricted cross-dimensional Operations: It gives techniques the ability to determine dimensional
order and to perform roll-up and drill-down operations within and across dimensions.
 Intuitive Data Manipulation: Reorientation (pivoting), drill-down and roll-up, and other manipulations
can be done intuitively and precisely on the cells of the scientific model using point-and-click and
drag-and-drop methods. It does away with the need for a menu or several visits to the user interface.
 Flexible Reporting: It provides efficiency to corporate clients by allowing them to organize columns,
rows, and cells in a way that allows for easy data manipulation, analysis, and synthesis.
 Infinite Dimensions and Aggregation Levels: There should be no limit to the number of data
dimensions. Within any given consolidation path, each of these common dimensions must allow for
an almost infinite number of customer-defined aggregation levels.

3. What do you understand about a fact table in the context of a data warehouse? What are the different
types of fact tables?

 In a Data Warehouse system, a Fact table is simply a table that holds all of the facts or business
information that can be exposed to reporting and analysis when needed. Fields that reflect direct
facts, as well as foreign fields that connect the fact table to other dimension tables in the Data
Warehouse system, are stored in these tables. Depending on the model type used to construct the
Data Warehouse, a Data Warehouse system can have one or more fact tables.

Following are the three types of fact tables:-


 Transactional Fact Table: This is a very basic and fundamental view of corporate processes. It can be
used to depict the occurrence of an event at any given time. The facts measure are only valid at that
specific time and for that specific incident. "One row per line in a transaction," according to the grain
associated with the transaction table. It typically comprises data at the detailed level, resulting in a
huge number of dimensions linked with it. It captures the smallest or atomic level of dimension
measurement. This allows the table to provide users with extensive dimensional grouping, roll-up,
and drill-down reporting features. It's packed yet sparse at the same time. It can also be big at the
same time, depending on the number of events (transactions) that have occurred.
 Snapshot Fact Table: The snapshot depicts the condition of things at a specific point in time,
sometimes known as a "picture of the moment." It usually contains a greater number of non-additive
and semi-additive information. It aids in the examination of the company's overall performance at
regular and predictable times. Unlike the transaction fact table, which adds a new row for each
occurrence of an event, this represents the performance of an activity at the end of each day, week,
month, or any other time interval. However, to retrieve the detailed data in the transaction fact table,
snapshot fact tables or periodic snapshots rely on the transaction fact table. The periodic snapshot
tables are typically large and take up a lot of space.
 Accumulating Fact Table: These are used to depict the activity of any process with a well-defined
beginning and end. Multiple data stamps are commonly found in accumulating snapshots, which
reflect the predictable stages or events that occur over the course of a lifespan. There is sometimes
an extra column with the date that indicates when the row was last updated.

You can download a PDF version of Data Warehouse Interview Questions.


Download PDF
4. What do you mean by dimension table in the context of data warehousing? What are the advantages of
using a dimension table?

A table in a data warehouse's star schema is referred to as a dimension table. Dimensional data
models, which are made up of fact and dimension tables, are used to create data warehouses.
Dimension tables contain dimension keys, values, and attributes and are used to describe
dimensions. It is usually of a tiny size. The number of rows might range from a few to thousands. It
is a description of the objects in the fact table. The term "dimension table" refers to a collection or
group of data pertaining to any quantifiable occurrence. They serve as the foundation for
dimensional modelling. It includes a column that serves as a primary key, allowing each dimension
row or record to be uniquely identified. Through this key, it is linked to the fact tables. When it's
constructed, a system-generated key called the surrogate key is used to uniquely identify the rows
in the dimension.

Following are the advantages of using a dimension table :

 It features a straightforward design.


 It is simple to study and comprehend.
 It stores data that has been de-normalized.
 It aids in the preservation of historical data for any dimension.
 It's simple to get info from it.
 It's simple to build and put into action.
 It provides the context for any business operation.

5. What are the different types of dimension tables in the context of data warehousing?

Following are the different types of dimension tables in the context of data warehousing:-
 

 Slowly Changing Dimensions (SCD): Slowly changing dimensions are dimension attributes that tend
to vary slowly over time rather than at a regular period of time. For example, the address and phone
number may change, but not on a regular basis. Consider the case of a man who travels to several
nations and must change his address according to the place he is visiting. This can be accomplished in
one of three ways:
o Type 1: Replaces the value that was previously entered. This strategy is simple to implement
and aids in the reduction of costs by saving space. However, in this circumstance, history is
lost.
o Type 2: Insert a new row containing the new value. This method saves the history and allows
it to be accessed at any time. However, it takes up a lot of space, which raises the price.
o Type 3: Add a new column to the table. It is the ideal strategy because history can be easily
preserved.
 Junk Dimension: A trash dimension is a collection of low-cardinality attributes. It contains a number
of varied or disparate features that are unrelated to one another. These can be used to implement
RCD (rapidly changing dimension) features like flags and weights, among other things.
 Conformed Dimension: Multiple subject areas or data marts share this dimension. It can be utilised in
a variety of projects without requiring any changes. This is used to keep things in order. Dimensions
that are exactly the same as or a proper subset of any other dimension are known as conformed
dimensions.
 Roleplay Dimension: Role-play dimension refers to the dimension table that has many relationships
with the fact table. In other words, it occurs when the same dimension key and all of its associated
attributes are linked to a large number of foreign keys in the fact table. Within the same database, it
might serve several roles.
 Degenerate Dimension: Degenerate dimension attributes are those that are contained in the fact
table itself rather than in a separate dimension table. For instance, a ticket number, an invoice
number, a transaction number, and so on.

6. Differentiate between fact table and dimension table.

The record of a reality or fact table could be made up of attributes from various dimension tables.
The Fact Table, also known as the Reality Table, assists the user in investigating the business
aspects that aid him in call taking in order to improve his firm. Dimension Tables, on the other
hand, make it easier for the reality table or fact table to collect dimensions from which
measurements must be taken.

The following table enlists the difference between a fact table and a dimension table:-
Fact Table  Dimension Table 

It contains the attributes' measurements, facts, or It is the companion table that has the attributes that the
metrics. fact table uses to derive the facts.

Data grain (the most atomic level by which facts


It is detailed, comprehensive, and lengthy.
may be defined) is what defines it.

It is used for analysis and decision-making and It contains information regarding a company's operations
contains measures. and procedures.

It contains information in both numeric and textual


It only contains textual information.
formats.

It has a primary key that works as a foreign key in It has a foreign key that is linked to the fact table's
the dimension table. primary key.

It stores the filter domain and reports labels in


It organizes the atomic data into dimensional structures.
dimension tables.

It does not have a hierarchy. It has a hierarchy. 

It has lesser attributes than a dimension table. It has more attributes than a fact table.

It has more records as compared to a dimension


It has fewer records than a fact table.
table.

Here, the table grows vertically. Here, the table grows horizontally.

It is created after the corresponding dimension table


It is created prior to the creation of the fact table.
has been created.
7. What are the advantages of a data warehouse?

Following are the advantages of using a data warehouse:

 Helps you save time:


o To stay ahead of your competitors in today's fast-paced world of cutthroat competition, your
company's ability to make smart judgments quickly is critical.
o A Data warehouse gives you instant access to all of your essential data, so you and your staff
don't have to worry about missing a deadline. All you have to do now is deploy your data
model to start collecting data in a matter of seconds. You can do this with most warehousing
solutions without utilising a sophisticated query or machine learning.
o With data warehousing, your company won't have to rely on a technical professional to
troubleshoot data retrieval issues 24 hours a day, seven days a week. You will save a lot of
time this way.
 Enhances the quality of data:
o The high-quality data ensures that your company's policies are founded on accurate
information about your operations.
o You can turn data from numerous sources into a shared structure using data warehousing. You
can assure the consistency and integrity of your company's data this way. This allows you to
spot and eliminate duplicate data, inaccurately reported data and disinformation.
o For your firm, implementing a data quality management program may be both costly and time-
consuming. You can easily use a data warehouse to reduce the number of these annoyances
while saving money and increasing the general productivity of your company.
 Enhances Business Intelligence (BI):
o Throughout your commercial endeavours, you can use a data warehouse to gather, absorb,
and derive data from any source. As a result of the capacity to easily consolidate data from
several sources, your BI will improve by leaps and bounds.
 Data standardization and Consistency are achieved:
o The uniformity of huge data is another key benefit of having central data repositories. In a
similar manner, a data storage or data mart might benefit your company. Because data
warehousing stores data from various sources in a consistent manner, such as a transactional
system, each source will produce results that are synchronized with other sources. This
ensures that data is of higher quality and homogeneous. As a result, you and your team can
rest assured that your data is accurate, resulting in more informed corporate decisions.
 Enhances Data Security:
o A data warehouse improves security by incorporating cutting-edge security features into its
design. For any business, consumer data is a vital resource. You can keep all of your data
sources integrated and properly protected by adopting a warehousing solution. The risk of a
data breach will be greatly reduced as a result of this.
 Ability to store historical data:
o Because a data warehouse can hold enormous amounts of historical data from operational
systems, you can readily study different time periods and inclinations that could be game-
changing for your business. You can make better corporate judgments about your business
plans if you have the correct facts in your hands.

8. What are the disadvantages of using a data warehouse?

Following are the disadvantages of using a data warehouse:-

 Loading time of data resources is undervalued: We frequently underestimate the time it will take to
gather, sanitize, and post data to the warehouse. Although some resources are in place to minimize
the time and effort spent on the process, it may require a significant amount of the overall
production time.
 Source system flaws that go unnoticed: After years of non-discovery, hidden flaws linked with the
source networks that provide the data warehouse may be discovered. Some fields, for example, may
accept nulls when entering new property information, resulting in workers inputting incomplete
property data, even if it was available and relevant.
 Homogenization of data: Data warehousing also deals with data formats that are comparable across
diverse data sources. It's possible that some important data will be lost as a result.

9. What are the different types of data warehouse?

Following are the different types of data warehouse:

 Enterprise Data Warehouse:


o An enterprise database is a database that brings together the various functional areas of an
organisation in a cohesive manner. It's a centralised location where all corporate data from
various sources and apps can be accessed. They can be utilised for analytics and by everyone
in the organisation once they've been saved. The data can be categorised by subject, and
access is granted according to the necessary division. The tasks of extracting, converting, and
conforming are taken care of in an Enterprise Datawarehouse.
o Enterprise Datawarehouse's purpose is to provide a comprehensive overview of any object in
the data model. This is performed by finding and wrangling the data from different systems.
This is then loaded into a model that is consistent and conformed. The data is acquired by
Enterprise Datawarehouse, which can provide access to a single site where various tools can
be used to execute analytical functions and generate various predictions. New trends or
patterns can be identified by research teams, which can then be focused on to help the
company expand.
 Operational Data Store (ODS):
o An operational data store is utilised instead of having an operational decision support system
application. It facilitates data access directly from the database, as well as transaction
processing. By checking the associated business rules, the data in the Operational Data Store
may be cleansed, and any redundancy found can be checked and rectified. It also aids in the
integration of disparate data from many sources so that business activities, analysis, and
reporting may be carried out quickly and effectively while the process is still ongoing. 
o The majority of current operations are stored here before being migrated to the data
warehouse for a longer period of time. It is particularly useful for simple searches and little
amounts of data. It functions as short-term or temporary memory, storing recent data. The
data warehouse keeps data for a long time and also keeps information that is generally
permanent.
 Data Mart:
o Data Mart is referred to as a pattern to get client data in a data warehouse environment. It's a
data warehouse-specific structure that's employed by the team's business domain. Every
company has its own data mart, which is kept in the data warehouse repository. Dependent,
independent, and hybrid data marts are the three types of data marts. Independent data
marts collect data from external sources and data warehouses, whereas dependent data marts
take data that has already been developed. Data marts can be thought of as logical subsets of
a data warehouse.

10. What are the different types of data marts in the context of data warehousing?

Following are the different types of data mart in data warehousing:

 Dependent Data Mart: A dependent data mart can be developed using data from operational,
external, or both sources. It enables the data of the source company to be accessed from a single
data warehouse. All data is centralized, which can aid in the development of further data marts.
 Independent Data Mart: There is no need for a central data warehouse with this data mart. This is
typically established for smaller groups that exist within a company. It has no connection to
Enterprise Data Warehouse or any other data warehouse. Each piece of information is self-contained
and can be used independently. The analysis can also be carried out independently. It's critical to
maintain a consistent and centralized data repository that numerous users can access.
 Hybrid Data Mart: A hybrid data mart is utilized when a data warehouse contains inputs from
multiple sources, as the name implies. When a user requires an ad hoc integration, this feature comes
in handy. This solution can be utilized if an organization requires various database environments and
quick implementation. It necessitates the least amount of data purification, and the data mart may
accommodate huge storage structures. When smaller data-centric applications are employed, a data
mart is most effective.

11. Differentiate between data warehouse and database.

Database: A database is a logically organized collection of structured data kept electronically in a


computer system. A database management system is usually in charge of a database (DBMS). The
data, the DBMS, and the applications that go with them are referred to as a database system,
which is commonly abbreviated to just a database.

The following table enlists the difference between data warehouse and database:-

Data Warehouse Database

Database uses the OnLine


Data Warehouse uses the OnLine Analytical Processing (OLAP).
Transactional Processing (OLTP).

Data Warehouse is mainly used for analyzing the historical data so as The database aids in the execution of
to make future decisions based on them. basic business procedures.

A database's tables and joins are


Because a data warehouse is denormalized, tables and joins are
complicated because they are
straightforward.
normalised.

It can be referred to as an application-


It can be referred to as a subject-oriented collection of data.
oriented collection of data.

In this, Entity-Relationship (ER)


In this, data modelling techniques are used for designing. modelling techniques are used for
designing.

Data may not be up to date in this. Data is generally up to date in this.


Data Warehouse Database

The data structure of Data Warehouse is based on a dimensional and


For data storing, the Flat Relational
normalised approach. For example, a star and snowflake schema is
Approach approach is employed.
employed.

Generally, detailed data is stored in a


Generally, highly summarized data is stored in a data warehouse.
database.

12. What do you mean by a factless fact table in the context of data warehousing?

A fact table with no measures is known as a factless fact table. It's essentially a crossroads of
dimensions (it contains nothing but dimensional keys). One form of factless table is used to capture
an event, while the other is used to describe conditions.

In the first type of factless fact table, there is no measured value for an event, but it develops the
relationship among the dimension members from several dimensions. The existence of the
relationship is itself the fact. This type of fact table can be utilised to create valuable reports on its
own. Various criteria can be used to count the number of occurrences.

The second type of factless fact table is a tool that's used to back up negative analytical reports.
Consider a store that did not sell a product for a period of time. To create such a report, you'll need
a factless fact table that captures all of the conceivable product combinations that were on offer.
By comparing the factless table to the sales table for the list of things that did sell, you can figure
out what's missing.

13. What do you mean by Real time data warehousing?

A system that reflects the condition of the warehouse in real time is referred to as real-time data
warehousing. If you perform a query on the real-time data warehouse to learn more about a
specific aspect of the company or entity described by the warehouse, the result reflects the status
of that entity at the time the query was run. Most data warehouses contain data that is highly
latent — that is, data that reflects the business at a specific point in time. A real-time data
warehouse provides current (or real-time) data with low latency.

14. What do you mean by Active Data Warehousing?

The technical capacity to collect transactions as they change and integrate them into the
warehouse, as well as maintaining batch or planned cycle refreshes, is known as active data
warehousing. Automating routine processes and choices is possible with an active data warehouse.
The active data warehouse sends decisions to the On-Line Transaction Processing (OLTP) systems
automatically. An active data warehouse is designed to capture and distribute data in real time.
They give you a unified view of your customers across all of your business lines. Business
Intelligence Systems are linked to it.

15. What are the characteristics of a data warehouse?

 Following are the characteristics of a data warehouse:-


 Subject-oriented : Because it distributes information about a theme rather than an organization's
actual operations, a data warehouse is always subject-oriented. It is possible to do so with a certain
theme. That is to say, the data warehousing procedure is intended to deal with a more defined theme.
These themes could include sales, distribution, and marketing, for example. The focus of a data
warehouse is never solely on present activities. Instead, it concentrates on demonstrating and
analyzing evidence in order to reach diverse conclusions. It also provides a simple and precise
demonstration around a specific theme by removing info that isn't needed to make conclusions.
 Integrated : It is similar to subject orientation in that it is created in a dependable format. Integration
entails the creation of a single entity to scale all related data from several databases. The data has to
be stored in several data warehouses in a shared and widely accessible manner. A data warehouse is
created by combining information from a variety of sources, such as a mainframe and a relational
database. It must also have dependable naming conventions, formats, and codes. The utilization of a
data warehouse allows for more effective data analysis. The consistency of name conventions,
column scaling, and encoding structure, among other things, should be validated. The data warehouse
integration handles a variety of subject-related warehouses.
 Time-Variant : Data is kept in this system at various time intervals, such as weekly, monthly, or
annually. It discovers a number of time limits that are structured between massive datasets and held
in the online transaction process (OLTP). Data warehouse time limitations are more flexible than
those of operational systems. The data in the data warehouse is predictable over a set period of time
and provides information from a historical standpoint. It contains explicit or implicit time elements.
Another property of time-variance is that data cannot be edited, altered, or updated once it has been
placed in the data warehouse.
 Non-volatile : The data in a data warehouse is permanent, as the name implies. It also means that
when new data is put, it is not erased or removed. It incorporates a massive amount of data that is
placed into logical business alteration between the designated quantity. It assesses the analysis in the
context of warehousing technologies. Data is read-only and refreshed at scheduled intervals. This is
useful for analyzing historical data and understanding how things work. It is not required to have a
transaction process, a recapture mechanism, or a concurrency control mechanism. In a data
warehouse environment, operations like delete, update, and insert that are performed in an
operational application are lost.
16. What do you understand about metadata and why is it used for?

Metadata is defined as information about data. Metadata is the context that provides data a more
complete identity and serves as the foundation for its interactions with other data. It can also be a
useful tool for saving time, staying organised, and getting the most out of the files you're working
with. Structural Metadata describes how an object should be classified in order to fit into a wider
system of things. Structural Metadata makes a link with other files that allows them to be
categorized and used in a variety of ways. Administrative Metadata contains information about an
object's history, who owned it previously, and what it can be used for. Rights, licences, and
permissions are examples. This information is useful for persons who are in charge of managing and
caring for an asset.

When a piece of information is placed in the correct context, it takes on a whole new meaning.
Furthermore, better-organized Metadata will considerably reduce search time.

17. Enlist a few data warehouse solutions that are currently being used in the industry.

Some of the major data warehouse solutions currently being used in the industry are as follows :

 Snowflakes
 Oracle Exadata
 Apache Hadoop
 SAP BW4HANA
 Microfocus Vertica
 Teradata
 AWS Redshift
 GCP Big Query

18. Enlist some of the renowned ETL tools currently used in the industry.

Some of the renowned ETL tools currently used in the industry are as follows :

 Informatica
 Talend
 Pentaho
 Abnitio
 Oracle Data Integrator
 Xplenty
 Skyvia
 Microsoft – SQL Server Integrated Services (SSIS)

19. Explain what you mean by a star schema in the context of data warehousing.

Star schema is a sort of multidimensional model and is used in a data warehouse. The fact tables
and dimension tables are both contained in the star schema. There are fewer foreign-key joins in
this design. With fact and dimension tables, this schema forms a star.

20. What do you mean by snowflake schema in the context of data warehousing?

Snowflake Schema is a multidimensional model that is also used in data warehouses. The fact
tables, dimension tables, and sub dimension tables are all contained in the snowflake schema. With
fact tables, dimension tables, and sub-dimension tables, this schema forms a snowflake.
21. What do you understand about a data cube in the context of data warehousing?

A data cube is a multidimensional data model that stores optimized, summarized, or aggregated
data for quick and easy analysis using OLAP technologies. The precomputed data is stored in a data
cube, which makes online analytical processing easier. We all think of a cube as a three-
dimensional structure, however in data warehousing, an n-dimensional data cube can be
implemented. A data cube stores information in terms of dimensions and facts.
Data Cubes have two categories. They are as follows :

 Multidimensional Data Cube : Data is stored in multidimensional arrays, which allows for a
multidimensional view of the data. A multidimensional data cube aids in the storage of vast amounts
of information. A multidimensional data cube uses indexing to represent each dimension of the data
cube, making it easier to access, retrieve, and store data.
 Relational Data Cube : The relational data cube can be thought of as an "expanded version of
relational DBMS." Data is stored in relational tables, and each relational table represents a data
cube's dimension. The relational data cube uses SQL to produce aggregated data, although it is
slower than the multidimensional data cube in terms of performance. The relational data cube, on the
other hand, is scalable for data that grows over time.

Data Warehouse Questions for Experienced


22. Explain the architecture of a data warehouse.

A data warehouse is a single schema that organizes a heterogeneous collection of multiple data
sources. There are two techniques to building a data warehouse. They are as follows:

Top-Down Approach in Data Warehouse:

Following are the major components :

 External Sources - An external source is a location from which data is collected, regardless of the
data format. Structured, semi-structured, and unstructured data are all possibilities.
 Stage Area - Because the data gathered from external sources does not follow a specific format, it
must be validated before being loaded into the data warehouse. ETL tool is used for this purpose in
the stage area.
 Data-warehouse - After data has been cleansed, it is kept as a central repository in the data
warehouse. The meta data is saved here, while the real data is housed in data marts. In this top-down
approach, the data warehouse stores the data in its purest form.
 Data Marts - A data mart is a storage component as well. It maintains information about a single
organization's function that is managed by a single authority. Depending on the functions, an
organization can have as many data marts as it wants. 
 Data Mining - Data mining is the process of analyzing large amounts of data in a data warehouse.
With the use of a data mining algorithm, it is used to discover hidden patterns in databases and data
warehouses.

Bottom Up Approach in Data Warehouse:

Following are the steps involved in the bottom up approach:

 The data is first gathered from external sources (same as happens in top-down approach).
 The data is then imported into data marts rather than data warehouses after passing through the
staging area (as stated above). The data marts are built first, and they allow for reporting. It focuses
on a specific industry.
 After that, the data marts are incorporated into the data warehouse.

23. What are the advantages and disadvantages of the top down approach of data warehouse
architecture?

Following are the advantages of the top down approach :

 Because data marts are formed from data warehouses, they have a consistent dimensional
perspective.
 This methodology is also thought to be the most effective for corporate reforms. As a result, large
corporations choose to take this method.
 It is simple to create a data mart from a data warehouse.

The disadvantage of the top down approach is that the cost, time, and effort required to design
and maintain it are all very expensive.
24. What are the advantages and disadvantages of the bottom up approach of data warehouse
architecture?

Following are the advantages of the bottom up approach :

 The reports are generated quickly since the data marts are created first.
 We can fit a greater number of data marts here, allowing us to expand our data warehouse.
 In addition, the cost and effort required to build this model are quite minimal.

Because the dimensional view of data marts is not consistent as it is in the top-down approach, this
model is not as strong as the top-down approach and this is a disadvantage of the bottom up
approach.

25. Differentiate between a data warehouse and a data mart.

Following table enlists the difference between a data warehouse and a data mart:

Data Warehouse  Data Mart 

A data mart is a Data Warehouse's single


A data warehouse is a huge collection of data gathered from
subtype. It is created to fulfill the requirements
several departments or groups inside a company.
of a certain user group.

It aids in the making of tactical business


It aids in strategic decision-making.
decisions.

The process of designing a Data Warehouse is fairly


The Data Mart design procedure is simple.
challenging.

Data warehousing involves a big portion of the company, Because they can only handle tiny amounts of
which is why it takes so long to process. data, data marts are simple to use, create, and
Data Warehouse  Data Mart 

install.

The primary goal of a Data Warehouse is to create a unified


A data mart is primarily used at the department
environment and a consistent view of the business at any
level in a business division.
given moment in time. 

When opposed to data mart, the data kept in the Data Data Marts are designed for certain user groups.
Warehouse is always detailed.  As a result, the data is brief and limited.

Data is collected from a variety of sources in a data Data in Data Mart comes from a limited number
warehouse. of sources.

The Data Warehouse might be anywhere from 100 GB to 1


Data Mart is less than 100 GB in size.
TB+ in size.

The time it takes to implement a Data Warehouse might The Data Mart implementation process is only a
range from months to years. few months long.

From the perspective of the end-users, the data stored is The transaction data is provided straight from
read-only. the Data Warehouse.

26. What do you mean by data purging in the context of data warehousing?

Data purging is a term that describes techniques for permanently erasing and removing data from a
storage space. Data purging, which is typically contrasted with data deletion, involves a variety of
procedures and techniques. 

Purging removes data permanently and frees up memory or storage space for other purposes,
whereas deletion is commonly thought of as a temporary preference. Automatic data purging
features are one of the methods for data cleansing in database administration. Some Microsoft
products, for example, feature an automatic purge strategy that uses a circular buffer mechanism,
in which older data is purged to create room for fresh data. Administrators must manually remove
data from the database in other circumstances.

27. What do you mean by dimensional modelling in the context of data warehousing?

Dimensional Modelling (DM) is a data structure technique that is specifically designed for data
storage in a data warehouse. The goal of dimensional modelling is to optimise the database so that
data can be retrieved more quickly. In a data warehouse, a dimensional model is used to read,
summarise, and analyse numeric data such as values, balances, counts, weights, and so on. Relation
models, on the other hand, are designed for adding, modifying, and deleting data in a real-time
Online Transaction System.

Following are the steps that should be followed while creating a dimensional model:
 Identifying the business process : The first step is to identify the specific business processes that a
data warehouse should address. This might be Marketing, Sales, or Human Resources, depending on
the organization's data analytic needs. The quality of data available for that process is also a factor in
deciding which business process to use. It is the most crucial step in the Data Modeling process, and
a failure here would result in a cascade of irreversible flaws.
 Identifying the grain : The level of detail for the business problem/solution is described by the grain.
It's the procedure for determining the lowest level of data in any table in your data warehouse. If a
table contains sales data for each day, the granularity should be daily. Monthly granularity is defined
as a table that contains total sales data for each month.
 Identifying the dimension : Date, shop, inventory, and other nouns are examples of dimensions. All of
the data should be saved in these dimensions. The date dimension, for example, could include
information such as the year, month, and weekday.
 Identifying the fact : This stage is linked to the system's business users because it is here that they
gain access to data housed in the data warehouse. The majority of the rows in the fact table are
numerical values such as price or cost per unit.
 Building the schema : The Dimension Model is implemented in this step. The database structure is
referred to as a schema (arrangement of tables).

28. What do you understand by data lake in the context of data warehousing? Differentiate between data
lake and data warehouse.

A Data Lake is a large-scale storage repository for structured, semi-structured, and unstructured
data. It's a location where you can save any type of data in its original format, with no restrictions
on account size or file size. It provides a significant amount of data for improved analytical
performance and native integration.

A data lake is a huge container that looks a lot like a lake or a river. Similar to how a lake has
various tributaries, a data lake has structured data, unstructured data, machine-to-machine
communication, and logs flowing through in real-time.

The following table enlists the differences between data lake and data warehouse:
Data Lake  Data Warehouse 

All data is stored in the data lake, regardless of its Data extracted from transactional systems or data
source or structure. The data is stored in its consisting of quantitative measures and their properties
unprocessed state. When it is ready to be used, it is will be stored in a data warehouse. The information has
converted. been cleansed and changed.

Captures semi-structured and unstructured data in Captures structured data and organises it according to
their original form from source systems. defined standards for data warehouse purposes.

The data lake is appropriate for those that perform


Because it is highly structured, easy to use, and
in-depth analysis. Data scientists, for example,
understand, the data warehouse is perfect for
require advanced analytical techniques that include
operational users.
predictive modelling and statistical analysis.

The cost of storing data in big data technology is less Data warehouse storage is more expensive and time-
than that of storing data in a data warehouse. consuming.

The schema is usually developed after the data has


Schema is usually defined before data is saved. Work is
been stored. This provides a great level of flexibility
required at the start of the process, but performance,
and convenience of data collecting, but it
security, and integration are all advantages.
necessitates labour at the end of the process.

Users can access data in data lakes before it has


Pre-defined inquiries for pre-defined data kinds are
been transformed, cleansed, or structured. In
answered by data warehouses. As a result, any updates
comparison to a traditional data warehouse, it allows
to the data warehouse take longer.
consumers to get to their results faster.

29. Differentiate between star schema and snowflake schema in the context of data warehousing.

Following table enlists the difference between the star schema and the snowflake schema:
Star Schema  Snowflake Schema 

The fact tables and dimension tables are both The fact tables, dimension tables, and sub dimension tables are
contained in the star schema. all contained in the snowflake schema.

It is a top-down model. It is, however, a bottom-up model.

The star schema takes up more room. It takes up less space.

Snowflake schema has a higher query complexity than star


Star schema has a low query complexity.
schema.

It is really simple to comprehend. It is tough to comprehend.

It contains less foreign keys. It has a greater number of foreign keys.

It has a lot of redundancy in its data. It has a low level of data redundancy.

The execution of queries takes less time. The execution of queries takes longer than star schema.

Normalization is not employed in the star


Both normalisation and denormalization are used in this.
schema.

It has a pretty simple design. It has a complicated design.

30. Differentiate between Agglomerative hierarchical clustering and Divisive clustering.

Agglomerative hierarchical clustering : Flat clustering returns an unstructured set of clusters. On


the other hand, this structure is more informative. We don't have to define the number of clusters
in advance with this clustering procedure. Bottom-up algorithms start by treating each piece of
data as a singleton cluster, then agglomerate pairs of clusters until all of them are merged into a
single cluster that contains all of the data.

Divisive Clustering : This approach also eliminates the need to define the number of clusters ahead
of time. It necessitates a method for breaking a cluster that contains all of the data and then
recursively splitting clusters until all of the data has been split into singletons.

Following are the differences between the two :

 When compared to agglomerative clustering, divisive clustering is more complicated since we require
a flat clustering algorithm as a "subroutine" to split each cluster until each data has its own singleton
cluster.
 If we don't create a complete hierarchy all the way down to individual data leaves, divisive clustering
is more efficient.
 A divisive algorithm is also more precise. Without first examining the global distribution of data,
agglomerative clustering makes judgments based on local patterns or neighbour points. These early
decisions are irreversible. When generating top-level dividing decisions, divisive clustering takes into
account the global distribution of data.

31. What are the advantages of a cloud based data warehouse?

Following are the advantages of a cloud-based data warehouse:


 Total cost of ownership is low: The low cost of cloud data warehouses is one of the reasons they are
becoming more popular. On-premises data warehouses necessitate high-cost technology, lengthy
upgrades, ongoing maintenance, and outage management.
 Increased performance and speed: To keep up with the expanding number of data sources, cloud
data warehouses are crucial. Cloud data warehouses can easily and quickly integrate with additional
data sources as needed, and deploy the updated solution to production. Cloud data warehouses
significantly improve speed and performance, allowing IT to focus on more innovative projects.
 Enhanced Security: Cloud security engineers can create and iterate on precise data-protection
measures. Furthermore, cloud encryption technologies such as multi-factor authentication make data
transfer between regions and resources extremely safe.
 Improved Disaster Recovery: Physical assets are not required to prepare cloud data warehouses for
disasters. Instead, almost all cloud data warehouses offer asynchronous data duplication and execute
automatic snapshots and backups. This data is kept across multiple nodes, allowing duplicate data to
be accessed at any time without stopping present activity.

Conclusion:

In this article, we have covered the most frequently asked interview questions on data
warehousing. ETL tools are often required in a data warehouse and so one can expect interview
questions on ETL tools as well in a data warehouse interview. 
Top 45+ SSRS Interview Questions and Answers for 2023

Top 30 SSRS Interview Questions and Answers for Professionals

If you are looking for a career in the field of database development, then SSRS is good to learn. Most
organizations value SSRS as additional expertise along with SQL. In this article, we have collated a
list of the top SSRS interview questions that you can expect in an interview. Go through the following
questions to understand the kind of questions you need to be prepared for while giving an interview
for a job related to anything with data science, data engineering, or data analysis.

1. Explain what SSRS is.

SQL Server Reporting Services or SSRS is a server-based reporting platform that provides detailed
reporting functionality for various data sources. Reporting services comprise an entire set of tools
to manage, generate and deliver reports and APIs that enables developers to coordinate data and
report processes in a custom application.

2. What are the essential architecture components of SSRS?

Following are the important architecture components of SSRS:

 Report Designer

 Report Manager

 Report Server

 Report Server Database

 Browser types supported by reporting services

 Report Server and Command line utilities

 Data Sources
3. Explain the term data regions and mention the different data regions.

Data regions are nothing but report items that display recurrent rows of summarized data or
information from datasets. Different data regions include

 Matrix

 Gauge

 Chart

 List

 Table

4. Explain the various stages of Report Processing

The various stages of Report Processing include:

 Compile: It analyses the expressions in the report definitions and saves the compiled intermediate
format on the server internally.

 Process: It executes dataset queries and combines intermediate format with layout and data.

 Render: It sends a processed report to a rendering extension to display how much information can
fit on each page and then creates the page report.

 Export: It exports reports to a different file format.

5. Explain what are parameterized reports. What are cascading parameters in SSRS reports? Do you feel that
issues exist when multi-select / multi-value parameters are allowed and utilized?

Reports which accept parameters from users to fetch and report data conditionally are called
parameterized reports. When there are multiple parameters available in a report and the values of
the different parameters are populated dynamically depending on the value of parent parameters,
then these parameters are known as cascading parameters. A tangent to cascading parameters is
the multi-value parameters. In this scenario, multiple values are selected (or all values) within a
parameter selector.
Become a Skilled Web Developer in Just 9 Months!
Caltech PGP Full Stack DevelopmentEXPLORE PROGRAM

6. Explain how you would develop an SSRS report.

The typical development methodology for an SSRS report is to begin by developing a data source.
Based on this data source, the report designer creates one or multiple datasets as required for the
parameters as well as the body of the report. Next, the report designer adds required controls from
the toolbox, which acts as a container for the fields available in the dataset. Subsequently, the
formatting of controls needs to take place. Next, the designer should verify and validate the report
and finally deploy the report. It is a good idea to follow specific best practices so that the report can
convey a story and perform optimally.

7. What is a dataset and what are the different types of datasets? How do these relate to a data source?

A dataset is identical to a query definition that is executed just as the report is executed. There are
two types of datasets - Embedded and Shared. An embedded dataset is exclusive to the report in
which it is available and can only be used by that specific report. A shared dataset can be shared
across reports. Once a dataset is shared, it has to be published to the Reporting Service Server to
allow it to be used across various reports. Suitable folder permissions need to be set to be able to
access shared datasets.

A dataset is the query definition of the data that is required and a data source is the "pipe" that is
used to link the SSRS to the root location of the data - be it a Teradata, SQL Server, or a whole
sundry of other sources. The data source typically comprises the credentials used to connect to the
source.

8. Would you rather store your query in a Database server or an SSRS report? State the reasons why or why
not.

SSRS has matured over the years and the answer to this question has pretty much changed too.
Earlier, storing SQL queries in text format directly in the dataset was generally not recommended.
However, since shared datasets have been introduced, an SSRS can store a single data set and the
data set can be shared by multiple reports. However, the ideal scenario would be to access a
database server to use a stored procedure. The advantage is that in a stored procedure, SQL will be
available in a compiled format that offers all the benefits of using an SP as compared to using an
ad-hoc query from the report. However, if you are using multi-select parameters you must
understand that using a query rooted in a report or a shared dataset enables the report designer to
take advantage of the multi-value parameter. 

9. Name the various types of reports that can be created while using the features of SSRS?

Following are the different types of the report such as;

 Snapshot reports

 Parameterized reports

 Click through reports

 Drill through reports

 Drill down reports

 Ad-hoc reports

 Cached reports

 Subreports

 Linked reports

10. Explain the key features of SSRS.

Following are the key features of SSRS:

 SSRS is cheaper and faster than other software

 SSRS allows you to generate different data reporting types

 No extra skill or training is required to learn SSRS

 Provides a quick and secure data integration

 SSRS is used to create a user interface and is available to access various parameters
11. Shed some light on the important highlights of SSRS.

Following are the most noticeable features of SSRS:

 You can work on the reports generated by using XML, Excel, or other multi-dimensional sources
and can recuperate information from OLEDB and ODBC association suppliers.

 In SSRS, clients can create reports in different structures, such as freestyle, even, diagrams,
graphical, and framework structure 

 SSRS strengthens online highlights; anyone can interact and collaborate with the report server
available on the web legitimately and can view reports in electronic applications. 

 Any number of unplanned reports can be created by using pictures, illustrations, or outer
substances and can be stored on a server. 

 All the reports created in SSRS can be sent out in multiple arrangements such as CSV, XML, PDF,
TIFF, HTML, and Excel. 

 SSRS has the automated choice for transferring the reports to the client's letter drop, movable and
shared area. 

 It supports the Simple Object Access Protocol (SOAP).

Here's How to Land a Top Software Developer Job


Full Stack Development-MEANEXPLORE PROGRAM

12. Explain what benefits you get after using the SSRS Services.

Following is the list of advantages you get by using the SSRS Service of Microsoft-

 SSRS can easily be utilized on your existing hardware, as reports are housed in one brought-
together web server from where clients can execute a report from one single spot. 

 As SSRS enables the cross-trading of reports in different organizations, it is quite simple to


perform an additional examination of reports with multiple document groups. 

 Conclusive reports can be created quickly on social or multi-dimensional information. 

 Data can be delivered quickly to a business association.


 There is no requirement for an expert to oversee SSRS, thus, you save on the cost of hiring pro
aptitudes. 

 Business clients can access the data without the help of IT experts. 

 Security can be applied to folders just as we do to reports and can be overseen in a determined job
order manner.

13. What is Tablix in SSRS?

The Tablix is a sum of the tables with matrices in SSRS. Each report that we create using the SSRS
technology is based on the Tablix data region. In other words, a Tablix can be managed with the
collective capabilities of a matrix and a table.

14. Explain the reporting life cycle of SSRS.

The reporting life cycle of SSRS comprises the following phases mainly:

 Development of Reports (Developer): It states that we need to develop a report, something that the
report developer primarily does.

 Management of Reports (DBA): It states that DBA should ensure that the report is being created.

 Security: It states that only an authorized user can access the report.

 Execution: It states how the report will be run to improve the performance of the data sources.

 Scheduling of reports: This is required so that the report can be executed on the timings as
scheduled.

 Report Delivery (DBA + Developer): It states that once the report is created and executed, the report
should reach the final recipients (business users), who then need to understand and analyze the
data of the report. If there are any changes, we need to go back to the development stage again.

15. In which programming language are the RDL files written?

RDL files are written in XML (Extensible Markup Language). These files are an extension of XML
used for SSRS reporting services. 
16. What are the different stages of Report Processing in SSRS?

The different stages of Report Processing in SSRS are:

 Compile: It internally analyses expressions in the report definitions and the compiled intermediate
format on the server.

 Process: It executes dataset queries and combines the intermediate format with the data and
layout.

 Render: It sends the processed report to a rendering extension to specify how much information
fits on each page. It also creates the page report.

 Export: It is used to export the reports to a different file format.

17. What is the Reporting Services Configuration file name in SSRS, and where does it exist?

The name of the Reporting Services Configuration file is "Rsreportserver.config" in SSRS. It is


available in the settings in the Report Manager used in the Report Server Web Service and
background processes.

18. What are the three different parts of the RDL file in SSRS?

The three different parts of the RDL file in SSRS are:

 Data: It includes the dataset on which the query is written and the data set is linked to the data
source.

 Design: You can design reports in the design reports and create matrix reports and tables. It also
helps us to drag column values from the source.

 Preview: This part is used to check the preview once the report is executed.

19. Explain what you understand by the term sub-report in SSRS?

In SSRS, sub-reports are the subpart of the main reports. These reports can be inserted into the
main part and queries and parameters can be passed to subreports just like the main report. A sub-
report can be thought of as an extension to the main report, but it includes a different data set. For
example, if you prepare a report for students, you can also use a sub-report to show the marks
associated with each student.

20. Do you think it is possible to implement data mining within SSRS? If yes, tell us how.

Yes, it is possible to implement data mining in SSRS. You can execute the implementation using a
DMX designer and develop data mining queries needed for SSRS reports. SSRS allows us to create
an exclusive custom data mining report that comprises images and text, which can be exported
HTML.

21. Explain what are Cache results of the SSRS reports.

Cache results are based on the format of the report. SSRS enables cache reports on the reporting
server, and it also offers built-in caching capability. However, the server only caches one instance of
the report in most cases. It also allows users to access and view reports quickly.

22. What are the different rendering extensions available in SSRS?

Following are the top six rendering extensions available in SSRS:

 PDF

 Word

 XML

 HTML

 CSV or Text

 PDF

23. Which tool do the Business Users use to create their reports?

Typically, business developers use the Report Builder tool to generate reports, and this is the best
choice for creating reports.
24. What is the role of a report manager in SSRS?

In SSRS, a report manager is a web application in SSRS that can be accessed by a URL. The report
manager interface depends on the user permissions. This means that the user must be allocated a
role to access any functionality or execute any task. A user assigned the role of full permission can
manage all the menus and features of the report. A URL must be defined to configure the report
manager.

25. What are the open-source software that can be used as an alternative to SSRS?

Following are some open-source software that can be used as an alternative to SSRS:

 DataVision Reports

 JFree Reports

 BIRT (Business Intelligence Reporting Tool)

 Jasper Reports

 OpenReport

26. What are the core components of SSRS?

The core components of SSRS are:

 SSRS needs a set of tools to Create, View and Manage reports.

 A report server component hosts and processes reports in different formats such as HTML, PDF,
Excel, and CSV.

 Developers integrating with custom applications or creating custom tools can use APIs to manage
or build reports.

27. How can we fine-tune reports in SSRS?

Following are the steps to fine-tune the reports in SSRS:

1. Create a big server or you can utilize the reporting services of other database servers.
2. Store the duplicate copy of the data for the advanced installation of the logic, report contents, and
characteristics of the report's application.

3. You can solve the locking issues by adopting no lock. It can also improvise the query of the
performance.

Kickstart Your UI/UX Career Right Here!


UI/UX Design ExpertEXPLORE PROGRAM

28. Explain what are the data types used to develop Radio Button Parameter Type in an SSRS Report.

The Radio Button Parameter Type in SSRS Reports is created with the help of a Boolean data type.
Ensure to set the data type to Boolean while using the bit-type column to add a query for the report.

29. What are the different reporting service components in SSRS?

The different reporting service components in SSRS are:

 Report Server: Used for services such as the delivery of implementations and reports.

 Report Designer: Specifies where the report is designed or created.

 Report Manager: A web-based administration tool used to manage the report server.

30. What are the different ways to deploy an SSRS report?

SSRS reports can be deployed through:

 Visual Studio 

 Report servers

 By creating the utility

31. List out what other servers you can use with SSRS?

SQL Server Reporting Services (SSRS) can be used with the following servers:
1. Microsoft SQL Server

2. SharePoint Server

3. Microsoft Azure SQL Database and Azure Synapse Analytics (formerly SQL Data Warehouse)

4. Oracle Database

32. Mention what the different types of SSRS reports are?

 Tabular Report: A tabular report is the most basic type of SSRS report that displays data in a
tabular format, similar to a spreadsheet. It is used for displaying data in a simple, organized
manner.

 Matrix Report: A matrix report, also known as a crosstab report, displays data in a grid format. It is
useful for comparing data across multiple dimensions and categories.

 Chart Report: A chart report displays data in a graphical format, such as a bar chart, line chart, or
pie chart. This type of report is useful for visualizing trends and patterns in data.

33. Name some of the open-source software you can use as an alternative to SSR? 

Some open-source alternatives to SSRS include JasperReports, BIRT (Business Intelligence and
Reporting Tools), and Pentaho Report Designer. 

34. Explain how you can configure a running aggregate in SSRS? 

To configure a running aggregate in SSRS, you can use the RunningValue function in a calculated
field or expression. You can specify the aggregate function (such as Sum, Count, or Average) and
the area to be aggregated. 

35. Mention what is the main function of a query parameter? 

The main function of a query parameter in SSRS is to allow users to filter the data that is displayed
in a report. 
36. Explain how SSRS reports Cache results?

Yes, SSRS reports can cache results. Caching allows an account to store the data retrieved from the
data source in memory to quickly access it without retrieving the data again. 

Become a Skilled Web Developer in Just 9 Months!


Caltech PGP Full Stack DevelopmentEXPLORE PROGRAM

37. Mention what are the three command line utilities and what are their primary functions? 

SSRS's three command line utilities are RsConfig.exe, RsKeymgmt.exe, and Rs.exe. RsConfig.exe is
used to configure the SSRS service and manage encryption keys. RsKeymgmt.exe is used to
manage encryption keys for SSRS. Rs.exe is used to execute SSRS scripts and manage report server
operations. 

38. What method can you use to reduce the overhead of Reporting Services data sources? 

 One method to reduce the overhead of Reporting Services data sources is to use shared data
sources. A shared data source allows multiple reports to use the same connection information,
reducing the need to duplicate connection information in numerous words. 

39. Explain what is the difference between Tabular and Matrix report? 

A tabular report is a simple, organized report that displays data in a tabular format, similar to a
spreadsheet. It is useful for displaying data in a simple, organized manner. A matrix report, also
known as a crosstab report, displays data in a grid format. It is useful for comparing data across
multiple dimensions and categories. 

40. What is an Ad Hoc Report? 

It is designed to meet the user's specific needs and can be customized as needed. Ad Hoc reports
are created using a drag-and-drop interface, allowing users to quickly create reports without needing
technical expertise. 
41. What are the command prompt utilities for SSRS? List out some and explain.

The command prompt utilities for SSRS include:

1. RS.exe: This utility is used for deploying and managing reports on the SSRS server. It can create
and manage report folders, upload and download messages, and set security permissions for
reports.

2. RSCONFIG.exe: This utility configures the SSRS server, including setting up the connection to the
report server database and managing the encryption keys.

3. RSKeyMgmt.exe: This utility is used for managing the encryption keys used by the SSRS server.

42. Explain the minimum software requirements for the SSRS framework.

 The minimum software requirements for the SSRS framework include the following:

 Windows Server 2008 R2 or later

 SQL Server 2008 R2 or later

 .NET Framework 3.5 SP1 or later

 Internet Information Services (IIS) 7 or later

 A web browser (such as Internet Explorer, Chrome, or Firefox) to access the report server

43. What is mixed-mode database security?

Mixed mode database security is a type of security that allows for both Windows and SQL Server
authentication to be used in a SQL Server database. This allows Windows and SQL Server users to
access the database with different permissions and access rights.

44. If you have created a report with a month name as its parameter, explain the easiest way to provide
values for the parameter?

One of the easiest ways to provide values for the parameter is to use the built-in functions in SSRS
to generate the list of month names dynamically. You can use the following steps:
1. In the report design view, navigate to the "Report Data" pane and right-click on the "Parameters"
folder. Select "New Parameter" to create a new parameter for the month name.

2. In the "Parameter Properties" window, set the "Prompt" to "Month Name" and set the "Data type" to
"String."

Become a Skilled Web Developer in Just 9 Months!


Caltech PGP Full Stack DevelopmentEXPLORE PROGRAM

45. How to Create a Calendar Parameter in SSRS Report

To create a calendar parameter in an SSRS report, you can follow these steps:

1. Create a new parameter in the report, giving it a name such as "CalendarDate."

2. Set the data type of the parameter to "Date."

3. In the "Available Values" section, select "None."

4. In the "Default Values" section, select "Today."

46. How would you generate a Sequence Number for all the Records in the SSRS Report?

To generate a sequence number for all records in an SSRS report, you can use the ROW_NUMBER()
function within the query for the dataset. 

47. How will you display data on a single Tablix extracted from two datasets in an SSRS report by joining on
the single column?

To display data on a single Tablix extracted from two datasets in an SSRS report by joining on a
single column, you can use the JOIN clause in the query for the primary dataset.
SSRS Interview Questions And Answers

Q1. What is Tablix?

Ans. In reporting services, the Tablix is a sum of tables with matrices. Every report we create using
SSRS technology is based on the Tablix data region. In other words, It can be managed with the
combined capabilities of a table and a matrix.

Q2. RDL files are written in which programming language?

Ans. It is an extension for XML used for SSRS reporting services. These files are written in XML
(Extensible Markup Language).

Q3. What is the name of the Reporting Services Configuration file and where it exists?

Ans. Rsreportserver.config is the reporting services file and it can be found in settings in Report
Manager or that used in Report Server Web Service and background processes.

Q4. Explain what a sub-report is?

Ans. In a subreport, you can pass parameters and queries to it. Simply, a sub-report is considered
to be an expanded version of your main report. Any report can be used as a subreport that is
inserted into the main domain, just like the main report. However, in the subreport, it consists of a
separate data set. For example, in a sub-report, you will be able to create a customer's report and
then a sub-report can show an order list for every customer.

Q5. What other servers to use with SSRS?

Ans. Companies use SQL servers with SSRS most of the time, however, other servers can be
integrated with SSRS as well.

 Oracle
 Flat XML files
 ODBC and OLEDB
 Teradata
 Hyperion
------     Related Article: What is SSRS     ------

Q6. What are the six rendering extensions available in SSRS?

Ans. There are the top 6 rendering extensions that will be available in SQL Server Reporting
Services.

 Excel
 Word
 XML
 HTML
 CSV or Text
 PDF

SSRS Interview Questions For Experienced

Q7. Which tool can be used by the Business Users to create their reports?

Ans. Report Builder could be the best choice for a tool that can be used by business users or even
by developers to create reports.

Q8. What is an Ad Hoc Report?

Ans. Ad Hoc Reports enable users with limited technical skills to create new, easy to access reports
on their own. Usually, these reports are created from report models. Also, users can select their
priorities and requirements whether to save the reports to a personal server or to share with
others by posting them to the reporting services center. Ad hoc reports can generate quick reports
which are created to meet user needs and requirements. Users can modify these reports with a
powerful analysis of report data.

Q9. How to implement data mining in SSRS?

Ans. Yes, the Implementation of data mining is possible in SSRS. You execute the implementation
using DMX designer and create data mining queries required for SSRS reports. In SSRS, you have
the option to create a custom data mining report containing text and images and export them into
HTML, email, and get prints to distribution.

SSRS Tutorial For Beginners in 2021

Q10. Explain SSRS reports Cache results?

Ans. Cache results are based on the format of the report.SSRS allows cache reports on the
reporting server, you will also find built-in caching capability. However, the server only caches for
one instance of the report in most cases. It also enables users to access and view reports quickly. 
Q11. Explain the different stages of Report Processing?

Ans. Different stages of report processing 

Compile: In this stage, it analyzes all expressions in the report definitions and saves the compiled
in intermediate format to the server.

Process: Process is the stage where SSRS runs dataset queries and compile intermediate format
with data and layout

Render: In the third stage it sends an end processed report to a rendering extension to show how
much data fits in each page to create the page report.

Export: Finally in this stage, It exports the reports into a different file format to be shared with 

------     Related Article: Types of Reports in SSRS     ------

Q12. How do You Create An SSRS Report?

Ans. Before you begin developing and using SSRS reports you need to start by creating data
sources, as to create an SSRS report one or multiple datasets are required for parameters and to
form the body of the report. Then add necessary controls from the toolbox to make it work as a
container for all datasets. Next format all the controls that you have added to the report body.
Then verify and validate the report to deploy the report.

SSRS Scenario Based Interview Questions

Q13. What data type should be used when creating Radio Button Parameter Type in SSRS
Report? 

Ans. When using the bit-type column to add a query for your report, go to Parameter properties to
set the data type to boolean. As it is to show Radio Buttons, Or else, a text box will appear for the
parameter value.

Q14. If you have created a report that has a month name as its parameter, Explain what
would be the easiest way to provide values for the parameter?

Ans. As we have fixed the set of month names, they will don't change in the report. We can add
month names in it as the static values. And If the values of parameters change often, then it is a
great idea to add them in a table and use a Query to get them for Parameters in the SSRS report.
With such practices, if we are adding or removing any of the values from the report we don't have
to make changes every time. We can simply add or remove values in the reports that will be
collected by the query.
Q15. How to Create a Calendar Parameter in SSRS Report

Ans. We would be able to create the parameters and have the Calendar icon to choose a date. As
we always have the column/s in the table which is the date of the DateTime type and we can write
our query as below.

1) Create a new parameter as StartDate, change the data type to DATE/TIME.

2) Click on the default value, if you want to set the current date as the default date, use this
expression: = Today.

3) Do not specify any value in the Available Values tab and leave it as a No Default value. 

By following this, your date parameter will be set and the user will be enabled with Calendar
control when the report is run on the server.

Q16. How would you generate a Sequence Number for all the Records in SSRS Report?

Ans. Use the row number function to generate a sequence number for all the records in your SSRS
report. You can do that by put in a new blank column to your Tablix and then click on the cell to
pivoted to expressions and write expressions.

Q17. How will you display data on a single Tablix extracted from two datasets in an SSRS
report by joining on the single column?

Ans. To display data on a single Tablix using the ‘Lookup Function’ in the SQL server report to find
corresponding values in a dataset that has unique values, join the data from two datasets. Also,
there are many other guidelines that you may be required to follow to create SSRS reports using
two or more datasets. Such as there should be at least one matching column on which we will join
the datasets.

We can use the Lookup Function and write our expressions as shown below.

Lookup Function with Parameters

LookupSet(source_expression, destination_expression, result_expression, dataset)

Q18. Will you store your query in an SSRS Report or a database server? Explain why?

Ans. The SQL queries should be stored in a stored procedure in a database server. As practices of
storing SQL queries in text format are not considered to be good anymore and it should be
avoided. By storing queries in SP to a data server SQL would be in an accumulated format while
providing all the benefits of using an SP.
SSRS Advanced Interview Questions

Q19. What are the command prompt utilities for SSRS? List out some and explain.

Ans. Following is the list of utilities of the command prompt for SSRS.

RSS Utility: Command ‘RS.exe’, is the command-line utility that supports SharePoint and Native
development modes. That can perform many scripted operations related to SQL SSRS and also be
used while publishing reports on the report server or move another report from the server.

Powershell cmdlets: Powershell is getting popular amongst power users as well as IT pros as


their preferred language and CLI. CMdlets are important to interact with CLI, in PowerShell, most of
them are likely to be written in 'C' and perform functions that return a .NET object. It supports only
SharePoint modes, and it installs SSRS service and proxy servers while providing provisioning and
managing of SSRS applications and proxies. 

List of the most useful cmdlets in PowerShell:

 Get-Command 
 Get-Member
 Get-Help
 Get-Process

Rsconfig utility: Rsconfig utility is a script host that is used to perform scripted operations such as
Publish reports, create items in a report server database, also configuring and managing report
server connection with report server database. The command file is ‘rsconfig.exe’, which only
supports native developer mode. 

RsKeymgmt utility: The command ‘rskeymgmt.exe’, it is an encryption key management tool


used in database recovery operations. It supports only the Native development mode. Also used to
back up, apply, recreate, and delete symmetric keys using the command line.

(For more information about SSRS blogs  Click Here]

Q20. Explaining is the Multi-value parameter for SSRS Report?

Ans. The multi-value parameter enables users to enter and pass more than one query for the
parameter while creating an SSRS report. In any report parameters that we use to filter out the data
and extract useful information that is required for the current scenario. In the multi-value
parameter, you can choose to enter either static values or we can get values from the databases.
Q21. List out the drawbacks reported in the previous versions of SSRS?

Ans. SSRS is most suitable for the reporting needs, however, it has drawbacks too, which are listed
below for better assessment of the reporting tool. (Modifications can be different for these
drawbacks in the current version before and after posting this article)

 No print button to take the printouts of excel files, PDFs, etc.


 Lack of availability of debugging the custom code.
 Numbers of pages not available for the whole content.
 Passing values from a subreport to the main report is difficult.
 Sub-reports can not be added to the header or footer.
 would also be able to choose values for the XML;’parameter.

[ Related Article: Power BI vs SSRS ]

Q22. Explain the minimum software requirements for the SSRS framework?

Ans. Here is the list of software required for the SSRS framework :

Operating System - Windows Server 2000, Windows XP, Windows Server 2003.

Processor - Intel Pentium 2.

Hard Disk - A report server, required approximately 50 MB space. 

                    For the .NET framework, it needs 100 MB of space. 

                    For a Report designer, it would be 30 MB and needs 145 MB for samples.

RAM - Needs a minimum of 256 MB RAM for the report server.

Database - Needs SQL server 2000 and a minimum service pack of 3.

Q23. Why should you use SSRS for your next project?

Ans. SSRS can have many advantages, compared to other reporting platforms. Following are:

 SSRS is cheaper and faster than others.


 Helps generate effective reports using exciting data of both databases (Oracle or MS SQL
Server)
 SSRS does not require special training or skills to use tools and features.
 Secure and quick integration with Visual Studio also uses .NET for generating reports.
 UI will be generated and available to access automatically as per requirements once
parameters are defined specifically by the developers.
Q24. What is mixed mode database security?

Ans. At the point when you install SQL Server, it's smarter to get a different SQL Server user name
and password when signing in to the database server. As windows are not considered to be the
most secured database security option. In SSRS you have the alternative to allow SQL Server to
integrate with Windows or require your users to keep a different SQL Server user ID and password.
Your reports will require their username and password key to run reports from SSRS.

PL/SQL Developer usage skills and


shortcut keys

Alibaba Cloud
·

Follow
3 min read

Jan 21, 2022


Oracle case: PL/SQL Developer usage skills, shortcut keys

1. SQL PLUS-like window: File->New->Command Window, this is


similar to oracle’s client tool SQL plus, but it is much easier to use.

2. Set the keyword to automatically capitalize: Tools->Preferences->Editor,


select the Keyword case as Uppercase. In this way, when you enter a SQL
statement in the window, the keywords will be automatically capitalized, while
others are lowercase. In this way, it is easier to read the code and maintain a
good coding style. Similarly, in Tools->Preferences->Code Assistant (assistant),
you can set the code prompt delay time, prompt when entering a few characters,
and uppercase and lowercase database objects. capitalization, etc.;

3. View the execution plan: Select the SQL statement to be analyzed, and then
click the Explain plan button (that is, the execution plan) on the toolbar, or press
F5 directly; this is mainly used to analyze the execution efficiency of the SQL
statement and the structure of the table, which is convenient for SQL Provide an
intuitive basis for tuning;

4. Automatic replacement: Quickly enter SQL statements, such as input s, press


space, and automatically replace with SELECT; for another example, enter sf,
press space, and automatically replace with SELECT * FROM, which is very
convenient and saves a lot of time to write Duplicate SQL statements.

Setting method: menu Tools–>Preferences–>Editor–>AutoReplace (automatic


replacement)–>Edit

1), create a text file shortcuts.txt, and write the following content:
s=SELECT
Copy the code and save it to the ~/PlugIns directory under the installation path
of PL/SQL Developer
2) Tools–>Preferences–>User Interface–>Editor–>AutoReplace, select the
Enable check box, then browse the file to select the shortcuts.txt created earlier,
and click Apply.
3) Restart PL/SQL Developer, enter s+space in the sql window, and sc+space to
test.
Note: shortcuts.txt cannot be deleted, otherwise the shortcut keys cannot be used

Some rules are defined below for reference


i=INSERT
u=UPDATE
s=SELECT
f=FROM
w=WHERE
o=ORDER BY
d=DELETE
df=DELETE FROM
sf=SELECT * FROM
sc=SELECT COUNT(*) FROM
sfu=SELECT * FROM FOR UPDATE
cor=CREATE OR REPLACE
p=PROCEDURE
fn=FUNCTION
t=TIGGER
v=VIEW
sso=SET serveroutput ON;

Set shortcut keys (setting method: menu Tools->Preferences->User Interface-


>Key Configuration)
New sql window: ctrl+shift+s
New command window: ctrl+shift+c
New test window: ctrl+shift+t
PL/SQL Developer beautifier: ctrl+shift+f
Redo: ctrl+shift+z
Undo: ctrl+z
Clear: ctrl+d (use with caution, no recovery, I am disabled O(∩_∩)O~)
Select all: ctrl+a
Indent: tab
Unindent: shift+tab
Uppercase: ctrl+shift+x
Lowercase: ctrl+shift+y
Comment: ctrl+h
Uncomment: ctrl+m
Find: ctrl+f
Show table structure: ctrl+mouse over the table name
Template list: shift+alt+r
Window list: ctrl+w

5. Execute a single SQL statement: press the F8 key

6. TNS Names: menu Help->Support Info (support information)->TNS Names,


you can view Oracle’s tnsnames.ora;

7. Debug the stored procedure


When using PL/SQL Developer to operate Oracle, sometimes some stored
procedures are called, or stored procedures are debugged;
Method to call stored procedure:
1) First, select Procedures in the Browser on the left of PL/SQL Developer to find
the stored procedure that needs to be called;
2) Then, select the debugged stored procedure, right-click, and select Test. In the
pop-up Test script window, for the parameter defined as in the type, you need to
enter a value for the value of the parameter; finally, click the number button
above: Start debugger or press F9;
3), the last click: RUN or Ctrl+R.

Debug shortcuts
Toggle breakpoint: ctrl+b
Start: f9
Run: ctrl+r
Step into: ctrl+n
Step over: ctrl+o
Step out: ctrl+t
Run to exception: ctrl+y
8. Template shortcut keys

9. My Objects is automatically selected by default after login

By default, after PLSQL Developer logs in, all objects will be selected in Brower.
If the user you log in is dba, to expand the tables directory, it normally takes a
few seconds to wait, but after selecting My Objects, the response rate is
calculated in milliseconds.

Setting method:
Tools menu -> Brower Filters, the order window of Brower Folders will be
opened, and “My Objects” can be set as the default.
In the Tools menu -> Brower Folders, move the directories you often click (for
example Tables Views Seq Functions Procedures) a little higher, and add color
distinctions so that your average time to find tables will be greatly shortened, try
it out.

priority, from left to right


Tables–>Tablespaces–>Procedures–>Users–>Roles

PL- SQL Basics

Vaishali Goilkar
·
Follow
2 min read

Apr 29, 2020

In this article, we learn about the basics of PL/SQL.

 PL/SQL was developed by Oracle Corporation in the 90s to


enhance the SQL.
 PL/SQL is a completely portable, high-performance transaction
processing language.
 It provides a completely built-in interpreted and OS independent
programming environment.

FEATURES OF PL/SQL

FEATURES OF PL/SQL
 PL/SQL is tightly integrated with SQL.
 It offers extensive error checking.
 It offers numerous data types.
 It offers a variety of programming structures.
 It supports structured programming through functions and
procedures.
 It supports object-oriented programming.
 It supports the development of web applications and server pages.

PL/SQL SYNTAX

 PL/SQL syntax is based on that of ADA and pascal programming


language.
 The basic syntax of PL/SQL is a block-structured language.
 PL/SQL programs are divided and written in the logical block of
code. Each block consists of three subparts.
 Every PL/SQL statement ends with a semicolon (;). PL/SQL blocks
can be nested within other PL/SQL blocks using BEGIN and END.

SYNTAX OF PL/SQL:

DECLARE

<declaration syntax>

BEGIN

<executable commands>
EXCEPTION

<exception handling>

END;

EXAMPLE OF PL/SQL:

DECLARE

message varchar2(20):= ‘Hello!’;

BEGIN

dbms_output.put_line(message);

END;

SECTIONS AND DESCRIPTION

DECLARATIONS:

 This section starts with the keyword DECLARE.


 It is an optional section and defines all variables, cursors,
subprograms, and other elements to be used in the program.

EXECUTABLE COMMANDS:
 This section is enclosed between the keyword BEGIN and END. It
is a mandatory section.
 It consists of the executable/SQL statement of the program.
 It should have at least one executable line of code, which may be
just a NULL command to indicate that nothing should be executed.

EXCEPTION HANDLING:

 This section starts with the keyword EXCEPTION.


 This optional section contains exceptions that handle errors in the
program.

Data Warehouse Architecture

Vaishali Goilkar
·

Follow
2 min read

May 5, 2020

2
In this article, we learn about data warehouse architecture.

DATA WAREHOUSE

 Data warehousing indicates the whole set of interrelated activities


involved in designing, implementing, and using a data warehouse.
 The data warehouse itself, together with additional data marts.
Data marts contain the data and the functions that allow the data to
be accessed visualized and perhaps modified.

DATA WAREHOUSE ARCHITECTURE

DESIGN APPROACHES OF DATA WAREHOUSE

TOP-DOWN

 The top-down methodology is based on the overall design of the


data warehouse.
 It is time-consuming and more time required for the initial set up.
BOTTOM-UP

 This model contains consistent data marts and these data marts
can be delivered quickly.
 Data is cleansed, transformed, and loaded into this layer using
back-end tools.

MIXED

 The mixed methodology is based on the overall design of the data


warehouse within a prototyping approach.
 This approach is highly practical and usually preferable.

ETL TOOLS

 ETL performs three main functions Extraction Transformation and


Loading of data into the data warehouse.
 Extraction- Data are extracted from the available internal and
external sources.
 Transformation- The goal of the cleaning and transformation
phase is to improve the quality of data extracted from the different
sources.
 Loading- After extraction and transformation, data are loaded
into the tables of the data warehouse.

METADATA

 Metadata is simply defined as data about data. The data that is


used to represent other data is known as metadata.
 In the Data Warehouse Architecture, meta-data plays an important
role as it specifies the source, usage, values, and features of data
warehouse data. It also defines how data can be changed and
processed. It is closely connected to the data warehouse.
 Metadata helps in the decision support system for the mapping of
data when data is transformed from the operational environment
to the data warehouse environment.
 Metadata also helps in summarization between lightly detailed data
and highly summarized data.

SSRS REPORT

Vaishali Goilkar
·

Follow
5 min read

Jan 27, 2020

1
 SSIS package Extract, Transform, Loaded the data into OLAP
databases. Data can be various formats like CSV, Excel.
 SSAS is about analysis. Analysis means calculations like sum, count
or some complicated formula which does forecasting, analysis
calculation. In SSAS we create a cube for precalculation which is to
allow queries to return data quickly.
 SSRS is a reporting service that helps to fetch data from cube also
fetch the data from SQL Server.

SSRS has two templates:-

 Report Server Project Wizard creates a new report server


project using the report wizard.
 Report Server Project creates an empty report template.

In this article, we create a report and fetch the data from the analysis cube. Here
I have already created a cube.

 Firstly we go to Solution Explorer and right-click it → Goto Add


project → Select Report Server Project Wizard → Click on OK.
When we clicking on ok new wizard is open that is Report Wizard.
REPORT WIZARD

 SSRS connects different types of data sources like XML, SQL


Server, etc.

DATA SOURCES
 Here we select Microsoft SQL Server Analysis Services because we
fetch the data from cube then click on Edit.

SELECT DATA SOURCE

 On the Edit option, we enter the server name as well cube name.
CONNECTION

 As per the selected data source, It generates a connection string to


obtain data for the report.
REPORT WIZARD

 We specify the MDX query to get data for the report by using Query
Builder.
 We write query an analysis cube use MDX. MDX is a
Multidimensional Expressions query language for OLAP.
DESIGN THE QUERY

 Here we fetch the required data and click on OK.


QUERY DESIGNER

 As per fetching data here creates an MDX query.


DESIGN THE QUERY

 Tabular report display data in tabular format which is a row and


column.
 Matrix report display summarized data. In matrix number of
columns can change dynamically depending on input data.
 We select Tabular report and click on Next.
REPORT TYPE

The report can have three sections:-

 Page (Header):- It displays Report title, Page number, Time, etc.


 Group:- It displays different types of the same data which are
repeating in a single project like country or state-wise report.
 Details:- It shows actual data of the report.

We drag and drop the field from the Available field to the Display field and click
Next.
DESIGN THE TABLE

 Provide a report name and click to Finish.


COMPLETE THE WIZARD

 REPORT. RDL is created under the SSRS folder. RDL is a Report


Definition Language nothing but XML file.

REPORT

 Here we rename of report title as customer Report and run the


report.
REPORT

 Report Data provides data to a report. Data Source tells where the
server is located. Data Set tells what kind of query we executed.

REPORT DATA

PARAMETER

 Now we report search by an Address. So we need to add parameter


on the project.
 There are two ways of adding a parameter. Add a Parameter or
Query Designer. Here we add a query.
QUERY

 Here we set the query over the Address table.

QUERY DESIGNER
 When we click on the OK parameter is created in the report data.

PARAMETER

 Here we select Mumbai and click on View Report then as per


address report is displayed.

REPORT AS PER ADDRESS

Drop Down By An Another Way

 Here we learn how to create a drop-down list. For creating a drop-


down list we uncheck the multiple values from created parameters.
REPORT DATA

 We create a new Dataset. And we select the data source and create
a query by using a query designer.
DATASET PROPERTIES

 As well in the parameter, we select a value for this parameter.


REPORT PARAMETER PROPERTIES

 If we run the report address shown in a drop-down list.

DROP DOWN LIST


COMPLEX SQL QUERY

Vaishali Goilkar
·

Follow
2 min read

Jan 6, 2020

EMPLOYEE TABLE

1. Find out second highest salary

SELECT DISTINCT [SALARY] FROM [dbo].[EMPLOYEE] e1


WHERE 2 = (SELECT COUNT(DISTINCT [SALARY])

FROM [dbo].[EMPLOYEE] e2

WHERE e1.SALARY <=e2.SALARY)

RESULT

2. Find out third highest salary

SELECT * FROM

SELECT Dense_Rank() over ( order by SALARY desc) as Rnk,E.*

FROM Employee E

)A

WHERE Rnk=3;

RESULT

3. Select all records from Employee table where name not in ‘RIA’
and ‘RAJ’
SELECT EMP_NAME

FROM Employee

WHERE [EMP_NAME] in(‘RIA’,’RAJ’)

RESULT

4. Select ID where an absence of 8 no ID.

SELECT ID

FROM EMPLOYEE

WHERE ID <> 8

RESULT

5. Select those values present in one table but missing in another


table.

Query 1:-
SELECT E.[EMP_CODE] FROM [dbo].[EMPLOYEE] E

LEFT JOIN [dbo].[EMP] E1

ON E.ID = E1.ID

WHERE E1.ID IS NULL

Query 2:-

SELECT E.[EMP_CODE]

FROM [dbo].[EMPLOYEE] E

WHERE E.ID NOT IN

SELECT [ID]

FROM [dbo].[EMP] E1

Query 3:-

SELECT E.[EMP_CODE]

FROM [dbo].[EMPLOYEE] E
WHERE NOT EXISTS

SELECT NULL

FROM [dbo].[EMP] E1

WHERE E1.ID = E.ID

RESULT

6. What is the output of the below query?

 SELECT 16
 SELECT $
 SELECT count(*)
RESULT

7. How to concatenate multiple rows into a single text string?

DECLARE @EMP_NAME VARCHAR(MAX);

SET @EMP_NAME = ‘’;

SELECT @EMP_NAME = @EMP_NAME + IsNull(EMP_NAME + ‘, ‘, ‘’)

FROM [dbo].[EMPLOYEE]

Select @EMP_NAME

RESULT
Stored procedure in PL/SQL

siva prasad
·

Follow
5 min read

Jan 31, 2020

4
 A Procedure is a subprogram unit that contains a group of PL/SQL
statements. A Stored procedure in PL/SQL defined as a series of
declarative SQL statements that can be stored in the database
record.
 It performs one or more specific tasks. It is the same as procedures
on other programming languages. Actually, the procedure means a
function or a method.
 They can be called through triggers, other procedures, or
applications on Java, PHP tec.The procedure contains two sections.
One is a Header and another one is a body. you can learn more
through sql online training
 The Header section contains the name of the procedure and the
parameters or variables passed to the procedure.
 The Body section contains the declaration, execution section and
exception section similar to a PL/SQL block.

How to pass parameters in PL/SQL:

When you want to create a procedure or function, then you have to define
parameters. So, first of all, we want to about what is a parameter?. Now let us
discuss about parameters.

Parameter:

The parameter is a variable or method. They are used to exchange data between
stored procedures and functions. They allow us to give input to the subprograms
and helps to divide them. The parameters are defined at the time of subprograms
creation. They contain calling statements of those subprograms as well as
interact with the values of subprograms. The data type of the parameter in the
subprogram and the calling statement must be the same. The size of the
parameter should not mention at the time of parameter declaration, because size
is dynamic for this type. become a professional developer in oracle
through oracle pl sql training

The parameters are classified into 3 types:

1. IN Parameter:

This can be referenced by the procedure of function. It is used to give input to the
subprograms. It is a read-only variable inside the programs. Hence, their values
are not changing inside the subprogram. In the calling statement, these
parameters can act as a variable or a literal value, or an expression.

2.OUT Parameter:

These are used to get output from the subprograms. They contain a read-write
variable inside the subprograms. So, their values can be changed inside the
subprograms. At the calling statement, these parameters always be a variable as
well as they hold the value from the current program.

3. IN OUT Parameter:

This parameter is used for giving input as well as for getting output from the
subprograms. This is one of the read-write variables inside the subprograms. So
their values can be changed inside the subprograms. The parameters should
always be a variable to hold the value from the subprograms in the calling
statement. They can be called at the time of creating subprograms. here is a blog
on oracle pl sql way to success
Syntax to create a Stored procedure:

The below syntax shows the creation of stored procedure in SQL

CREATE [OR REPLACE] PROCEDURE procedure_name

[ (parameter [,parameter]) ]

IS

[declaration_section]

BEGIN

executable_section

[EXCEPTION

exception_section]

END [procedure_name];

Declarative section:

In this section, a subprogram starts with the DECLARE keyword. It has a type,
cursors, constants, variables, exceptions, and nested subprograms.prepare over
the important pl sql interview questions

Executable section:
It is a mandatory section and it contains statements to perform the allocated
actions.

Exception Section:

This is a section of code that used to handle the exceptions.

Example of creating a Stored procedure:


The below code shows the creation of the pl sql procedure example with the
parameters of a customer. It contains first name, last name, and email.

CREATE OR REPLACE PROCEDURE print-contact( in_customer_id


NUMBER)

IS

r_contact contacts%ROWTYPE;

BEGIN

get contact based on customer id

SELECT *

INTO r_contact

FROM contacts

WHERE customer_id = p_customer_id;


print out contact’s information

dbms_output.put_line( r_contact.first_name || ‘ ‘ ||

r_contact.last_name || ‘<’ || r_contact.email ||’>’ );

EXCEPTION

WHEN OTHERS THEN

dbms_output.put_line( SQLERRM );

END;

To compile the procedure, click on the Run statement button as shown in the
below diagram.

If the procedure is compiled successfully, then u will see the new procedure at
the procedure node as shown below.
Editing the procedure:

If you want to change the code in an existing procedure, then you follow these
steps.

1. First, click the procedure name at the Procedure node.


2. And edit the code of the procedure.

3. Finally, click the menu option to recompile the procedure.

It looks as below:

Removing a procedure:
If you want to delete a procedure, you can use “DROP PROCEDURE”. The
syntax for the Removing procedure is given below.

DROP PROCEDURE procedure_name;

The following steps describe the dropping of a procedure in SQL.

1. First Right-click on the procedure name, then drop the procedure which you
want.

2. Then choose the “Drop” menu option.

3. Finally, in the prompt dialog, click the “Apply” button to remove the
procedure.

It is shown below.
Advantages of PL/SQL:

● They help to give a good performance, as well as gives an improvement in the


application.

● They decrease the traffic between the database and the application. This
because, the lengthy statements already spread into the database, so we don’t
need to sent again via the application.

● These are having Code reusability, functions in pl sql and methods work in
other languages such as C/C++ and Java.

● They improve database performance as well as provides reusability.

● They maintain integrity as well as security.

Disadvantages of PL/SQL:

● Stored procedures need a lot of memory usage. Hence, the database


administrator should decide an upper bound as to how many stored procedures
are useful for a particular application.
● They haven’t provided the functionality debugging in stored procedures.

1. What is Oracle?

Oracle is a relational database management system, which Establishes data in the form of tables. Oracle
makes skillful use of all system resources, on all hardware architecture, to deliver Incomparable performance,
price performance, and scalability.

Related Article - What is Oracle?

2. What is Oracle table?

A table is the vital unit of data storage in an Oracle database. The tables of a database hold all of the user
Available data. Table data is stored in rows and columns. Tables are the Entire unit of data storage in an
Oracle Database. Data is stored in rows and columns. To determine a table with a table name, such as
employees, and a set of columns. A row is a collection of column information Related to a single record.

3. What is Oracle View? To Write the Syntax

In Oracle, the view is a virtual table. Every view has a query attached to it. (The query is a SELECT
statement that identifies the columns and rows of the table(s) the view uses.) It is stored in the Oracle data
dictionary and does not store any data. It can be executed when called. A view is generated by a query
joining one or Major tables.

Syntax:

CREATE VIEW view name AS  

SELECT columns

FROM tables  

WHERE conditions;

4. How many memory layers are in the Oracle shared pool?

Oracles shared pool Contains of two layers.They are

1. Library cache
2. Data dictionary cache.

Library cache: This layer has information about SQL statements that were parsed, information about cursors
and any plan data.

Data Dictionary cache: this Layer has information about the accounts of the users, their Advantage and
segments information

5. What is PL/SQL?
PL/SQL Determines for Procedural Language extension of Structured Query Language (SQL). It is a block-
structured language having true/false blocks which are made up of 3 sub-blocks i.e. a declarative Segment,
an executable Segment, and an exception building Segment. PL/SQL is combined with Oracle and the
functionalities of PL/SQL extend after each release of Oracle database.

It combines procedural language elements like conditions and loops and allows declaration of constants and
variables, procedures and functions. It also helps the users to develop difficult database applications using
control structures, procedures, modules, etc. PL/SQL is not finite by any case-sensitive letters so you are free
to use lower case letters or upper case letters.

6. What is The Structure of PL /SQL?

The vital structure of PL/SQL is the BLOCK structure. Each program of PL/SQL Consist Of both the SQL
statement and the PL/SQL statement, which eventually forms the PL/SQL block. Every PL/SQL block has 3
defined sections out of which two sections are optional i.e. the declaration section and the exception handling
section and one more section is mandatory i.e. the execution section.

Related Article - Oracle SQL and PL SQL way to success

7. Explain Difference Between The Triggers and Constraints?

Constraints will do memory location to table Contrast whereas triggers will do table to table comparison. For
this triggers will use magic tables(inserted, deleted).In the order of procession first Constraints next Triggers,
But performance wise triggers will give the best performance because the table to table comparison is faster
than memory location to table comparison.

Triggers Constraints

Trigger Influence only those rows, which are added Constraints affect all the rows i.e. the one that existed
after it is enabled. before and the ones that were newly added.

Triggers, unlike constraints, is capable of A constraint is responsible only to Preserve the


implementing high-end business rules that are Difficult integrity of the database.

8. Explain Difference Between Truncate and Delete?

TRUNCATE:

TRUNCATE SQL query deletes all rows from a table, without logging the individual row deletions. It is a DDL
command is executed using a table lock and the entire table is locked to remove all records. We cannot use
WHERE clause with TRUNCATE. It deletes all rows from a table. To use Truncate on a table you need at
least ALTER acceptance on the table.

TRUNCATE TABLE Customers;

DELETE:

To execute a DELETE queue, delete acceptance is required on the target table. If you need to use a WHERE
clause in a DELETE, select permissions are required as well. It is a DML command. It is completed using a
row lock, each row in the table is locked for deletion. We can use where clause with DELETE to filter & delete
particular records. The DELETE command is used to delete rows from a table based on WHERE condition. It
maintains the log, so it slower than truncates. The DELETE statement deletes rows one at a time and records
an entry in the transaction log for each deleted row. Identification of column keep DELETE retains the
identity. To use Delete you need DELETE acceptance on the table
DELETE FROM Customers;

9. What is the difference between DBMS and RDBMS?

A database management system (DBMS) consists of a collection of interrelated data and a set of programs
to access that data. A Relational Database Management System (RDBMS) is a DBMS that is relational in the
name. This means that the Inner l workings access data in a relational manner. Oracle is an RDBMS.

Get the best pl sql training from onlineitguru and complete certification also.

10. Define Schema? What is schema objects?

A schema is a Group of objects associated with the database.

Schema objects are abstractions or logical structures that Assign to database objects or structures. Schema
objects are found of such things as clusters, indexes Hold Data, packages, sequences, stored procedures,
synonyms, tables, views, and so on...

Note:  It is no relationship between a table space and a schema.

11. What is SQL*Plus?

SQL*Plus is the ad-hoc user coherence tool for the Oracle RDBMS. With SQL*Plus, you can Attach into the
RDBMS and run SQL commands and PL SQL programs. This is the Main no application interface into the
Oracle RDBMS. SQL*Plus is actually an interactive query tool, with some Programming capabilities. It is a
non-GUI, character-based tool that has been Entire since the dawn of the Oracle age. Using SQL*Plus, you
can Get in an SQL statement, such as a SELECT query, and view the results. You can also execute Data
Definition Language (DDL) commands that allow you to Manage and modify your database. You can even
enter and execute PL/SQL code.

12. What is the Oracle Enterprise Manager?

The Oracle Enterprise Manager is the Advance graphical administration tool designed to help the DBA
manage one or more Oracle systems. , In case their data centers or in the Oracle Cloud. Through Inner
integration with Oracle’s product stack, Enterprise Manager Gives market-leading and maintenance
management and automation support for Oracle applications, databases, middle ware, hardware, and
engineered systems.

13. Difference between the Syntax and Run Time Errors?

The errors such as spelling mistakes come below syntax errors, which can easily be found using a PL/SQL
compiler. As far as Run time error is concerned, they are found in the PL/SQL block. You need to add an
exception handling section to handle those errors. Such errors come under SELECT INTO statements that
rebound no rows.

14. What do you understand by Redo Log file mirroring?

Mirroring is a procedure of having a copy of Redo log files. It is done by Build group of log files together. This
Establish that LGWR automatically writes them to all the members of the current online redo log group. If the
group suddenly fails, the database automatically switches over to the next group. It diminishes the
performance.

15. Define constraints? Different Types of Constraints?


The constraint is a rule or restriction concerning a piece of data that is enforced at the data level rather than
the object or application level. A constraint clause can constraint a single column or collection of columns in a
table.

 There are five types of constraints namely are:

 Null / Not Null


 Primary Key
 Unique
 Check or Validation
 Foreign key or References key

16. Define foreign key?

A foreign key is a Collection of columns with value is based on the primary key values from another table. A
foreign key constraint, is known as referential integrity constraint. Foreign key identifies the column collection
of columns in the child table that makes up of the foreign key.

17. What is system global area (SGA)?

The SGA is a shared memory region that Oracle uses to Reserve data and control information for one Oracle
instance. The SGA is allocated when the Oracle instance begins: it is deal located when the Oracle instance
shuts down.  Each Oracle instance that begins lias its own SGA. The information in the SGA is made up of
the database buffers, the redo log buffer, and the shared pool: each has a fixed size and is created at
instance started

18. What is a Table space?

A database is Split into Logical Storage Unit called table space. A table space is used to grouped related
logical structures stable

19. What Are the Pl /SQL packages?

PL/SQL packages are schema objects that groups logically relevant functions stored procedures, cursors,
and variables at one place. The package is compiled and stored in a database and its contents can be
shared. Packages have 2 parts: a specification and a body.

20. The Compare between the ROLLBACK and ROLLBACK TO Statements?

There is a considerable Compare between the ROLLBACK and ROLLBACK TO statement. When you apply
the ROLLBACK command a transaction is ending undone where all the blocks are released. As far as
ROLLBACK TO command is concerned, the transaction is undone but till a SAVEPOINT. Thus, the
transaction remains real and active even after the command is implemented.

21. What are The Some Predefined Exceptions in PL/SQL?

    Here is a list of some predefined exceptions that can be used in PL/SQL:

 DUP_VAL_ON_INDEX
 NO_DATA_FOUND
 CURSOR_ALREADY_OPEN
 INVALID_NUMBER
 INVALID_CURSOR
 TIMEOUT _ON_RESOURCE
 LOGON_DENIED
 ZERO_DIVIDE
 TOO_MANY_ROWS
 PROGRAM_ERROR
 STORAGE_ERROR
 VALUE_ERROR

22. Why Trigger Is Used?

Triggers are used to define an action when database related events are performed. It is used for preventing
invalid transactions, enforcing complex business rules, etc. Triggers mean activating an action.

23. What is Mutating Table and Constraining Table?

A mutating table is the one, which is modified using a DML statement or a table with defined triggers. A
constraining table is the one, which is being read for a referential integrity constraint.

24. What are the various types of queries?

The types of queries are:

 Normal Queries
 Sub Queries
 Co-related queries
 Nested queries
 Compound
 queries

25. What is Join?

Join Bringing to gather columns and data from two or more tables. Join is a query in which data is retrieved
from 2 or more table. A join matches data from 2 or more tables, based on the values of one or more columns
in Exclusive table.

26. What is a stored procedure?

A stored procedure is a Set of SQL statements that are pre-parsed and stored in the database. When the
stored procedure is Appeal, only the input and output data is passed; the SQL statements are not transferred
or parsed.

27. What Is a Save point Command?

A The SAVE POINT command is utilized to set a point within a transaction to which you may rollback. This
command helps in cancelling the portions of the current transaction. Using ROLLBACK with the SAVE POINT
TO clause, a transaction can be undone in parts rather than rolling back the entire transaction. What Is a
Save point Command?

28. How Can You Generate Debugging Output from Pl/SQL?

Use the DBMS_OUTPUT package. One More possible method is to just use the SHOW ERROR command,
but this only shows errors. The DBMS_OUTPUT package can be utilized to show intermediate results from
loops and the status of variables as the procedure is executed. The new package UTL_FILE can also be
used
[contact-form-7 id="5350" title="Post insertion"]

29. What Is The Use Of Cascade Constraints?

When this Section is used with the DROP command, a parent table can be dropped even when a child table
exists

30. Explain An Ora-01555?

You get this error when you get a Print too old within rollback. It can usually be solved by expanding the undo
retention or increasing the size of rollbacks. You should also look at the logic Tangled in the application
getting the error message.

------Best of Luck-------

You might also like