SQL Questions
SQL Questions
SQL Questions
SQL Questions 1
same way as a constraint on the value of attribute.
For example: XYZ doesn’t fulfill the domain constraint but the integer value as 899
fulfills the criteria of domain constraint. Hence, domain is of high importance.
Relationship between the store data is called base relations and implementation
of it is called as tables. Whereas, relations which don’t store the data, but can be
found out by applying relational operations on other relations are called as
derived relations. When these are implemented they are termed as views or
queries.
Derived relations are more useful then base relation, as they can have more
information from many relations, but they act as a single relation.
Referential integrity: it is used to maintain integrity on all the values which have
been referenced.
SQL Questions 2
Entity integrity tells that in a database every entity should have a unique key; on
the other hand referential integrity tells that in the database every table values
for all foreign keys will remain valid.
Referential integrity is based on entity integrity but it is not the other way around.
For example: if a table is present and there is a set of column out of which one
column has parent key set then to ensure that the table doesn’t contain any
duplicate values, a unique index is defined on the column that contains the
parent key.
Primary key remains one only for the table, whereas there can be more than one
foreign key.
Primary key is unique and won’t be shared between many tables, but foreign key
will be shared between more than one table and will be used to tell the
relationship between them.
For example: It is same as the book where by using the index you can directly
jump to a defined section. In relational database there is a provision to give
multiple indexing techniques to optimize the data distribution.
SQL Questions 3
What are the relational operations that can be performed on the
database?
There are many relational operators that are used to perform actions on relational
database. These operators are as follows:
1. Union operator that combines the rows of two relations and doesn’t include any
duplicate. It also removes the duplicates from the result.
2. Intersection operator provides a set of rows that two relations have in common.
3. Difference operator provide the output by taking two relations and producing the
difference of rows from first that don’t exist in second.
It consists of set of procedures that eliminates the domains that are non-atomic
and redundancy of data that prevents data manipulation and loss of data
integrity.
1. First normal form: in this table represents a relation that has no repeating
groups.
2. Second normal form: non- prime attributes are not functional dependent on
subset of any candidate key.
SQL Questions 4
5. Boyce codd normal form: “every non-trivial functional dependency in the table is
dependent on superkey”.
7. Fifth normal form (5NF): “Every non-trivial join dependency in the table is implied
by the superkeys of the table”.
9. Sixth normal form (6NF): “Table features no non-trivial join dependencies at all”.
It is used to extract the data that are ancient and accumulated over long period
of time. For this purpose de-normalization occurs that provide smart business
applications.
There should be constraint that user should not be allowed to view the state till it
is consistent.
– It describes the definition of the database design which is different from the first
normal form.
It keeps the values in structured and specialized types with their own domain
specific languages.
The query language used in this is extended to incorporate more support for
relational domain values by adding more operators.
SQL Questions 5
How many levels of data abstraction are available?
There are three levels of data abstraction available in database model and these are
as follows:
1. Physical level: It is the lowest level that describes how data is stored inside the
database.
2. Logical level: It is the next higher level in the hierarchy that provides the
abstraction. It describes what data are stored and the relationship between
them.
3. View level: It is the highest level in hierarchy that describes part of the entire
database. It allows user to view the database and do the query.
Extension tells about the number of tuples presented in a table at any instance,
whereas intension gives the name, structure and constraint of the table.
2. System relational system: This includes the relational data that a system has to
produce and keep everything in relation.
SQL Questions 6
1. Physical data independence: It allows the modification to be done in physical
level and doesn’t affect the logical level.
It is derived from the base table. The view is stored in the data dictionary and
represents the file directly.
It is related to the logical data independence as it is at the logical level and not at
the physical level.
These properties allow the database to be more convenient to access and use.
This allows data to be shared more safely in between the tables.
If these properties are not being implemented then the data will become
inconsistent and inaccurate.
SQL Questions 7
It is used in E-R diagrams and used to show the relationship between
entities/tables.
It has many forms like the basic is one to one, which associate one entity with
another.
Second is one to many: which relates one entity with many entities in a table.
Last is many to one that allows the many entities to be associated with one
entity.
RDBS supports a tabular structure for data and relationship between them in the
system whereas DBMS supports only the tabular structure.
What is Index?
A pointer to data having physical representation is called as Index.
Each index in a table has some valid name and we can have more than one
index in different columns of a table.
In a table , each row is examined by sql server to fulfil our query is known as
table scan and it only happen when there is no index available to help the query.
Clustered and Non clustered indexes are the most widely used indexes in a
database.
What is Trigger?
SQL Questions 8
A Trigger is a process of firing an action when some event like Insert, Update or
Delete occurs.
A trigger can’t be called or even executed rather they are automatically become
active by the DBMS whenever some modification in associated table occur.
Triggers are event driven and can attached to particular table in a database.
Triggers are implicitly executed and stored procedures are also executed by
triggers.
Referential integrity is maintained by the trigger and they are managed and
stored by DBMS.
Triggers can be nested also, in which Insert, Update or Delete logic can be fired
from the trigger itself.
What is a NOLOCK?
NOLOCK is used to improve concurrency on a busy system.
When some other process is updating the data on the same time you are
reading it is known as dirty read.
What is the STUFF function and how does it differ from the
REPLACE function?
Using STUFF function we can overwrite the specified characters of a string.
The syntax of STUFF function is:
STUFF (stringToChange, startIndex, length, new_characters )
where stringToChange is the string which will have the characters those we want to
overwrite, startIndex is the starting position, length is the number of characters in the
string that are to be overwrited, and new_characters are the new characters to write
into the string.
SQL Questions 9
While REPLACE function is used to replace specified character at all its existing
occurrences.
A self join can be used as any type, if both the tables are same.
The simple example where we can use SELF JOIN is if in a company have a
hierarchal reporting structure and an employee reports to another.
A cross join give the number of rows in the first table multiplied by the number of
rows in second table.
The logic can be encapsulated using procedures and can help to change
procedure's code without interacting to application.
For using this function first specify the function name, followed by the empty
parentheses.
SQL Questions 10
Then mention the OVER function. For this function, you have to pass an ORDER
BY clause as an argument. The clause identifies the column on which you are
going to apply the RANK function.
For Example:
SELECT RANK() OVER(ORDER BY BirthDate DESC) AS [RowNumber], FirstName,
BirthDate FROM EmpDetails
In the result you will see that the eldest employee got the first rank and the
youngest employee got the last rank. Here the rows with equal age will get same
ranks.
The rank depends on the row's position in the result set, but not on the
sequential number of the row.
When it is required to perform the row by row operations which are not possible
with the set-based operations then cursor is used.
1. Implicate Cursor
SQL Server automatically manages cursors for all data manipulation statements.
These cursors are called implicit cursors.
1. Explicit Cursor
When the programmer wants to perform the row by row operations for the result
set containing more than one row, then he explicitly declare a cursor with a
name.
SQL Questions 11
Both Truncate and Delete command will delete data from given table and they
will not delete the table structure from the database.
Difference
3. We can use any condition in WHERE clause using DELETE but it is not possible
with TRUNCATE.
5. TRUNCATE is faster than DELETE, because when you use DELETE to delete
the data, at that time it store the whole data in rollback space from where you
can get the data back after deletion, whereas TRUNCATE will not store data in
rollback space and will directly delete it. You can’t get the deleted data back
when you use TRUNCATE.
Syntax: COMMIT;
Example:
BEGIN
UPDATE EmpDetails SET EmpName = ‘Arpit’ where Dept = ‘Developer’
COMMIT;
END;
ROLLBACK statement is used to end the current transaction and undone the
changes which was made by that transaction.
Example:
BEGIN
Statement1;
SQL Questions 12
SAVEPOINT mysavepoint;
BEGIN
Statement2;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK TO mysavepoint;
Statement5;
END;
END;
What is a WITH(NOLOCK)?
WITH(NOLOCK) is used to unlock the data which is locked by the transaction
that is not yet committed. This command is used before SELECT statement.
When the transaction is committed or rolled back then there is no need to use
NOLOCK function because the data is already released by the committed
transaction.
Syntax: WITH(NOLOCK)
Example:
It has a reference to a value from the row selected by the outer query.
Nested subquery executes only once for the entire nesting (outer) query. It does
not contain any reference to the outer query row.
For example,
Correlated Subquery:
select e.EmpFirstName, e.Salary, e.DeptId from Employee e where e.Salary =
(select max(Salary) from Employee ee where ee.DeptId = e.DeptId)
Nested Subquery:
select EmpFirstName, Salary, DeptId from Employee where (DeptId, Salary) in
(select DeptId, max(Salary) from Employee group by DeptId)
SQL Questions 13
Differentiate UNION, MINUS, UNION ALL and INTERSECT?
INTERSECT - It will give all the distinct rows from both select queries.
MINUS - It will give distinct rows returned by the first query but not by the second
query.
UNION - It will give all distinct rows selected by either first query or second
query.
UNION ALL - It will give all rows returned by either query with all duplicate
records.
1. JOIN: Return details from tables if there is at least one matching row in both
tables.
2. LEFT JOIN: It will return all rows from the left table, even if there are no
matching row in the right table.
3. RIGHT JOIN: It will return all rows from the right table, even if there is no
matching row in the left table.
4. FULL JOIN: It will return rows when there is a match in either of tables.
1. DDL: The SQL commands which deals with database schemas and information
of how the data will be generated in database are classified as Data Definition
Language.
-For example: CREATE TABLE or ALTER TABLE belongs to DDL.
2. DML: The SQL commands which deals with data manipulation are classified as
Data Manipulation Language.
For example: SELECT, INSERT, etc.
3. DCL: The SQL commands which deal with rights and permission over the
database are classified as DCL.
For example: GRANT, REVOKE
SQL Questions 14
What is Index tuning?
Query performance as well as speed improvement of a database can be done
using Indexes.
It uses the query optimizer to perform the analysis of queries with respect to the
workload and based on this knowledge, it recommends the best usage of
indexes.
The changes in the usage of index, query distribution and their performance are
analysed for checking the effect.
It also recommends ways of tuning the database for a small set of problem
queries.
Subsequently, Enterprise manager is used for starting the Index Tuning Wizard
and instructs to recommended indexes which are based on the trace that is
captured. An estimation of increased performance after making changes is
provided apart from appropriate columns suggestion.
SQL Questions 15
Following are the reasons for the poor performance of a query:
No indexes.
Queries with predicates that use operators, and any one of the following are
true:
There are no statistics on the columns involved on either side of the operators.
The distribution of values in the statistics is not uniform, but the query seeks a
highly selective value set. This situation can be especially true if the operator is
anything other than the equality (=) operator.
The predicate uses the not equal to (!=) comparison operator or the NOT logical
operator.
Queries that use any of the SQL Server built-in functions or a scalar-valued,
user-defined function whose argument is not a constant value.
Queries that compare variables whose values are not known when the query is
compiled and optimized.SQL stands for
SQL Questions 16