SQL Questions With Solutions
SQL Questions With Solutions
2Q.Clustered index
Clustered index are sort and store the rows in tables or views. It is faster and less space taken.
Only one clustered index have in one table and the primary key constraint creates default
clustered index.
The non-clustered index is created to improve the performance of frequently used queries not
covered by a clustered index
Unique constraint creates by default non clustered index and it is slower and consume more
space.
It does not sort the data in table.
Clustered Index
5Q.Type of functions
1.Scalar Function
2. Table values function
3. Inline table valued function
4. Mutistatement table valued function
end
create procedure get department salary (@dept name varchar(50),@sal money output)
as
begin
select @sal = sum(salary)
from employees a
join departments b
on a.department_id = b.department_id
where department_name = @deptname
end
---Executing procedure
Temp tables
1.Are real materialized tables that exist in tempdb
2.Can be indexed
3.Can have constraints
4.Persist for the life of the current CONNECTION
5.Can be referenced by other queries or subprocedures
6.Have dedicated stats generated by the engine.
A trigger is a special type of stored procedure that automatically runs when an event occurs in
the database server.
Types of trigger
1.DDL Trigger
2.DML Trigger
3.After or For trigger
4.Instead of trigger
1.Loaing excel file to sql table using aggregate, lookup, merge, merge join,union
all,multicast,conditional split,derived column transformation.
2.Loaiding multiple files by using Foreach loop,for loop containers.
3.Using File system task copy,move,rename the files from the source to destination.
4.Working on Excute SQL task,Excute process task,Excute package task in SSIS
Loading the flat file into SQL table using conditonal split transformation
2.In control flow choose Data flow task for extract the file from the source.
3.In data flow task,select flat file source and choose our alredy created connection manager.
4.Basically flat files data in the format of string,so for sql operation we want appropriate data
type for example number columns are integer, text columns are
strings, data columns are date time formats so in this data conversion transformation used.
5.After data conversion I want to add some other columns like load date using get date function
and create full name using first name and last name column so in
6.Next i am going to split the data based on condition, for example india related data loading to
one india table, USA related data loading USA table so on a conditional split
7.After splitting the data, loading the multiple tables into sql though OLEDB destination.
5.For each ADO dataset schema enumarator for exporting data from SQL to multiple excel
sheets
Odbc
1.Originally designed for relational databases. (since changed)
2.On-going support for SQL
3.Component based
4.More difficult to deploy
Oledb
1.Originally designed for non-relational and relational databases.
2.SQL support void 2019
3.Procedural based
4.Easier to deploy
15Q.Truncate vs delete
Truncate
1 It is DDL command.
2.It deletes the rows in one shot hence it is faster than delete.
3.No triggers can be used.
4.Where cluse can not be used here.
5.There is no log file history
Delete
2.It delete the data in row by row hence it is slower than trunacte.
3.Triggers can be used.
4.Where clause can be used for delete the filterd rows.
5.Have log file history
Rank
RANK numbers are skipped so there may be a gap in rankings, and may not be unique.
Dense_Rank
DENSE_RANK numbers are not skipped so there will not be a gap in rankings, and may not be
unique.
17Q.Null vs coalesce
Null
It handel null values in single column.
Colesce
It handle the null values in multiple columns.
If all the columns mentioned in the colesce is null then it will return the use value.
Otherwise it will return the first non-null value.
15.Null vs null if
Null
A field with a NULL value is a field with no value. If a field in a table is optional,
it is possible to insert a new record or update a record without adding a value to this field.
Nullif
The NULLIF() function returns NULL if two expressions are equal, otherwise it returns the first
expression.
18Q.Replace vs stuff
Replace
The REPLACE() function replaces all occurrences of a substring within a string, with a new
substring.
Stuff
The STUFF() function deletes a part of a string and then inserts another part into the string,
starting at a specified position.
SQL QUESTIONS WITH SOLUTIONS
19Q.Primary vs unique
Primary key
1.It not allows duplicates and null values.
2.A table have only one Primary key.
3.It creates clustered index
Unique key
1.It will not allows duplicates but allow only one null values.
2.A table have multiple unique keys.
3.It creates non-clusterd index.
20Q.Check constraints
1.It checks the conditon,if condition satisfy then only record inserted.
2.It can defined in multiple times.
3.No index will be created.
4.It will be dfined both column and table level.
5.This allow null and duplicates
Whenever we are creating index on table, it will create a indexed table with binary tree structure
in backend, we are try to retiving the data it will not fetch
from the physical table,it will fetch from the indexed table.
SQL QUESTIONS WITH SOLUTIONS
23Q.Types of joins
(INNER) JOIN : Returns records that have matching values in both tables.
LEFT (OUTER) JOIN : Returns all records from the left table, and the matched records from the
right table.
RIGHT (OUTER) JOIN : Returns all records from the right table, and the matched records from
the left table.