0% found this document useful (0 votes)
5 views5 pages

SQL

The document provides an overview of various SQL concepts including normalization, triggers, views, indexes, and constraints. It explains the differences between clustered and non-clustered indexes, primary and unique keys, and the use of DELETE vs. TRUNCATE commands. Additionally, it covers SQL Profiler, stored procedures, data integrity, and types of joins, among other topics related to database management and SQL operations.

Uploaded by

yahsuD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

SQL

The document provides an overview of various SQL concepts including normalization, triggers, views, indexes, and constraints. It explains the differences between clustered and non-clustered indexes, primary and unique keys, and the use of DELETE vs. TRUNCATE commands. Additionally, it covers SQL Profiler, stored procedures, data integrity, and types of joins, among other topics related to database management and SQL operations.

Uploaded by

yahsuD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 5

What is normalization?

Well a relational database is basically composed of tables that contain


related data. So the Process of organizing this data into tables is actually referred to as normalization.
What is a trigger? - Triggers are basically used to implement business rules. Triggers is also similar
to stored procedures. The difference is that it can be activated when data is added or edited or deleted
from a table in a database.
What is a view? - If we have several tables in a db and we want to view only specific columns from
specific tables we can go for views. It would also suffice the needs of security some times allowing
specfic users to see only specific columns based on the permission that we can configure on the view.
Views also reduce the effort that is required for writing queries to access specific columns every time.
What is an Index? - When queries are run against a db, an index on that db basically helps in the
way the data is sorted to process the query for faster and data retrievals are much faster when we
have an index
What are the types of indexes available with SQL Server? - There are basically two types of
indexes that we use with the SQL Server. Clustered and the Non-Clustered.
What is the basic difference between clustered and a non-clustered index? - The difference is that,
Clustered index is unique for any given table and we can have only one clustered index on a table. The
leaf level of a clustered index is the actual data and the data is resorted in case of clustered index.
Whereas in case of non-clustered index the leaf level is actually a pointer to the data in rows so we can
have as many non-clustered indexes as we can on the db.
When do you use SQL Profiler? - SQL Profiler utility allows us to basically track connections to the
SQL Server and also determine activities such as which SQL Scripts are running, failed jobs etc..
Can you explain the role of each service? - SQL SERVER - is for running the databases SQL AGENT
- is for automation such as Jobs
How do you troubleshoot SQL Server if its running very slow? - First check the processor and
memory usage to see that processor is not above 80% utilization and memory not above 40-45%
utilization then check the disk utilization using Performance Monitor, Secondly, use SQL Profiler to
check for the users and current SQL activities and jobs running which might be a problem. Third would
be to run UPDATE_STATISTICS command to update the indexes
What are the difference between clustered and a non-clustered index?
1.A clustered index is a special type of index that reorders the way records in the table are physically
stored. Therefore table can have only one clustered index. The leaf nodes of a clustered index contain
the data pages.
2.A non clustered index is a special type of index in which the logical order of the index does not
match the physical stored order of the rows on disk. The leaf node of a non clustered index does not
consist of the data pages. Instead, the leaf nodes contain index rows.

What’s the difference between a primary key and a unique key?


Both primary key and unique enforce uniqueness of the column on which they are defined. But by
default primary key creates a clustered index on the column, where are unique creates a nonclustered
index by default. Another major difference is that, primary key doesn’t allow NULLs, but unique key
allows one NULL only

What is difference between DELETE and TRUNCATE commands?


Delete command removes the rows from a table based on the condition that we provide with a WHERE
clause. Truncate will actually remove all the rows from a table and there will be no data in the table
after we run the truncate command.
1.TRUNCATE: 1.TRUNCATE is faster and uses fewer system and transaction log resources than
DELETE.
2.TRUNCATE removes the data by deallocating the data pages used to store the table's data,
and only the page deallocations are recorded in the transaction log.
3.TRUNCATE removes all rows from a table, but the table structure, its columns, constraints,
indexes and so on, remains. The counter used by an identity for new rows is reset to the seed
for the column.
4.You cannot use TRUNCATE TABLE on a table referenced by a FOREIGN KEY constraint.
Because TRUNCATE TABLE is not logged, it cannot activate a trigger.
5.TRUNCATE cannot be rolled back.
6.TRUNCATE is DDL Command.
7.TRUNCATE Resets identity of the table
2.DELETE: 1.DELETE removes rows one at a time and records an entry in the transaction log for each
deleted row.
2.If you want to retain the identity counter, use DELETE instead. If you want to remove table definition
and its data, use the DROP TABLE statement.
3.DELETE Can be used with or without a WHERE clause
4.DELETE Activates Triggers.
5.DELETE can be rolled back.
6.DELETE is DML Command.
7.DELETE does not reset identity of the table.
Note: DELETE and TRUNCATE both can be rolled back when surrounded by TRANSACTION if the current
session is not closed. If TRUNCATE is written in Query Editor surrounded by TRANSACTION and if
session is closed, it can not be rolled back but DELETE can be rolled back.

What is the difference between a HAVING CLAUSE and a WHERE CLAUSE?


HAVING can be used only with the SELECT statement. HAVING is typically used in a GROUP BY clause.
When GROUP BY is not used, HAVING behaves like a WHERE clause. Having Clause is basically used
only with the GROUP BY function in a query whereas WHERE Clause is applied to each row before they
are part of the GROUP BY function in a query.

What are the properties and different Types of Sub-Queries?


1.Properties of Sub-Query 1.A sub-query must be enclosed in the parenthesis.
2.A sub-query must be put in the right hand of the comparison operator, and
3.A sub-query cannot contain an ORDER-BY clause.
4.A query can contain more than one sub-query.

2.Types of Sub-Query
1.Single-row sub-query, where the sub-query returns only one row.
2.Multiple-row sub-query, where the sub-query returns multiple rows,. and
3.Multiple column sub-query, where the sub-query returns multiple columns

What is SQL Profiler?


SQL Profiler is a graphical tool that allows system administrators to monitor events in an instance of
Microsoft SQL Server. You can capture and save data about each event to a file or SQL Server table to
analyze later. For example, you can monitor a production environment to see which stored procedures
are hampering performances by executing too slowly.
Use SQL Profiler to monitor only the events in which you are interested. If traces are becoming too
large, you can filter them based on the information you want, so that only a subset of the event data is
collected. Monitoring too many events adds overhead to the server and the monitoring process and
can cause the trace file or trace table to grow very large, especially when the monitoring process takes
place over a long period of time.

Can a stored procedure call itself or recursive stored procedure? How much level SP
nesting is possible?
Yes. Because Transact-SQL supports recursion, you can write stored procedures that call themselves.
Recursion can be defined as a method of problem solving wherein the solution is arrived at by
repetitively applying it to subsets of the problem. A common application of recursive logic is to perform
numeric computations that lend themselves to repetitive evaluation by the same processing steps.
Stored procedures are nested when one stored procedure calls another or executes managed code by
referencing a CLR routine, type, or aggregate. You can nest stored procedures and managed code
references up to 32 levels.

Name 3 ways to get an accurate count of the number of records in a table?


SELECT * FROM table1
SELECT COUNT(*) FROM table1
SELECT rows FROM sysindexes WHERE id = OBJECT_ID(table1) AND indid < 2

What is PRIMARY KEY?


A PRIMARY KEY constraint is a unique identifier for a row within a database table. Every table should
have a primary key constraint to uniquely identify each row and only one primary key constraint can
be created for each table. The primary key constraints are used to enforce entity integrity.

What is UNIQUE KEY constraint?


A UNIQUE constraint enforces the uniqueness of the values in a set of columns, so no duplicate values
are entered. The unique key constraints are used to enforce entity integrity as the primary key
constraints.

What is FOREIGN KEY?


A FOREIGN KEY constraint prevents any actions that would destroy links between tables with the
corresponding data values. A foreign key in one table points to a primary key in another table. Foreign
keys prevent actions that would leave rows with foreign key values when there are no primary keys
with that value. The foreign key constraints are used to enforce referential integrity.
What is CHECK Constraint?
A CHECK constraint is used to limit the values that can be placed in a column. The check constraints
are used to enforce domain integrity.
What is NOT NULL Constraint?
A NOT NULL constraint enforces that the column will not accept null values. The not null constraints
are used to enforce domain integrity, as the check constraints.
What is a Scheduled Jobs or What is a Scheduled Tasks?
Scheduled tasks let user automate processes that run on regular or predictable cycles. User can
schedule administrative tasks, such as cube processing, to run during times of slow business activity.
User can also determine the order in which tasks run by creating job steps within a SQL Server Agent
job. E.g. back up database, Update Stats of Tables. Job steps give user control over flow of execution. If
one job fails, user can configure SQL Server Agent to continue to run the remaining tasks or to stop
execution.
What are the advantages of using Stored Procedures?
1.Stored procedure can reduced network traffic and latency, boosting application performance.
2.Stored procedure execution plans can be reused, staying cached in SQL Server's memory, reducing
server overhead.
3.Stored procedures help promote code reuse.
4.Stored procedures can encapsulate logic. You can change stored procedure code without affecting
clients.
5.Stored procedures provide better security to your data.
How to implement one-to-one, one-to-many and many-to-many relationships while
designing tables?
One-to-One relationship can be implemented as a single table and rarely as two tables with primary
and foreign key relationships. One-to-Many relationships are implemented by splitting the data into
two tables with primary key and foreign key relationships. Many-to-Many relationships are
implemented using a junction table with the keys from both the tables forming the composite primary
key of the junction table.

What are different Types of Join?


1.Cross Join A cross join that does not have a WHERE clause produces the Cartesian product of the
tables involved in the join. The size of a Cartesian product result set is the number of rows in the first
table multiplied by the number of rows in the second table. The common example is when company
wants to combine each product with a pricing table to analyze each product at each price.
2.Inner Join A join that displays only the rows that have a match in both joined tables is known as
inner Join. This is the default type of join in the Query and View Designer.
3.Outer Join A join that includes rows even if they do not have related rows in the joined table is an
Outer Join. You can create three different outer join to specify the unmatched rows to be included:
1.Left Outer Join: In Left Outer Join all rows in the first-named table i.e. "left" table, which
appears leftmost in the JOIN clause are included. Unmatched rows in the right table do not
appear.
2.Right Outer Join: In Right Outer Join all rows in the second-named table i.e. "right" table,
which appears rightmost in the JOIN clause are included. Unmatched rows in the left table are
not included.
3.Full Outer Join: In Full Outer Join all rows in all joined tables are included, whether they are
matched or not.
4.Self Join This is a particular case when one table joins to itself, with one or two aliases to avoid
confusion. A self join can be of any type, as long as the joined tables are the same. A self join is rather
unique in that it involves a relationship with only one table. The common example is when company
has a hierarchal reporting structure whereby one member of staff reports to another. Self Join can be
Outer Join or Inner Join.
How to find 6th highest salary from Employee table
SELECT TOP 1 salary FROM (SELECT DISTINCT TOP 6 salary FROM employee ORDER BY salary DESC) a
ORDER BY salary

What is the Referential Integrity?


Referential integrity refers to the consistency that must be maintained between primary and foreign
keys, i.e. every foreign key value must have a corresponding primary key value

Define candidate key, alternate key, composite key.


A candidate key is one that can identify each row of a table uniquely. Generally a candidate key
becomes the primary key of the table. If the table has more than one candidate key, one of them will
become the primary key, and the rest are called alternate keys.
A key formed by combining at least two or more columns is called composite key.

Difference between Store Procedure and Trigger?


 we can call stored procedure explicitly.
 but trigger is automatically invoked when the action defined in trigger is done.
ex: create trigger after Insert on
 this trigger invoked after we insert something on that table.
 Stored procedure can't be inactive but trigger can be Inactive.
Triggers are used to initiate a particular activity after fulfilling certain condition.It need to define and
can be enable and disable according to need.
What the difference between UNION and UNIONALL?
Union will remove the duplicate rows from the result set while Union all does'nt.

How would you find out the total number of rows in a table?
Use SELECT COUNT(*) … in query

How do you eliminate duplicate values in SELECT?


Use SELECT DISTINCT … in SQL query
Difference between Function and Stored Procedure?
UDF can be used in the SQL statements anywhere in the WHERE/HAVING/SELECT section where as
Stored procedures cannot be.
UDFs that return tables can be treated as another rowset. This can be used in JOINs with other tables.
Inline UDF's can be though of as views that take parameters and can be used in JOINs and other
Rowset operations.
What is data integrity? Explain constraints?
Data integrity is an important feature in SQL Server. When used properly, it ensures that data is
©http://www.sqlauthority.com
©http://www.sqlauthority.com
accurate, correct, and valid. It also acts as a trap for otherwise undetectable bugs within applications.
If one job fails, user can configure SQL Server Agent to continue to run the remaining tasks or to stop
execution.

What is a Database Lock?


Database lock tell a transaction if the data item in questions is currently being used by other
transactions.
What are the type of locks?
1. Shared Lock : When a shared lock is applied on data item, other transactions can only read the
item, but cant write into it.
2. Exclusive Lock : When a exclusive lock is applied on data item, other transactions cant read or write
into the data item.
What are the advantages and disadvantages of views in a database?
Advantages:
1. Views doesn't store data in a physical location.
2. View can be use to hide some of the columns from the table
3. Views can provide Access Restriction, since data insertion , update and deletion is not possible on
the view.
Disadvantages:
1. When a table is dropped , associated view become irrelevant.
2. Since view are created when a query requesting data from view is triggered, its bit slow
3. When views are created for large tables, it occupy more memory
Can a table have multiple unique, foreign, and/or primary keys?
A table can have multiple unique and foreign keys. However, a table can have only one
primary key. Can a foreign key contain null values?
Yes, a foreign key can hold NULL values. Because foreign keys can reference unique, non-primary keys
– which can hold NULL values – this means that foreign keys can themselves hold NULL values as well.
how would you retrieve the unique values for the employee_location without using the
DISTINCT keyword?
SELECT employee_location from employee
GROUP BY employee_location
How do you add a column to a table?
ALTER TABLE Department ADD (AGE, NUMBER);
What is DML and DDL?
DML and DDL are subsets of SQL. DML stands for Data Manipulation Language and DDL – Data
Definition Language.
DML consist of INSERT, UPDATE and DELETE
DDL commands
CREATE TABLE, ALTER TABLE, DROP TABLE, RENAME TABLE, CREATE INDEX, ALTER INDEX, DROP
INDEX.
CREATE/ALTER/DROP VIEW

The DROP command removes a table from the database. All the tables' rows, indexes and privileges
will also be removed.
Dropping Constraints:
Any constraint that you have defined can be dropped using the ALTER TABLE command with the DROP CONSTRAINT option.

You might also like