SQL Query Interview Questions and Answers: (Salary) Employee Salary NOT ( (Salary) Employee)
SQL Query Interview Questions and Answers: (Salary) Employee Salary NOT ( (Salary) Employee)
SQL Query Interview Questions and Answers: (Salary) Employee Salary NOT ( (Salary) Employee)
In this query, we have used RIGHT OUTER JOIN because we need the name of the
department from Department table which is on the right side of JOIN clause, even if
there is no reference of dept_id on Employee table.
Question 3: Write SQL Query to display the current date.
Answer: SQL has built-in function called GetDate() which returns the current
timestamp. This will work in Microsoft SQL Server, other vendors like Oracle
and MySQL also has equivalent functions.
SELECT GetDate();
Question 4: Write an SQL Query to check whether date passed to
Query is the date of given format or not.
Answer: SQL has IsDate() function which is used to check passed value is a
date or not of specified format, it returns 1(true) or 0(false) accordingly.
Question 10: Write an SQL Query to find the year from date.
Answer: Here is how you can find Year from a Date in SQL Server 2008
SELECT YEAR(GETDATE()) as "Year";
Question 11: Write SQL Query to find duplicate rows in a database? and then
write SQL query to delete them?
Answer: You can use the following query to select distinct records:
SELECT * FROM emp a WHERE rowid = (SELECT MAX(rowid) FROM EMP b WHERE
a.empno=b.empno)
to Delete:
DELETE FROM emp a WHERE rowid != (SELECT MAX(rowid) FROM emp b WHERE
a.empno=b.empno);
Question 12: There is a table which contains two column Student and Marks, you
need to find all the students, whose marks are greater than average marks i.e. list
of above average students.
Answer: This query can be written using subquery as shown below:
SELECT student, marks from table where marks > SELECT AVG(marks) from table)
Question 13: How do you find all employees which are also manager? .
You have given a standard employee table with an additional column mgr_id, which
contains employee id of the manager.
Answer: You need to know about self-join to solve this problem. In Self Join, you can
join two instances of the same table to find out additional details as shown below
this will show employee name and manager name in two column e.g.
name manager_name
John David
One follow-up is to modify this query to include employees which don't have a manager.
To solve that, instead of using the inner join, just use left outer join, this will also include
employees without managers.
Question 14: You have a composite index of three columns, and you only provide
the value of two columns in WHERE clause of a select query? Will Index be used
for this operation? For example if Index is on EmpId, EmpFirstName,
and EmpSecondName and you write query like
If the given two columns are secondary index column then the index will not invoke, but
if the given 2 columns contain the primary index(first column while creating index) then
the index will invoke. In this case, Index will be used
because EmpId and EmpFirstName are primary columns.