SQL Interview Questions
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.'
execute queries , retrieve data ,inserts records, updates records , delete records,
create new databases , create new tables , create views
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
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.
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.
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.
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.
RDBMS is one of the most often used databases due to its easy accessibility and supports
regarding complex queries.
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.
NOT NULL - Restricts NULL value from being inserted into a column.
DEFAULT - Automatically assigns a default value if no value has been specified for the field.
Normalization is used to minimize redundancy and dependency by organizing fields and table of a
database.
Using these steps, the redundancy, anomalies, inconsistency of the data in the database can be
removed.
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.
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
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.
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.
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).
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
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.
Yes. You can use the column alias in the ORDER BY instead of WHERE clause for sorting.
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.
o Unique Index
o Clustered Index
o Non Clustered Index
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.
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.
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.
Database Relationship is defined as the connection between the tables in a database. There are
various data basing relationships, and they are as follows:.
SubQuery is always executed first, and the result of subquery is passed on to the main query.
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.
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
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.
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.
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.
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.
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.
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.
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.
This is used to compare values in a column with other values in the same column in the same
table.
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.
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.
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.
Scalar Function: As explained earlier, user-defined scalar functions return a single scalar
value.
Multi-statement: returns a tabular result-set but, unlike inline, multiple SELECT statements can
be used inside the function body.
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.
*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.
C] EmpSalary
5] Select the details of the employee who work either for department E-104 or E-102.
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.
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
17] How to get distinct records from the Employees table without using distinct keyword.
**
***
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.
30] Write a query to display all the triggers that are present in a particular database.
33] Write a trigger query to calculate the percentage of the student as soon as his details
are updated to the database
ID NAME
1 abhi
2 adam
4 alex
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
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.