Sunday, July 9, 2023

MS SQL Server Interview Questions. Part-1

 1. How to get 1st Highest Salary without using any Functions?

Solution: Suppose a Table [Emp] exists is DB with following fields.


SELECT 
    TOP 1 EMPID, EMPNAME, SALARY 
FROM 
    EMP 
ORDER BY 
    SALARY DESC 

2. Create table without using CREATE command?

Solution:
SELECT * INTO NEW_Table_Name FROM Table_Name
Suppose want to create new table [Emp_Backup] from [Emp] table containing all data.

SELECT * INTO Emp_Backup FROM Emp

However instead of selecting all columns (*) for selected fields also a new table can be created.
WHERE clause can be used to create new table for specific condition.

3. What is Computed Columns?

Solution:
Computed columns is a virtual column in table which is used to compute values from expression.
SQL Server does not store these virtual columns physically, so it did not require any storage.
In fact, computed columns are not stored but calculated every time the column is accessed in a query.
Below is a simple example where 2 columns [CompCol1, CompCol2] 
are added to give result in Computed Columns [Total_CompCol].


4. Joining is faster or Sub Query is faster?
Solution: Both Joining and Sub Query are used to combine data from different tables into a single result set. Joins are faster then Sub Queries as indexing and optimization can be applied on Joins.
Joins are faster as they require less time to display output than Sun Queries.






1 comment:

Windows Management and Toolbar Keyboard Shortcuts

  Action Standard Sql Server 2000/2005 Close the current MDI child window CTRL+F4 CTRL+F4 Print CTRL+P CTRL+P Exit ALT+F4 ALT+F4 Toggle full...