SQL interview questions with answers
SQL interview questions with answers
USE ORG;
FIRST_NAME CHAR(25),
LAST_NAME CHAR(25),
SALARY INT(15),
JOINING_DATE DATETIME,
DEPARTMENT CHAR(25)
);
WORKER_REF_ID INT,
BONUS_AMOUNT INT(10),
BONUS_DATE DATETIME,
REFERENCES Worker(WORKER_ID)
ON DELETE CASCADE
);
WORKER_REF_ID INT,
WORKER_TITLE CHAR(25),
AFFECTED_FROM DATETIME,
FOREIGN KEY (WORKER_REF_ID)
REFERENCES Worker(WORKER_ID)
ON DELETE CASCADE
);
Ans.
The required query is:
Copy
Q-2. Write an SQL query to fetch “FIRST_NAME” from the
Worker table in upper case.
Ans.
The required query is:
Copy
Q-3. Write an SQL query to fetch unique values of
DEPARTMENT from the Worker table.
Ans.
The required query is:
Copy
Q-4. Write an SQL query to print the first three characters of
FIRST_NAME from the Worker table.
Ans.
The required query is:
Copy
Q-5. Write an SQL query to find the position of the alphabet
(‘a’) in the first name column ‘Amitabh’ from the Worker
table.
Ans.
The required query is:
Copy
Notes.
The INSTR does a case-insensitive search.
Using the BINARY operator will make INSTR work as the case-
sensitive function.
Q-6. Write an SQL query to print the FIRST_NAME from the
Worker table after removing white spaces from the right
side.
Ans.
The required query is:
Copy
Q-7. Write an SQL query to print the DEPARTMENT from the
Worker table after removing white spaces from the left side.
Ans.
The required query is:
Copy
Q-8. Write an SQL query that fetches the unique values of
DEPARTMENT from the Worker table and prints its length.
Ans.
The required query is:
Copy
Q-9. Write an SQL query to print the FIRST_NAME from the
Worker table after replacing ‘a’ with ‘A’.
Ans.
The required query is:
Copy
Q-10. Write an SQL query to print the FIRST_NAME and
LAST_NAME from the Worker table into a single column
COMPLETE_NAME. A space char should separate them.
Ans.
The required query is:
Copy
Q-11. Write an SQL query to print all Worker details from the
Worker table order by FIRST_NAME Ascending.
Ans.
The required query is:
Copy
Q-12. Write an SQL query to print all Worker details from the
Worker table order by FIRST_NAME Ascending and
DEPARTMENT Descending.
Ans.
The required query is:
Copy
Q-13. Write an SQL query to print details for Workers with
the first names “Vipul” and “Satish” from the Worker table.
Ans.
The required query is:
Copy
Q-14. Write an SQL query to print details of workers
excluding first names, “Vipul” and “Satish” from the Worker
table.
Ans.
The required query is:
Copy
Q-15. Write an SQL query to print details of Workers with
DEPARTMENT name as “Admin”.
Ans.
The required query is:
Select * from Worker where DEPARTMENT like 'Admin%';
Copy
Q-16. Write an SQL query to print details of the Workers
whose FIRST_NAME contains ‘a’.
Ans.
The required query is:
Copy
Q-17. Write an SQL query to print details of the Workers
whose FIRST_NAME ends with ‘a’.
Ans.
The required query is:
Copy
Q-18. Write an SQL query to print details of the Workers
whose FIRST_NAME ends with ‘h’ and contains six alphabets.
Ans.
The required query is:
Copy
Q-19. Write an SQL query to print details of the Workers
whose SALARY lies between 100000 and 500000.
Ans.
The required query is:
Copy
Q-20. Write an SQL query to print details of the Workers who
joined in Feb’2014.
Ans.
The required query is:
Select * from Worker where year(JOINING_DATE) = 2014 and
month(JOINING_DATE) = 2;
Copy
Q-21. Write an SQL query to fetch the count of employees
working in the department ‘Admin’.
Ans.
The required query is:
Copy
Q-22. Write an SQL query to fetch worker names with
salaries >= 50000 and <= 100000.
Ans.
The required query is:
FROM worker
WHERE WORKER_ID IN
Copy
Q-23. Write an SQL query to fetch the no. of workers for
each department in descending order.
Ans.
The required query is:
FROM worker
GROUP BY DEPARTMENT
Ans.
The required query is:
FROM Worker W
ON W.WORKER_ID = T.WORKER_REF_ID
Copy
Q-25. Write an SQL query to fetch duplicate records having
matching data in some fields of a table.
Ans.
The required query is:
FROM Title
Copy
Q-26. Write an SQL query to show only odd rows from
a table.
Ans.
The required query is:
Copy
Q-27. Write an SQL query to show only even rows from
a table.
Ans.
The required query is:
Copy
Q-28. Write an SQL query to clone a new table from another
table.
Ans.
The general query to clone a table with data is:
Copy
The general way to clone a table without information is:
Copy
An alternate way to clone a table (for MySQL) without data is:
Copy
Q-29. Write an SQL query to fetch intersecting records of
two tables.
Ans.
The required query is:
INTERSECT
Copy
Q-30. Write an SQL query to show records from one table
that another table does not have.
Ans.
The required query is:
SELECT * FROM Worker
MINUS
Copy
Q-31. Write an SQL query to show the current date and time.
Ans.
The following MySQL query returns the current date:
SELECT CURDATE();
Copy
And the following MySQL query returns the current date and time:
SELECT NOW();
Copy
Here is a SQL Server query that returns the current date and time:
SELECT getdate();
Copy
Find this Oracle query that also returns the current date and time:
Copy
Q-32. Write an SQL query to show the top n (say 10) records
of a table.
Ans.
MySQL query to return the top n records using the LIMIT method:
Copy
SQL Server query to return the top n records using the TOP
command:
Copy
Q-33. Write an SQL query to determine the nth (say n=5)
highest salary from a table.
Ans.
MySQL query to find the nth highest salary:
Copy
SQL Server query to find the nth highest salary:
FROM (
FROM Worker
Copy
Q-34. Write an SQL query to determine the 5th highest
salary without using the TOP or limit method.
Ans.
The following query is using the correlated subquery to return the
5th highest salary:
SELECT Salary
FROM Worker W1
WHERE 4 = (
FROM Worker W2
);
Copy
Use the following generic method to find the nth highest salary
without using TOP or limit.
SELECT Salary
FROM Worker W1
WHERE n-1 = (
FROM Worker W2
);
Copy
Q-35. Write an SQL query to fetch the list of employees with
the same salary.
Ans.
The required query is:
Copy
Q-36. Write an SQL query to show the second-highest salary
from a table.
Ans.
The required query is:
Copy
Q-37. Write an SQL query to show one row twice in the
results from a table.
Ans.
The required query is:
union all
Copy
Q-38. Write an SQL query to fetch intersecting records of
two tables.
Ans.
The required query is:
INTERSECT
Copy
Q-39. Write an SQL query to fetch the first 50% of records
from a table.
Ans.
The required query is:
SELECT *
FROM WORKER
WHERE WORKER_ID <= (SELECT count(WORKER_ID)/2 from Worker);
Copy
Q-40. Write an SQL query to fetch the departments that have
less than five people in them.
Ans.
The required query is:
Copy
Q-41. Write an SQL query to show all departments along
with the number of people in there.
Ans.
The following query returns the expected result:
Copy
Q-42. Write an SQL query to show the last record from a
table.
Ans.
The following query will return the last record from the Worker table:
Copy
Q-43. Write an SQL query to fetch the first row of a table.
Ans.
The required query is:
Copy
Q-44. Write an SQL query to fetch the last five records from
a table.
Ans.
The required query is:
UNION
Copy
Q-45. Write an SQL query to print the name of employees
having the highest salary in each department.
Ans.
The required query is:
and TempNew.TotalSalary=t.Salary;
Copy
Q-46. Write an SQL query to fetch three max salaries from a
table.
Ans.
The required query is:
Copy
Q-47. Write an SQL query to fetch three min salaries from a
table.
Ans.
The required query is:
Ans.
The required query is:
Copy
Q-49. Write an SQL query to fetch departments along with
the total salaries paid for each of them