SQL Query Interview Questions and Answers
SQL Query Interview Questions and Answers
Question 5: Write an SQL Query to print the name of the distinct employee
whose DOB is between 01/01/1960 to 31/12/1975.
Answer: This SQL query is tricky, but you can use BETWEEN clause to get all records
whose date fall between two dates.
SELECT DISTINCT EmpName FROM Employees WHERE DOB BETWEEN 01/01/1960
AND 31/12/1975;
Answer :
SELECT * FROM Employees WHERE EmpName like 'M%';
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)