SQL QUESTIONS WITH SOLUTIONS
1Q.Create a table with primary key
Create table Insurance
( Id int not null constraint P Id Primary key,
Name varchar(40) not null,
Age tiny int not null,
City varchar(50) not null
)
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.
3Q. Non 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.
4Q.Clustered Vs non clustered
Clustered Index
1. Primary key constraint creates default clustered index.
2. It is faster than non-clustered index.
3. It requires less memory of operation.
4. Only one clustered index available in one table.
5. It sort the rows in tables.
Non clustered Index
1. Unique key constraint creates default non clustered index.
2.It is slower than clustered index.
3.It requires more memory of operation.
4.A table have multiple non clustered index
5.It does not sort the data
SQL QUESTIONS WITH SOLUTIONS
5Q.Type of functions
1.Scalar Function
2. Table values function
3. Inline table valued function
4. Mutistatement table valued function
6Q.syntax to create a stored procedure
create procedure <Procedure_name> (<Parameter 1>,<<Parameter 2>>)
as
begin
< SQL statement>
end
executing store procedure
Execute <Procedure_name> <Parameter>
7Q.Stored procedure with output parameter
--Getting department wise total salary
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
declare @totalsal money
execute get department salary 'IT',@total sal out
print @total sal
SQL QUESTIONS WITH SOLUTIONS
8Q.cte vs temp table
CTE (Common temporary table)
1.Are unindexable (but can use existing indexes on referenced objects)
2.Cannot have constraints
3.Are essentially disposable VIEWs
4.Persist only until the next query is run
5.Can be recursive
6.Do not have dedicated stats (rely on stats on the underlying objects)
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.
9Q.Uses of stored procedure
1. It is Bunch of SQL statements which will run in sequential order.
2. It is pre compiled object and it give better performance than the normal queries.
3. It is resuable and we can allow for end use to pass the parameter as per the requirement.
4. We can pass input, Output and Return parameter.
5. We can alter procedure using 'ALTER' command.
6. Use 'Execute or Exec' keyword for executing procedure.
10Q.Have you done any performance on stored procedure
1.Use schema names before the table name
2.Avoid cursors
3.Do not use sp_<procname>
4.Prefer sq_executesql over EXECUTE command
5.Fetch only the required columns
SQL QUESTIONS WITH SOLUTIONS
6.Use table variables instead of temporary tables
7.Avoid unnecessary statements in between transaction-SQL derived tables, views and JOINs
8.Avoid costly operators, implicit and explicit functions
9.Use EXISTS instead of COUNT in sub queries
11Q.What are triigers
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
12Q.What are the packages you worked on?
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
13Q.Explain one end to end package
Loading the flat file into SQL table using conditonal split transformation
1. Creating connection manager for flat file and configure it.
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
this derived column transformation used.
SQL QUESTIONS WITH SOLUTIONS
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
transformation is used for this.
7.After splitting the data, loading the multiple tables into sql though OLEDB destination.
14Q.What are the connection types used?
1.Flat file connections
2.Excel file connections
3.for each file enumarator for loading multiple files
4.For each ADO enumarator for exporting data from SQL table to multiple files
5.For each ADO dataset schema enumarator for exporting data from SQL to multiple excel
sheets
15Q.difference between oledb vs odbc
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
1.It is DML command
SQL QUESTIONS WITH SOLUTIONS
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
16Q.Rank vs dense rank
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
21Q.Delete duplicate: types of methods
1.SQL delete duplicate Rows using Group By and having clause
2.SQL delete duplicate Rows using Common Table Expressions (CTE)
3.RANK function to SQL delete duplicate rows
22Q.What is meant by index ?
Index is used in sql server for fast retrival of data.
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.
Canvas-07 posters based on wild valley
SEO -BACKING HOMOEOPATHY FOR SAVI BINDHU WEBSI