SQL Question and Answer
SQL Question and Answer
go
delete
from table1_view
8) Difference between View vs Materialized
View in database
• 1) Views query result is not stored in the disk
or database but Materialized view allow to
store the query result in disk or table
• 2) In case of View we always get latest data
but in case of Materialized view we need to
refresh the view for getting latest data.
• 3) Performance of View is less than
Materialized view
9) can I call view inside another view
• Yes
Indexing view
• Indexing on view used when data not change
frequently. Like reporting
Trigger
• A trigger is a stored procedure in database
which automatically invokes whenever a
special event in the database occurs.
Differences between a Stored Procedure
and a Trigger
• Stored procedures can return values but a trigger
cannot return a value.
• We can execute a stored procedure whenever we
want with the help of the exec command, but a
trigger can only be executed whenever an event
(insert, delete, and update) is fired on the table on
which the trigger is defined.
• We can use transaction statements like begin
transaction, commit transaction, and rollback inside
a stored procedure but we can't use transaction
statements inside a trigger.
What is the maximum number of triggers
can apply to a single table?
• here is no limit. You can create as many as you want.
how many time trigger fired
• It gets fired only ONCE at the end of BULK
UPDATE
• Triggers fire once per statement
sql-query-order-of-operations
User-Defined Data Types?
• User-defined data types can be used when
several tables must store the same type of
data in a column and you must ensure that
these columns have exactly the same data
type, length, and NULLability.
• For example, a user-defined data type called
postal_code could be created based on the
char data type. User-defined data types are
not supported in TABLE variables.
Type of User-Defined Data Types?
• There are six user-defined types:
• Distinct type.
• Structured type
• Reference type
• Array type
• Row type
• Cursor type
Common Table Expressions (CTE)
• CTE is a temporary result set which can be used
within a execution of a SINGLE insert,update,delete
or select query.
• Common Table Expressions (CTE) have two types,
recursive and non-recursive.
• SELECT, INSERT, UPDATE, DELETE, or MERGE
statement.
• more than one CTE in your WITH clause, you must
separate them with commas. CTE can be defined in
functions, stored procedures, triggers or even views.
Advantages of CTE
• CTE improves the code readability.
• CTE provides recursive programming.
• CTE makes code maintainability easier.
• Though it provides similar functionality as a
view, it will not store the definition in
metadata.
CTE not allow following clause
• ORDER BY, unless you also use as TOP
clauseINTOOPTION clause with query hints
FOR BROWSE
What is SQL?
• Structured Query Language is a database tool
which is used to create and access database to
support software application.
What are tables in SQL?
• https://www.softwaretestinghelp.com/50-
popular-sql-interview-questions-for-testers/
What is the difference between Having
clause and Where clause?
• Both specify a search condition but Having
clause is used only with the SELECT statement
and typically used with GROUP BY clause.