Professional Documents
Culture Documents
Table 1: – EmployeeDetails
Table 2: – EmployeeSalary
1.Write a SQL query to fetch the count of employees working in project ‘P1’.
Ans: SELECT COUNT(*) FROM EmployeeSalary WHERE Project = 'P1';
2.Write a SQL query to fetch employee names having a salary greater than or equal to
5000 and less than or equal 10000.
4. Write a query to fetch only the first name(string before space) from the FullName
column of EmployeeDetails table.
Or
SELECT LEFT(FullName, CHARINDEX(' ',FullName) - 1) FROM EmployeeDetails;
5. Write a query to fetch employee names and salary records. Return employee details
even if the salary record is not present for the employee.
Ans: SELECT E.FullName, S.Salary FROM EmployeeDetails E LEFT JOIN EmployeeSalary S
ON E.EmpId = S.EmpId;
6.Write a SQL query to fetch all the Employees who are also managers from
EmployeeDetails table.
Note: Here, we must use Self-Join as the requirement wants us to analyze the EmployeeDetails table as two
different tables, each for Employee and manager records.
Ans:SELECT DISTINCT E.FullName FROM EmpDetails E INNER JOIN EmpDetails M ON E.EmpID = M.ManagerID;
7. Write a SQL query to fetch all employee records from EmployeeDetails table who have
a salary record in EmployeeSalary table.
Note: The EXISTS condition in SQL is used to check whether the result of a correlated nested query is empty
(contains no tuples) or not. The result of EXISTS is a Boolean value True or False. It can be used in a SELECT,
UPDATE, INSERT or DELETE statement.
SELECT getdate();
17. Write a SQL query to fetch all the Employees details from EmployeeDetails table who
joined in the Year 2016.
SELECT TOP 1 Salary FROM ( SELECT DISTINCT TOP N Salary FROM Employee
ORDER BY Salary DESC )ORDER BY Salary ASC
19. Write SQL query to find the 3rd highest salary from table without using TOP/limit
keyword.
Both Char and Varchar2 are used for characters datatype but varchar2 is used for character
strings of variable length whereas Char is used for strings of fixed length.
23.What is a Foreign key?
A foreign key is a key used to link two tables together. This is sometimes also called as a
referencing key.
24. What is Index? What is the difference between clustered and non clustered index ?
Indexes are used to retrieve data from the database more quickly. Indexes are special lookup
tables that the database search engine can use to speed up data retrieval.
Clustered index is used for easy retrieval of data from the database and its faster whereas
reading from non clustered index is relatively slower.
A NULL value is not at all same as that of zero or a blank space. NULL value represents a value
which is unavailable, unknown, assigned or not applicable whereas a zero is a number and blank
space is a character
The cross join produces the cross product or Cartesian product of two tables whereas the natural
join is based on all the columns having the same name and data types in both the tables.
27.What is a View?
A view is a virtual table which consists of a subset of data contained in a table. Since views are
not present, it takes less space to store. View can have data of one or more tables combined and
it depends on the relationship.