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

SQL_Server_Topics_Practical_Questions

Uploaded by

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

SQL_Server_Topics_Practical_Questions

Uploaded by

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

SQL Server Practical Questions (Topic-Wise)

1. SQL Basics
1. Write a query to create a table with columns for ID, Name, and Salary.

2. Insert multiple rows of data into the created table.

3. Update the Salary of an employee based on their ID.

4. Delete an employee record where the Name is 'John'.

5. Fetch all records from the table.

6. Write a query to fetch only the Name and Salary of employees.

7. Retrieve employees whose Salary is greater than ₹50,000.

8. Fetch distinct departments from an employee table.

9. Sort employees by Name in descending order.

10. Count the total number of employees in the table.

2. Joins
1. Write a query to perform an INNER JOIN between two tables.

2. Fetch all records using a LEFT JOIN.

3. Retrieve unmatched records using a RIGHT JOIN.

4. Combine data from two tables using a FULL OUTER JOIN.

5. Write a query to perform a SELF JOIN to compare rows within the same table.

6. Find the employees and their department names using a join.

7. Retrieve employees who do not have a department assigned using a join.

8. Perform a join with more than two tables.

9. Write a query to use CROSS JOIN between two tables.

10. Fetch employees along with department details, handling nulls gracefully.

3. Stored Procedures
1. Create a stored procedure to retrieve employee details by Department.

2. Write a stored procedure to update an employee’s Salary by a given percentage.


3. Create a procedure to delete records older than a certain date.

4. Write a stored procedure to insert a new employee into the table.

5. Create a stored procedure to calculate the average Salary of employees in a department.

6. Develop a stored procedure to count employees by Department.

7. Write a procedure to fetch employees with a Salary greater than a given value.

8. Create a stored procedure with input and output parameters.

9. Write a procedure to log changes made to employee details into a log table.

10. Handle errors in a stored procedure using TRY...CATCH.

4. Functions
1. Create a scalar function to calculate the age of an employee based on their DateOfBirth.

2. Write a function to return the full name (FirstName + LastName) of an employee.

3. Develop a table-valued function to return employees in a given department.

4. Create a function to check if a Salary is within a specific range.

5. Write a function to calculate the factorial of a given number.

6. Build a function to return the department name given a DepartmentID.

7. Write a function to return the number of employees in a department.

8. Create a function to calculate the sum of salaries in a department.

9. Write a scalar function to return the initials of an employee’s name.

10. Build a function to validate email format for employees.

5. Indexes
1. Create a clustered index on the EmployeeID column.

2. Create a non-clustered index on the Name column.

3. Drop an index from the Salary column.

4. Write a query to check if an index is being used in a query plan.

5. Identify missing indexes using sys.dm_db_missing_index_details.

6. Optimize a query using a non-clustered index.

7. Create a composite index on Name and Department.


8. Rebuild an index to improve performance.

9. Write a query to check the fragmentation of an index.

10. Disable and then re-enable an index on a table.

6. Views
1. Create a view to show only Name and Salary from the Employees table.

2. Modify an existing view to include the Department column.

3. Query a view to fetch employees earning more than ₹50,000.

4. Create a view to calculate the average Salary by department.

5. Write a query to update a record through a view.

6. Drop a view from the database.

7. Create a view to display employee details, excluding employees in the 'HR' department.

8. Query a view using a WHERE condition to filter data.

9. Write a query to join two tables using a view.

10. Check if a view exists in the database before creating it.

7. Transactions
1. Write a transaction to update the Salary of multiple employees and commit it.

2. Implement a transaction that rolls back if an error occurs during an update.

3. Write a query to check if a transaction is active.

4. Use SAVEPOINT to partially roll back a transaction.

5. Implement a nested transaction and commit the changes.

6. Begin a transaction to insert multiple rows and rollback if any insertion fails.

7. Write a query to demonstrate the XACT_ABORT setting.

8. Use a transaction to delete records from two related tables.

9. Demonstrate how deadlocks can occur using transactions.

10. Write a query to manually rollback a transaction using ROLLBACK TRANSACTION.

8. Common Table Expressions (CTE)


1. Write a CTE to fetch employees earning more than ₹40,000.
2. Create a recursive CTE to generate a sequence of numbers from 1 to 10.

3. Use a CTE to calculate the total salary for each department.

4. Write a query using a CTE to fetch the top 3 highest-paid employees.

5. Use a CTE to find duplicate rows in a table.

6. Create a recursive CTE to find the factorial of a given number.

7. Combine a CTE with a join query to fetch department details for each employee.

8. Write a query using a CTE to paginate employee records.

9. Use a CTE to fetch employees reporting to a specific manager (hierarchical data).

10. Drop a table after verifying its data using a CTE.

9. Backup and Restore


1. Take a full backup of a database.

2. Write a query to take a differential backup.

3. Create a transaction log backup.

4. Restore a database from a full backup.

5. Perform a point-in-time recovery using a transaction log backup.

6. Verify the integrity of a database backup.

7. Write a query to automate database backups using a SQL Server Agent job.

8. Create a script to back up a specific table.

9. Restore a database to a different server.

10. Write a query to list all available backups in the system.

10. Performance Tuning


1. Write a query to fetch the execution plan of a query.

2. Use SET STATISTICS IO to measure query performance.

3. Create an index to optimize a slow-running query.

4. Use sys.dm_exec_query_stats to identify long-running queries.

5. Write a query to find unused indexes in a database.

6. Analyze the query plan for a stored procedure and suggest optimizations.
7. Rewrite a CURSOR-based query to a set-based query for better performance.

8. Demonstrate the effect of table partitioning on query performance.

9. Write a query to clear the query execution cache.

10. Use database tuning advisor to optimize query performance.

You might also like