0% found this document useful (0 votes)
17 views

Document 1

The document discusses the key differences between clustered and non-clustered indexes in a database. A clustered index physically orders the data on disk based on the index key, while a non-clustered index stores index keys separately from the data with pointers. Only one clustered index is allowed per table, and it is best for range queries and ordered retrieval, though it uses more storage. Non-clustered indexes allow more versatility with less storage usage. The best choice depends on performance and storage needs.

Uploaded by

mohitsharma83881
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Document 1

The document discusses the key differences between clustered and non-clustered indexes in a database. A clustered index physically orders the data on disk based on the index key, while a non-clustered index stores index keys separately from the data with pointers. Only one clustered index is allowed per table, and it is best for range queries and ordered retrieval, though it uses more storage. Non-clustered indexes allow more versatility with less storage usage. The best choice depends on performance and storage needs.

Uploaded by

mohitsharma83881
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Ans 1-The main difference between a clustered and non-clustered index lies in

the organization of data on disk. A clustered index dictates the physical order of
data rows on disk, rearranging them to match the order of the index key. In
contrast, a non-clustered index creates a separate structure, storing index key
values along with pointers to the actual data rows without changing their
physical order.
A table can have only one clustered index, while multiple non-clustered
indexes are allowed. Clustered indexes are beneficial for range queries and
ordered retrieval, but they require more storage space. Non-clustered indexes
offer versatility in query optimization and consume less storage. The choice
depends on the specific performance and storage requirements of the
database.

Ans 2-whenever a query return null we can use a keyword “exists”.


Ans 3-
SELECT EmpId, FullName
FROM EmployeeDetails
WHERE ManagerId = '986';
Ans 4-
SELECT DISTINCT Project
FROM EmployeeSalary;
Ans 5-
SELECT COUNT(*) AS EmployeeCount
FROM EmployeeSalary
WHERE Project = 'P1';
Ans 6-
SELECT MAX(Salary) AS MaxSalary,
MIN(Salary) AS MinSalary,
AVG(Salary) AS AvgSalary
FROM EmployeeSalary;
Ans 7-
SELECT *
FROM EmployeeDetails
WHERE FullName LIKE '__hn%';
Ans 8-
Ans 9-
SELECT DISTINCT E1 *
FROM EmployeeDetails E1
JOIN EmployeeDetails E2 ON E1.EmpId = E2.ManagerId;
Ans 10-
SELECT FullName, ManagerId, DateOfJoining, City
FROM EmployeeDetails
GROUP BY FullName , ManagerId, DateOfJoining, City
HAVING COUNT(*) > 1;
Ans 11-
Ans 12-
WITH EvenRowsCTE AS (
SELECT
EmpId,
FullName,
ManagerId,
DateOfJoining,
City,
ROW_NUMBER() OVER (ORDER BY EmpId) AS RowNum
FROM EmployeeDetails
)
SELECT
EmpId,
FullName,
ManagerId,
DateOfJoining,
City
FROM EvenRowsCTE
WHERE RowNum % 2 = 0;
Ans 13- The choice between clustered and non-clustered indexes depends on
the specific use case, with clustered indexes typically chosen based on the
primary key or a column frequently used in range queries, and non-clustered
indexes selected for efficient searches without affecting the physical order of
the data. Clustered indexes are physically sorted and non clustered index is like
a separate list.
Ans 14-
Example:
CREATE INDEX idx_employee_name
ON EmployeeDetails (FullName);
using a create index help us in creating indexes though it is default non
clustered index.
Ans 15-
In SQL, stored procedures and functions serve distinct purposes. Stored
procedures are designed to execute actions involving data manipulation, and
may have both input and output parameters. They are not intended to return
values directly in a query. On the other hand, functions are primarily used for
computation and are expected to return a value, making them suitable for use
within queries and expressions. Functions can have input parameters but
typically lack the ability to modify data directly using transaction control
statements. While stored procedures are broader in scope and can participate
in transactions, functions are more focused on returning specific values based
on their computations. The choice between them depends on the nature of the
task and the desired outcome.
Ans 16-

Indexed views automatically update when the underlying tables change,


regular views do not. For regular views, the results are generated dynamically
when the view is queried, so any changes to the underlying tables are reflected
in the view's result set at the time of the query.

Ans 17-

The key differences between executing a query and calling a stored procedure
in a database is executing a query is a single SQL statement for data retrieval or
manipulation, while calling a stored procedure involves invoking a predefined
set of actions with potentially complex logic encapsulated within the database.
Stored procedures provide modularity, reusability, and transaction control,
making them beneficial for more complex tasks.

Ans 18-

In SQL the grant statement is used to provide specific privileges or permissions


to database users or roles. It allows database administrators to control access
to various database objects such as tables, views, procedures. The grant
statement is part of the Data Control Language in SQL, which manages
permissions and access control.

It is essential for maintaining security throughout the database.


Ans 19-
A cross join combines rows from two different tables, producing a Cartesian
product, while a self join involves combining rows from the same table based
on a specified relationship. Cross joins create all possible combinations,
whereas self joins are used for relationships within a single table.
Ans 20-
In SQL, joins are used to combine rows from two or more tables based on a
related column between them. Types:
1. inner join
- Returns rows that have matching values in both tables.
SELECT Employees.EmployeeID, Employees.EmployeeName,
Departments.DepartmentName
FROM Employees
INNER JOIN Departments ON Employees.DepartmentID =
Departments.DepartmentID;

2. left join
- Returns all rows from the left table and the matched rows from the right
table. If there is no match, NULL values are returned for columns from the right
table.
SELECT Customers.CustomerID, Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

3.right join
- Returns all rows from the right table and the matched rows from the left
table. If there is no match, NULL values are returned for columns from the left
table.
SELECT Orders.OrderID, Orders.OrderDate, Customers.CustomerName
FROM Orders
RIGHT JOIN Customers ON Orders.CustomerID = Customers.CustomerID;

4. full join
- Returns all rows when there is a match in either the left or the right table. If
there is no match, NULL values are returned for columns from the table
without a match.

SELECT Customers.CustomerID, Customers.CustomerName, Orders.OrderID


FROM Customers
FULL JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

5. cross join
- Returns the Cartesian product of the two tables, i.e., all possible
combinations of rows.

SELECT Employees.EmployeeName, Departments.DepartmentName


FROM Employees
CROSS JOIN Departments;

6. self join
- Joins a table with itself based on a related column. Commonly used when
dealing with hierarchical or recursive structures.

SELECT e1.EmployeeName AS Employee, e2.EmployeeName AS Manager


FROM Employees e1
INNER JOIN Employees e2 ON e1.ManagerID = e2.EmployeeID;

You might also like