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

SQL Interview Questions

SQL is a standard query language used to create, manipulate, and retrieve data from relational databases. It has several key uses including executing queries, retrieving data, inserting/updating/deleting records, and creating/modifying database structures. SQL includes several sublanguages - DDL for defining database schema, DML for manipulating data, and DCL for controlling access. Database normalization reduces redundancy and inconsistencies by organizing data into tables and relationships between tables. Indexing improves query performance by allowing faster data retrieval.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

SQL Interview Questions

SQL is a standard query language used to create, manipulate, and retrieve data from relational databases. It has several key uses including executing queries, retrieving data, inserting/updating/deleting records, and creating/modifying database structures. SQL includes several sublanguages - DDL for defining database schema, DML for manipulating data, and DCL for controlling access. Database normalization reduces redundancy and inconsistencies by organizing data into tables and relationships between tables. Indexing improves query performance by allowing faster data retrieval.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

SQL Interview Questions:

Theory Questions:-
1] What is SQL?

SQL is a standard query language used for maintaining the relational database and perform many
different operations of data manipulation on the data.

It is a database language used for database creation, deletion, fetching rows and modifying rows,
etc. sometimes it is pronounced as 'sequel.'

2] What are the usages of SQL?

execute queries , retrieve data ,inserts records, updates records , delete records,
create new databases , create new tables , create views

3] What are the subsets of SQL?

Data definition language (DDL): CREATE, ALTER, DROP, etc.


Data manipulation language (DML): SELECT, UPDATE, INSERT, etc.
Data control language (DCL):GRANT, REVOKE.

4] What is Data Control Language?

Data control language allows you to control access to the database.


DCL is the only subset of the database which decides that what part of the database should be
accessed by which user at what point of time.

GRANT: to grant the specific user to perform a particular task


REVOKE: to cancel previously denied or granted permissions.

5] What is DBMS?

A Database Management System (DBMS) is a program that controls creation, maintenance and
use of a database.
DBMS can be termed as File Manager that manages data in a database rather than saving it in
file systems

Fortune Cloud Technologies Group, Pune 1


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
6] What are tables and Fields?

A table is a set of data that are organized in a model with Columns and Rows. Columns can be
categorized as vertical, and Rows are horizontal. A table has specified number of column called
fields but can have any number of rows which is called record.

7] What is a primary key?

A primary key is a combination of fields which uniquely specify a row. This is a special kind of
unique key, and it has implicit NOT NULL constraint. It means, Primary key values cannot be
NULL.

8] What is a unique key?

A Unique key constraint uniquely identified each record in the database. This provides uniqueness
for the column or set of columns.

A Primary key constraint has automatic unique constraint defined on it. But not, in the case of
Unique Key.

There can be many unique constraint defined per table, but only one Primary key constraint
defined per table.

9] What is a foreign key?

A foreign key is one table which can be related to the primary key of another table. Relationship
needs to be created between two tables by referencing foreign key with the primary key of another
table.

10] What is the difference between primary key and unique key?

Primary key carries unique value but the field of the primary key cannot be Null on the other hand
unique key also carry unique value but it can have a single Null value field.

We can have only one primary key column in table, but we can have multiple unique key column in
table.

11] What are the different types of database management systems?

There are four types of database:

Fortune Cloud Technologies Group, Pune 2


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
o Hierarchical databases (DBMS)
o Relational databases (RDBMS)
o Network databases (IDMS)
o Object-oriented databases

RDBMS is one of the most often used databases due to its easy accessibility and supports
regarding complex queries.

12] What is RDBMS?

RDBMS stands for Relational Database Management System.

It is a database management system based on a relational model.

RDBMS stores the data into the collection of tables and links those table using the relational
operators easily whenever required.

It facilitates you to manipulate the data stored in the tables by using relational operators.

13] What are Constraints in SQL?

NOT NULL - Restricts NULL value from being inserted into a column.

CHECK - Verifies that all values in a field satisfy a condition.

DEFAULT - Automatically assigns a default value if no value has been specified for the field.

UNIQUE - Ensures unique values to be inserted into the field.

INDEX - Indexes a field providing faster retrieval of records.

PRIMARY KEY - Uniquely identifies each record in a table.

FOREIGN KEY - Ensures referential integrity for a record in another table.

14] What is Normalization in a Database?

Normalization is used to minimize redundancy and dependency by organizing fields and table of a
database.

Fortune Cloud Technologies Group, Pune 3


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
There are some rules of database normalization which commonly known as Normal From and
they are:

o First normal form(1NF)


o Second normal form(2NF)
o Third normal form(3NF)
o Boyce-Codd normal form(BCNF)

Using these steps, the redundancy, anomalies, inconsistency of the data in the database can be
removed.

15] What are the disadvantages of not performing Database Normalization?

o The occurrence of redundant terms in the database which causes the waste of the space in
the disk.
o Due to redundant terms inconsistency may also occur id any change will be made in the
data of one table but not made in the same data of another table then inconsistency will
take place, which will lead to the maintenance problem and effects the ACID properties as
well.

16] What is an inconsistent dependency?

Inconsistent dependency refers to the difficulty of accessing particular data as the path to reach
the data may be missing or broken.

Inconsistent dependency will leads users to search the data in the wrong table which will afterward
give the error as an output

17] What is Denormalization in a Database?

Denormalization is used to access the data from higher or lower normal form of database.

It also processes redundancy into a table by incorporating data from the related tables.

Denormalization adds required redundant term into the tables so that we can avoid using complex
joins and many other complex operations.
Fortune Cloud Technologies Group, Pune 4
3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
Denormalization doesn’t mean that normalization will not be done, but the denormalization
process takes place after the normalization process.

18] What is Data Integrity?

Data Integrity is the assurance of accuracy and consistency of data over its entire life-cycle, and is
a critical aspect to the design, implementation and usage of any system which stores, processes,
or retrieves data.

It also defines integrity constraints to enforce business rules on the data when it is entered into an
application or a database.

19] What are the types of operators available in SQL?

1. Arithmetic operators: addition (+), subtraction (-), multiplication (*), division (/), etc.
2. Logical operators: ALL, AND, ANY, ISNULL, EXISTS, BETWEEN, IN, LIKE, NOT, OR,
UNIQUE.
3. Comparison operator: =, !=, <>, <, >, <=, >=, !<, !>

20] What is the difference between CHAR and VARCHAR2 datatype in SQL?

CHAR is used to store fixed-length character strings, and VARCHAR2 is used to store variable-
length character strings.

For example, suppose you store string ‘Database’ in a CHAR(20) field and a VARCHAR2(20) field.

The CHAR field will use 22 bytes (2 bytes for leading length).
The VARCHAR2 field will use 10 bytes only (8 for the string, 2 bytes for leading length).

21] What are ACID properties in a transaction


In order to maintain consistency in a database ‘before and after’ transactions, certain properties
are followed. They are

 Atomicity: This means the transaction must happen fully and cannot be left midway.
 Consistency: To maintain integrity constraints hence valid data enters the database
 Isolation: Controls Concurrency
 Durability: Once a transaction is committed it remains committed

Fortune Cloud Technologies Group, Pune 5


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
22] What is view in SQL?

A view is a virtual table which contains a subset of data within a table.

Views are not originally present, and it takes less space to store.

A view can have data from one or more tables combined, and it depends on the relationship.
Views are used to apply security mechanism in the SQL Server.

The view of the database is the searchable object we can use a query to search the view as we
use for the table.

23] Is it possible to sort a column using a column alias?

Yes. You can use the column alias in the ORDER BY instead of WHERE clause for sorting.

24] What is an Index in SQL?

SQL indexes are the medium of reducing the cost of the query as the high cost of the query will
lead to the fall in the performance of the query.

An index is used to increase the performance and allow faster retrieval of records from the table.

Indexing reduces the number of data pages we need to visit to find a particular data page.

Indexing also has a unique value that means that the index cannot be duplicated.

An index creates an entry for each value, and it will be faster to retrieve data.

25] Which are the different types of indexes in SQL?

There are three types of Indexes in SQL:

o Unique Index
o Clustered Index
o Non Clustered Index

Fortune Cloud Technologies Group, Pune 6


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
26] What is a join?

This is a keyword used to query data from more tables based on the relationship between the
fields of the tables. Keys play a major role when JOINs are used.

27] What are the types of join and explain each?

There are various types of join which can be used to retrieve data and it depends on the
relationship between tables.

 Inner Join : Inner join return rows when there is at least one match of rows between the
tables.

 Right Join : Right join return rows which are common between the tables and all rows of
Right hand side table.

 Left Join : Left join return rows which are common between the tables and all rows of Left
hand side table.

 Full Join : Full join return rows when there are matching rows in any one of the tables.

28] What is a Cursor?

A database Cursor is a control which enables traversal over the rows or records in the table.

This can be viewed as a pointer to one row in a set of rows. Cursor is very much useful for
traversing such as retrieval, addition and removal of database records.

29] What is a relationship and what are they?

Database Relationship is defined as the connection between the tables in a database. There are
various data basing relationships, and they are as follows:.

 One to One Relationship.


 One to Many Relationship.
 Many to One Relationship.
 Self-Referencing Relationship.

30] What is subquery?

A subquery is a query within another query.

Fortune Cloud Technologies Group, Pune 7


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
The outer query is called as main query, and inner query is called subquery.

SubQuery is always executed first, and the result of subquery is passed on to the main query.

31] What are the types of subquery?

There are two types of subquery – Correlated and Non-Correlated.

A correlated subquery cannot be considered as independent query, but it can refer the column in a
table listed in the FROM the list of the main query.

A Non-Correlated sub query can be considered as independent query and the output of subquery
are substituted in the main query.

32] What is the difference between DELETE and TRUNCATE commands?

DELETE command is used to remove rows from the table, and WHERE clause can be used for
conditional set of parameters. Commit and Rollback can be performed after delete statement.

TRUNCATE removes all rows from the table. Truncate operation cannot be rolled back

33] What is the difference between BETWEEN and IN condition operators?

The BETWEEN operator is used to display rows based on a range of values. The values can be
numbers, text, and dates as well. BETWEEN operator gives us the count of all the values occurs
between a particular range.

The IN condition operator is used to check for values contained in a specific set of values. IN
operator is used when we have more than one value to choose.

34]What are local and global variables and their differences?

Local variables are the variables which can be used or exist inside the function. They are not
known to the other functions and those variables cannot be referred or used. Variables can be
created whenever that function is called.

Global variables are the variables which can be used or exist throughout the program. Same
variable declared in global cannot be used in functions. Global variables cannot be created
whenever that function is called.

Fortune Cloud Technologies Group, Pune 8


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
35] What is the difference between Cluster and Non-Cluster Index?

Clustered index is used for easy retrieval of data from the database by altering the way that the
records are stored. Database sorts out rows by the column which is set to be clustered index.

A nonclustered index does not alter the way it was stored but creates a complete separate object
within the table. It point back to the original table rows after searching.

36] What is a stored procedure?

Stored Procedure is a function consists of many SQL statement to access the database system.

Several SQL statements are consolidated into a stored procedure and execute them whenever
and wherever required.

37] Advantages and Disadvantages of Stored Procedure?

Stored procedure can be used as a modular programming – means create once, store and call for
several times whenever required. This supports faster execution instead of executing multiple
queries. This reduces network traffic and provides better security to the data.

Disadvantage is that it can be executed only in the Database and utilizes more memory in the
database server.

38] What is a trigger?

A DB trigger is a code or programs that automatically execute with response to some event on a
table or view in a database. Mainly, trigger helps to maintain the integrity of the database.

Example: When a new student is added to the student database, new records should be created
in the related tables like Exam, Score and Attendance tables.

39] How many row comparison operators are used while working with a subquery?

There are 3-row comparison operators that are used in sub queries such as IN, ANY and ALL.

Fortune Cloud Technologies Group, Pune 9


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
40] What is Auto Increment?

Auto increment keyword allows the user to create a unique number to be generated when a new
record is inserted into the table.

AUTO INCREMENT keyword can be used in Oracle and IDENTITY keyword can be used in SQL
SERVER.

Mostly this keyword can be used whenever PRIMARY KEY is used.

41] What is Self-Join?

Self-join is set to be query used to compare to itself.

This is used to compare values in a column with other values in the same column in the same
table.

ALIAS ES can be used for the same table comparison.

42] What is CLAUSE?

SQL clause is defined to limit the result set by providing condition to the query. This usually filters
some rows from the whole set of records.

Example – Query that has WHERE condition

Query that has HAVING condition.

43] What is an ALIAS command?

ALIAS name can be given to a table or column. This alias name can be referred in WHERE clause
to identify the table or column.

Example-.

Select st.StudentID, Ex.Result from student st, Exam as Ex where st.studentID = Ex. StudentID

Here, st refers to alias name for student table and Ex refers to alias name for exam table.

44] Which operator is used in query for pattern matching?

LIKE operator is used for pattern matching, and it can be used as -.


Fortune Cloud Technologies Group, Pune 10
3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
1. % - Matches zero or more characters.
2. _(Underscore) – Matching exactly one character.

45] What is the difference between TRUNCATE and DROP statements?

TRUNCATE removes all the rows from the table, and it cannot be rolled back.

DROP command removes a table from the database and operation cannot be rolled back.

46] Explain the difference between Rename and Alias?

Rename is a permanent name given to a table or column whereas

Alias is a temporary name given to a table or column.

47] What is User-defined function? What are its various types?

 Scalar Function: As explained earlier, user-defined scalar functions return a single scalar
value.

 Table Valued Functions: User-defined table-valued functions return a table as output.

 Inline: returns a table data type based on a single SELECT statement.

 Multi-statement: returns a tabular result-set but, unlike inline, multiple SELECT statements can
be used inside the function body.

48] What is a Recursive Stored Procedure?

A stored procedure which calls itself until a boundary condition is reached, is called a recursive
stored procedure.

This recursive function helps the programmers to deploy the same set of code several times as
and when required.

Some SQL programming languages limit the recursion depth to prevent an infinite loop of
procedure calls from causing a stack overflow, which slows down the system and may lead to
system crashes.

49] What is a schema?


A schema is a collection of database objects in a database for a particular user/owner.

Fortune Cloud Technologies Group, Pune 11


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
Objects can be tables, views, indices and so on.

50] Can we call Stored Procedure inside Stored Procedure?


Yes, we can call a stored procedure from another stored procedure.
For example, Procedure2 is the procedure which is called Procedure1. Both Procedure1 and
Procedure2 can have business logic implemented in it.

SQL Query Questions:-

*Note:
1] If any column or value given in a query, if it is not present in your table, add it first to
your table then form a query.

2] You can also create a table if table is not present for any particular query.

1] Create a query to get output as below table and insert values for all table.

A] Table name Employee

Empid EmpName Department ContactNo EmailId Emp Head Id

B] Table name EmpDept

DeptId DeptName Dept_off DeptHead

C] EmpSalary

EmpId Salary IsPermanent

2] Select the detail of the employee whose name start with P.

3] How many candidate take salary more than 5000.

Fortune Cloud Technologies Group, Pune 12


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
4] Select the detail of employee whose emailId is in gmail.

5] Select the details of the employee who work either for department E-104 or E-102.

6] What is the department name for DeptID E-102?

7] What is total salarythat is paid to employees?

8] List name of all employees whose name ends with a.

9] select the name of the employee whose name's 3rd charactor is 'h'.

10] Select the department name of the company which is assigned to the employee whose
employee id is grater 103.

11] Select the employee whose department off is Monday

12] What is query to fetch last record from Employees table?

13] How to find 2nd highest salary of Employees using Self join?

14] What is query to display odd rows from the Employees table?

15] Find Query to get information of Employee where Employee is not assigned to the
department

Fortune Cloud Technologies Group, Pune 13


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
16] How to Show the Max salary and min salary together from Employees table?

17] How to get distinct records from the Employees table without using distinct keyword.

18] How to display following using query?

**

***

19] What is SQL Query to find maximum salary of each department?

20] Consider the following table STUDENT.

REGD.NO NAME BRANCH

0001 Ram CSE

0002 Hari MECH

0003 Pradeep EEE

0004 Deepak ETC

Write a query to add another colunm in table as "address".

Fortune Cloud Technologies Group, Pune 14


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
21] Write a query to display the name ( first name and last name ) for those employees who
gets more salary than the employee whose ID is 163.

22] Write a query to display the name ( first name and last name ), salary, department id, job
id for those employees who works in the same designation as the employee works whose
id is 169.

23] Write a query to display the name ( first name and last name ), salary, department id for
those employees who earn such amount of salary which is the smallest salary of any of the
departments.

24] Write a query to display the employee id, employee name (first name and last name ) for
all employees who earn more than the average salary.

25] Write a query to display all the information of the employees whose salary is within the
range 1000 and 3000.

26] Write a query to display all the information of the employees whose salary is within the
range of smallest salary and 2500.

27] Write a query to display all the information for those employees whose id is any id who
earn the second highest salary.

28] Write a query to display the department id and the total salary for those departments
which contains at least one employee.

Fortune Cloud Technologies Group, Pune 15


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
29] Write a subquery that returns a set of rows to find all departments that do actually have
one or more employees assigned to them.

30] Write a query to display all the triggers that are present in a particular database.

31] Write a query to show example of before trigger

32] Write a query to show example of after trigger

33] Write a trigger query to calculate the percentage of the student as soon as his details
are updated to the database

34] Write a query to drop a particular trigger.

Consider the following - class table,

ID NAME

1 abhi

2 adam

4 alex

and the class_info table,

Fortune Cloud Technologies Group, Pune 16


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
ID Address

1 DELHI

2 MUMBAI

3 CHENNAI

35] Write a query to join both the above tables using Cross Join:

36] Write a query to join both the above table using Inner Join:

37] Get Id, name and address field from above tabel using natural join

38] Display all the fields of both the tabels using left outer join

39] Display all the fields of both the tabels using right outer join

40] Display all the fields of both the tabels using full outer join

41] How to display 1 to 100 Numbers with query?

42] How to remove duplicate rows from table?

43] Delete duplicate rows

44] How to find count of duplicate rows?

Fortune Cloud Technologies Group, Pune 17


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
45] How to display Date in DD-MON-YYYY table?

46] How to create the Student_1 table, which is exact replica of Student table?

47] Write query to find the repeated characters from your name?

48] How to calculate number of rows in table without using count function?

49] How to fetch common records from two different tables which has not any joining
condition.

50] What is query to fetch last record of table?

Study sincerely to become Employable

“There is no shortcut for Success”

All the Best..!

Fortune Cloud Technologies Group, Pune 18


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com

You might also like